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:

  1. i read posts saying add __global__ __device__ keywords in front of class members work, need change suffix of cpp file cu?
  2. 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.

  1. create cuda project, let's call cudasuperprojector , add 2 files superprojector.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 should cuda c/c++.

  2. go properties of project , in general set value of configuration type dynamic library (.dll). creating library ready. compile project , in output folder find cudasuperprojector.dll , cudasuperprojector.lib. create directory cudasuperprojector\lib , copy cudasuperprojector.dll , cudasuperprojector.lib there. create cudasuperprojector\include , copy cudasuperprojector.h in it.

  3. create visual c++ project, let's call superprojector. add file superprojector.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; } 
  4. in properties of project add path dll , lib files vc++ directories -> library directories, example d:\cudasuperprojector\lib;, in vc++ directories -> include directories add path header, example d:\cudasuperprojector\include;. go linker -> input , add cudasuperprojector.lib;.

  5. 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 folder superprojector.exe. can manually or add

    copy d:\cudasuperprojector\lib\cudasuperprojector.dll $(solutiondir)$(configuration)\ 

    in build events -> post-build events -> command line, $(solutiondir)$(configuration)\ output path solution (see configuration properties -> general -> output directory).


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -