0

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;
}
JudgeDeath
  • 151
  • 1
  • 2
  • 9

1 Answers1

2

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;
  }
Dark Falcon
  • 43,592
  • 5
  • 83
  • 98
  • Ok, this is what I need :). What about situation, when we have got three classes: class A, class B with std::vector and class C with std::vector ? – JudgeDeath Jun 26 '15 at 17:54
  • Every piece of memory you must explicitly list in an `in` clause if it is to be available on the coprocessor. That memory cannot contain pointers unless what those pointers point to is also in the `in` clause. This precludes using `std::vector` and most other standard containers, as they all use pointers internally and do not expose them sufficiently to allow using an `in`. One notable exception would probably be `std::array`, which you might be able to marshal by just `in(the_array)` – Dark Falcon Jun 26 '15 at 17:59