如何使用extern CUDA设备变量 [英] How to use extern cuda device variables

查看:1165
本文介绍了如何使用extern CUDA设备变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要写code分成几个.CU文件。但我应该在哪里定义设备变量,是使用了很多.CU文件。

I need to write the code into several .cu files. But where should I define the device variables which are use for many .cu files.

一个例子

文件COMMON.H

File common.h

__device__ int x;

文件A.cu

__global__ void a() 

文件B.cu

__global__ void b() 

A(),B()都使用X。我该怎么办?

a(),b() both use x. what should I do?

在C语言中,我喜欢写东西
EXTERN 设备 INT X;
然后,我定义的设备 INT中X另一个地方。但在CUDA我不能这样做。如果我这样做,它告诉我'..........'$ P $这里pviously声明

In C language, I should write something like extern device int x; Then I define device int x in another place. But in CUDA I can not do it. If I do, it tells me ‘..........’ previously declared here

推荐答案

修改:@talonmies是正确的(像往常一样)。所以,我已经删除了我的意见约4.1 CUDA

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

此外,我给了编译命令是不完全正确。因此,让我有一个demonstrably工作并具有适当的指示代替我的答案。

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.

您需要CUDA 5.0和计算能力2.0或为此工作更多的设备。

我敢肯定,可能有更好的方法,但是这似乎为我工作:

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:​​

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;
}

编译:

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

输出:

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

对不起,我的previous错误。

Sorry for my previous errors.

这篇关于如何使用extern CUDA设备变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆