分配显存空间

示例代码:

  1. #define N 10
  2. char *p =NULL
  3. cudaMalloc((void**)&p,N*sizeof(char));

为什么要使用void**?

All CUDA API functions return an error code (or cudaSuccess if no error occured). All other parameters are passed by reference. However, in plain C you cannot have references, that’s why you have to pass an address of the variable that you want the return information to be stored. Since you are returning a pointer, you need to pass a double-pointer.

Another well-known function which operates on addresses for the same reason is the scanf function. How many times have you forgotten to write this & before the variable that you want to store the value to? ;)
翻译过来核心观点就是:
因为cudaMalloc 返回的的error code ,没有返回指针,且所有参数都是引用传递。当想要修改一个值的时候,必须传递这个值的地址。所以想要修改一个指针的值,就必须传递二级指针

GPU调用显存内的空间:

示例代码:

对字符在GPU进行加密操作
操作完成后通过CPU在显示器上显示

  1. __global__ encrypt_p(char *s1,char *s2){
  2. int idx= threadIdx.x;
  3. s1[id]=s2[id]+3;
  4. }

分配显存空间

  1. char *d_A=NULL;
  2. char *d_B=NULL;
  3. cudaMalloc((void **)&d_A, strlen(A)*sizeof(char));
  4. cudaMalloc((void **)&d_B, strlen(A)*sizeof(char));

将内存中的字符串赋值到显存中的字符串中

  1. cudaMemcpy(d_A, A, strlen(A)*sizeof(char), cudaMemcpyHostToDevice);

在GPU中对显存中字符串进行操作

  1. encrypt_p<<<1, strlen(A)>>>(d_A, d_B);

将显存中的字符串传回到内存中的字符串中

  1. cudaMemcpy(B, d_B, strlen(A)*sizeof(char), cudaMemcpyDeviceToHost);