Intel OneAPI MKL + Visual Studio 2019 C配置

利用VS2019+MKL加速矩陣運(yùn)算。

軟件版本

Intel OneAP 2022.0.2
Visual Studio 2019 Community

工程配置

  • 新建空工程


    image.png
  • 打開OneAPI選項(xiàng)


    image.png
  • 加入代碼(官方示例)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "mkl.h"

void print_arr(int N, char* name, double* array);
void init_arr(int N, double* a);
void Dgemm_multiply(double* a, double* b, double* c, int N);

int main(int argc, char* argv[])
{
    clock_t start, stop;
    int i, j;
    int N;
    double* a;
    double* b;
    double* c;
    if (argc < 2)
    {
        printf("Enter matrix size N=");
        //please enter small number first to ensure that the 
        //multiplication is correct! and then you may enter 
        //a "reasonably" large number say like 500 or even 1000
        scanf("%d", &N);
    }
    else
    {
        N = atoi(argv[1]);
    }

    a = (double*)malloc(sizeof(double) * N * N);
    b = (double*)malloc(sizeof(double) * N * N);
    c = (double*)malloc(sizeof(double) * N * N);

    init_arr(N, a);
    init_arr(N, b);

    //DGEMM Multiply
    //reallocate to force cash to be flushed
    a = (double*)malloc(sizeof(double) * N * N);
    b = (double*)malloc(sizeof(double) * N * N);
    c = (double*)malloc(sizeof(double) * N * N);
    init_arr(N, a);
    init_arr(N, b);

    start = clock();
    //for(i=0;i<1000;i++)
    Dgemm_multiply(a, b, c, N);
    stop = clock();

    printf("Dgemm_multiply(). Elapsed time = %g seconds\n",
        ((double)(stop - start)) / CLOCKS_PER_SEC);
    //print simple test case of data to be sure multiplication is correct
    if (N < 7) {
        print_arr(N, "a", a);
        print_arr(N, "b", b);
        print_arr(N, "c", c);
    }

    free(a);
    free(b);
    free(c);

    return 0;
}


//DGEMM way. The PREFERED way, especially for large matrices
void Dgemm_multiply(double* a, double* b, double* c, int N)
{

    double alpha = 1.0, beta = 0.;
    int incx = 1;
    int incy = N;
    cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, N, N, N, alpha, b, N, a, N, beta, c, N);
}

//initialize array with random data
void init_arr(int N, double* a)
{
    int i, j;
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            a[i * N + j] = (i + j + 1) % 10; //keep all entries less than 10. pleasing to the eye!
        }
    }
}

//print array to std out
void print_arr(int N, char* name, double* array)
{
    int i, j;
    printf("\n%s\n", name);
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++) {
            printf("%g\t", array[N * i + j]);
        }
        printf("\n");
    }
}

運(yùn)行和調(diào)試

  • 偶然出現(xiàn)的奇怪問題,在設(shè)置OneAPI方式為Sequential的時(shí)候,程序一直可以運(yùn)行,但是OneAPI設(shè)置為Parallel后,輸入的N只能是小于15(多次實(shí)測),不然就會(huì)報(bào)錯(cuò)!


    Parallel N=14

    Parallel N=15

    錯(cuò)誤
  • 解決方法,在debug出來的exe文件夾下放入libiomp5md.dll


點(diǎn)亮“在看”
歡迎關(guān)注、收藏、轉(zhuǎn)載哈!
最有意思的GNSS NEWS…不定期更新…

?著作權(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)容

  • 聽師兄說IT++庫可以用來做ICA,google找了半天資料才裝好,為了后來的同學(xué)們少走彎路,把安裝過程記錄下來。...
    Yuu_CX閱讀 2,002評論 0 0
  • 軟件測試用例 1.測試用例的概念和作用 1.1.引言 對一個(gè)測試工程師來說,測試用例的設(shè)計(jì)編寫是一項(xiàng)必須掌握的能力...
    ae1c0a8ab70d閱讀 890評論 0 0
  • 一 測試用例(Test Case) 測試用例(Test Case)是指對一項(xiàng)特定的軟件產(chǎn)品進(jìn)行測試任務(wù)的描述,體現(xiàn)...
    IT_Bears閱讀 2,025評論 0 0
  • GW1NR-9 開發(fā)板手冊 1.概述非常感謝選擇SZFPGA GW1NR-9開發(fā)板。 GW1NR-9 開發(fā)板特性:...
    lichenllin閱讀 714評論 0 1
  • 1.測試用例的概念和作用 1.1.引言 對一個(gè)測試工程師來說,測試用例的設(shè)計(jì)編寫是一項(xiàng)必須掌握的能力,但有效的設(shè)計(jì)...
    Anwfly閱讀 1,262評論 0 2

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