Reputation: 1
say i have class like this
class TheBox{
public:
int value1;
int value2;
int **p;
int size;
int result;
int test(){
result = 0;
for (int i = 0; i < size; i++){
result += *p[i];
}
return result;
}
};
and i use it like this
int main(){
TheBox b;
b.value1 = 5;
b.value2 = 7;
b.size = 10;
b.p = (int**)malloc(sizeof(int*)*b.size);
b.p[0] = &b.value1;
b.p[1] = &b.value1;
b.p[2] = &b.value2;
b.p[3] = &b.value1;
b.p[4] = &b.value1;
b.p[5] = &b.value2;
b.p[6] = &b.value2;
b.p[7] = &b.value1;
b.p[8] = &b.value1;
b.p[9] = &b.value1;
cout << b.host() << endl; //should output 56
b.value2 = 8;
cout << b.host() << endl; //should output 59
}
My question is how do i do cudaMemCpy for object "b"?
The problem occurs when trying to copy array "p" i have to know beforehand whether it contains value1 or value2.
Is there a way to correctly do cudaMemcpy for object "b" without this information?
Upvotes: 0
Views: 339
Reputation: 72349
Is there a way to correctly do
cudaMemcpy
for object "b" without this information?
In a word, no. And it isn't even obvious what "correctly do cudaMemcpy
" would even mean in this context, given that the contents of the array of pointers contains host addresses and, therefore, could only be correctly set after a instance of the class had already been established in GPU memory.
The only possible way to do this that makes any sense would be to construct an instance in memory which is accessible to the GPU directly. You could do this with Unified Memory (see here for an example), or by building a copy of the device structure in host memory and copying it to the device (see here for one approach).
Upvotes: 2