I tried to transfer Pointers to CUDA memory, but I ran into some problems and finally found a solution. Here’s a note:

First, there is no problem with ordinary transmission:

cudaMalloc((void**)&a_dev, 50 * sizeof(int));

a_host = (int*)malloc(sizeof(int) * 50);

for (int i = 0; i < 50; i++) a_host[i] = i;

cudaMemcpy(a_dev, a_host, 50 * sizeof(int), cudaMemcpyHostToDevice);

Cal <<<1, 50 >>> (a_dev);

cudaMemcpy(a_host, a_dev, 50 * sizeof(int), cudaMemcpyDeviceToHost); In the Cal program I set the entire array to 27 and the output is fine.

Class array pass problem

class myRandom {

public:

myRandom() {}

int *a_dev;

int *a_host;
Copy the code

};

myRandom mr; In the Cal calculation, the argument I pass is __global__ void Calls(myRandom *pist)

Then in Cal, pist->a_dev is used to point to the memory that needs to be modified, but the modification result is not correct, because even if the pointer is passed into the GPU, it will be copied again. But after copying it on the GPU, the pointer doesn’t point to the same area, right?

I thought the copy should point to the same region, so I did another test: first, verify that the address is different (the copy was actually carried out in the GPU).

int* b_dev;

int b_host = 12;

cudaMalloc((void**)&(b_dev), sizeof(int));

printf(“&&&&&&&&&&&&&&&&&    %d”, &mr);

Calls << <1, 50 >> > (&mr,b_dev); //

cudaMemcpy(&b_host, b_dev, sizeof(int), cudaMemcpyDeviceToHost);

printf(“*************** %d\n”, b_host); Cal functions:

global void Calls(myRandom *pist,int *b) {

int ii = threadIdx.x;

if (ii >= 50)return;

if (ii == 1)

    *b = (int)(&pist);
Copy the code

} The printed result is different, indicating that it is indeed copied when passed in.

And test the results of the copy.

printf(“&&&&&&&&&&&&&&&&&    %d”, (mr.a_dev));

Cal:

*b = (int)(pist->a_dev);

But it prints out the wrong results. Does this memory region not exist, or is it an error?

Class passed into the GPU copy and I set a member in myRandom,

myRandom() { sss = 88; }

int sss; And then in Cal:

if (ii == 1)

*b = pist->sss; The result is not properly amplitude. In other words, the myRandom pointer passed in has nothing inside the parameter, just an address.

I used to pass constructs that passed entities directly instead of Pointers, so:

Solution: Create an area on the GPU and copy the structure into it.