创建Cuda DLL并在VC ++项目中使用它

时间:2014-02-22 15:11:54

标签: c++ visual-studio dll cuda

我在Visual Studio 2012中有一个CUDA项目,其中包含一个我希望在VC ++项目中使用它的功能,感谢Mr.Crovella我将我的CUDA项目目标从 .exe 更改为 项目属性/配置属性/常规/配置类型中的.dll 。这是我的头文件定义我的函数:

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 中使用)以及我如何尝试使用我的< strong> .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文件复制到解决方案目录和解决方案/调试目录中)我得到了这些错误,请你告诉我是否在创建或使用这个dll时做错了什么? / p>

error LNK2019: unresolved external symbol __imp__cuspDsolver referenced in function          "public: virtual int __thiscall ProfileSPDLinDirectSolver::solve(void)" (?     solve@ProfileSPDLinDirectSolver@@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

先谢谢。

2 个答案:

答案 0 :(得分:0)

从kernel.cu创建dll之后,您将生成一个dll库文件(例如,kernel.dll)和一个dll库导入文件(例如,kernel.lib)。

在任何希望使用该dll的项目的VC / VS项目定义中,您必须链接到dll库导入文件:

nvcc ... -lkernel

将此库添加到项目定义的过程与添加任何其他库时使用的过程相同。确保kernel.dllkernel.lib也在您指定的链接器路径上。

并且,在运行时,您的kernel.dll库需要与可执行文件位于同一目录中,或者位于windows dll加载路径中。

答案 1 :(得分:0)

您需要在函数声明上添加__declspec(dllexport)修饰符。此修饰符告诉编译器和链接器从DLL导出函数或变量,以供其他应用程序使用。 将以下内容添加到您的kernel.h,

#pragma once

#ifdef KERNEL_EXPORTS
#define KERNEL_API __declspec(dllexport)
#else
#define KERNEL_API __declspec(dllimport)
#endif

extern "C" KERNEL_API void cuspDsolver(int *rowOffset, int *colIndex, double *values ,double *X, double *rhs, int size, int nnz);

有关详细信息,请参考: https://docs.microsoft.com/zh-tw/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2019#to-add-a-header-file-to-the-dll