问题
I'm using cusp v.0.4.0, cuda V5.5 on VS2012 Ultimate. I created a CUDA project using the new project wizard and added the cusp path to it's project properties\VC++ Directories\Include Directories. I wrote my code in the *.cu file generated by VS2012, the project compiled and built successfully but I got R6010 error on execution. I solved this issue by changing the default value of project properties\CUDA C/C++\Device\Code Generation from compute_10,sm_10 to compute_30,sm_30, which is my sm version. Everything worked well.
Now I want to use the same code in a C++ project. When I added it to a new C++ project and I added the cusp path to VC++ Include Directories, the project build failed with numerous syntax errors in several files:
Error 5 error C2144: syntax error : 'void' should be preceded by ';' c:\users\administrator\downloads\android\cusplibrary-master\cusplibrary-master\cusp\detail\device\spmv\coo_flat.h 164
22 IntelliSense: expected a ';' c:\Users\Administrator\Downloads\Android\cusplibrary-master\cusplibrary-master\cusp\detail\device\spmv\coo_flat.h 272
...
There are 108 more errors like these. If these are syntax errors, why didn't any of them show up in my CUDA solution? How can I successfully build my code in a C++ project?
#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;
}
回答1:
"Now I want to use this piece of code in a written Project in C++, but when I added that very same code to a new C++ project ... "
That probably won't work. If this code is in a .cpp in a C++ project, it won't work.
cusp is a template library that is built on top of thrust which is built on top of CUDA, so cusp codes must be compiled by nvcc
, i.e. it must be in a CUDA project in VS, not an ordinary C++ project.
So go back to using a CUDA project in VS for your cusp codes.
来源:https://stackoverflow.com/questions/21942540/using-cusp-in-a-visual-studio-c-project