在Visual Studio C ++项目中使用cusp

时间:2014-02-21 18:37:31

标签: c++ visual-studio cuda cusp-library

我在VS2012旗舰版上使用cusp v.0.4.0,cuda V5.5。我使用新项目向导创建了一个CUDA项目,并为其项目属性\ VC ++目录\包含目录添加了cusp路径。我在VS2012生成的 *。cu 文件中编写了我的代码,该项目已成功编译和构建,但执行时出现 R6010 错误。我通过将项目属性\ CUDA C / C ++ \ Device \ Code Generation 的默认值从 compute_10,sm_10 更改为 compute_30,sm_30 ,这是我的sm版本。一切都运作良好。

现在我想在C ++项目中使用相同的代码。当我将它添加到新的C ++项目并且我添加了 VC ++包含目录的cusp路径时,项目构建失败,在多个文件中出现了大量语法错误:

  

错误5错误C2144:语法错误:'void'前面应加';' c:\ users \ administrator \ downloads \ android \ cusplibrary-master \ cusplibrary-master \ cusp \ detail \ device \ spmv \ coo_flat.h 164

     

22智能感知:期待';' c:\ Users \ Administrator \ Downloads \ Android \ cusplibrary-master \ cusplibrary-master \ cusp \ detail \ device \ 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;
}

1 个答案:

答案 0 :(得分:0)

  

&#34;现在我想在C ++编写的Project中使用这段代码,但是当我将相同的代码添加到新的C ++项目时......&#34;

这可能不会奏效。如果此代码在C ++项目中的.cpp中,则无法工作。

cusp是一个建立在CUDA之上的推力之上的模板库,所以cusp代码必须nvcc编译,即它必须在CUDA中VS中的项目,而不是普通的C ++项目。

所以回到VS中使用CUDA项目来获取尖点代码。