Software Archive
Read-only legacy content
17060 Discussions

How to malloc a buffer on co-processor in offload mode?

songlinhai
Beginner
525 Views

Hi,

   How could I malloc a buffer on co-processor in offload mode? I only know that I can malloc a buffer on host, and use "offload in" to malloc the same buffer on co-processor. Is there some ways that I can malloc the buffer on co-processor directly?

   Thanks a lot!

 

0 Kudos
6 Replies
Bachar_Z_
Beginner
525 Views

Hi,

You can use malloc directly inside the offload region!

if you want to use the buffer later on, you can use an output variable in the (size_t) and use it later as in variable for aother offload region.

you should do the cast!

0 Kudos
Kevin_D_Intel
Employee
525 Views

Perhaps Explicitly managed Heap-allocated Data discussed here provides the functionality you’re looking for.

0 Kudos
Bachar_Z_
Beginner
525 Views

Kevin Davis (Intel) wrote:

Perhaps Explicitly managed Heap-allocated Data discussed here provides the functionality you’re looking for.

if we want to use the pointer p in more than one function, this way does not work!

0 Kudos
Kevin_D_Intel
Employee
525 Views

With the local pointer that is correct; however, with a global pointer as demonstrated below the method is usable with multiple functions. I also confirmed this with our Developers.

$ icc -V

Intel(R) C Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 13.1.2.183 Build 20130514

$ icc example.c -o example && example

q = 77

[cpp]#include <stdio.h>

#include <offload.h>

 

__attribute__ ((target (mic))) int *p;

 

extern void f1();

extern void f2();

 

void f1()

{

  // The nocopy clause ensures CPU values pointed to by p

  // are not transferred to coprocessor

  #pragma offload target(mic:0) nocopy(p)

  {

    // Allocate dynamic memory for p on coprocessor

    p = (int *)malloc(sizeof (int) * 100);

    p[99] = 77;

  }

}

 

void f2()

{

int q = 0;

 

  // The nocopy clause ensures p is not altered by the offload process

  #pragma offload target(mic:0) nocopy(p) out(q)

  {

    // Reuse dynamic memory pointed to by p

    q = p[99]; // Will be 77

  }

 

  printf("q = %d\n",q);

}

 

int main()

{

f1();

f2();

}[/cpp]

 

0 Kudos
Bachar_Z_
Beginner
527 Views

My case was the following:

I have an object with a pointer sould be in Phi mem, and I use that object in may functionality inside the system.

in my case, the global array( pointer) does not work because I have many of this object. " static does have the same problem"

And then I used the size_t variable as output for the alloc part ofthe object and I can use that value whenever I need to apply some functionality to any obect of that type!

0 Kudos
Corey_P_
Beginner
527 Views

With reference to Kevin's code example above using nopy to reference a buffer that is created on the Mics. This method currently has a problem when using multiple Mics, though works fine for a single device. Read here for further discussion on that specific method:

http://software.intel.com/en-us/forums/topic/393649

Corey

0 Kudos
Reply