【问题标题】:Creating Cuda dll and using it on VC++ project创建 Cuda dll 并在 VC++ 项目中使用它
【发布时间】:2014-03-24 05:57:36
【问题描述】:

我在 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 中使用)以及我如何尝试使用我的 .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 时是否做错了什么?

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

提前致谢。

【问题讨论】:

  • 您是否将 dll 作为库添加到您的 system.lib 项目定义中进行链接?
  • 我的问题解决了,谢谢你,这就是问题所在,但我还有一个问题,我在“CUDA 解决方案”中的函数非常快,但是当我调用这个从 VC++ 项目转换为 dll 的函数时,它神秘地慢了这正常吗?我的意思是在 dll 中调用函数通常很慢或者还有其他问题?感谢先进
  • 在不知道您所指的时间范围的情况下很难说。我不认为 dll 机制会对整体时序产生很大影响。如果您的代码以 100 毫秒静态链接运行,并且 200 毫秒作为 dll 运行,我想这可能是正常的,我不确切知道 windows dll 开销应该是多少。无论如何,这似乎与您的链接问题不同,如果您需要帮助,我建议发布一个新的 SO 问题 - 并提供更多详细信息。你为什么不发布一个答案来解释你在这里做了什么。

标签: c++ visual-studio dll cuda


【解决方案1】:

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

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

nvcc ... -lkernel

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

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

【讨论】:

    【解决方案2】:

    您需要在函数声明中添加 __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

    【讨论】:

    • 请在问题本身中包含相关链接的要点,因为外部链接可能会随着时间而改变或中断。在 Stack Overflow 上不鼓励仅使用代码的答案,因为它们没有解释它如何解决问题,因此这些信息将使它成为更好的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 2012-08-15
    • 2013-05-01
    • 1970-01-01
    相关资源
    最近更新 更多