我在VS2012 Ultimate上使用cusp v.0.4.0,cuda V5.5。我使用新项目向导创建了一个CUDA项目,并添加了cusp路径到项目属性\ VC++目录\包含目录。我写了我的代码在由VS2012生成的* .cu文件中,项目编译和构建成功,但我得到了R6010执行错误。我通过改变项目属性的默认值,解决了这个问题\ CUDA C/C++ \设备\代码生成从compute_10,sm_10到compute_30,sm_30,这是我的SM版本。一切运作良好。在Visual Studio C++项目中使用cusp
现在我想在C++项目中使用相同的代码。
错误5错误C2144:语法错误:当我把它添加到一个新的C++项目,我加入了风口浪尖路径VC++包含目录,该项目构建在几个文件中有许多语法错误而导致失败“无效'应该在'之前';' c:\ users \ administrator \ downloads \ android \ cusplibrary-master \ cusplibrary-master \ cusp \ detail \ device \ spmv \ coo_flat.h 164
22 IntelliSense:expect a';' C:\用户\管理员\下载\的Android \ cusplibrary主\ cusplibrary主\尖\详细\设备\ SPMV \ coo_flat.h 272
...
有108个以上的错误等这些。如果这些是语法错误,为什么他们中没有任何一个出现在我的CUDA解决方案中?我如何在C++项目中成功构建我的代码?
#include <cuda.h>
#include <cuda_runtime.h>
#include <device_launch_parameters.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;
int main()
{
int N = 10000;
int nnz = 1005070;
DWORD dw1 = GetTickCount();
cusp::csr_matrix<int,double,cusp::device_memory> A(N,N,nnz);
DWORD dw2 = GetTickCount();
double dw3 = dw2 - dw1;
cout << "alocating A matrix time : " << dw3 << endl;
ifstream rowOffseFile;
ifstream colIndexFile;
ifstream valuesFile;
ifstream ansFile;
rowOffseFile.open("C:\\Users\\Administrator\\Documents\\MATLAB\\10000_0.01_RO.txt");
int *rowOffset = NULL;
rowOffset = (int *)malloc((N+1)*sizeof(int));
for (int i = 0; i < N+1; i++)
{
rowOffset[i] = 0;
}
int i =0;
if (rowOffseFile.is_open()) {
while (!rowOffseFile.eof()) {
rowOffseFile >> rowOffset[i];
i+=1;
}
}
rowOffseFile.close();
DWORD dw10 = GetTickCount();
for (int i = 0; i < (N+1); i++)
{
A.row_offsets[i] = rowOffset[i];
}
DWORD dw11 = GetTickCount();
double dw12 =dw11 - dw10;
///////////////////////////////////////////////////////////////////////////////////
colIndexFile.open("C:\\Users\\Administrator\\Documents\\MATLAB\\10000_0.01_CI.txt");
int *colIndex = NULL;
colIndex = (int *)malloc((nnz)*sizeof(int));
for (int i = 0; i < nnz; i++)
{
colIndex[i] = 0;
}
i =0;
if (colIndexFile.is_open()) {
while (!colIndexFile.eof()) {
colIndexFile >> colIndex[i];
//int temp = (int)output;
//cout<< colIndex[i] << endl;
i+=1;
}
}
colIndexFile.close();
DWORD ex1 = GetTickCount();
for (int i = 0; i < nnz; i++)
{
A.column_indices[i] = colIndex[i];
}
DWORD ex2 = GetTickCount();
double t = ex2-ex1;
/////////////////////////////////////////////////////////////
valuesFile.open("C:\\Users\\Administrator\\Documents\\MATLAB\\10000_0.01_V.txt");
double *values = NULL;
values = (double *)malloc((nnz)*sizeof(double));
for (int i = 0; i < nnz; i++)
{
values[i] = 0;
}
i =0;
if (valuesFile.is_open()) {
while (!valuesFile.eof()) {
valuesFile >> values[i];
//int temp = (int)output;
//cout<< colIndex[i] << endl;
i+=1;
}
}
valuesFile.close();
DWORD ex3 = GetTickCount();
for (int i = 0; i < nnz; i++)
{
A.values[i] = values[i];
}
DWORD ex4 = GetTickCount();
t = t+ex4-ex3+dw12;
cout << "time spent on initializing: " << t <<endl;
DWORD dw7 = GetTickCount();
cusp::array1d<double,cusp::device_memory> X(N,0.);
cusp::array1d<double,cusp::device_memory> B(N,1.);
DWORD dw8 = GetTickCount();
double dw9 = dw8-dw7;
cout << "time spent on allocating X and B :" << dw9 << endl;
DWORD dw4 = GetTickCount();
cusp::krylov::cg(A,X,B);
DWORD dw5 = GetTickCount();
double dw6 = dw5 - dw4;
std::cout << "time spenton solving : " << dw6 << std::endl;
//cusp::print(X);
ansFile.open("C:\\Users\\Administrator\\Documents\\MATLAB\\10000_0.01_X.txt");
double *ans = NULL;
ans = (double *)malloc((N)*sizeof(double));
for (int i = 0; i < N; i++)
{
ans[i] = 0;
}
i =0;
if (ansFile.is_open()) {
while (!ansFile.eof()) {
ansFile >> ans[i];
//int temp = (int)output;
//cout<< rowOffset[i] << endl;
i+=1;
}
}
ansFile.close();
double tol = 0;
double temp = 0;
for (int i = 0; i < N; i++)
{
temp = abs(X[i] - ans[i]);
if (temp>tol)
{
tol = temp;
}
}
cout << "max tol is :" << tol << endl;
getch();
return 0;
}
噢,我的上帝......没有办法呢?甚至无法从中创建静态或动态库并将其链接到项目? – Alexander1991
cusp代码需要由'nvcc'编译。这将在CUDA项目中以相对简单的方式进行。是的,如果你创建一个静态或动态库,这可能是另一种方式,尽管静态库仍然可能会带来一些挑战。单独的DLL是可行的。您也可以通过C++项目的包装函数调用您的cusp代码,即创建一个包含多个项目的VS解决方案,其中一个是C++,其中一个是CUDA项目。 –
亲爱的罗伯特非常感谢你,你建议的第二种方式似乎很容易,但是请你给我看一个关于创建一个包含cusp的DLL的教程或插图?感谢你的宝贵时间 – Alexander1991