2014-02-21 190 views
0

我在VS2012 Ultimate上使用cusp v.0.4.0,cuda V5.5。我使用新項目嚮導創建了一個CUDA項目,並添加了cusp路徑到項目屬性\ VC++目錄\包含目錄。我寫了我的代碼在由VS2012生成的* .cu文件中,項目編譯和構建成功,但我得到了R6010執行錯誤。我通過改變項目屬性的默認值,解決了這個問題\ CUDA C/C++ \設備\代碼生成compute_10,sm_10compute_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; 
} 

回答

0

「現在我想用這段代碼在C++的書面計劃,但是當我補充說,同樣的代碼到一個新的C++項目......」

那可能無法工作。如果此代碼位於C++項目中的.cpp中,則該代碼無效。

風口浪尖的是,建立在推力其頂部是建立在CUDA之上的模板庫,所以風口浪尖碼必須nvcc進行編譯,即它必須是在VS一個CUDA工程,不是一個普通的C++項目。

因此,回到在VS中使用CUDA項目來獲取您的cusp代碼。

+0

噢,我的上帝......沒有辦法呢?甚至無法從中創建靜態或動態庫並將其鏈接到項目? – Alexander1991

+0

cusp代碼需要由'nvcc'編譯。這將在CUDA項目中以相對簡單的方式進行。是的,如果你創建一個靜態或動態庫,這可能是另一種方式,儘管靜態庫仍然可能會帶來一些挑戰。單獨的DLL是可行的。您也可以通過C++項目的包裝函數調用您的cusp代碼,即創建一個包含多個項目的VS解決方案,其中一個是C++,其中一個是CUDA項目。 –

+0

親愛的羅伯特非常感謝你,你建議的第二種方式似乎很容易,但是請你給我看一個關於創建一個包含cusp的DLL的教程或插圖?感謝你的寶貴時間 – Alexander1991

相關問題