How to use extern cuda device variables

我怕爱的太早我们不能终老 提交于 2019-12-03 17:17:22

EDIT : @talonmies was right (as usual). So I've deleted my comment about CUDA 4.1

Furthermore the compiling commands I gave were not quite right. So let me replace my answer with one that demonstrably works and has the proper instructions.

You need CUDA 5.0 and a compute capability 2.0 or greater device for this to work.

I'm sure there's probably a better way, but this seems to work for me:

com.h:

#ifndef DEVMAIN
extern __device__ int x;
#endif

a.cu:

#include "com.h"
__global__ void a(){

  x = -5;
}

b.cu:

#include <stdio.h>
#define DEVMAIN
#include "com.h"

extern __global__ void a();
__device__ int x;

__global__ void b(){

  x = 5;
}

int main() {
  int temp=7;
  cudaMemcpyToSymbol(x,&temp, sizeof(int));
  a<<<1,1>>>();
  cudaMemcpyFromSymbol(&temp,x,sizeof(int));
  printf("in host : %d\n",temp);
  b<<<1,1>>>();
  cudaMemcpyFromSymbol(&temp,x,sizeof(int));
  printf("in host2 : %d\n",temp);
  return 0;
}

compiling:

nvcc -arch=sm_20 -dc a.cu
nvcc -arch=sm_20 -dc b.cu
nvcc -arch=sm_20 -o ab a.o b.o

output:

$ ./ab
in host : -5
in host2 : 5
$

Sorry for my previous errors.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!