OpenACC for the C++ std::vector

Hi,

This discussion follows the old threads at

I tried to use OpenACC data copy for C++ std::vector likes

vector< float > vec(size);

#pragma enter data copyin(vec[0:size])
#pragma acc parallel loop reduction(+:tmp)
for (int s = 0; s < size; s++) {
tmp += vec[s];
}

but got “Segmentation fault (core dumped)”. If the variable “vec” is a pointer or array it works fine.

Only the access operator needed in my cases. How can I directly use the std::vector in the OpenAcc directives?

Thanks. /Jing

Hi Jing,

As I noted in the previous post, it’s highly advisable to use CUDA Unified Memory (i.e. -gpu=managed) when using C++ vectors. If needed, I can go into more details and show you how to manually mange the data but it’s rather tricky so I don’t recommend it.

Here’s an example using C++ vectors with UM:

% cat vec.cpp
#include <vector>
#include <iostream>
#include <cstdlib>

using namespace std;

int main () {

int size = 1024;
float tmp = 0;
vector<float> vec(size);
vec.assign(size,1.0);

#pragma acc parallel loop reduction(+:tmp)
for (int s = 0; s < size; s++) {
   tmp += vec[s];
}
cout << tmp << endl;
exit(0);
}
% nvc++ vec.cpp -acc -gpu=managed ; a.out
1024

Note that in your example your “enter data” pragma is missing the “pragma acc” so would be ignored.

Hope this helps,
Mat

Hi Mat,

The flag -gpu=managed makes sense.

Thanks. /Jing