From bd93f87c6ed98f94c8acbc884b06689afdfa08c8 Mon Sep 17 00:00:00 2001 From: Aleksei Tonkov Date: Tue, 8 Sep 2020 12:21:07 +0300 Subject: [PATCH 1/6] Added oneMKL/matrix_mul Signed-off-by: Aleksei Tonkov --- Libraries/oneMKL/matrix_mul/License.txt | 8 + Libraries/oneMKL/matrix_mul/Makefile | 27 +++ Libraries/oneMKL/matrix_mul/Makefile.win | 24 +++ Libraries/oneMKL/matrix_mul/README.md | 51 +++++ .../oneMKL/matrix_mul/matrix_mul_mkl.cpp | 190 ++++++++++++++++++ Libraries/oneMKL/matrix_mul/sample.json | 32 +++ 6 files changed, 332 insertions(+) create mode 100644 Libraries/oneMKL/matrix_mul/License.txt create mode 100644 Libraries/oneMKL/matrix_mul/Makefile create mode 100644 Libraries/oneMKL/matrix_mul/Makefile.win create mode 100644 Libraries/oneMKL/matrix_mul/README.md create mode 100644 Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp create mode 100644 Libraries/oneMKL/matrix_mul/sample.json diff --git a/Libraries/oneMKL/matrix_mul/License.txt b/Libraries/oneMKL/matrix_mul/License.txt new file mode 100644 index 0000000000..9cde07f558 --- /dev/null +++ b/Libraries/oneMKL/matrix_mul/License.txt @@ -0,0 +1,8 @@ +Copyright Intel Corporation + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/Libraries/oneMKL/matrix_mul/Makefile b/Libraries/oneMKL/matrix_mul/Makefile new file mode 100644 index 0000000000..5ec9dbfb07 --- /dev/null +++ b/Libraries/oneMKL/matrix_mul/Makefile @@ -0,0 +1,27 @@ +DPCPP_CXX = dpcpp +DPCPP_CXXFLAGS = -g -o + +MKL_CXXFLAGS = -I$(MKLROOT)/include -DMKL_ILP64 +MKL_LDFLAGS = ${MKLROOT}/lib/intel64/libmkl_sycl.a -L${MKLROOT}/lib/intel64 -lmkl_intel_ilp64 -lmkl_sequential -lmkl_core -lOpenCL -ldl +MKL_EXE_NAME = matrix_mul_mkl +MKL_SOURCES = src/matrix_mul_mkl.cpp + +all: + $(DPCPP_CXX) $(MKL_CXXFLAGS) $(DPCPP_CXXFLAGS) $(MKL_EXE_NAME) $(MKL_SOURCES) $(MKL_LDFLAGS) + +build_mkl: + $(DPCPP_CXX) $(MKL_CXXFLAGS) $(DPCPP_CXXFLAGS) $(MKL_EXE_NAME) $(MKL_SOURCES) $(MKL_LDFLAGS) + + +run: + SYCL_BE=PI_OPENCL ./$(MKL_EXE_NAME) + +run_mkl: + SYCL_BE=PI_OPENCL ./$(MKL_EXE_NAME) + + +clean: + rm -rf $(MKL_EXE_NAME) + + + diff --git a/Libraries/oneMKL/matrix_mul/Makefile.win b/Libraries/oneMKL/matrix_mul/Makefile.win new file mode 100644 index 0000000000..1698210904 --- /dev/null +++ b/Libraries/oneMKL/matrix_mul/Makefile.win @@ -0,0 +1,24 @@ +DPCPP_CXX = dpcpp-cl +DPCPP_CXXFLAGS = /Zi /EHsc + +MKL_CXXFLAGS = -I"$(MKLROOT)/include" -DMKL_ILP64 +MKL_LDFLAGS = /link /libpath:"$(MKLROOT)\lib\intel64" mkl_intel_ilp64.lib mkl_sequential.lib mkl_core.lib mkl_sycl.lib opencl.lib +MKL_EXE_NAME = matrix_mul_mkl.exe +MKL_SOURCES = src/matrix_mul_mkl.cpp + +all: + $(DPCPP_CXX) $(MKL_CXXFLAGS) $(DPCPP_CXXFLAGS) -o $(MKL_EXE_NAME) $(MKL_SOURCES) $(MKL_LDFLAGS) + +build_mkl: + $(DPCPP_CXX) $(MKL_CXXFLAGS) $(DPCPP_CXXFLAGS) -o $(MKL_EXE_NAME) $(MKL_SOURCES) $(MKL_LDFLAGS) + + +run: + $(MKL_EXE_NAME) + +run_mkl: + $(MKL_EXE_NAME) + + +clean: + del -rf $(MKL_EXE_NAME) *.pdb diff --git a/Libraries/oneMKL/matrix_mul/README.md b/Libraries/oneMKL/matrix_mul/README.md new file mode 100644 index 0000000000..03ec4ba4e1 --- /dev/null +++ b/Libraries/oneMKL/matrix_mul/README.md @@ -0,0 +1,51 @@ +# matrix_mul_mkl sample +matrix_mul_mkl is a simple program that multiplies together two large matrices and verifies the results. +This program is implemented using C++ with oneAPI Math Kernel Library (oneMKL): + +| Optimized for | Description +|:--- |:--- +| OS | Linux* Ubuntu* 18.04, Windows 10* +| Hardware | Skylake with GEN9 or newer +| Software | Intel® oneAPI DPC++ Compiler beta, oneMKL +| What you will learn | Offloads computations on 2D arrays to GPU using oneMKL +| Time to complete | 15 minutes + +## Key implementation details +oneMKL implementation explained. + +## License +This code sample is licensed under MIT license. + +## How to Build for oneMKL + +### on Linux + * Build the program using Make + cd matrix_mul_mkl && + make build_mkl + + * Run the program + make run_mkl + + * Clean the program + make clean + +### on Windows + +#### Command Line using MSBuild + * MSBuild matrix_mul.sln /t:Rebuild /p:Configuration="release" + +#### Command Line using nmake + Build matrix_mul_mkl oneMKL version + * nmake -f Makefile.win build_mkl + * nmake -f Makefile.win run_mkl + + Build matrix_mul_mkl DPCPP version + * nmake -f Makefile.win build_dpcpp + * nmake -f Makefile.win run_dpcpp + +#### Visual Studio IDE + * Open Visual Studio 2017 + * Select Menu "File > Open > Project/Solution", find "matrix_mul" folder and select "matrix_mul.sln" + * Select Menu "Project > Build" to build the selected configuration + * Select Menu "Debug > Start Without Debugging" to run the program + diff --git a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp new file mode 100644 index 0000000000..8838d7dc3a --- /dev/null +++ b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp @@ -0,0 +1,190 @@ +//============================================================== +// Copyright © 2020 Intel Corporation +// +// SPDX-License-Identifier: MIT +// ============================================================= + +#include +#include +#include +#include "mkl.h" +#include "mkl_blas_sycl.hpp" + +using namespace std; +using namespace cl::sycl; + +// Matrix size constants +#define SIZE 4800 // Must be a multiple of 8. +#define M SIZE / 8 +#define N SIZE / 4 +#define P SIZE / 2 + +/** + * Perform the matrix multiplication on host to verify results from mkl. + */ +int VerifyResult(double *c_back); + +int main() { + // + // Initialize data for Gemm + // + // C = alpha * op(A) * op(B) + beta * C + // + mkl::transpose transA = mkl::transpose::nontrans; + mkl::transpose transB = mkl::transpose::nontrans; + + // matrix data sizes + int m = M; + int n = P; + int k = N; + + // leading dimensions of data + int ldA = m; + int ldB = k; + int ldC = m; + + // set scalar fp values + double alpha = 1.0; + double beta = 0.0; + + // 1D arrays on host side + double *A; + double *B; + double *C; + + A = new double[M * N]; + B = new double[N * P]; + C = new double[M * P]; + + // prepare matrix data with column-major style + int i, j; + // A(M, N) is a matrix whose values are column number plus one + for (i = 0; i < N; i++) + for (j = 0; j < M; j++) A[i * M + j] = i + 1.0; + + // B(N, P) is matrix whose values are row number plus one + for (i = 0; i < P; i++) + for (j = 0; j < N; j++) B[i * N + j] = j + 1.0; + + cout << "Problem size: c(" << M << "," << P << ") = a(" << M << "," << N + << ") * b(" << N << "," << P << ")" << std::endl; + + // + // Execute Gemm + // + auto asyncHandler = [&](cl::sycl::exception_list eL) { + for (auto &e : eL) { + try { + std::rethrow_exception(e); + } catch (cl::sycl::exception &e) { + std::cout << e.what() << std::endl; + std::cout << "fail" << std::endl; + // std::terminate() will exit the process, return non-zero, and output a + // message to the user about the exception + std::terminate(); + } + } + }; + + try { + // Initializing the devices queue with the default selector + // The device queue is used to enqueue the kernels and encapsulates + // all the states needed for execution + default_selector device_selector; + queue device_queue(device_selector, asyncHandler); + + std::cout << "Device: " + << device_queue.get_device().get_info() + << std::endl; + + // Creating 1D buffers for matrices which are bound to host memory array + buffer a{A, range<1>{M * N}}; + buffer b{B, range<1>{N * P}}; + buffer c{C, range<1>{M * P}}; + + mkl::blas::gemm(device_queue, transA, transB, m, n, k, alpha, a, ldA, b, + ldB, beta, c, ldC); + } catch (cl::sycl::exception const &e) { + std::cout << "\t\tSYCL exception during GEMM\n" + << e.what() << std::endl + << "OpenCL status: " << e.get_cl_code() << std::endl; + } + + int result; + result = VerifyResult(C); + + delete[] A; + delete[] B; + delete[] C; + + return result; +} + +bool ValueSame(double a, double b) { + return std::fabs(a - b) < std::numeric_limits::epsilon(); +} + +int VerifyResult(double *c_back) { + // Check that the results are correct by comparing with host computing + int i, j, k; + + // 2D arrays on host side + double(*a_host)[N]; + double(*b_host)[P]; + double(*c_host)[P]; + + a_host = new double[M][N]; + b_host = new double[N][P]; + c_host = new double[M][P]; + + // a_host is a matrix whose values are column number plus one + for (i = 0; i < M; i++) + for (j = 0; j < N; j++) a_host[i][j] = j + 1.0; + + // b_host is a matrix whose values are row number plus one + for (i = 0; i < N; i++) + for (j = 0; j < P; j++) b_host[i][j] = i + 1.0; + + // c_host is initialized to zero + for (i = 0; i < M; i++) + for (j = 0; j < P; j++) c_host[i][j] = 0; + + for (i = 0; i < M; i++) { + for (k = 0; k < N; k++) { + for (j = 0; j < P; j++) { + c_host[i][j] += a_host[i][k] * b_host[k][j]; + } + } + } + + bool MismatchFound = false; + + // compare host side results with the result buffer from device side: print + // fail data 5 times only. + int printf_count = 0; + for (i = 0; i < M; i++) { + for (j = 0; j < P; j++) { + if (!ValueSame(c_back[i + j * M], c_host[i][j])) { + cout << "fail - The result is incorrect for element: [" << i << ", " + << j << "], expected: " << c_host[i][j] + << " , but got: " << c_back[i + j * M] << std::endl; + MismatchFound = true; + printf_count++; + if (printf_count >= 5) break; + } + } + if (printf_count >= 5) break; + } + + delete[] a_host; + delete[] b_host; + delete[] c_host; + + if (!MismatchFound) { + cout << "success - The results are correct!" << std::endl; + return 0; + } else { + cout << "fail - The results mis-match!" << std::endl; + return -1; + } +} diff --git a/Libraries/oneMKL/matrix_mul/sample.json b/Libraries/oneMKL/matrix_mul/sample.json new file mode 100644 index 0000000000..e739d2c1bf --- /dev/null +++ b/Libraries/oneMKL/matrix_mul/sample.json @@ -0,0 +1,32 @@ +{ + "guid": "5E72947A-B5C2-465A-8514-85671EDCADF8", + "name": "Matrix Multiplication using oneMKL", + "categories": [ "Toolkit/Intel® oneAPI HPC Toolkit" ], + "toolchain": [ "dpcpp", "icc" ], + "dependencies": [ "mkl" ], + "description": "Simple program that multiplies two large matrices in parallel using oneMKL.", + "languages": [ { "cpp": { "properties": { "projectOptions": [ { "projectType": "makefile" } ] } } } ], + "targetDevice": [ "CPU", "GPU" ], + "os": [ "linux", "windows" ], + "builder": [ "ide", "make" ], + "ciTests": { + "linux": [{ + "steps": [ + "mkdir build", + "cd build", + "cmake ..", + "make", + "make run" + ] + }], + "windows": [ + { + "steps": [ + "MSBuild matrix_mul.sln /t:Rebuild /p:Configuration=\"release\"", + "cd x64/Release", + "matrix_mul_mkl.exe" + ] + } + ] + } +} From baac2d686fcaeb5915a7bbfd87e4c00d1e3904ee Mon Sep 17 00:00:00 2001 From: Aleksei Tonkov Date: Thu, 10 Sep 2020 14:10:06 +0300 Subject: [PATCH 2/6] Applied suggestions. Changed std::endl -> std:cerr. Changed #define -> constexpr. Added auto in creation of A variable. Changed count in some places to std:ceer --- .../oneMKL/matrix_mul/matrix_mul_mkl.cpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp index 8838d7dc3a..14da3aa4e6 100644 --- a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp +++ b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp @@ -1,5 +1,5 @@ //============================================================== -// Copyright © 2020 Intel Corporation +// Copyright � 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= @@ -14,10 +14,10 @@ using namespace std; using namespace cl::sycl; // Matrix size constants -#define SIZE 4800 // Must be a multiple of 8. -#define M SIZE / 8 -#define N SIZE / 4 -#define P SIZE / 2 +auto constexpr SIZE = 4800 // Must be a multiple of 8. +auto constexpr M = SIZE / 8 +auto constexpr N = SIZE / 4 +auto constexpr P = SIZE / 2 /** * Perform the matrix multiplication on host to verify results from mkl. @@ -52,9 +52,9 @@ int main() { double *B; double *C; - A = new double[M * N]; - B = new double[N * P]; - C = new double[M * P]; + auto A = new double[M * N]; + auto B = new double[N * P]; + auto C = new double[M * P]; // prepare matrix data with column-major style int i, j; @@ -67,7 +67,7 @@ int main() { for (j = 0; j < N; j++) B[i * N + j] = j + 1.0; cout << "Problem size: c(" << M << "," << P << ") = a(" << M << "," << N - << ") * b(" << N << "," << P << ")" << std::endl; + << ") * b(" << N << "," << P << ")" << std::cerr; // // Execute Gemm @@ -77,8 +77,8 @@ int main() { try { std::rethrow_exception(e); } catch (cl::sycl::exception &e) { - std::cout << e.what() << std::endl; - std::cout << "fail" << std::endl; + std::cout << e.what() << std::cerr; + std::cout << "fail" << std::cerr; // std::terminate() will exit the process, return non-zero, and output a // message to the user about the exception std::terminate(); @@ -95,7 +95,7 @@ int main() { std::cout << "Device: " << device_queue.get_device().get_info() - << std::endl; + << std::cerr; // Creating 1D buffers for matrices which are bound to host memory array buffer a{A, range<1>{M * N}}; @@ -105,9 +105,9 @@ int main() { mkl::blas::gemm(device_queue, transA, transB, m, n, k, alpha, a, ldA, b, ldB, beta, c, ldC); } catch (cl::sycl::exception const &e) { - std::cout << "\t\tSYCL exception during GEMM\n" - << e.what() << std::endl - << "OpenCL status: " << e.get_cl_code() << std::endl; + std::cerr << "\t\tSYCL exception during GEMM\n" + << e.what() << std::cerr + << "OpenCL status: " << e.get_cl_code() << std::cerr; } int result; @@ -167,7 +167,7 @@ int VerifyResult(double *c_back) { if (!ValueSame(c_back[i + j * M], c_host[i][j])) { cout << "fail - The result is incorrect for element: [" << i << ", " << j << "], expected: " << c_host[i][j] - << " , but got: " << c_back[i + j * M] << std::endl; + << " , but got: " << c_back[i + j * M] << std::cerr; MismatchFound = true; printf_count++; if (printf_count >= 5) break; @@ -181,10 +181,10 @@ int VerifyResult(double *c_back) { delete[] c_host; if (!MismatchFound) { - cout << "success - The results are correct!" << std::endl; + cout << "success - The results are correct!" << std::cerr; return 0; } else { - cout << "fail - The results mis-match!" << std::endl; + std::cerr << "fail - The results mis-match!" << std::cerr; return -1; } } From 8846f3882c1dd64d952f76f249a8a15f5c4131e6 Mon Sep 17 00:00:00 2001 From: Aleksei Tonkov Date: Thu, 10 Sep 2020 14:28:26 +0300 Subject: [PATCH 3/6] README.md rework --- Libraries/oneMKL/matrix_mul/README.md | 58 ++++++++++++++++----------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/Libraries/oneMKL/matrix_mul/README.md b/Libraries/oneMKL/matrix_mul/README.md index 03ec4ba4e1..68ae246440 100644 --- a/Libraries/oneMKL/matrix_mul/README.md +++ b/Libraries/oneMKL/matrix_mul/README.md @@ -1,5 +1,5 @@ -# matrix_mul_mkl sample -matrix_mul_mkl is a simple program that multiplies together two large matrices and verifies the results. +# `Matrix Multiplication` sample +Matrix Multiplication is a simple program that multiplies together two large matrices and verifies the results. This program is implemented using C++ with oneAPI Math Kernel Library (oneMKL): | Optimized for | Description @@ -16,32 +16,44 @@ oneMKL implementation explained. ## License This code sample is licensed under MIT license. -## How to Build for oneMKL - -### on Linux - * Build the program using Make - cd matrix_mul_mkl && - make build_mkl - - * Run the program - make run_mkl - - * Clean the program +## Bulding `Matrix Multiplication` for oneMKL + +### On a Linux* System +Perform the following steps: +1. Build the program using the following `cmake` commands. +``` +$ mkdir build +$ cd build +$ cmake .. +$ make +``` + +> Note: by default, exectables are created for both USM and buffers. You can build individually with the following: +> Create buffers executable: make mandelbrot +> Create USM executable: make mandelbrot_usm + +2. Run the program (default uses buffers): + ``` + make run + ``` +> Note: for USM use `make run_usm` + +3. Clean the program using: + ``` make clean + ``` -### on Windows +### On a Windows* System Using Visual Studio* Version 2017 or Newer -#### Command Line using MSBuild - * MSBuild matrix_mul.sln /t:Rebuild /p:Configuration="release" +* Build the program using VS2017 or VS2019 + Right click on the solution file and open using either VS2017 or VS2019 IDE. + Right click on the project in Solution explorer and select Rebuild. + From top menu select Debug -> Start without Debugging. -#### Command Line using nmake - Build matrix_mul_mkl oneMKL version - * nmake -f Makefile.win build_mkl - * nmake -f Makefile.win run_mkl - Build matrix_mul_mkl DPCPP version - * nmake -f Makefile.win build_dpcpp - * nmake -f Makefile.win run_dpcpp +* Build the program using MSBuild + Open "x64 Native Tools Command Prompt for VS2017" or "x64 Native Tools Command Prompt for VS2019" + Run - MSBuild matrix_mul.sln /t:Rebuild /p:Configuration="Release" #### Visual Studio IDE * Open Visual Studio 2017 From b1c5d8a066165eb91bdc3a8a9a537f583f4a892f Mon Sep 17 00:00:00 2001 From: Aleksei Tonkov Date: Thu, 10 Sep 2020 17:01:02 +0300 Subject: [PATCH 4/6] Removed std:: and cl::sycl specification and went on with using namespace only instead --- .../oneMKL/matrix_mul/matrix_mul_mkl.cpp | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp index 14da3aa4e6..f4a37ecad9 100644 --- a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp +++ b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp @@ -67,21 +67,21 @@ int main() { for (j = 0; j < N; j++) B[i * N + j] = j + 1.0; cout << "Problem size: c(" << M << "," << P << ") = a(" << M << "," << N - << ") * b(" << N << "," << P << ")" << std::cerr; + << ") * b(" << N << "," << P << ")" << cerr; // // Execute Gemm // - auto asyncHandler = [&](cl::sycl::exception_list eL) { + auto asyncHandler = [&](exception_list eL) { for (auto &e : eL) { try { - std::rethrow_exception(e); - } catch (cl::sycl::exception &e) { - std::cout << e.what() << std::cerr; - std::cout << "fail" << std::cerr; - // std::terminate() will exit the process, return non-zero, and output a + rethrow_exception(e); + } catch (exception &e) { + cout << e.what() << cerr; + cout << "fail" << cerr; + // terminate() will exit the process, return non-zero, and output a // message to the user about the exception - std::terminate(); + terminate(); } } }; @@ -93,9 +93,9 @@ int main() { default_selector device_selector; queue device_queue(device_selector, asyncHandler); - std::cout << "Device: " + cout << "Device: " << device_queue.get_device().get_info() - << std::cerr; + << cerr; // Creating 1D buffers for matrices which are bound to host memory array buffer a{A, range<1>{M * N}}; @@ -104,10 +104,10 @@ int main() { mkl::blas::gemm(device_queue, transA, transB, m, n, k, alpha, a, ldA, b, ldB, beta, c, ldC); - } catch (cl::sycl::exception const &e) { - std::cerr << "\t\tSYCL exception during GEMM\n" - << e.what() << std::cerr - << "OpenCL status: " << e.get_cl_code() << std::cerr; + } catch (exception const &e) { + cerr << "\t\tSYCL exception during GEMM\n" + << e.what() << cerr + << "OpenCL status: " << e.get_cl_code() << cerr; } int result; @@ -121,7 +121,7 @@ int main() { } bool ValueSame(double a, double b) { - return std::fabs(a - b) < std::numeric_limits::epsilon(); + return fabs(a - b) < numeric_limits::epsilon(); } int VerifyResult(double *c_back) { @@ -167,7 +167,7 @@ int VerifyResult(double *c_back) { if (!ValueSame(c_back[i + j * M], c_host[i][j])) { cout << "fail - The result is incorrect for element: [" << i << ", " << j << "], expected: " << c_host[i][j] - << " , but got: " << c_back[i + j * M] << std::cerr; + << " , but got: " << c_back[i + j * M] << cerr; MismatchFound = true; printf_count++; if (printf_count >= 5) break; @@ -181,10 +181,10 @@ int VerifyResult(double *c_back) { delete[] c_host; if (!MismatchFound) { - cout << "success - The results are correct!" << std::cerr; + cout << "success - The results are correct!" << cerr; return 0; } else { - std::cerr << "fail - The results mis-match!" << std::cerr; + cerr << "fail - The results mis-match!" << cerr; return -1; } } From 09f151bbdbe55228f8aaa9514378f3eaa4b22c56 Mon Sep 17 00:00:00 2001 From: Aleksei Tonkov Date: Thu, 10 Sep 2020 18:45:55 +0300 Subject: [PATCH 5/6] Made A,B,C and a/b/c_host variables initialize with value at declaration, using auto --- Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp index f4a37ecad9..42083a2497 100644 --- a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp +++ b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp @@ -48,9 +48,6 @@ int main() { double beta = 0.0; // 1D arrays on host side - double *A; - double *B; - double *C; auto A = new double[M * N]; auto B = new double[N * P]; @@ -129,13 +126,10 @@ int VerifyResult(double *c_back) { int i, j, k; // 2D arrays on host side - double(*a_host)[N]; - double(*b_host)[P]; - double(*c_host)[P]; - a_host = new double[M][N]; - b_host = new double[N][P]; - c_host = new double[M][P]; + auto a_host = new double[M][N]; + auto b_host = new double[N][P]; + auto c_host = new double[M][P]; // a_host is a matrix whose values are column number plus one for (i = 0; i < M; i++) From d6dd9f7a3044cae7d9b5f298c220998aeb4e9fdf Mon Sep 17 00:00:00 2001 From: Aleksei Tonkov Date: Fri, 11 Sep 2020 14:40:41 +0300 Subject: [PATCH 6/6] Applied several cosmetic changes --- .../oneMKL/matrix_mul/matrix_mul_mkl.cpp | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp index 42083a2497..2f1f9d31be 100644 --- a/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp +++ b/Libraries/oneMKL/matrix_mul/matrix_mul_mkl.cpp @@ -3,6 +3,9 @@ // // SPDX-License-Identifier: MIT // ============================================================= +// +// Matrix Multiplication is a simple program that multiplies together two large matrices and verifies the results. +// This program is implemented using C++ with oneAPI Math Kernel Library (oneMKL) #include #include @@ -11,13 +14,13 @@ #include "mkl_blas_sycl.hpp" using namespace std; -using namespace cl::sycl; +using namespace sycl; // Matrix size constants -auto constexpr SIZE = 4800 // Must be a multiple of 8. -auto constexpr M = SIZE / 8 -auto constexpr N = SIZE / 4 -auto constexpr P = SIZE / 2 +auto constexpr size = (600 * 8) // Must be a multiple of 8. +auto constexpr M = size / 8 +auto constexpr N = size / 4 +auto constexpr P = size / 2 /** * Perform the matrix multiplication on host to verify results from mkl. @@ -33,17 +36,17 @@ int main() { mkl::transpose transA = mkl::transpose::nontrans; mkl::transpose transB = mkl::transpose::nontrans; - // matrix data sizes + // Matrix data sizes int m = M; int n = P; int k = N; - // leading dimensions of data + // Meading dimensions of data int ldA = m; int ldB = k; int ldC = m; - // set scalar fp values + // Set scalar fp values double alpha = 1.0; double beta = 0.0; @@ -53,7 +56,7 @@ int main() { auto B = new double[N * P]; auto C = new double[M * P]; - // prepare matrix data with column-major style + // Prepare matrix data with column-major style int i, j; // A(M, N) is a matrix whose values are column number plus one for (i = 0; i < N; i++) @@ -66,9 +69,7 @@ int main() { cout << "Problem size: c(" << M << "," << P << ") = a(" << M << "," << N << ") * b(" << N << "," << P << ")" << cerr; - // // Execute Gemm - // auto asyncHandler = [&](exception_list eL) { for (auto &e : eL) { try { @@ -153,7 +154,7 @@ int VerifyResult(double *c_back) { bool MismatchFound = false; - // compare host side results with the result buffer from device side: print + // Compare host side results with the result buffer from device side: print // fail data 5 times only. int printf_count = 0; for (i = 0; i < M; i++) {