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

查看:26
本文介绍了使用 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

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

void hello(const char *s) {
        printf("Hello %s
", 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 对象,然后使用 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 FilesNVIDIA GPU Computing ToolkitCUDAv5.0libx64" -lcudart

它工作正常并生成 kernel.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 FilesNVIDIA GPU Computing ToolkitCUDAv5.0include" -L. -lkernel

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

Result is a memory access error when execution starts.

尽管如此,如果我在 .c 中重命名 .cu 文件(因为它只是 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?

编辑:我忘记了一些可能很重要的信息.警告出现在第一次调用 g++ 时(当创建 dll 时),它们根据 .cu .c 或 .cpp 的不同而不同.

EDIT: I forgot some info which may be important. Warnings appear in the first call to g++ (when dll is created) and they are different depending on whether .cu .c or .cpp.

.cu

Warning: .drectve `/FAILIFMISMATCH:"_MSC_VER=1600" /FAILIFMISMATCH:"_ITERATOR_DEBUG_LEVEL=0" 
/DEFAULTLIB:"libcpmt" /DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" /EXPORT:hello ' unrecognized

它不起作用.

.cpp 和 .c

Warning: .drectve `/DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" /EXPORT:hello ' unrecognized

它有效.

推荐答案

解决了.我仍然不知道为什么会发生(可能是因为没有像 Robert Crovella 所说的那样使用 official 编译器),但是用这个替换用于制作 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天全站免登陆