使用nvcc从CUDA创建DLL [英] Creating DLL from CUDA using nvcc

查看:386
本文介绍了使用nvcc从CUDA创建DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个CUDA代码( kernel.cu )创建一个.dll文件,以便从外部C程序中使用这个库。经过一些尝试,我只是在.cu文件中留下一个简单的C函数。代码如下:

I want to create a .dll from a CUDA code (kernel.cu) in order to use this library from an external C program. After some attempts I just left a simple C function in .cu file. Code follows:

kernel.cu

kernel.cu

#include <stdio.h>
#include "kernel.h"

void hello(const char *s) {
        printf("Hello %s\n", s);
}/*

kernel.h

#ifndef KERNEL_H
#define KERNEL_H

#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#ifdef __cplusplus
extern "C" {
#endif

void __declspec(dllexport) hello(const char *s);

#ifdef __cplusplus
}
#endif

#endif  // KERNEL_H

我试着先用 nvcc kernel.o c>,然后我使用 g ++ 创建DLL如下:

I tried to first generate a kernel.o object with nvcc and after i used g++ for creating DLL as following:

nvcc -c kernel.cu -o kernel.o
g++ -shared -o kernel.dll kernel.o -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\lib\x64" -lcudart

它工作正常,并生成 dll 。要测试DLL文件我写了这个简单的程序 main.c

It works fine and generates kernel.dll. To test DLL file I wrote this simple program main.c:

#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

void __declspec ( dllimport ) hello(const char *s);

#ifdef __cplusplus
}
#endif

int main(void) {
        hello("World");
        return 0;
}

编译:

g++ -o app.exe main.c -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\include" -L. -lkernel

结果是执行开始时出现内存访问错误。

Result is a memory access error when execution starts.

然而,如果我重命名.cu文件在.c(因为它只是C代码),使用相同的命令,它的工作。 nvcc的输出变化,据我所知,因为它使用默认C编译器而不是CUDA一个。

Nevertheless, if I rename .cu file in .c (as it is just C code), using the same commands, it does work. nvcc's output changes, as far as I know because it uses default C compiler instead of CUDA one.

你认为,是与nvcc有关的问题吗?

What do you think, is it a problem related with nvcc? Or am I making any mistake?

.cu

.cu

不起作用。

.cpp和.c

.cpp and .c


推荐答案

我仍然不知道为什么发生了(也许是因为不使用编译器,如Robert Crovella所说),但是替换这两个命令,使得一个DLL工作:

Solved. I still don't know why happened (maybe it is because of not using official compiler like Robert Crovella said), but replacing the two commands for making a DLL by this one works:

nvcc -o kernel.dll --shared kernel.cu

注意双破折号(nvcc以这种方式工作),而直接使用它而不是创建第一个 .o DLL从对象。

Note the double dash (nvcc works this way), and the fact of making it directly instead of creating first .o and then making DLL from the object.

这篇关于使用nvcc从CUDA创建DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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