【发布时间】: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