#include <cuda.h>
#include <stdio.h>
int getMulprocessorCount(){
        cudaDeviceProp prop;
        cudaGetDeviceProperties(&prop,0);
        return prop.multiProcessorCount;
}

__constant__ int a[10]={1,2,3,4,5,6,7,8,9};
__global__  void add(int *c){
   c[1]=a[2];
}

int main(){
        int *c;
        int h_c[10];
        int mpc = getMulprocessorCount();
        cudaMalloc((void **)&c,10*sizeof(int));
        add<<<1,1>>>(c);
        cudaMemcpy(h_c,c,sizeof(int)*10,cudaMemcpyDeviceToHost);
        printf("num is %d",h_c[1]);
}

  常量内存可以在定义的时候初始化,可以直接在核函数中使用。

相关文章:

  • 2022-12-23
  • 2021-11-10
  • 2021-10-01
  • 2022-03-04
  • 2021-12-22
  • 2021-09-26
  • 2021-06-01
  • 2021-12-29
猜你喜欢
  • 2021-09-20
  • 2021-07-13
  • 2021-09-22
  • 2022-12-23
  • 2021-10-04
  • 2022-12-23
相关资源
相似解决方案