Software Archive
Read-only legacy content
17061 Discussions

Use memory allocated in offload region on host

Martin_O_
Beginner
245 Views

Hello,

This post covers two questions. I actually just need a (positive) answer for one of them, as that would be enough to solve my problem. But it would be nice to get an answer for both.

1. Is it possible to write to disk from the offload region?

2. How can I use memory allocated inside the offload region in the host?

I'm porting a program to use Xeon Phi. This program needs to write to disk, and this happens inside the offload region. AFAICS, that is not possible, right (question 1)? In that case, my strategy would be to store all data in a big char array and, later, at the host, use fwrite() to write everything in one step. The problem is that I don't know in advance the array's length, I only find it out once I'm in the offload region. I have something like this:

int main()
{
    char *array;
    int size;
    FILE *fp = fopen("file", "w");

    #pragma offload target(mic:0) inout(size) 
    {
        size = ...; /* determine array's size */
        array = malloc(size);
        /* populate array */
    }

    fwrite(array, size, 1, fp);
    return 0;
}

This doesn't work because when the program tries to copyout the array, it's not allocated in the host. How can I use the memory allocated in the co-processor in the host (question 2)?

Thank you

0 Kudos
1 Reply
Ravi_N_Intel
Employee
245 Views

Try something like the following

 

#include <malloc.h>

#define ALLOC  alloc_if(1) free_if(0)
#define REUSE  alloc_if(0) free_if(0)
#define FREE   alloc_if(0) free_if(1)
#define TARGETPTR targetptr
#define PREALLOCATED preallocated

 __declspec(target(mic)) int* mic_p;

 int main(int argc, char* argv[])
{
    int* cpu_p ;
    int size;

#pragma offload target(mic:0) out(size)
{
    //  get size of memory
}
// Allocate CARD memory and register
#pragma offload target(mic:0) \
                nocopy(mic_p : alloc(mic_p[3 : size]) ALLOC PREALLOCATED TARGETPTR)
{
    mic_p = (int*)malloc(size*sizeof(int));
}

//  Allocate host memory
 cpu_p = (int*)_mm_malloc(size * sizeof(int), 64);

//  Do computation and get the results back
 #pragma offload target(mic:0) \
          out(mic_p[0 : size] : into(cpu_p[0 : size]) REUSE TARGETPTR)
{
   for (unsigned i = 0; i < size; i++) {
      mic_p = 10 + i;
   }
}
// Remove CARD memory registration and CARD  memory
#pragma offload target(mic) \
     nocopy(mic_p : FREE preallocated targetptr )
{
      free(mic_p);
}

   return 0;
}

0 Kudos
Reply