C++ class dll with CUDA member? -
i have c++ class-based dll. i'd convert of class members cuda based operation.
i using vs2012, windows 7, cuda6.5, sm_20;
say original superprojector.h file like:
class __declspec(dllexport) superprojector { public: superprojector(){}; ~superprojector(){}; void sumvectors(float* c, float* a, float* b, int n); };
and original sumvector()
function in superprojector.cpp
void superprojector::sumvectors(float* c, float* a, float* b, int n) { (int n = 1; n < n; b++) c[n] = a[n] + b[n]; }
i stuck on how should convert sumvector() cuda. specifically:
- i read posts saying add
__global__ __device__
keywords in front of class members work, need change suffix of cpp file cu? - i tried create cuda project beginning, seems vs2012 not give me option of creating dll once chose create cuda project.
i confused best way convert of members of tthis c++ class based dll cuda kernel functions. appreciate can offer ideas, or better simple examples.
create cuda project, let's call
cudasuperprojector
, add 2 filessuperprojector.cu
,superprojector.h
cudasuperprojector.h
class __declspec(dllexport) cudasuperprojector { public: cudasuperprojector(){ } ~cudasuperprojector(){ } void sumvectors(float* c, float* a, float* b, int n); };
cudasuperprojector.cu
#include <stdio.h> #include "cuda_runtime.h" #include "device_launch_parameters.h" #include "cudasuperprojector.h" __global__ void addkernel(float *c, const float *a, const float *b) { int = threadidx.x; c[i] = a[i] + b[i]; } // helper function using cuda add vectors in parallel. cudaerror_t addwithcuda(float *c, const float *a, const float *b, unsigned int size) { float *dev_a = 0; float *dev_b = 0; float *dev_c = 0; cudaerror_t cudastatus; // choose gpu run on, change on multi-gpu system. cudastatus = cudasetdevice(0); // allocate gpu buffers 3 vectors (two input, 1 output) . cudastatus = cudamalloc((void**)&dev_c, size * sizeof(float)); cudastatus = cudamalloc((void**)&dev_a, size * sizeof(float)); cudastatus = cudamalloc((void**)&dev_b, size * sizeof(float)); // copy input vectors host memory gpu buffers. cudastatus = cudamemcpy(dev_a, a, size * sizeof(float), cudamemcpyhosttodevice); cudastatus = cudamemcpy(dev_b, b, size * sizeof(float), cudamemcpyhosttodevice); // launch kernel on gpu 1 thread each element. addkernel << <1, size >> >(dev_c, dev_a, dev_b); // check errors launching kernel cudastatus = cudagetlasterror(); // cudadevicesynchronize waits kernel finish, , returns // errors encountered during launch. cudastatus = cudadevicesynchronize(); // copy output vector gpu buffer host memory. cudastatus = cudamemcpy(c, dev_c, size * sizeof(float), cudamemcpydevicetohost); return cudastatus; } void cudasuperprojector::sumvectors(float* c, float* a, float* b, int n) { cudaerror_t cudastatus = addwithcuda(c, a, b, n); if (cudastatus != cudasuccess) { fprintf(stderr, "cudasuperprojector::sumvectors failed!"); } }
note: in properties of file
cudasuperprojector.cu
item type
shouldcuda c/c++
.go properties of project , in
general
set value ofconfiguration type
dynamic library (.dll)
. creating library ready. compile project , in output folder findcudasuperprojector.dll
,cudasuperprojector.lib
. create directorycudasuperprojector\lib
, copycudasuperprojector.dll
,cudasuperprojector.lib
there. createcudasuperprojector\include
, copycudasuperprojector.h
in it.create
visual c++
project, let's callsuperprojector
. add filesuperprojector.cpp
project.superprojector.cpp
#include <stdio.h> #include "cudasuperprojector/cudasuperprojector.h" int main(int argc, char** argv) { float a[6] = { 0, 1, 2, 3, 4, 5 }; float b[6] = { 1, 2, 3, 4, 5, 6 }; float c[6] = { }; cudasuperprojector csp; csp.sumvectors(c, a, b, 6); printf("c = {%f, %f, %f, %f, %f, %f}\n", c[0], c[1], c[2], c[3], c[4], c[5]); return 0; }
in properties of project add path
dll
,lib
filesvc++ directories -> library directories
, exampled:\cudasuperprojector\lib;
, invc++ directories -> include directories
add path header, exampled:\cudasuperprojector\include;
. golinker -> input
, addcudasuperprojector.lib;
.now project should compile fine, when run it show error
the program can't start because cudasuperprojector.dll missing computer. try reinstalling program fix problem.
you need copy
cudasuperprojector.dll
output folder of project, under same foldersuperprojector.exe
. can manually or addcopy d:\cudasuperprojector\lib\cudasuperprojector.dll $(solutiondir)$(configuration)\
in
build events -> post-build events -> command line
,$(solutiondir)$(configuration)\
output path solution (seeconfiguration properties -> general -> output directory
).
Comments
Post a Comment