C++ & Intel MKL 混合程式設計

Santorinisu發表於2018-05-10

1. 軟體版本資訊

  • Windows 10
  • Visual Studio 2015 Professional
  • Intel MKL


2. 軟體來源連結

Intel MKL下載連結:

方式 1:直接進入Intel Software官網,進行註冊後下載Intel Math Kernel Library(MKL) Package.

下載連結:https://software.intel.com/en-us/performance-libraries

方式 2:點選進入網盤分享連結。

下載連結:https://pan.baidu.com/s/16RZsLyJawUrRkYis3HuemA

密碼:9w7s


3. 程式程式碼

3.1 圖片形式



3.2 程式碼形式

程式碼主要源於Intel MKL Package - Examples.

#include "stdafx.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include "mkl.h"
#include "mkl_dss.h"
#include "mkl_types.h"
/*
** Define the array and rhs vectors
*/
#define NROWS       5
#define NCOLS       5
#define NNONZEROS   9
#define NRHS        1
static const MKL_INT nRows = NROWS;
static const MKL_INT nCols = NCOLS;
static const MKL_INT nNonZeros = NNONZEROS;
static const MKL_INT nRhs = NRHS;
static MKL_INT rowIndex[NROWS + 1] = { 1, 3, 5, 7, 9, 10 };
static MKL_INT columns[NNONZEROS] = { 1, 2, 1, 2, 3, 4, 3, 4, 5 };
static double values[NNONZEROS] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
static _DOUBLE_PRECISION_t rhs[NCOLS * 2];
static _DOUBLE_PRECISION_t solValues[NROWS] = { 0, 1, 2, 3, 4 };

MKL_INT
main()
{
	MKL_INT i, j;
	/* Allocate storage for the solver handle and the right-hand side. */
	_MKL_DSS_HANDLE_t handle;
	_INTEGER_t error;
	_CHARACTER_t statIn[] = "determinant";
	_CHARACTER_t *uplo = "initialize";
	_DOUBLE_PRECISION_t statOut[5], eps = 1e-6;
	MKL_INT opt = MKL_DSS_DEFAULTS, opt1;
	MKL_INT sym = MKL_DSS_NON_SYMMETRIC;
	MKL_INT type = MKL_DSS_INDEFINITE;
	/* --------------------- */
	/* Initialize the solver */
	/* --------------------- */

	error = dss_create(handle, opt);
	if (error != MKL_DSS_SUCCESS)
		goto printError;
	/* ------------------------------------------- */
	/* Define the non-zero structure of the matrix */
	/* ------------------------------------------- */
	error = dss_define_structure(handle, sym, rowIndex, nRows, nCols, columns, nNonZeros);
	if (error != MKL_DSS_SUCCESS)
		goto printError;
	/* ------------------ */
	/* Reorder the matrix */
	/* ------------------ */
	error = dss_reorder(handle, opt, 0);
	if (error != MKL_DSS_SUCCESS)
		goto printError;
	/* ------------------ */
	/* Factor the matrix  */
	/* ------------------ */
	error = dss_factor_real(handle, type, values);
	if (error != MKL_DSS_SUCCESS)
		goto printError;
	/* ------------------------ */
	/* Get the solution vector for Ax=b and ATx=b and check correctness */
	/* ------------------------ */
	for (i = 0; i < 3; i++)
	{
		if (i == 0)
		{
			uplo = "non-transposed";
			opt1 = MKL_DSS_DEFAULTS;
		}
		else if (i == 1)
		{
			uplo = "transposed";
			opt1 = MKL_DSS_TRANSPOSE_SOLVE;
		}
		else
			// Conjugate transposed == transposed for real matrices
			if (i == 2)
			{
				uplo = "conjugate transposed";
				opt1 = MKL_DSS_CONJUGATE_SOLVE;
			}

		printf("\nSolving %s system...\n", uplo);

		// Compute rhs respectively to uplo to have solution solValue
		mkl_dcsrgemv(uplo, &nRows, values, rowIndex, columns, solValues, rhs);

		// Nullify solution on entry (for sure)
		for (j = 0; j < nCols; j++)
			solValues[j] = 0.0;

		// Apply trans or non-trans option, solve system
		opt |= opt1;
		error = dss_solve_real(handle, opt, rhs, nRhs, solValues);
		if (error != MKL_DSS_SUCCESS)
			goto printError;
		opt &= ~opt1;

		// Check solution vector: should be {0,1,2,3,4}
		for (j = 0; j < nCols; j++)
		{
			if ((solValues[j] > j + eps) || (solValues[j] < j - eps))
			{
				printf("Incorrect solution\n");
				error = 1000 + i;
				goto printError;
			}
		}
		printf("Print solution array: ");
		for (j = 0; j < nCols; j++)
			printf(" %g", solValues[j]);

		printf("\n");
	}
	/* -------------------------- */
	/* Deallocate solver storage  */
	/* -------------------------- */
	error = dss_delete(handle, opt);
	if (error != MKL_DSS_SUCCESS)
		goto printError;
	/* ---------------------- */
	/* Print solution vector  */
	/* ---------------------- */
	printf("\nExample successfully PASSED!\n");

	getchar();
	exit(0);
printError:
	printf("Solver returned error code %d\n", error);
	exit(1);
}


3.3 編譯引數設定

因需採用C++語言呼叫Intel MKL包,所以需正確載入MKL的相關庫檔案和引用目錄等檔案。所需檔案目錄一覽如下:

所需檔案位置:


專案工程檔案路徑設定:




4. 結果顯示



5. 重要連結

  • https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/index.htm
  • https://software.intel.com/en-us/mkl/documentation/code-samples

相關文章