Intel OneAPI MKL + Visual Studio 2019 C配置

利用VS2019+MKL加速矩阵运算。

软件版本

Intel OneAP 2022.0.2
Visual Studio 2019 Community

工程配置

  • 新建空工程


    image.png
  • 打开OneAPI选项


    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");
    }
}

运行和调试

  • 偶然出现的奇怪问题,在设置OneAPI方式为Sequential的时候,程序一直可以运行,但是OneAPI设置为Parallel后,输入的N只能是小于15(多次实测),不然就会报错!


    Parallel N=14

    Parallel N=15

    错误
  • 解决方法,在debug出来的exe文件夹下放入libiomp5md.dll


点亮“在看”
欢迎关注、收藏、转载哈!
最有意思的GNSS NEWS…不定期更新…

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 听师兄说IT++库可以用来做ICA,google找了半天资料才装好,为了后来的同学们少走弯路,把安装过程记录下来。...
    Yuu_CX阅读 1,794评论 0 0
  • 软件测试用例 1.测试用例的概念和作用 1.1.引言 对一个测试工程师来说,测试用例的设计编写是一项必须掌握的能力...
    ae1c0a8ab70d阅读 834评论 0 0
  • 一 测试用例(Test Case) 测试用例(Test Case)是指对一项特定的软件产品进行测试任务的描述,体现...
    IT_Bears阅读 1,741评论 0 0
  • GW1NR-9 开发板手册 1.概述非常感谢选择SZFPGA GW1NR-9开发板。 GW1NR-9 开发板特性:...
    lichenllin阅读 559评论 0 1
  • 1.测试用例的概念和作用 1.1.引言 对一个测试工程师来说,测试用例的设计编写是一项必须掌握的能力,但有效的设计...
    Anwfly阅读 1,041评论 0 2