2014-02-22 37 views
0

我有一個Visual Studio 2012中的CUDA項目,包含一個函數,我想在VC++項目中使用它,謝謝Mr.Crovella我將我的CUDA項目目標從.exe.dll項目屬性/配置屬性/常規/配置類型。這裏是我的頭文件中定義我的功能:創建cuda dll並在VC++項目中使用

kernel.h當

#ifndef KERNEL_H 
#define KERNEL_H 

#ifdef __cplusplus 
    extern "C" { 
#endif 

void __declspec(dllexport) cuspDsolver(int *rowOffset, int *colIndex, double *values , double *X, double *rhs, int size, int nnz); 

#ifdef __cplusplus 
    } 
#endif 

#endif 

,這裏是我的功能實現:

kernel.cu

#include "kernel.h" 
#include <cusp/krylov/cg.h> 
#include <cusp/csr_matrix.h> 
#include <cusp/hyb_matrix.h> 
#include <cusp/gallery/poisson.h> 
#include <cusp/io/matrix_market.h> 
#include <cusp\print.h> 
#include <fstream> 
#include <conio.h> 
#include <math.h> 
#include <iostream> 
#include <windows.h> 

using namespace std; 


void cuspDsolver(int *rowOffset, int *colIndex, double *values , double *X, double *rhs, int size, int nnz) 
{ 
    cusp::csr_matrix<int,double,cusp::device_memory> A(size,size,nnz); 
    for (int i = 0; i < (size+1); i++) 
    { 
     A.row_offsets[i] = rowOffset[i]; 
    } 
     for (int i = 0; i < nnz; i++) 
    { 
     A.column_indices[i] = colIndex[i]; 
     A.values[i] = values[i]; 
    } 
    cusp::array1d<double,cusp::device_memory> XX(size,0.); 
    cusp::array1d<double,cusp::device_memory> B(size,0.); 

    for (int i = 0; i < size; i++) 
    { 
     B[i] = rhs[i]; 
    } 

    cusp::krylov::cg(A,XX,B); 

    for (int i = 0; i < size; i++) 
    { 
     X[i] = XX[i]; 
    } 
} 

,這裏是我的VC++項目(這是一個靜態庫system.lib項目,將用於quickMain.exe)我如何試圖用我的的.dll文件

system.lib

#ifdef __cplusplus 
    extern "C" { 
#endif 

void __declspec (dllimport) cuspDsolver(int *rowOffset, int *colIndex, double *values , double *X, double *rhs, int size, int nnz); 

#ifdef __cplusplus 
} 
#endif 
. 
. 
. 
. 
. 
. 
. 
int 
ProfileSPDLinDirectSolver::solve(void){ 
. 
. 
. 
. 
cuspDsolver(rowOffset,colIndex,values,answer,rightHandSide,theSize,nnz); 
. 
. 
. 
. 
} 

當我想建立這個項目(我做了我的dll文件複製到解決方案目錄和解決方案/ Debug目錄)我得到這些錯誤,請問我是否在創建或使用此dll時做了任何錯誤?

error LNK2019: unresolved external symbol __imp__cuspDsolver referenced in function   "public: virtual int __thiscall ProfileSPDLinDirectSolver::solve(void)" (?  [email protected]@@UAEHXZ)   2012\Projects\Ardalan_12\Win32\proj\quickMain\system.lib(ProfileSPDLinDirectSolver.obj) 

error LNK1120: 1 unresolved externals C:\Users\Administrator\Documents\Visual Studio 2012\Projects\Ardalan_12\Win32\bin\quickMain.exe 1 

謝謝先進。

+0

您是否將dll作爲庫鏈接到您的'system.lib'項目定義中? –

+0

我的問題解決了謝謝你,這是問題,但我有另一個問題,我在「CUDA解決方案」的功能非常快,但是當我將這個函數轉換爲VC++項目的dll時,它神祕緩慢,這是正常的嗎?我的意思是在dll中調用函數通常很慢或者還有另一個臃腫?在先進的感謝 – Alexander1991

+0

不知道你指的時間範圍很難說。我不希望dll機制對整體時間有很大影響。如果你的代碼在靜態鏈接的100毫秒內運行,並且200毫秒的dll運行,我想這可能是正常的,我不知道windows dll開銷應該是什麼。無論如何,這似乎是一個不同於你的鏈接問題的問題,如果你需要幫助,我會建議發佈一個新的SO問題 - 並提供更多細節。你爲什麼不發表一個答案來解釋你在這裏做了什麼。 –

回答

0

一旦dll從kernel.cu創建,你將有一個dll庫文件(比如kernel.dll)和一個dll庫導入文件(比如kernel.lib)。

在爲希望使用該DLL的任何項目,必須對dll庫導入文件鏈接你的VC/VS項目定義:

nvcc ... -lkernel 

添加此庫到你的項目定義的過程是您將用於添加任何其他庫的相同過程。確保kernel.dllkernel.lib也位於您指定的鏈接器路徑上。

而且,在運行時,您的kernel.dll庫需要與您的可執行文件在同一個目錄中,否則需要在Windows DLL加載路徑中。