Software Archive
Read-only legacy content
17061 Discussions

Offload pointer in struct

Martin_O_
Beginner
256 Views

Hello,

I'm new to MIC programming, so this is probably a silly question, but I've searched and could not find a solution.

I have a struct with a pointer in it and I want to offload that pointer (the array) to MIC. AFAIK, I can't offload the whole struct, because it's not bitwise copyable. But I was hoping I could offload just the pointer, as a normal array. Below is a minimal example, which segfaults. What am I doing wrong?

As a side question, what are my options if I need to copy the whole struct?

#include <stdlib.h>
#include <omp.h>
#define SIZE 1000

typedef struct {
    int *v;
} my_struct;

int main (void)
{
    my_struct s;
    s.v = (int*) malloc(SIZE * sizeof(int));

    #pragma offload target(mic:0) inout(s.v:length(SIZE))
    {
        #pragma omp parallel for
        for(int i =0;i<SIZE;i++)
        {
            s.v = i;
        }
    }
    return 0;
}

 

0 Kudos
1 Reply
Frances_R_Intel
Employee
256 Views

Sadly, no that doesn't work. Instead what you would need is:

int * v1 = s.v
#pragma offload target(mic:0) inout(v1:length(SIZE))
{
   s.v = v1;
and so on

I know it seems like there shouldn't be any difference between these two but there is a subtle difference. In one case, offload is allocating memory on the coprocessor and then setting the variable, v1, to point to that memory, In the other case, offload is allocating memory on the coprocessor, and then setting part of the variable, s, to point to that memory. V may be the only member of s but it is still just a member. Subtle and the compiler people might have chosen to do it differently but the behavior is consistent with the way structs are dealt with in C and it is predictable, regardless of what members are in the struct.

You can find a lovely discussion of issues like this in Effective Use of the Intel Compiler's Offload Features

0 Kudos
Reply