c++vectorstlintel-micoffloading

Intel Xeon Phi offload code + STL vector


i would like to copy data stored in STL vector to Intel Xeon Phi coprocessor. In my code, I created class which contains vector with data needed to computation. I want to create class object on host, initialize data on host too and then I want to send this object to coprocessor. This is simple code which illustrate what i want to do. After copy object to the coprocessor vector is empty. What can be problem? How do it correctly?

#pragma offload_attribute (push, target(mic))
#include <vector>
#include "offload.h"
#include <stdio.h>
#pragma offload_attribute (pop)

class A
{
    public:
    A() {}
    std::vector<int> V;
};

int main()
{
    A* wsk = new A();
    wsk->V.push_back(1);

    #pragma offload target(mic) in(wsk)
    {
        printf("%d", wsk->V.size());

        printf("END OFFLOAD");
    }
    return 0;
}

Solution

  • When an object is copied to the coprocessor, only the memory of that element itself, which is of type A. std::vector allocates a separate block of memory to store its elements. Copying over the std::vector embedded within A does not copy over its elements. I would recommend against trying to use std::vector directly. You can copy its elements, but not the vector itself.

      int main()
      {
          A* wsk = new A();
          wsk->V.push_back(1);
    
          int* data = &wsk->V[0];
          int size = wsk->V.size();
    
          #pragma offload target(mic) in(data : length(size))
          {
              printf("%d", size);
    
              printf("END OFFLOAD");
          }
          return 0;
      }