Software Archive
Read-only legacy content
17061 讨论

Change pointer value inside offload region

Jun_Z_2
初学者
752 次查看

What will happen if I change a pointer value inside the offload region like the code below?

[cpp]#pragma offload inout(p,q: length(10))
{
int* temp = p;
p = q;
q = temp;
}[/cpp]

Will the memory content of p and q on host exchange?

Thanks.

0 项奖励
5 回复数
pbkenned1
员工
752 次查看

No.  You can't manipulate pointers like that with the hetrogeneous offload model.  Your pointers p & q have totally different addresses on host and coprocessor.  All your example does is swap pointers on the coprocessor without doing anything on the host.

If you want to manipulate pointers, you would need to use the Cilk_shared offload model, which can maintain common virtual addresses of arbitrarily complex objects (including pointers) between host and coprocessor.

On the other hand, if you just want to swap the data pointed to, you can use the hetrogeneous offload model.  The 'temp' pointer is only known to the coprocessor and is created just to swap the data:

#include <iostream>
using namespace std;

int * p=NULL;
int * q=NULL;

int main()
{
   p = new(int);
   q = new(int);
   *p = 1;
   *q = 2;

   cout << "\n before offload *q = " << *q << endl;

   #pragma offload target(mic) inout(p,q: length(8))
   {
     int * temp = new(int);
     *temp = *p;
     *p = *q;
     *q = *temp;
   }

   cout << "\n after offload *q = " << *q << endl;

   return 0;
}

 

$ icc -V
Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 14.0.0.080 Build 20130728
Copyright (C) 1985-2013 Intel Corporation.  All rights reserved.

$ icc U477910-pragma-offload.cpp -offload-attribute-target=mic
$ ./a.out

 before offload *q = 2

 after offload *q = 1
$

 

0 项奖励
jimdempseyatthecove
名誉分销商 III
752 次查看

Then by extension, the following should the following work as well:

[cpp]

#include <iostream>
using namespace std;

int * p=NULL;
int * q=NULL;

int main()
{
   p = new(int);
   q = new(int);
   *p = 1;
   *q = 2;

   cout << "\n before offload *q = " << *q << endl;

   int** pp = &p;
   int** pq = &q;

   #pragma offload target(mic) inout(pp,qq: length(8))
   {
     int ** temp = pp;
     *pp = *pq;
     *pq = temp;
   }

   cout << "\n after offload *q = " << *q << endl;

   return 0;
}
[/cpp]

Though I think you only need intent in

Jim Dempsey

0 项奖励
pbkenned1
员工
752 次查看

You would think double indirection should work the same, but apparently that confuses the compiler.  I'm getting 'offload error: address range partially overlaps with existing allocation' with your example. 

0 项奖励
Jun_Z_2
初学者
752 次查看

Thanks Patrick and jim. Does this mean offload will only bind the memory address of host and MIC? No matter how I manipulating the pointer inside offload region, compiler will still copy in/out according to the address specified in the directive?

0 项奖励
jimdempseyatthecove
名誉分销商 III
752 次查看

The "useless" case I was illustrating was essentially passing intptr_t integers into and out of the offload region.

Jim Dempsey

0 项奖励
回复