本文介绍了如何设置我的code / VS10认识到.c文件中的CUDA函数调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有编译器编译我的.CU文件,我已(我认为)这些.CU文件中的全部操作,但是当我尝试打电话给他们(内核<<< 1 ,1>>>(无效)),编译器注册,由于CUDA语法语法错误。此外,调用,例如cudaMalloc C文件中失败。

下面有三个真的很短的文件,所以我可以告诉你它在哪里示数。

  // kernel.cu
#包括kernel.h
#包括LT&;&cuda.​​h GT;
#包括LT&;&cuda_runtime_api.h GT;__global__ int内核(无效){
    返回5;
}

  // kernel.h
的#ifndef _KERNEL_h_
#定义_KERNEL_h_为externCint内核(无效);#万一

  //的main.c
#包括kernel.h
#包括LT&;&cuda.​​h GT;
#包括LT&;&cuda_runtime_api.h GT;诠释主(){
   INT *设备_A;   cudaMalloc((无效**)及设备_A,sizeof的(INT));   内核<<< 1,1 GT;>>();
}

我从一些SDK例子头文件。另外,我有我的构建配置设置与CUDA 4.2,所以为什么.CU文件编译。如果我做了任何意外的语法错误,那是因为我简化它张贴,不,它实际上是在源,但请注明以防万一。

kernel.cu编译罚款。

kernel.h有一个错误:在外部......行串的错误C2059:语法错误。 (难道这是因为我把从一个C ++的例子吗?)

main.c中有一个错误:错误C2065:'内核':未声明的标识符

和:错误C2059:语法错误:'<'

但是当我注释掉内核调用,所以它只是cudaMalloc,我得到:错误LNK2019:​​解析外部符号_cudaMalloc @ 8函数_main引用

和:致命错误LNK1120:1无法解析的外部

有什么事情与Visual Studio 2010,或者是什么我不包括?从SDK的例子,我不知道我做错了什么,其他然后他们找到了一种方法,我认为,不使用三联支架(CTRL + F没有发现任何)。任何帮助是AP preciated。谢谢你在前进!

编辑:在看一些例子后,他们确实使用了三重括号语法就好了

编辑:对于使用此作为参考 __ __全球函数只能返回void。如果您尝试其他任何回报,像我一样,你会收到编译器错误。


解决方案

  1. 把那只调用CUDA内核函数的 .CU 文件。


  2. 设置VS2010编译使用CUDA编译器,而不是建在一个(使用CUDA规则文件(Cuda.props,Cuda.xml,Cuda.targets)位于CUDA SDK中)。CU文件


  3. 我建议将内核中的文件扩展名不同(如 .curnel 文件),使他们不会直接(仅当调用)编译。


  4. 我建议把那调用CUDA内核中的函数声明的 .cuh 文件。


Basically, I have the compiler compiling my .cu files and I have (I think) full operation within those .cu files, but when I try to call them (kernel<<<1,1>>>(void)), the compiler registers syntax errors due to the CUDA syntax. Also, calls like cudaMalloc fail within c files.

Here are three really short files, so I can tell you where it is erroring.

//kernel.cu    
#include "kernel.h"
#include <cuda.h>
#include <cuda_runtime_api.h>

__global__ int kernel(void){     
    return 5;
}

and

//kernel.h
#ifndef _KERNEL_h_
#define _KERNEL_h_

extern "C" int kernel(void);

#endif

and

//main.c
#include "kernel.h"
#include <cuda.h>
#include <cuda_runtime_api.h>

int main() {
   int* device_a;

   cudaMalloc( (void**)&device_a, sizeof(int) );

   kernel<<<1,1>>>();
}

I got the header file from some of the SDK examples. Also, I have my build configuration set with CUDA 4.2, hence why the .cu file compiles. If I made any incidental syntax errors, it is because I simplified it for posting, not that it is actually in the source, although please mention it just in case.

kernel.cu compiles fine.

kernel.h has an error: "error C2059: syntax error : 'string'" on the "extern..." line. (Could this be because I took that from a c++ example?)

main.c has an error: "error C2065: 'kernel' : undeclared identifier"

and: "error C2059: syntax error : '<'"

but when I comment out the kernel call, so it is just cudaMalloc, I get: "error LNK2019: unresolved external symbol _cudaMalloc@8 referenced in function _main"

and: "fatal error LNK1120: 1 unresolved externals"

Is it something with Visual Studio 2010, or is it something I am not including? From the SDK examples, I can't tell what I'm doing wrong, other then they found a way, I think, to not use the triple bracket (CTRL+F doesn't find any). Any help is appreciated. Thank you in advance!

EDIT: after looking at some more examples, they do use the triple bracket syntax just fine.

EDIT: For those using this as reference, __global__ functions can only return void. If you try to return anything else, as I did, you will receive compiler errors.

解决方案
  1. Put the functions that invoke CUDA kernel in .cu files.

  2. Set up VS2010 to compile CU files with the CUDA compiler, not the built in one (use the CUDA rules files (Cuda.props, Cuda.xml, Cuda.targets) located within the CUDA SDK).

  3. I recommend placing kernels in files with a different extension (e.g. .curnel files), so that they will not be compiled directly (only if called).

  4. I recommend putting the declaration of the functions that invoke CUDA kernels in .cuh files.

这篇关于如何设置我的code / VS10认识到.c文件中的CUDA函数调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 08:25