Here is the code which reproduces the unexplained behavior:
main.cpp
#include <iostream>
#include <openacc.h>
extern "C" int findme(float *ARRAY);
int main(){
float *ARRAY = new float [10];
int position;
ARRAY[0] = 97.7302;
ARRAY[1] = 108.154;
ARRAY[2] = 99.8558;
ARRAY[3] = 88.9383;
ARRAY[4] = 98.4755;
ARRAY[5] = 109.186;
ARRAY[6] = 107.205;
ARRAY[7] = 110.886;
ARRAY[8] = 86.0737;
ARRAY[9] = 94.9976;
#pragma acc enter data copyin(ARRAY[0:10])
#pragma acc host_data use_device(ARRAY)
position = findme(ARRAY);
#pragma acc exit data delete(ARRAY[0:10])
std::cout << position << std::endl;
}
findme.cu
#include <thrust/binary_search.h>
#include <thrust/device_vector.h>
#include <thrust/functional.h>
#include <thrust/sort.h>
extern "C" int findme(float *ARRAY){
thrust::device_ptr<float> ARRAY_ptr(ARRAY);
thrust::device_vector<float> ARRAY_SORTED(10);
thrust::copy(ARRAY_ptr, ARRAY_ptr+10, ARRAY_SORTED.begin());
thrust::sort(ARRAY_SORTED.begin(), ARRAY_SORTED.end(), thrust::less<float>());
thrust::device_vector<float>::iterator iter = thrust::lower_bound(ARRAY_SORTED.begin(), ARRAY_SORTED.end(), 100);
return iter - ARRAY_SORTED.begin();
}
There is one device-to-host transfer of 1 byte (I guess the 8 byte device-to-host transfer is the position
int
, but why 8 bytes instead of 4?)...
... and one host to device transfer of 4 bytes...
... which I cannot explain. Note that the copying in of ARRAY
is not seen on the screenshots but is included in the Details tab for the host-to-device transfer (40 bytes). Any clues as to what data is being transferred exactly? Are they inherent to the Thrust algorithms and therefore unavoidable?