How to use device pointer on host side(e.g. cudaMalloc)?
I first define a device pointer and then want to allocate memory for it. But it failed.
It seems that I can not use the device point directly on host side just like on device side.
But I don’t know how to realize this. (Allocating memory for a device pointer on host side)
Someone suggest me this way define pointer first on host side as the following
float *Ad;
int main(void)
{
cudaMalloc((void**)&Ad,size);
return 0;
}
But I don’t like this way because I have to pass lots of pointer arguments to gpu side functions.
I prefer to define all of the variables in global scope and use them directly in my gpu side functions.
Anyone can help me? Thank you in advance.
The following is the simplest code:
[codebox]
#include “stdio.h”
device int* Ad;
int *Ah;
global void test_kernel(void)
{
const int tid=threadIdx.x;
Ad[tid]=tid;
return;
}
int main(void)
{
int size=10;
Ah=(int*)malloc(size*sizeof(int));
cudaMalloc(Ad,size*sizeof(int));
test_kernel<<<1,size>>>();
cudaMemcpyFromSymbol(Ah,“Ad”,size*sizeof(int));
for(int i=0;i<size;i++)
printf("%d %d \n",i,Ah[i]);
free(Ah);
cudaFree(Ad);
return 0;
}
[/codebox]
I also tryied using a temporary pointer on host side and then copy the value to device pointer , but also failed.
In theory, it should work. But …
The following is the source code.
[codebox]
#include “stdio.h”
device int* Ad;
int *Ah;
global void test_kernel(void)
{
const int tid=threadIdx.x;
Ad[tid]=tid;
return;
}
int main(void)
{
int size=10;
int *tmp;
Ah=(int*)malloc(size*sizeof(int));
if(cudaMalloc((void**)&tmp,size*sizeof(int))!=cudaSuccess)
{
printf("cuda malloc error\n");
return 1;
}
if(cudaMemcpyToSymbol(“Ad”,tmp,sizeof(void*))!=cudaSucess)
{
printf("cuda mem copy error\n");
return 1;
};
test_kernel<<<1,size>>>();
cudaMemcpyFromSymbol(Ah,“Ad”,size*sizeof(int));
for(int i=0;i<size;i++)
printf("%d %d \n",i,Ah[i]);
free(Ah);
cudaFree(Ad);
return 0;
}
[/codebox]