CUDA8.0+VS2013的安裝和配置


  首先聲明,本文借鑒自:http://blog.csdn.net/u011314529/article/details/51505029

所以,可參考鏈接的博文。但原文有個瑕疵就是,cublas.lib錯寫成了cudlas.lib。

  其次,我還是記下我的CUDA8.0的安裝和測試過程,是為備忘。

  步驟如下:

    1.下載安裝CUDA:

       1.1  下載。請到 cuda官網,選擇合適的版本。如果版本不合適,安裝的時候會提示的,但還是下載最新的比較好;

       1.2  安裝。雙擊cuda_7.5.18_win10.exe,一步步來就好。

 

    2.VS2013配置和測試

       2.1 重啟計算機。關於是否添加環境變量,筆者安裝的時候系統已自動添加好對應的環境變量,如果沒有,請查看上文鏈接的博文;

       2.2 配置VS。也請參考上述博文,不再贅述。

 

    3.測試

    上兩個測試文件。

    3.1

 1 #include< stdio.h> 
 2     #include "cuda_runtime.h" 
 3     #include "device_launch_parameters.h" 
 4     bool InitCUDA() 
 5     { 
 6         int count; 
 7         cudaGetDeviceCount(&count); 
 8         if(count == 0) 
 9         { 
10             fprintf(stderr, "There is no device.\n"); 
11             return false; 
12         } 
13         int i; 
14         for(i = 0; i < count; i++) 
15         { 
16             cudaDeviceProp prop; 
17             if(cudaGetDeviceProperties(&prop, i) == cudaSuccess) 
18             { 
19                 if(prop.major >= 1) 
20                 { 
21                     break; 
22                 } 
23             } 
24         } 
25         if(i == count) 
26         { 
27             fprintf(stderr, "There is no device supporting CUDA 1.x.\n"); 
28             return false; 
29         } 
30         cudaSetDevice(i); 
31         return true; 
32     } 
33      
34     int main() 
35     { 
36         if(!InitCUDA()) 
37         { 
38             return 0; 
39         } 
40         printf("HelloWorld, CUDA has been initialized.\n"); 
41         return 0; 
42     } 

 

 

      3.2

  1 // CUDA runtime 庫 + CUBLAS 庫 
  2 #include "cuda_runtime.h"
  3 #include "cublas_v2.h"
  4 
  5 #include <time.h>
  6 #include <iostream>
  7 
  8 using namespace std;
  9 
 10 // 定義測試矩陣的維度
 11 int const M = 5;
 12 int const N = 10;
 13 
 14 int main() 
 15 {   
 16     // 定義狀態變量
 17     cublasStatus_t status;
 18 
 19     // 在 內存 中為將要計算的矩陣開辟空間
 20     float *h_A = (float*)malloc (N*M*sizeof(float));
 21     float *h_B = (float*)malloc (N*M*sizeof(float));
 22     
 23     // 在 內存 中為將要存放運算結果的矩陣開辟空間
 24     float *h_C = (float*)malloc (M*M*sizeof(float));
 25 
 26     // 為待運算矩陣的元素賦予 0-10 范圍內的隨機數
 27     for (int i=0; i<N*M; i++) {
 28         h_A[i] = (float)(rand()%10+1);
 29         h_B[i] = (float)(rand()%10+1);
 30     
 31     }
 32     
 33     // 打印待測試的矩陣
 34     cout << "矩陣 A :" << endl;
 35     for (int i=0; i<N*M; i++){
 36         cout << h_A[i] << " ";
 37         if ((i+1)%N == 0) cout << endl;
 38     }
 39     cout << endl;
 40     cout << "矩陣 B :" << endl;
 41     for (int i=0; i<N*M; i++){
 42         cout << h_B[i] << " ";
 43         if ((i+1)%M == 0) cout << endl;
 44     }
 45     cout << endl;
 46     
 47     /*
 48     ** GPU 計算矩陣相乘
 49     */
 50 
 51     // 創建並初始化 CUBLAS 庫對象
 52     cublasHandle_t handle;
 53     status = cublasCreate(&handle);
 54     
 55     if (status != CUBLAS_STATUS_SUCCESS)
 56     {
 57         if (status == CUBLAS_STATUS_NOT_INITIALIZED) {
 58             cout << "CUBLAS 對象實例化出錯" << endl;
 59         }
 60         getchar ();
 61         return EXIT_FAILURE;
 62     }
 63 
 64     float *d_A, *d_B, *d_C;
 65     // 在 顯存 中為將要計算的矩陣開辟空間
 66     cudaMalloc (
 67         (void**)&d_A,    // 指向開辟的空間的指針
 68         N*M * sizeof(float)    // 需要開辟空間的字節數
 69     );
 70     cudaMalloc (
 71         (void**)&d_B,    
 72         N*M * sizeof(float)    
 73     );
 74 
 75     // 在 顯存 中為將要存放運算結果的矩陣開辟空間
 76     cudaMalloc (
 77         (void**)&d_C,
 78         M*M * sizeof(float)    
 79     );
 80 
 81     // 將矩陣數據傳遞進 顯存 中已經開辟好了的空間
 82     cublasSetVector (
 83         N*M,    // 要存入顯存的元素個數
 84         sizeof(float),    // 每個元素大小
 85         h_A,    // 主機端起始地址
 86         1,    // 連續元素之間的存儲間隔
 87         d_A,    // GPU 端起始地址
 88         1    // 連續元素之間的存儲間隔
 89     );
 90     cublasSetVector (
 91         N*M, 
 92         sizeof(float), 
 93         h_B, 
 94         1, 
 95         d_B, 
 96         1
 97     );
 98 
 99     // 同步函數
100     cudaThreadSynchronize();
101 
102     // 傳遞進矩陣相乘函數中的參數,具體含義請參考函數手冊。
103     float a=1; float b=0;
104     // 矩陣相乘。該函數必然將數組解析成列優先數組
105     cublasSgemm (
106         handle,    // blas 庫對象 
107         CUBLAS_OP_T,    // 矩陣 A 屬性參數
108         CUBLAS_OP_T,    // 矩陣 B 屬性參數
109         M,    // A, C 的行數 
110         M,    // B, C 的列數
111         N,    // A 的列數和 B 的行數
112         &a,    // 運算式的 α 值
113         d_A,    // A 在顯存中的地址
114         N,    // lda
115         d_B,    // B 在顯存中的地址
116         M,    // ldb
117         &b,    // 運算式的 β 值
118         d_C,    // C 在顯存中的地址(結果矩陣)
119         M    // ldc
120     );
121     
122     // 同步函數
123     cudaThreadSynchronize();
124 
125     // 從 顯存 中取出運算結果至 內存中去
126     cublasGetVector (
127         M*M,    //  要取出元素的個數
128         sizeof(float),    // 每個元素大小
129         d_C,    // GPU 端起始地址
130         1,    // 連續元素之間的存儲間隔
131         h_C,    // 主機端起始地址
132         1    // 連續元素之間的存儲間隔
133     );
134     
135     // 打印運算結果
136     cout << "計算結果的轉置 ( (A*B)的轉置 ):" << endl;
137 
138     for (int i=0;i<M*M; i++){
139             cout << h_C[i] << " ";
140             if ((i+1)%M == 0) cout << endl;
141     }
142     
143     // 清理掉使用過的內存
144     free (h_A);
145     free (h_B);
146     free (h_C);
147     cudaFree (d_A);
148     cudaFree (d_B);
149     cudaFree (d_C);
150 
151     // 釋放 CUBLAS 庫對象
152     cublasDestroy (handle);
153 
154     getchar();
155     
156     return 0;
157 }

 

特別注意,是cublas.lib,不是cudlas.lib

祝好運。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM