这个有教程:
https://blog.csdn.net/menglongbor/article/details/7526493
原文:https://stackoverflow.com/questions/16423643/creating-dll-from-cuda-using-nvcc
nvcc编译多个库,不知道是否成功:
nvcc -arch=sm_35 -o ps_roi_align_op.dll --shared ps_roi_align_op.cu rotated_ps_roi_align_op.cu ps_roi_align_grad_op.cu -I"C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0/include" -I"D:/ProgramData/Miniconda3/envs/python3.6/Lib/site-packages/tensorflow/include"
python调用:
if __name__ == '__main__': dll = CDLL(r"./kernel.dll") # CMPFUNC = CFUNCTYPE(c_void_p, c_void_p, c_int, c_int, c_int, c_int) # m_callback = CMPFUNC(callb_stream) # filename=b"D:\\project\\hik_client_dll\\x64\\Debug\\201805171600.dat" filename = b"201805032342.dat" # dll.play_file(str2char_p(filename), m_callback) begin=datetime.datetime.now() dll.hello(str2char_p(filename)) end=datetime.datetime.now() print("time",(end-begin).microseconds)
编译:
nvcc -arch=sm_35 -o kernel.dll --shared kernel.cu
使用NVCC的CUDA创建的DLL
我要创建从CUDA代码。DLL(kernel.cu
)为了从外部C程序这个库。经过努力我只留下了一个简单的C函数.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
我试图首先生成一个
kernel.o
使用对象
nvcc
后
g++
创建DLL如下:
nvcc -c kernel.cu -o kernel.o
g++ -shared -o kernel.dll kernel.o -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\lib\x64" -lcudart
它工作正常,并产生
kernel.dll
。为了测试DLL文件我写了这个简单的程序
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\v8.0\include" -L. -lkernel
结果是访问错误开始执行时。 不过,如果我.CU在c文件(因为它只是C代码),使用的命令,它的工作。输出变化,据我所知违约,而不是CUDA之一。 你觉得 CodeGo.net,是不是有相关的问题,或者我在做什么错误? 编辑:我忘了这可能是重要的信息。警告出现在以克++(创建DLL时),优先个呼叫,它们是不同的,具体取决于.CU c或.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
和它的作品。
本文地址 :CodeGo.net/3794193/
-------------------------------------------------------------------------------------------------------------------------
1. 解决了。我仍然不知道为什么发生了(不知是不是官方的编译器就像罗伯特Crovella说的),但在更换制作一个DLL通过这一个工程:
nvcc -o kernel.dll --shared kernel.cu
注意双破折号(以这种方式工作),并使其直接,而不是创建优先的事实
.o
然后使得DLL从对象。