使用CUBLAS庫遇到的問題

http://blog.163.com/nuc_baixu/blog/static/251246078201591475128294/
http://blog.csdn.net/k531623594/article/details/50957528
http://blog.csdn.net/u012033124/article/details/52169823
出現(xiàn)錯(cuò)誤:無法解析的外部命令,在main函數(shù)中引用。
1.必須要添加相應(yīng)的依賴庫
2.調(diào)試–>屬性–>平臺(tái)–>活動(dòng)(x64)位

// CUDA runtime 庫 + CUBLAS 庫 
#include "cuda_runtime.h"
#include "cublas_v2.h"
#include <time.h>
#include <iostream>
using namespace std;
// 定義測試矩陣的維度
int const M = 5;
int const N = 10;

int main()
{
    // 定義狀態(tài)變量
    cublasStatus_t status;

    // 在 內(nèi)存 中為將要計(jì)算的矩陣開辟空間
    float *h_A = (float*)malloc(N*M*sizeof(float));
    float *h_B = (float*)malloc(N*M*sizeof(float));

    // 在 內(nèi)存 中為將要存放運(yùn)算結(jié)果的矩陣開辟空間
    float *h_C = (float*)malloc(M*M*sizeof(float));

    // 為待運(yùn)算矩陣的元素賦予 0-10 范圍內(nèi)的隨機(jī)數(shù)
    for (int i = 0; i<N*M; i++) {
        h_A[i] = (float)(rand() % 10 + 1);
        h_B[i] = (float)(rand() % 10 + 1);

    }

    // 打印待測試的矩陣
    cout << "矩陣 A :" << endl;
    for (int i = 0; i<N*M; i++){
        cout << h_A[i] << " ";
        if ((i + 1) % N == 0) cout << endl;
    }
    cout << endl;
    cout << "矩陣 B :" << endl;
    for (int i = 0; i<N*M; i++){
        cout << h_B[i] << " ";
        if ((i + 1) % M == 0) cout << endl;
    }
    cout << endl;

    /*
    ** GPU 計(jì)算矩陣相乘
    */

    // 創(chuàng)建并初始化 CUBLAS 庫對象
    cublasHandle_t handle;
    status = cublasCreate(&handle);

    if (status != CUBLAS_STATUS_SUCCESS)
    {
        if (status == CUBLAS_STATUS_NOT_INITIALIZED) {
            cout << "CUBLAS 對象實(shí)例化出錯(cuò)" << endl;
        }
        getchar();
        return EXIT_FAILURE;
    }

    float *d_A, *d_B, *d_C;
    // 在 顯存 中為將要計(jì)算的矩陣開辟空間
    cudaMalloc(
        (void**)&d_A,    // 指向開辟的空間的指針
        N*M * sizeof(float)    // 需要開辟空間的字節(jié)數(shù)
        );
    cudaMalloc(
        (void**)&d_B,
        N*M * sizeof(float)
        );

    // 在 顯存 中為將要存放運(yùn)算結(jié)果的矩陣開辟空間
    cudaMalloc(
        (void**)&d_C,
        M*M * sizeof(float)
        );

    // 將矩陣數(shù)據(jù)傳遞進(jìn) 顯存 中已經(jīng)開辟好了的空間
    cublasSetVector(
        N*M,    // 要存入顯存的元素個(gè)數(shù)
        sizeof(float),    // 每個(gè)元素大小
        h_A,    // 主機(jī)端起始地址
        1,    // 連續(xù)元素之間的存儲(chǔ)間隔
        d_A,    // GPU 端起始地址
        1    // 連續(xù)元素之間的存儲(chǔ)間隔
        );
    cublasSetVector(
        N*M,
        sizeof(float),
        h_B,
        1,
        d_B,
        1
        );

    // 同步函數(shù)
    cudaThreadSynchronize();

    // 傳遞進(jìn)矩陣相乘函數(shù)中的參數(shù),具體含義請參考函數(shù)手冊。
    float a = 1; float b = 0;
    // 矩陣相乘。該函數(shù)必然將數(shù)組解析成列優(yōu)先數(shù)組
    cublasSgemm(
        handle,    // blas 庫對象 
        CUBLAS_OP_T,    // 矩陣 A 屬性參數(shù)
        CUBLAS_OP_T,    // 矩陣 B 屬性參數(shù)
        M,    // A, C 的行數(shù) 
        M,    // B, C 的列數(shù)
        N,    // A 的列數(shù)和 B 的行數(shù)
        &a,    // 運(yùn)算式的 α 值
        d_A,    // A 在顯存中的地址
        N,    // lda
        d_B,    // B 在顯存中的地址
        M,    // ldb
        &b,    // 運(yùn)算式的 β 值
        d_C,    // C 在顯存中的地址(結(jié)果矩陣)
        M    // ldc
        );

    // 同步函數(shù)
    cudaThreadSynchronize();

    // 從 顯存 中取出運(yùn)算結(jié)果至 內(nèi)存中去
    cublasGetVector(
        M*M,    //  要取出元素的個(gè)數(shù)
        sizeof(float),    // 每個(gè)元素大小
        d_C,    // GPU 端起始地址
        1,    // 連續(xù)元素之間的存儲(chǔ)間隔
        h_C,    // 主機(jī)端起始地址
        1    // 連續(xù)元素之間的存儲(chǔ)間隔
        );

    // 打印運(yùn)算結(jié)果
    cout << "計(jì)算結(jié)果的轉(zhuǎn)置 ( (A*B)的轉(zhuǎn)置 ):" << endl;

    for (int i = 0; i<M*M; i++){
        cout << h_C[i] << " ";
        if ((i + 1) % M == 0) cout << endl;
    }

    // 清理掉使用過的內(nèi)存
    free(h_A);
    free(h_B);
    free(h_C);
    cudaFree(d_A);
    cudaFree(d_B);
    cudaFree(d_C);

    // 釋放 CUBLAS 庫對象
    cublasDestroy(handle);

    getchar();

    return 0;
}```
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/1516503-e3eec46b6a735e79.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容