repo_name
stringclasses
10 values
file_path
stringlengths
29
222
content
stringlengths
24
926k
extention
stringclasses
5 values
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-1/swap.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char* argv[]) try { dpct::device_ext& dev_ct1 = dpct::get_current_device(); sycl::queue& q_ct1 = dev_ct1.default_queue(); sycl::queue* cublasH = NULL; dpct::queue_ptr stream = &q_ct1; /* * A = | 1.0 2.0 3.0 4.0 | * B = | 5.0 6.0 7.0 8.0 | */ std::vector<data_type> A = {1.0, 2.0, 3.0, 4.0}; std::vector<data_type> B = {5.0, 6.0, 7.0, 8.0}; const int incx = 1; const int incy = 1; data_type* d_A = nullptr; data_type* d_B = nullptr; printf("A\n"); print_vector(A.size(), A.data()); printf("=====\n"); printf("B\n"); print_vector(B.size(), B.data()); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type*)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type*)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::swap(*cublasH, A.size(), d_A, incx, d_B, incy); /* step 4: copy data to host */ stream->memcpy(A.data(), d_A, sizeof(data_type) * A.size()); stream->memcpy(B.data(), d_B, sizeof(data_type) * B.size()); stream->wait(); /* * A = | 5.0 6.0 7.0 8.0 | * B = | 1.0 2.0 3.0 4.0 | */ printf("A\n"); print_vector(A.size(), A.data()); printf("=====\n"); printf("B\n"); print_vector(B.size(), B.data()); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const& exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-1/rotmg.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; /* * A = 1.0 * B = 5.0 * X = 2.1 * Y = 1.2 */ data_type A = 1.0; data_type B = 5.0; data_type X = 2.1; data_type Y = 1.2; std::vector<data_type> param = {1.0, 5.0, 6.0, 7.0, 8.0}; // flag = param[0] data_type *d_A = nullptr; data_type *d_B = nullptr; printf("A\n"); std::printf("%0.2f\n", A); printf("=====\n"); printf("B\n"); std::printf("%0.2f\n", B); printf("=====\n"); printf("X\n"); std::printf("%0.2f\n", X); printf("=====\n"); printf("Y\n"); std::printf("%0.2f\n", Y); printf("=====\n"); printf("param\n"); print_vector(param.size(), param.data()); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 3: compute */ [&]() { double *d1_ct13 = &A; double *d2_ct14 = &B; double *x1_ct15 = &X; double *param_ct16 = param.data(); if (sycl::get_pointer_type(&A, cublasH->get_context()) != sycl::usm::alloc::device && sycl::get_pointer_type(&A, cublasH->get_context()) != sycl::usm::alloc::shared) { d1_ct13 = sycl::malloc_shared<double>(8, dpct::get_default_queue()); d2_ct14 = d1_ct13 + 1; x1_ct15 = d1_ct13 + 2; param_ct16 = d1_ct13 + 3; *d1_ct13 = A; *d2_ct14 = B; *x1_ct15 = X; } oneapi::mkl::blas::column_major::rotmg(*cublasH, d1_ct13, d2_ct14, x1_ct15, Y, param_ct16); if (sycl::get_pointer_type(&A, cublasH->get_context()) != sycl::usm::alloc::device && sycl::get_pointer_type(&A, cublasH->get_context()) != sycl::usm::alloc::shared) { cublasH->wait(); A = *d1_ct13; B = *d2_ct14; X = *x1_ct15; dpct::get_default_queue() .memcpy(param.data(), param_ct16, sizeof(double) * 5) .wait(); sycl::free(d1_ct13, dpct::get_default_queue()); } return 0; }(); stream->wait(); /* * A = 3.10 * B = 0.62 * X = 1.94 */ printf("A\n"); std::printf("%0.2f\n", A); printf("=====\n"); printf("B\n"); std::printf("%0.2f\n", B); printf("=====\n"); printf("X\n"); std::printf("%0.2f\n", X); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-1/dotc.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <complex> #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = sycl::double2; int main(int argc, char* argv[]) try { dpct::device_ext& dev_ct1 = dpct::get_current_device(); sycl::queue& q_ct1 = dev_ct1.default_queue(); sycl::queue* cublasH = NULL; dpct::queue_ptr stream = &q_ct1; /* * A = | 1.1 + 1.2j | 2.3 + 2.4j | 3.5 + 3.6j | 4.7 + 4.8j | * B = | 5.1 + 5.2j | 6.3 + 6.4j | 7.5 + 7.6j | 8.7 + 8.8j | */ const std::vector<data_type> A = { {1.1, 1.2}, {2.3, 2.4}, {3.5, 3.6}, {4.7, 4.8}}; const std::vector<data_type> B = { {5.1, 5.2}, {6.3, 6.4}, {7.5, 7.6}, {8.7, 8.8}}; const int incx = 1; const int incy = 1; data_type result = {0.0, 0.0}; data_type* d_A = nullptr; data_type* d_B = nullptr; printf("A\n"); print_vector(A.size(), A.data()); printf("=====\n"); printf("B\n"); print_vector(B.size(), B.data()); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type*)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type*)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ [&]() { sycl::double2* res_temp_ptr_ct6 = &result; if (sycl::get_pointer_type(&result, cublasH->get_context()) != sycl::usm::alloc::device && sycl::get_pointer_type(&result, cublasH->get_context()) != sycl::usm::alloc::shared) { res_temp_ptr_ct6 = sycl::malloc_shared<sycl::double2>(1, dpct::get_default_queue()); } oneapi::mkl::blas::column_major::dotc( *cublasH, A.size(), (std::complex<double>*)d_A, incx, (std::complex<double>*)d_B, incy, (std::complex<double>*)res_temp_ptr_ct6); if (sycl::get_pointer_type(&result, cublasH->get_context()) != sycl::usm::alloc::device && sycl::get_pointer_type(&result, cublasH->get_context()) != sycl::usm::alloc::shared) { cublasH->wait(); result = *res_temp_ptr_ct6; sycl::free(res_temp_ptr_ct6, dpct::get_default_queue()); } return 0; }(); stream->wait(); /* * result = 178.44+-1.60j */ printf("Result\n"); printf("%0.2f+%0.2fj\n", result.x(), result.y()); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const& exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-1/amax.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; /* * A = | 1.0 2.0 3.0 4.0 | */ const std::vector<data_type> A = {1.0, 2.0, 3.0, 4.0}; const int incx = 1; int result = 0.0; data_type *d_A = nullptr; printf("A\n"); print_vector(A.size(), A.data()); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); /* step 3: compute */ [&]() { int64_t *res_temp_ptr_ct1 = sycl::malloc_shared<int64_t>(1, dpct::get_default_queue()); oneapi::mkl::blas::column_major::iamax(*cublasH, A.size(), d_A, incx, res_temp_ptr_ct1) .wait(); int res_temp_host_ct2 = A[(int)*res_temp_ptr_ct1]; dpct::dpct_memcpy(&result, &res_temp_host_ct2, sizeof(int)); sycl::free(res_temp_ptr_ct1, dpct::get_default_queue()); return 0; }(); stream->wait(); /* * result = 4 */ printf("result\n"); std::printf("%d\n", result); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/hemm.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <complex> #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = sycl::double2; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | * * B = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | */ const std::vector<data_type> A = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; const std::vector<data_type> B = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; std::vector<data_type> C(m * n); const data_type alpha = {1.0, 1.0}; const data_type beta = {0.0, 0.0}; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::side side = oneapi::mkl::side::left; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::hemm( *cublasH, side, uplo, m, n, std::complex<double>(alpha.x(), alpha.y()), (std::complex<double> *)d_A, lda, (std::complex<double> *)d_B, ldb, std::complex<double>(beta.x(), beta.y()), (std::complex<double> *)d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | -17.38 + 18.62j | -23.14 + 26.78j | * | 4.82 + 38.90j | 10.58 + 55.70j | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/trsm.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.0 | 2.0 | * | 3.0 | 4.0 | * * B = | 5.0 | 6.0 | * | 7.0 | 8.0 | */ const std::vector<data_type> A = {1.0, 3.0, 2.0, 4.0}; std::vector<data_type> B = {5.0, 7.0, 6.0, 8.0}; const data_type alpha = 1.0; data_type *d_A = nullptr; data_type *d_B = nullptr; oneapi::mkl::side side = oneapi::mkl::side::left; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; oneapi::mkl::diag diag = oneapi::mkl::diag::nonunit; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B (in) \n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::trsm(*cublasH, side, uplo, transa, diag, m, n, alpha, d_A, lda, d_B, ldb); /* step 4: copy data to host */ stream->memcpy(B.data(), d_B, sizeof(data_type) * B.size()); stream->wait(); /* * B = | 1.50 | 2.00 | * | 1.75 | 2.00 | */ printf("B (out)\n"); print_matrix(m, n, B.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/syr2k.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.0 | 3.0 | * | 3.0 | 4.0 | * * B = | 5.0 | 7.0 | * | 7.0 | 8.0 | */ const std::vector<data_type> A = {1.0, 3.0, 3.0, 4.0}; const std::vector<data_type> B = {5.0, 7.0, 7.0, 8.0}; std::vector<data_type> C(m * n); const data_type alpha = 1.0; const data_type beta = 0.0; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::syr2k(*cublasH, uplo, transa, n, k, alpha, d_A, lda, d_B, ldb, beta, d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 52.0 | 74.0 | * | 0.0 | 106.0 | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/gemm3m.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <complex> #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = sycl::float2; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | * * B = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | */ const std::vector<data_type> A = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; const std::vector<data_type> B = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; std::vector<data_type> C(m * n); const data_type alpha = {1.0, 1.0}; const data_type beta = {0.0, 0.0}; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; oneapi::mkl::transpose transb = oneapi::mkl::transpose::nontrans; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::gemm( *cublasH, transa, transb, m, n, k, std::complex<float>(alpha.x(), alpha.y()), (std::complex<float> *)d_A, lda, (std::complex<float> *)d_B, ldb, std::complex<float>(beta.x(), beta.y()), (std::complex<float> *)d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | -20.14 + 18.50j -28.78 + 26.66j | * | -43.18 + 40.58j -63.34 + 60.26j | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/syrk.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldc = 2; /* * A = | 1.0 | 3.0 | * | 3.0 | 4.0 | */ const std::vector<data_type> A = {1.0, 3.0, 3.0, 4.0}; std::vector<data_type> C(m * n); const data_type alpha = 1.0; const data_type beta = 0.0; data_type *d_A = nullptr; data_type *d_C = nullptr; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::trans; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::syrk(*cublasH, uplo, transa, n, k, alpha, d_A, lda, beta, d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 10.0 | 15.0 | * | 0.0 | 25.0 | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/herk.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <complex> #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = sycl::double2; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldc = 2; /* * A = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | */ const std::vector<data_type> A = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; std::vector<data_type> C(m * n); const double alpha = 1.0; const double beta = 0.0; data_type *d_A = nullptr; data_type *d_C = nullptr; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::herk(*cublasH, uplo, transa, n, k, alpha, (std::complex<double> *)d_A, lda, beta, (std::complex<double> *)d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 13.70 + 0.00j | 30.50 + 0.48j | * | 0.00 + 0.00j | 70.34 + 0.00j | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/trsmBatched.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int batch_count = 2; /* * A = | 1.0 | 2.0 | 5.0 | 6.0 | * | 3.0 | 4.0 | 7.0 | 8.0 | * * B = | 5.0 | 6.0 | 9.0 | 10.0 | * | 7.0 | 8.0 | 11.0 | 12.0 | */ const std::vector<std::vector<data_type>> A_array = {{1.0, 3.0, 2.0, 4.0}, {5.0, 7.0, 6.0, 8.0}}; std::vector<std::vector<data_type>> B_array = {{5.0, 7.0, 6.0, 8.0}, {9.0, 11.0, 10.0, 12.0}}; const data_type alpha = 1.0; data_type **d_A_array = nullptr; data_type **d_B_array = nullptr; std::vector<data_type *> d_A(batch_count, nullptr); std::vector<data_type *> d_B(batch_count, nullptr); oneapi::mkl::side side = oneapi::mkl::side::left; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; oneapi::mkl::diag diag = oneapi::mkl::diag::nonunit; printf("A[0]\n"); print_matrix(m, k, A_array[0].data(), lda); printf("=====\n"); printf("A[1]\n"); print_matrix(m, k, A_array[1].data(), lda); printf("=====\n"); printf("B[0] (in)\n"); print_matrix(k, n, B_array[0].data(), ldb); printf("=====\n"); printf("B[1] (in)\n"); print_matrix(k, n, B_array[1].data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1, 0; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ for (int i = 0; i < batch_count; i++) { d_A[i] = (data_type *)sycl::malloc_device( sizeof(data_type) * A_array[i].size(), q_ct1); d_B[i] = (data_type *)sycl::malloc_device( sizeof(data_type) * B_array[i].size(), q_ct1); } d_A_array = (data_type **)sycl::malloc_device( sizeof(data_type *) * batch_count, q_ct1); d_B_array = (data_type **)sycl::malloc_device( sizeof(data_type *) * batch_count, q_ct1); for (int i = 0; i < batch_count; i++) { stream->memcpy(d_A[i], A_array[i].data(), sizeof(data_type) * A_array[i].size()); stream->memcpy(d_B[i], B_array[i].data(), sizeof(data_type) * B_array[i].size()); } stream->memcpy(d_A_array, d_A.data(), sizeof(data_type *) * batch_count); stream->memcpy(d_B_array, d_B.data(), sizeof(data_type *) * batch_count); /* step 3: compute */ dpct::trsm_batch(*cublasH, side, uplo, transa, diag, m, n, &alpha, (const void **)d_A_array, dpct::library_data_t::real_double, lda, (void **)d_B_array, dpct::library_data_t::real_double, ldb, batch_count, dpct::library_data_t::real_double); /* step 4: copy data to host */ for (int i = 0; i < batch_count; i++) { stream->memcpy(B_array[i].data(), d_B[i], sizeof(data_type) * B_array[i].size()); } stream->wait(); /* * B = | 1.50 | 2.00 | 0.15 | 0.20 | * | 1.75 | 2.00 | 1.38 | 1.50 | */ printf("B[0] (out)\n"); print_matrix(k, n, B_array[0].data(), ldb); printf("=====\n"); printf("B[1] (out)\n"); print_matrix(k, n, B_array[1].data(), ldb); printf("=====\n"); /* free resources */ sycl::free(d_A_array, q_ct1); sycl::free(d_B_array, q_ct1); for (int i = 0; i < batch_count; i++) { sycl::free(d_A[i], q_ct1); sycl::free(d_B[i], q_ct1); } cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/her2k.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <complex> #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = sycl::double2; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | * * B = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | */ const std::vector<data_type> A = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; const std::vector<data_type> B = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; std::vector<data_type> C(m * n); const data_type alpha = {1.0, 1.0}; const double beta = 0.0; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::her2k( *cublasH, uplo, transa, n, k, std::complex<double>(alpha.x(), alpha.y()), (std::complex<double> *)d_A, lda, (std::complex<double> *)d_B, ldb, beta, (std::complex<double> *)d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 27.40 + 0.00j | 61.00 + 0.96j | * | 0.00 + 0.00j | 140.68 + 0.00j | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/symm.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.0 | 3.0 | * | 3.0 | 4.0 | * * B = | 5.0 | 7.0 | * | 7.0 | 8.0 | */ const std::vector<data_type> A = {1.0, 3.0, 3.0, 4.0}; const std::vector<data_type> B = {5.0, 7.0, 7.0, 8.0}; std::vector<data_type> C(m * n); const data_type alpha = 1.0; const data_type beta = 0.0; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::side side = oneapi::mkl::side::left; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::symm(*cublasH, side, uplo, m, n, alpha, d_A, lda, d_B, ldb, beta, d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 26.0 | 31.0 | * | 43.0 | 53.0 | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/gemm.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.0 | 2.0 | * | 3.0 | 4.0 | * * B = | 5.0 | 6.0 | * | 7.0 | 8.0 | */ const std::vector<data_type> A = {1.0, 2.0, 3.0, 4.0}; const std::vector<data_type> B = {5.0, 6.0, 7.0, 8.0}; std::vector<data_type> C(m * n); const data_type alpha = 1.0; const data_type beta = 0.0; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; oneapi::mkl::transpose transb = oneapi::mkl::transpose::nontrans; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::gemm(*cublasH, transa, transb, m, n, k, alpha, d_A, lda, d_B, ldb, beta, d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 23.0 | 31.0 | * | 34.0 | 46.0 | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/trmm.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.0 | 2.0 | * | 3.0 | 4.0 | * * B = | 5.0 | 6.0 | * | 7.0 | 8.0 | */ const std::vector<data_type> A = {1.0, 3.0, 2.0, 4.0}; const std::vector<data_type> B = {5.0, 7.0, 6.0, 8.0}; std::vector<data_type> C(m * n); const data_type alpha = 1.0; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::side side = oneapi::mkl::side::left; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; oneapi::mkl::diag diag = oneapi::mkl::diag::nonunit; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ dpct::trmm(*cublasH, side, uplo, transa, diag, m, n, &alpha, d_A, lda, d_B, ldb, d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 19.0 | 22.0 | * | 28.0 | 32.0 | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/gemmBatched.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; const int batch_count = 2; /* * A = | 1.0 | 2.0 | 5.0 | 6.0 | * | 3.0 | 4.0 | 7.0 | 8.0 | * * B = | 5.0 | 6.0 | 9.0 | 10.0 | * | 7.0 | 8.0 | 11.0 | 12.0 | */ const std::vector<std::vector<data_type>> A_array = {{1.0, 3.0, 2.0, 4.0}, {5.0, 7.0, 6.0, 8.0}}; const std::vector<std::vector<data_type>> B_array = {{5.0, 7.0, 6.0, 8.0}, {9.0, 11.0, 10.0, 12.0}}; std::vector<std::vector<data_type>> C_array(batch_count, std::vector<data_type>(m * n)); const data_type alpha = 1.0; const data_type beta = 0.0; data_type **d_A_array = nullptr; data_type **d_B_array = nullptr; data_type **d_C_array = nullptr; std::vector<data_type *> d_A(batch_count, nullptr); std::vector<data_type *> d_B(batch_count, nullptr); std::vector<data_type *> d_C(batch_count, nullptr); oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; oneapi::mkl::transpose transb = oneapi::mkl::transpose::nontrans; printf("A[0]\n"); print_matrix(m, k, A_array[0].data(), lda); printf("=====\n"); printf("A[1]\n"); print_matrix(m, k, A_array[1].data(), lda); printf("=====\n"); printf("B[0]\n"); print_matrix(k, n, B_array[0].data(), ldb); printf("=====\n"); printf("B[1]\n"); print_matrix(k, n, B_array[1].data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ for (int i = 0; i < batch_count; i++) { d_A[i] = (data_type *)sycl::malloc_device( sizeof(data_type) * A_array[i].size(), q_ct1); d_B[i] = (data_type *)sycl::malloc_device( sizeof(data_type) * B_array[i].size(), q_ct1); d_C[i] = (data_type *)sycl::malloc_device( sizeof(data_type) * C_array[i].size(), q_ct1); } d_A_array = (data_type **)sycl::malloc_device( sizeof(data_type *) * batch_count, q_ct1); d_B_array = (data_type **)sycl::malloc_device( sizeof(data_type *) * batch_count, q_ct1); d_C_array = (data_type **)sycl::malloc_device( sizeof(data_type *) * batch_count, q_ct1); for (int i = 0; i < batch_count; i++) { stream->memcpy(d_A[i], A_array[i].data(), sizeof(data_type) * A_array[i].size()); stream->memcpy(d_B[i], B_array[i].data(), sizeof(data_type) * B_array[i].size()); } stream->memcpy(d_A_array, d_A.data(), sizeof(data_type *) * batch_count); stream->memcpy(d_B_array, d_B.data(), sizeof(data_type *) * batch_count); stream->memcpy(d_C_array, d_C.data(), sizeof(data_type *) * batch_count); /* step 3: compute */ dpct::gemm_batch(*cublasH, transa, transb, m, n, k, &alpha, (const void **)d_A_array, dpct::library_data_t::real_double, lda, (const void **)d_B_array, dpct::library_data_t::real_double, ldb, &beta, (void **)d_C_array, dpct::library_data_t::real_double, ldc, batch_count, dpct::library_data_t::real_double); /* step 4: copy data to host */ for (int i = 0; i < batch_count; i++) { stream->memcpy(C_array[i].data(), d_C[i], sizeof(data_type) * C_array[i].size()); } stream->wait(); /* * C = | 19.0 | 22.0 | 111.0 | 122.0 | * | 43.0 | 50.0 | 151.0 | 166.0 | */ printf("C[0]\n"); print_matrix(m, n, C_array[0].data(), ldc); printf("=====\n"); printf("C[1]\n"); print_matrix(m, n, C_array[1].data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A_array, q_ct1); sycl::free(d_B_array, q_ct1); sycl::free(d_C_array, q_ct1); for (int i = 0; i < batch_count; i++) { sycl::free(d_A[i], q_ct1); sycl::free(d_B[i], q_ct1); sycl::free(d_C[i], q_ct1); } cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/syrkx.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.0 | 3.0 | * | 3.0 | 4.0 | * * B = | 5.0 | 7.0 | * | 7.0 | 8.0 | */ const std::vector<data_type> A = {1.0, 3.0, 3.0, 4.0}; const std::vector<data_type> B = {5.0, 7.0, 7.0, 8.0}; std::vector<data_type> C(m * n); const data_type alpha = 1.0; const data_type beta = 0.0; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ dpct::syrk(*cublasH, uplo, transa, n, k, &alpha, d_A, lda, d_B, ldb, &beta, d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 26.0 | 31.0 | * | 0.0 | 53.0 | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/herkx.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <complex> #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = sycl::double2; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; /* * A = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | * * B = | 1.1 + 1.2j | 2.3 + 2.4j | * | 3.5 + 3.6j | 4.7 + 4.8j | */ const std::vector<data_type> A = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; const std::vector<data_type> B = { {1.1, 1.2}, {3.5, 3.6}, {2.3, 2.4}, {4.7, 4.8}}; std::vector<data_type> C(m * n); const data_type alpha = {1.0, 1.0}; const double beta = 0.0; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::uplo uplo = oneapi::mkl::uplo::upper; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; printf("A\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("B\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ dpct::herk(*cublasH, uplo, transa, n, k, &alpha, d_A, lda, d_B, ldb, &beta, d_C, ldc); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 13.70 + 0.00j | 30.02 + 30.98j | * | 0.00 + 0.00j | 70.34 + 0.00j | */ printf("C\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuBLAS_examples_SYCL_Migration/02_sycl_dpct_migrated/Level-3/gemmStridedBatched.cpp
/* * Copyright 2020 NVIDIA Corporation. All rights reserved. * * NOTICE TO LICENSEE: * * This source code and/or documentation ("Licensed Deliverables") are * subject to NVIDIA intellectual property rights under U.S. and * international Copyright laws. * * These Licensed Deliverables contained herein is PROPRIETARY and * CONFIDENTIAL to NVIDIA and is being provided under the terms and * conditions of a form of NVIDIA software license agreement by and * between NVIDIA and Licensee ("License Agreement") or electronically * accepted by Licensee. Notwithstanding any terms or conditions to * the contrary in the License Agreement, reproduction or disclosure * of the Licensed Deliverables to any third party without the express * written consent of NVIDIA is prohibited. * * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND. * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE. * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THESE LICENSED DELIVERABLES. * * U.S. Government End Users. These Licensed Deliverables are a * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT * 1995), consisting of "commercial computer software" and "commercial * computer software documentation" as such terms are used in 48 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government * only as a commercial end item. Consistent with 48 C.F.R.12.212 and * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all * U.S. Government End Users acquire the Licensed Deliverables with * only those rights set forth herein. * * Any use of the Licensed Deliverables in individual and commercial * software must include, in the user documentation and internal * comments to the code, the above Disclaimer and U.S. Government End * Users Notice. */ #include <cstdio> #include <cstdlib> #include <dpct/dpct.hpp> #include <dpct/blas_utils.hpp> #include <oneapi/mkl.hpp> #include <sycl/sycl.hpp> #include <vector> #include "cublas_utils.h" using data_type = double; int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); sycl::queue *cublasH = NULL; dpct::queue_ptr stream = &q_ct1; const int m = 2; const int n = 2; const int k = 2; const int lda = 2; const int ldb = 2; const int ldc = 2; const int batch_count = 2; const long long int strideA = m * k; const long long int strideB = k * n; const long long int strideC = m * n; /* * A = | 1.0 | 2.0 | 5.0 | 6.0 | * | 3.0 | 4.0 | 7.0 | 8.0 | * * B = | 5.0 | 6.0 | 9.0 | 10.0 | * | 7.0 | 8.0 | 11.0 | 12.0 | */ const std::vector<data_type> A = {1.0, 3.0, 2.0, 4.0, 5.0, 7.0, 6.0, 8.0}; const std::vector<data_type> B = {5.0, 7.0, 6.0, 8.0, 9.0, 11.0, 10.0, 12.0}; std::vector<data_type> C(m * n * batch_count); const data_type alpha = 1.0; const data_type beta = 0.0; data_type *d_A = nullptr; data_type *d_B = nullptr; data_type *d_C = nullptr; oneapi::mkl::transpose transa = oneapi::mkl::transpose::nontrans; oneapi::mkl::transpose transb = oneapi::mkl::transpose::nontrans; printf("A[0]\n"); print_matrix(m, k, A.data(), lda); printf("=====\n"); printf("A[1]\n"); print_matrix(m, k, A.data() + (m * k), lda); printf("=====\n"); printf("B[0]\n"); print_matrix(k, n, B.data(), ldb); printf("=====\n"); printf("B[1]\n"); print_matrix(k, n, B.data() + (k * n), ldb); printf("=====\n"); /* step 1: create cublas handle, bind a stream */ cublasH = &q_ct1; stream = dev_ct1.create_queue(); cublasH = stream; /* step 2: copy data to device */ d_A = (data_type *)sycl::malloc_device(sizeof(data_type) * A.size(), q_ct1); d_B = (data_type *)sycl::malloc_device(sizeof(data_type) * B.size(), q_ct1); d_C = (data_type *)sycl::malloc_device(sizeof(data_type) * C.size(), q_ct1); stream->memcpy(d_A, A.data(), sizeof(data_type) * A.size()); stream->memcpy(d_B, B.data(), sizeof(data_type) * B.size()); /* step 3: compute */ oneapi::mkl::blas::column_major::gemm_batch( *cublasH, transa, transb, m, n, k, alpha, d_A, lda, strideA, d_B, ldb, strideB, beta, d_C, ldc, strideC, batch_count); /* step 4: copy data to host */ stream->memcpy(C.data(), d_C, sizeof(data_type) * C.size()); stream->wait(); /* * C = | 19.0 | 22.0 | 111.0 | 122.0 | * | 43.0 | 50.0 | 151.0 | 166.0 | */ printf("C[0]\n"); print_matrix(m, n, C.data(), ldc); printf("=====\n"); printf("C[1]\n"); print_matrix(m, n, C.data() + (m * n), ldc); printf("=====\n"); /* free resources */ sycl::free(d_A, q_ct1); sycl::free(d_B, q_ct1); sycl::free(d_C, q_ct1); cublasH = nullptr; dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/random_sampling_without_replacement/lottery_device_api.cpp
//============================================================== // Copyright © 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= /* * * Content: * This file contains Multiple Simple Random Sampling without replacement * for DPC++ device interface of random number generators * *******************************************************************************/ #include <iostream> #include <sycl/sycl.hpp> #include "oneapi/mkl/rng/device.hpp" using namespace oneapi; // Initialization value for random number generator static const auto seed = 777; // Lottery default parameters static const auto m_def = 6; // lottery first parameter static const auto n_def = 49; // lottery second parameter static const auto num_exp_def = 11969664; // number of lottery experiments void lottery_device_api(sycl::queue& q, size_t m, size_t n, size_t num_exp, std::vector<size_t>& result_vec) { { sycl::buffer<size_t, 1> result_buf(result_vec.data(), result_vec.size()); q.submit([&](sycl::handler& h) { auto res_acc = result_buf.template get_access<sycl::access::mode::write>(h); sycl::accessor<size_t, 1, sycl::access::mode::read_write, sycl::access::target::local> local_buf(sycl::range<1>{n}, h); h.parallel_for(sycl::nd_range<1>(num_exp, 1), [=](sycl::nd_item<1> item) { size_t id = item.get_group(0); // Let buf contain natural numbers 1, 2, ..., N for (size_t i = 0; i < n; ++i) { local_buf[i] = i + 1; } // Create an object of basic random numer generator (engine) oneapi::mkl::rng::device::philox4x32x10 engine(seed, id * m); // Create an object of distribution (by default float, a = 0.0f, b = 1.0f) oneapi::mkl::rng::device::uniform distr; for (size_t i = 0; i < m; ++i) { auto res = oneapi::mkl::rng::device::generate(distr, engine); // Generate random natural number j from {i,...,N-1} auto j = i + (size_t)(res * (float)(n - i)); // Swap local_buf[i] and local_buf[j] auto tmp = local_buf[i]; local_buf[i] = local_buf[j]; local_buf[j] = tmp; } for (size_t i = 0; i < m; ++i) { // Copy shuffled buffer res_acc[id * m + i] = local_buf[i]; } }); }); } } // Prints last 3 lottery samples void print_results(std::vector<size_t>& res, size_t m) { for (size_t i = res.size() / m - 3; i < res.size() / m; ++i) { std::cout << "Sample " << i << " of lottery of " << res.size() / m << ": "; for (size_t j = 0; j < m; ++j) { std::cout << res[i * m + j] << ", "; } std::cout << std::endl; } std::cout << std::endl; } int main(int argc, char ** argv) { std::cout << std::endl; std::cout << "Multiple Simple Random Sampling without replacement" << std::endl; std::cout << "Device Api" << std::endl; std::cout << "---------------------------------------------------" << std::endl; size_t m = m_def; size_t n = n_def; size_t num_exp = num_exp_def; if(argc >= 4) { m = atol(argv[1]); n = atol(argv[2]); num_exp = atol(argv[3]); if(m == 0 || n == 0 || num_exp == 0 || m > n) { m = m_def; n = n_def; num_exp = num_exp_def; } } std::cout << "M = " << m << ", N = " << n << ", Number of experiments = " << num_exp << std::endl; // This exception handler will catch async exceptions auto exception_handler = [&](sycl::exception_list exceptions) { for(std::exception_ptr const& e : exceptions) { try { std::rethrow_exception(e); } catch (sycl::exception const& e) { std::cout << "Caught asynchronous SYCL exception:\n" << e.what() << std::endl; std::terminate(); } } }; // Result storage std::vector<size_t> result_vec(m * num_exp); try { // Queue constructor passed exception handler sycl::queue q(sycl::default_selector{}, exception_handler); // Launch lottery for device API lottery_device_api(q, m, n, num_exp, result_vec); } catch (...) { // Some other exception detected std::cout << "Failure" << std::endl; std::terminate(); } // Print output print_results(result_vec, m); return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/random_sampling_without_replacement/lottery.cpp
//============================================================== // Copyright © 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= /* * * Content: * This file contains Multiple Simple Random Sampling without replacement * for DPC++ USM-based interface of random number generators * *******************************************************************************/ #include <iostream> #include <sycl/sycl.hpp> #include "oneapi/mkl.hpp" using namespace oneapi; // Initialization value for random number generator static const auto seed = 777; // Lottery default parameters static const auto m_def = 6; // lottery first parameter static const auto n_def = 49; // lottery second parameter static const auto num_exp_def = 11969664; // number of lottery experiments void lottery(sycl::queue& q, size_t m, size_t n, size_t num_exp, size_t* result_ptr) { // Generate (m * num_exp) random numbers // Generator initialization // Create an object of basic random numer generator (engine) oneapi::mkl::rng::philox4x32x10 engine(q, seed); // Create an object of distribution (by default float, a = 0.0f, b = 1.0f) oneapi::mkl::rng::uniform distr; float* rng_buf = sycl::malloc_device<float>(m * num_exp, q); // Random number generation auto event = oneapi::mkl::rng::generate(distr, engine, m * num_exp, rng_buf); // Make sure, that generation is finished event.wait_and_throw(); { event = q.submit([&] (sycl::handler& h) { sycl::accessor<size_t, 1, sycl::access::mode::read_write, sycl::access::target::local> local_buf(sycl::range<1>{n}, h); h.parallel_for(sycl::nd_range<1>(num_exp, 1), [=](sycl::nd_item<1> item) { size_t id = item.get_group(0); // Let buf contain natural numbers 1, 2, ..., N for (size_t i = 0; i < n; ++i) { local_buf[i] = i + 1; } // Shuffle copied buffer for (size_t i = 0; i < m; ++i) { // Generate random natural number j from {i,...,N-1} auto j = i + (size_t)(rng_buf[id * m + i] * (float)(n - i)); // Swap local_buf[i] and local_buf[j] auto tmp = local_buf[i]; local_buf[i] = local_buf[j]; local_buf[j] = tmp; } for (size_t i = 0; i < m; ++i) { // Copy shuffled buffer result_ptr[id * m + i] = local_buf[i]; } }); }); } event.wait_and_throw(); } // Prints last 3 lottery samples void print_results(size_t* result_ptr, size_t m, size_t num_exp) { for (size_t i = num_exp - 3; i < num_exp; ++i) { std::cout << "Sample " << i << " of lottery of " << num_exp << ": "; for (size_t j = 0; j < m; ++j) { std::cout << result_ptr[i * m + j] << ", "; } std::cout << std::endl; } std::cout << std::endl; } int main(int argc, char ** argv) { std::cout << std::endl; std::cout << "Multiple Simple Random Sampling without replacement" << std::endl; std::cout << "Buffer Api" << std::endl; std::cout << "---------------------------------------------------" << std::endl; size_t m = m_def; size_t n = n_def; size_t num_exp = num_exp_def; if(argc >= 4) { m = atol(argv[1]); n = atol(argv[2]); num_exp = atol(argv[3]); if(m == 0 || n == 0 || num_exp == 0 || m > n) { m = m_def; n = n_def; num_exp = num_exp_def; } } std::cout << "M = " << m << ", N = " << n << ", Number of experiments = " << num_exp << std::endl; // This exception handler will catch async exceptions auto exception_handler = [&](sycl::exception_list exceptions) { for(std::exception_ptr const& e : exceptions) { try { std::rethrow_exception(e); } catch (sycl::exception const& e) { std::cout << "Caught asynchronous SYCL exception:\n" << e.what() << std::endl; std::terminate(); } } }; // Pointer to result storage size_t* result_ptr; try { // Queue constructor passed exception handler sycl::queue q(sycl::default_selector{}, exception_handler); // Allocate memory result_ptr = sycl::malloc_shared<size_t>(m * num_exp, q); // Launch lottery for Host USM API lottery(q, m, n, num_exp, result_ptr); } catch (...) { // Some other exception detected std::cout << "Failure" << std::endl; std::terminate(); } // Print output print_results(result_ptr, m, num_exp); return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/random_sampling_without_replacement/lottery_usm.cpp
//============================================================== // Copyright © 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= /* * * Content: * This file contains Multiple Simple Random Sampling without replacement * for DPC++ USM-based interface of random number generators * *******************************************************************************/ #include <iostream> #include <sycl/sycl.hpp> #include "oneapi/mkl.hpp" using namespace oneapi; // Initialization value for random number generator static const auto seed = 777; // Lottery default parameters static const auto m_def = 6; // lottery first parameter static const auto n_def = 49; // lottery second parameter static const auto num_exp_def = 11969664; // number of lottery experiments void lottery(sycl::queue& q, size_t m, size_t n, size_t num_exp, size_t* result_ptr) { // Generate (m * num_exp) random numbers // Generator initialization // Create an object of basic random numer generator (engine) oneapi::mkl::rng::philox4x32x10 engine(q, seed); // Create an object of distribution (by default float, a = 0.0f, b = 1.0f) oneapi::mkl::rng::uniform distr; float* rng_buf = sycl::malloc_device<float>(m * num_exp, q); // Random number generation auto event = oneapi::mkl::rng::generate(distr, engine, m * num_exp, rng_buf); // Make sure, that generation is finished event.wait_and_throw(); { event = q.submit([&] (sycl::handler& h) { sycl::accessor<size_t, 1, sycl::access::mode::read_write, sycl::access::target::local> local_buf(sycl::range<1>{n}, h); h.parallel_for(sycl::nd_range<1>(num_exp, 1), [=](sycl::nd_item<1> item) { size_t id = item.get_group(0); // Let buf contain natural numbers 1, 2, ..., N for (size_t i = 0; i < n; ++i) { local_buf[i] = i + 1; } // Shuffle copied buffer for (size_t i = 0; i < m; ++i) { // Generate random natural number j from {i,...,N-1} auto j = i + (size_t)(rng_buf[id * m + i] * (float)(n - i)); // Swap local_buf[i] and local_buf[j] auto tmp = local_buf[i]; local_buf[i] = local_buf[j]; local_buf[j] = tmp; } for (size_t i = 0; i < m; ++i) { // Copy shuffled buffer result_ptr[id * m + i] = local_buf[i]; } }); }); } event.wait_and_throw(); } // Prints last 3 lottery samples void print_results(size_t* result_ptr, size_t m, size_t num_exp) { for (size_t i = num_exp - 3; i < num_exp; ++i) { std::cout << "Sample " << i << " of lottery of " << num_exp << ": "; for (size_t j = 0; j < m; ++j) { std::cout << result_ptr[i * m + j] << ", "; } std::cout << std::endl; } std::cout << std::endl; } int main(int argc, char ** argv) { std::cout << std::endl; std::cout << "Multiple Simple Random Sampling without replacement" << std::endl; std::cout << "Unified Shared Memory Api" << std::endl; std::cout << "---------------------------------------------------" << std::endl; size_t m = m_def; size_t n = n_def; size_t num_exp = num_exp_def; if(argc >= 4) { m = atol(argv[1]); n = atol(argv[2]); num_exp = atol(argv[3]); if(m == 0 || n == 0 || num_exp == 0 || m > n) { m = m_def; n = n_def; num_exp = num_exp_def; } } std::cout << "M = " << m << ", N = " << n << ", Number of experiments = " << num_exp << std::endl; // This exception handler will catch async exceptions auto exception_handler = [&](sycl::exception_list exceptions) { for(std::exception_ptr const& e : exceptions) { try { std::rethrow_exception(e); } catch (sycl::exception const& e) { std::cout << "Caught asynchronous SYCL exception:\n" << e.what() << std::endl; std::terminate(); } } }; // Pointer to result storage size_t* result_ptr; try { // Queue constructor passed exception handler sycl::queue q(sycl::default_selector{}, exception_handler); // Allocate memory result_ptr = sycl::malloc_shared<size_t>(m * num_exp, q); // Launch lottery for Host USM API lottery(q, m, n, num_exp, result_ptr); } catch (...) { // Some other exception detected std::cout << "Failure" << std::endl; std::terminate(); } // Print output std::cout << "Results with Host API:" << std::endl; print_results(result_ptr, m, num_exp); return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/common/curand_utils.h
/* * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of NVIDIA CORPORATION nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> // CUDA API error checking /* DPCT1001:0: The statement could not be removed. */ /* DPCT1000:1: Error handling if-stmt was detected but could not be rewritten. */ #define CUDA_CHECK(err) \ do { \ dpct::err0 err_ = (err); \ if (err_ != 0) { \ std::printf("CUDA error %d at %s:%d\n", err_, __FILE__, __LINE__); \ throw std::runtime_error("CUDA error"); \ } \ } while (0) // curand API error checking #define CURAND_CHECK(err) \ do { \ int err_ = (err); \ if (err_ != 0) { \ std::printf("curand error %d at %s:%d\n", err_, __FILE__, __LINE__); \ throw std::runtime_error("curand error"); \ } \ } while (0) template <typename T> void print_vector(const std::vector<T> &data); template <> void print_vector(const std::vector<float> &data) { for (auto &i : data) std::printf("%0.6f\n", i); } template <> void print_vector(const std::vector<unsigned int> &data) { for (auto &i : data) std::printf("%d\n", i); }
h
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/scrambled_sobol32/curand_scrambled_sobol32_normal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1033:8: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:9: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1033:10: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:11: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1033:12: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:13: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/scrambled_sobol32/curand_scrambled_sobol32_lognormal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1033:2: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1033:4: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:5: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1033:6: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:7: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/scrambled_sobol32/curand_scrambled_sobol32_uniform_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1033:20: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:21: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->generate_uniform(d_data, h_data.size()))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1033:22: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:23: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_uniform(h_data.data(), h_data.size()))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1033:24: Migrated code uses a basic Sobol generator. Initialize oneapi::mkl::rng::sobol generator with user-defined direction numbers to use it as Scrambled Sobol generator. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; /* Create stream */ /* DPCT1025:25: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/scrambled_sobol32/curand_scrambled_sobol32_poisson_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:14: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:15: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_poisson(d_data, h_data.size(), lambda))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:16: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:17: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_poisson(h_data.data(), h_data.size(), lambda))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:18: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ /* DPCT1025:19: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mtgp32/curand_mtgp32_poisson_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_poisson(d_data, h_data.size(), lambda))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_poisson(h_data.data(), h_data.size(), lambda))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 10; const double lambda = 10.0; const unsigned long long seed = 1234ULL; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mtgp32/curand_mtgp32_lognormal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt2203; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mtgp32/curand_mtgp32_uniform_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->generate_uniform(d_data, h_data.size()))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_uniform(h_data.data(), h_data.size()))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long seed = 1234ULL; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mtgp32/curand_mtgp32_normal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt2203; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/sobol64/curand_sobol64_normal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:8: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:9: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:10: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:11: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:12: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:13: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/sobol64/curand_sobol64_lognormal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:2: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:4: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:5: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:6: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:7: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/sobol64/curand_sobol64_uniform_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:20: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:21: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->generate_uniform(d_data, h_data.size()))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:22: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:23: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_uniform(h_data.data(), h_data.size()))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:24: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; /* Create stream */ /* DPCT1025:25: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/sobol64/curand_sobol64_poisson_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:14: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:15: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_poisson(d_data, h_data.size(), lambda))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:16: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:17: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_poisson(h_data.data(), h_data.size(), lambda))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:18: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ /* DPCT1025:19: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/sobol32/curand_sobol32_lognormal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/sobol32/curand_sobol32_uniform_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasi random floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:11: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->generate_uniform(d_data, h_data.size()))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:12: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_uniform(h_data.data(), h_data.size()))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ /* DPCT1025:13: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/sobol32/curand_sobol32_poisson_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:8: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_poisson(d_data, h_data.size(), lambda))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:9: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_poisson(h_data.data(), h_data.size(), lambda))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ /* DPCT1025:10: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/sobol32/curand_sobol32_normal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:5: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:6: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:7: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mt19937/curand_mt19937_normal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 12; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mt19937/curand_mt19937_poisson_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_poisson(d_data, h_data.size(), lambda))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_poisson(h_data.data(), h_data.size(), lambda))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 12; const double lambda = 10.0; const unsigned long long seed = 1234ULL; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mt19937/curand_mt19937_uniform_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->generate_uniform(d_data, h_data.size()))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_uniform(h_data.data(), h_data.size()))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 12; const unsigned long long seed = 1234ULL; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mt19937/curand_mt19937_lognormal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 12; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/scrambled_sobol64/curand_scrambled_sobol64_normal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:8: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:9: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:10: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:11: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:12: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:13: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/scrambled_sobol64/curand_scrambled_sobol64_lognormal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:2: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:4: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:5: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:6: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:7: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/scrambled_sobol64/curand_scrambled_sobol64_poisson_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:14: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:15: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_poisson(d_data, h_data.size(), lambda))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:16: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:17: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_poisson(h_data.data(), h_data.size(), lambda))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:18: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ /* DPCT1025:19: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/scrambled_sobol64/curand_scrambled_sobol64_uniform_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * quasi random floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create quasi-random number generator */ /* DPCT1032:20: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:21: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->generate_uniform(d_data, h_data.size()))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ /* DPCT1032:22: A different random number generator is used. You may need to adjust the code. */ CURAND_CHECK(DPCT_CHECK_ERROR( gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set Dimension */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_dimensions(num_dimensions))); /* Set ordering */ /* DPCT1007:23: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Generate n floats on host */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_uniform(h_data.data(), h_data.size()))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; /* DPCT1032:24: A different random number generator is used. You may need to adjust the code. */ dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ /* DPCT1025:25: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mrg32k3a/curand_mrg32k3a_poisson_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_poisson(d_data, h_data.size(), lambda))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_poisson(h_data.data(), h_data.size(), lambda))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 10; const double lambda = 10.0; const unsigned long long seed = 1234ULL; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mrg32k3a/curand_mrg32k3a_uniform_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->generate_uniform(d_data, h_data.size()))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_uniform(h_data.data(), h_data.size()))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mrg32k3a/curand_mrg32k3a_lognormal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/mrg32k3a/curand_mrg32k3a_normal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK(DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/philox/curand_philox_lognormal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK( DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:2: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK( DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:3: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::philox4x32x10; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 12; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:4: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/philox/curand_philox_poisson_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK( DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:8: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_poisson(d_data, h_data.size(), lambda))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK( DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:9: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_poisson(h_data.data(), h_data.size(), lambda))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::philox4x32x10; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 12; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; /* Create stream */ /* DPCT1025:10: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, seed, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, seed, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/philox/curand_philox_uniform_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK( DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:11: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->generate_uniform(d_data, h_data.size()))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK( DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:12: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK( DPCT_CHECK_ERROR(gen->generate_uniform(h_data.data(), h_data.size()))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::philox4x32x10; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 12; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; /* Create stream */ /* DPCT1025:13: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/01_sycl_dpct_output/philox/curand_philox_normal_example.cpp.dp.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ CUDA_CHECK(DPCT_CHECK_ERROR(d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1))); /* Create pseudo-random number generator */ CURAND_CHECK( DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:5: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on device */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(d_data, h_data.size(), mean, stddev))); /* Copy data to host */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()))); /* Sync stream */ CUDA_CHECK(DPCT_CHECK_ERROR(stream->wait())); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(sycl::free(d_data, q_ct1))); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ CURAND_CHECK( DPCT_CHECK_ERROR(gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10))); /* Set cuRAND to stream */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_queue(stream))); /* Set offset */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->skip_ahead(offset))); /* Set ordering */ /* DPCT1007:6: Migration of curandSetGeneratorOrdering is not supported. */ CURAND_CHECK(curandSetGeneratorOrdering(gen, order)); /* Set seed */ CURAND_CHECK(DPCT_CHECK_ERROR(gen->set_seed(seed))); /* Generate n floats on host */ CURAND_CHECK(DPCT_CHECK_ERROR( gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev))); /* Cleanup */ CURAND_CHECK(DPCT_CHECK_ERROR(gen.reset())); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::philox4x32x10; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 12; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ /* DPCT1025:7: The SYCL queue is created ignoring the flag and priority options. */ CUDA_CHECK(DPCT_CHECK_ERROR(stream = dev_ct1.create_queue())); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.destroy_queue(stream))); CUDA_CHECK(DPCT_CHECK_ERROR(dev_ct1.reset())); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/Common/curand_utils.h
/* * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of NVIDIA CORPORATION nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> // CUDA API error checking /* DPCT1001:0: The statement could not be removed. */ /* DPCT1000:1: Error handling if-stmt was detected but could not be rewritten. */ #define CUDA_CHECK(err) \ do { \ dpct::err0 err_ = (err); \ if (err_ != 0) { \ std::printf("CUDA error %d at %s:%d\n", err_, __FILE__, __LINE__); \ throw std::runtime_error("CUDA error"); \ } \ } while (0) // curand API error checking #define CURAND_CHECK(err) \ do { \ int err_ = (err); \ if (err_ != 0) { \ std::printf("curand error %d at %s:%d\n", err_, __FILE__, __LINE__); \ throw std::runtime_error("curand error"); \ } \ } while (0) template <typename T> void print_vector(const std::vector<T> &data); template <> void print_vector(const std::vector<float> &data) { for (auto &i : data) std::printf("%0.6f\n", i); } template <> void print_vector(const std::vector<unsigned int> &data) { for (auto &i : data) std::printf("%d\n", i); } /** * CURAND ordering of results in memory */ enum curandOrdering { CURAND_ORDERING_PSEUDO_BEST = 100, ///< Best ordering for pseudorandom results CURAND_ORDERING_PSEUDO_DEFAULT = 101, ///< Specific default thread sequence for pseudorandom results, same as CURAND_ORDERING_PSEUDO_BEST CURAND_ORDERING_PSEUDO_SEEDED = 102, ///< Specific seeding pattern for fast lower quality pseudorandom results CURAND_ORDERING_PSEUDO_LEGACY = 103, ///< Specific legacy sequence for pseudorandom results, guaranteed to remain the same for all cuRAND release CURAND_ORDERING_PSEUDO_DYNAMIC = 104, ///< Specific ordering adjusted to the device it is being executed on, provides the best performance CURAND_ORDERING_QUASI_DEFAULT = 201 ///< Specific n-dimensional ordering for quasirandom results }; /* * CURAND ordering of results in memory */ /** \cond UNHIDE_TYPEDEFS */ typedef enum curandOrdering curandOrdering_t; /** \endcond */
h
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/scrambled_sobol32/scrambled_sobol32_uniform.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on device */ gen->generate_uniform(d_data, h_data.size()); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on host */ gen->generate_uniform(h_data.data(), h_data.size()); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/scrambled_sobol32/scrambled_sobol32_lognormal.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on device */ gen->generate_lognormal(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on host */ gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/scrambled_sobol32/scrambled_sobol32_poisson.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_poisson(d_data, h_data.size(), lambda); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_poisson(h_data.data(), h_data.size(), lambda); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/scrambled_sobol32/scrambled_sobol32_normal.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on device */ gen->generate_gaussian(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on host */ gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mtgp32/mtgp32_poisson.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_poisson(d_data, h_data.size(), lambda); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_poisson(h_data.data(), h_data.size(), lambda); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 10; const double lambda = 10.0; const unsigned long long seed = 1234ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mtgp32/mtgp32_lognormal.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_lognormal(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt2203; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mtgp32/mtgp32_uniform.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_uniform(d_data, h_data.size()); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_uniform(h_data.data(), h_data.size()); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long seed = 1234ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mtgp32/mtgp32_normal.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_gaussian(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt2203; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/sobol64/sobol64_lognormal.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on device */ gen->generate_lognormal(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on host */ gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/sobol64/sobol64_normal.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on device */ gen->generate_gaussian(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on host */ gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/sobol64/sobol64_uniform.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on device */ gen->generate_uniform(d_data, h_data.size()); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Generate n floats on host */ gen->generate_uniform(h_data.data(), h_data.size()); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const unsigned long long offset = 0ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/sobol64/sobol64_poisson.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_poisson(d_data, h_data.size(), lambda); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_poisson(h_data.data(), h_data.size(), lambda); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 10; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/sobol32/sobol32_uniform.cpp
/* * This program uses the host CURAND API to generate 100 * quasi random floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_uniform(d_data, h_data.size()); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_uniform(h_data.data(), h_data.size()); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/sobol32/sobol32_poisson.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_poisson(d_data, h_data.size(), lambda); /* Copy data to host */ stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_poisson(h_data.data(), h_data.size(), lambda); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/sobol32/sobol32_normal.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_gaussian(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; const data_type mean = 1.0f; const data_type stddev = 2.0f; stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/sobol32/sobol32_lognormal.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_lognormal(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mt19937/mt19937_uniform.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_uniform(d_data, h_data.size()); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_uniform(h_data.data(), h_data.size()); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 12; const unsigned long long seed = 1234ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mt19937/mt19937_normal.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_gaussian(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 12; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mt19937/mt19937_poisson.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_poisson(d_data, h_data.size(), lambda); /* Copy data to host */ stream->memcpy( h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_poisson(h_data.data(), h_data.size(), lambda); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 12; const double lambda = 10.0; const unsigned long long seed = 1234ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mt19937/mt19937_lognormal.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_lognormal(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt19937); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mt19937; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 12; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/scrambled_sobol64/scrambled_sobol64_poisson.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_poisson(d_data, h_data.size(), lambda); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_poisson(h_data.data(), h_data.size(), lambda); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/scrambled_sobol64/scrambled_sobol64_uniform.cpp
/* * This program uses the host CURAND API to generate 100 * quasi random floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_uniform(d_data, h_data.size()); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_uniform(h_data.data(), h_data.size()); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/scrambled_sobol64/scrambled_sobol64_lognormal.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_lognormal(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/scrambled_sobol64/scrambled_sobol64_normal.cpp
/* * This program uses the host CURAND API to generate 100 * quasirandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on device */ gen->generate_gaussian(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned int &num_dimensions, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create quasi-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::sobol); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set Dimension */ gen->set_dimensions(num_dimensions); /* Generate n floats on host */ gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::sobol; curandOrdering_t order = CURAND_ORDERING_QUASI_DEFAULT; const int n = 12; const unsigned long long offset = 0ULL; const unsigned int num_dimensions = 1; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, num_dimensions, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mrg32k3a/mrg32k3a_uniform.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_uniform(d_data, h_data.size()); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_uniform(h_data.data(), h_data.size()); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mrg32k3a/mrg32k3a_normal.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_gaussian(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mrg32k3a/mrg32k3a_poisson.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_poisson(d_data, h_data.size(), lambda); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_poisson(h_data.data(), h_data.size(), lambda); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_DEFAULT; const int n = 10; const double lambda = 10.0; const unsigned long long seed = 1234ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, seed, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, seed, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/mrg32k3a/mrg32k3a_lognormal.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_lognormal(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mrg32k3a); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::mrg32k3a; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 10; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ gen.reset(); dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/philox/philox_poisson.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = unsigned int; void run_on_device(const int &n, const unsigned long long &offset, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_poisson(d_data, h_data.size(), lambda); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned long long &seed, const double &lambda, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_poisson(h_data.data(), h_data.size(), lambda); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::philox4x32x10; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 12; const double lambda = 10.0; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, seed, lambda, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, seed, lambda, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/philox/philox_normal.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_gaussian(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_gaussian(h_data.data(), h_data.size(), mean, stddev); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::philox4x32x10; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 12; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/philox/philox_lognormal.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_lognormal(d_data, h_data.size(), mean, stddev); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const data_type &mean, const data_type &stddev, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_lognormal(h_data.data(), h_data.size(), mean, stddev); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::philox4x32x10; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 12; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; const data_type mean = 1.0f; const data_type stddev = 2.0f; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, mean, stddev, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/guided_cuRAND_examples_SYCL_migration/02_sycl_dpct_migrated/philox/philox_uniform.cpp
/* * This program uses the host CURAND API to generate 100 * pseudorandom floats. */ #include <sycl/sycl.hpp> #include <dpct/dpct.hpp> #include <cstdio> #include <cstdlib> #include <cstring> #include <stdexcept> #include <vector> #include <dpct/rng_utils.hpp> #include "curand_utils.h" using data_type = float; void run_on_device(const int &n, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); sycl::queue &q_ct1 = dev_ct1.default_queue(); data_type *d_data = nullptr; /* C data to device */ d_data = (data_type *)sycl::malloc_device( sizeof(data_type) * h_data.size(), q_ct1); /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on device */ gen->generate_uniform(d_data, h_data.size()); /* Copy data to host */ stream->memcpy(h_data.data(), d_data, sizeof(data_type) * h_data.size()); /* Sync stream */ stream->wait(); /* Cleanup */ sycl::free(d_data, q_ct1); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } void run_on_host(const int &n, const unsigned long long &offset, const unsigned long long &seed, const curandOrdering_t &order, const dpct::rng::random_engine_type &rng, const dpct::queue_ptr &stream, dpct::rng::host_rng_ptr &gen, std::vector<data_type> &h_data) try { /* Create pseudo-random number generator */ gen = dpct::rng::create_host_rng( dpct::rng::random_engine_type::philox4x32x10); /* Set cuRAND to stream */ gen->set_queue(stream); /* Set offset */ gen->skip_ahead(offset); /* Set seed */ gen->set_seed(seed); /* Generate n floats on host */ gen->generate_uniform(h_data.data(), h_data.size()); /* Cleanup */ gen.reset(); } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); } int main(int argc, char *argv[]) try { dpct::device_ext &dev_ct1 = dpct::get_current_device(); dpct::queue_ptr stream = &dpct::get_default_queue(); dpct::rng::host_rng_ptr gen = NULL; dpct::rng::random_engine_type rng = dpct::rng::random_engine_type::philox4x32x10; curandOrdering_t order = CURAND_ORDERING_PSEUDO_BEST; const int n = 12; const unsigned long long offset = 0ULL; const unsigned long long seed = 1234ULL; /* Create stream */ stream = dev_ct1.create_queue(); /* Allocate n floats on host */ std::vector<data_type> h_data(n, 0); run_on_host(n, offset, seed, order, rng, stream, gen, h_data); printf("Host\n"); print_vector(h_data); printf("=====\n"); run_on_device(n, offset, seed, order, rng, stream, gen, h_data); printf("Device\n"); print_vector(h_data); printf("=====\n"); /* Cleanup */ dev_ct1.destroy_queue(stream); dev_ct1.reset(); return EXIT_SUCCESS; } catch (sycl::exception const &exc) { std::cerr << exc.what() << "Exception caught at file:" << __FILE__ << ", line:" << __LINE__ << std::endl; std::exit(1); }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/block_lu_decomposition/dgeblttrs.cpp
//============================================================== // Copyright © 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= /* * Content: * Function DGEBLTTRS for solving a system of linear equations * with LU-factored block tridiagonal coefficient matrix and * multiple right hand sides. ************************************************************************ * Definition: * =========== * int64_t dgeblttrs(sycl::queue queue, int64_t n, int64_t nb, int64_t nrhs, double* d, double* dl, double* du1, double* du2, int64_t* ipiv, double* f, int64_t ldf) * * Purpose: * ======== * DGEBLTTRS solves system of linear equations AX = F with general block * tridiagonal coefficient matrix * (D_1 C_1 ) * (B_1 D_2 C_2 ) * ( B_2 D_3 C_3 ) * A= ( ......... ) * ( B_N-2 D_N-1 C_N-1 ) * ( B_N-1 D_N ) * * LU-factored by DGEBLTTRF and multiple RHS F. * * Arguments: * ========== * QUEUE (input) sycl queue * The device queue * * N (input) int64_t * The number of block rows of the matrix A. N > 0. * * NB (input) int64_t * The size of blocks. NB > 0. * * NRHS (input) int64_t * The number of right hand sides. NRHS > 0. * * D (input) double array, dimension (NB) * (N*NB) * The array stores N diagonal blocks (each of size NB by NB) * of triangular factors L and U as they are returned by * DGEBLTTRF. Diagonal blocks of factors L and U are lower and * upper triangular respectively, and their diagonal blocks * with the same index are stored in a block of D with the same * index occupying respectively lower and upper triangles of a * block in D. Unit diagonal elements of factor L are not stored. * * DL (input) double array, dimension (NB) * ((N-1)*NB) * The array stores subdiagonal blocks of lower triangular factor L * as they are returned by DGEBLTTRF. * * DU1 (input) double array, dimension (NB) * ((N-1)*NB) * The array stores superdiagonal blocks of upper triangular factor U * as they are returned by DGEBLTTRF. * * DU2 (input) double array, dimension (NB) * ((N-2)*NB) * The array stores blocks of the second superdiagonal of upper * triangular factor U as they are returned by DGEBLTTRF. * * IPIV (input) int64_t array, dimension (NB) * (N) * The array stores pivot 'local' row indices ('local' means indices * vary in the range 1..2*NB. Global row index is * IPIV(I,K) + (K-1)*NB ). * * F (input/output) double array, dimension (LDF) * (NRHS) * On entry, the array stores NRHS columns of right hand F of the * system of linear equations AX = F. * On exit, the array stores NRHS columns of unknowns of the system * of linear equations AX = F. * * LDF (input) int64_t. LDF >= N*NB * Leading dimension of the array F * * INFO (return) int64_t * = 0: successful exit * < 0: if INFO = -i, the i-th argument had an illegal value ***********************************************************************/ #include <cstdint> #include <sycl/sycl.hpp> #include "oneapi/mkl.hpp" using namespace oneapi; int64_t dgeblttrs(sycl::queue queue, int64_t n, int64_t nb, int64_t nrhs, double* d, double* dl, double* du1, double* du2, int64_t* ipiv, double* f, int64_t ldf) { // Matrix accessors auto D = [=,&d] (int64_t i, int64_t j) -> double& { return d[i + j*nb]; }; auto DL = [=,&dl] (int64_t i, int64_t j) -> double& { return dl[i + j*nb]; }; auto DU1 = [=,&du1] (int64_t i, int64_t j) -> double& { return du1[i + j*nb]; }; auto DU2 = [=,&du2] (int64_t i, int64_t j) -> double& { return du2[i + j*nb]; }; auto IPIV = [=,&ipiv] (int64_t i, int64_t j) -> int64_t& { return ipiv[i + j*nb]; }; auto F = [=,&f] (int64_t i, int64_t j) -> double& { return f[i + j*ldf]; }; // Test the input arguments. int64_t info = 0; if (n <= 0) info = -1; else if (nb <= 0) info = -2; else if (nrhs <= 0) info = -3; else if (ldf < n*nb) info = -10; if (info) return info; sycl::context context = queue.get_context(); sycl::device device = queue.get_device(); // Forward substitution // In the loop compute components Y_K stored in array F for (int64_t k = 0; k < n-2; k++) { for (int64_t i = 0; i < nb; i++) { if (IPIV(i,k) != i+1){ auto event1 = mkl::blas::swap(queue, nrhs, &F(k*nb+i, 0), ldf, &F(k*nb+IPIV(i,k)-1, 0), ldf); event1.wait_and_throw(); } } auto event1 = mkl::blas::trsm(queue, mkl::side::left, mkl::uplo::lower, mkl::transpose::nontrans, mkl::diag::unit, nb, nrhs, 1.0, &D(0, k*nb), nb, &F(k*nb, 0), ldf); auto event2 = mkl::blas::gemm(queue, mkl::transpose::nontrans, mkl::transpose::nontrans, nb, nrhs, nb, -1.0, &DL(0, k*nb), nb, &F(k*nb, 0), ldf, 1.0, &F((k+1)*nb, 0), ldf, {event1}); event2.wait_and_throw(); } // Apply two last pivots for (int64_t i = 0; i < nb; i++) { if (IPIV(i,n-2) != i+1){ auto event1 = mkl::blas::swap(queue, nrhs, &F((n-2)*nb+i, 0), ldf, &F((n-2)*nb+IPIV(i,n-2)-1, 0), ldf); event1.wait_and_throw(); } } for (int64_t i = 0; i < nb; i++) { if (IPIV(i,n-1) != i+1 + nb){ auto event1 = mkl::blas::swap(queue, nrhs, &F((n-1)*nb+i, 0), ldf, &F((n-2)*nb+IPIV(i,n-1)-1, 0), ldf); event1.wait_and_throw(); } } // Computing components Y_N-1 and Y_N out of loop { auto event1 = mkl::blas::trsm(queue, mkl::side::left, mkl::uplo::lower, mkl::transpose::nontrans, mkl::diag::unit, nb, nrhs, 1.0, &D(0, (n-2)*nb), nb, &F((n-2)*nb, 0), ldf); auto event2 = mkl::blas::gemm(queue, mkl::transpose::nontrans, mkl::transpose::nontrans, nb, nrhs, nb, -1.0, &DL(0, (n-2)*nb), nb, &F((n-2)*nb, 0), ldf, 1.0, &F((n-1)*nb, 0), ldf, {event1}); auto event3 = mkl::blas::trsm(queue, mkl::side::left, mkl::uplo::lower, mkl::transpose::nontrans, mkl::diag::unit, nb, nrhs, 1.0, &D(0, (n-1)*nb), nb, &F((n-1)*nb, 0), ldf, {event2}); event3.wait_and_throw(); } // Backward substitution // Computing _N out of loop and store in array F { auto event1 = mkl::blas::trsm(queue, mkl::side::left, mkl::uplo::upper, mkl::transpose::nontrans, mkl::diag::nonunit, nb, nrhs, 1.0, &D(0, (n-1)*nb), nb, &F((n-1)*nb, 0), ldf); event1.wait_and_throw(); } // Computing _N-1 out of loop and store in array F { auto event1 = mkl::blas::gemm(queue, mkl::transpose::nontrans, mkl::transpose::nontrans, nb, nrhs, nb, -1.0, &DU1(0, (n-2)*nb), nb, &F((n-1)*nb, 0), ldf, 1.0, &F((n-2)*nb, 0), ldf); auto event2 = mkl::blas::trsm(queue, mkl::side::left, mkl::uplo::upper, mkl::transpose::nontrans, mkl::diag::nonunit, nb, nrhs, 1.0, &D(0, (n-2)*nb), nb, &F((n-2)*nb, 0), ldf, {event1}); event2.wait_and_throw(); } // In the loop computing components _K stored in array F for (int64_t k = n-3; k >= 0; k--) { auto event1 = mkl::blas::gemm(queue, mkl::transpose::nontrans, mkl::transpose::nontrans, nb, nrhs, nb, -1.0, &DU1(0, k*nb), nb, &F((k+1)*nb, 0), ldf, 1.0, &F(k*nb, 0), ldf); auto event2 = mkl::blas::gemm(queue, mkl::transpose::nontrans, mkl::transpose::nontrans, nb, nrhs, nb, -1.0, &DU2(0, k*nb), nb, &F((k+2)*nb, 0), ldf, 1.0, &F(k*nb, 0), ldf, {event1}); auto event3 = mkl::blas::trsm(queue, mkl::side::left, mkl::uplo::upper, mkl::transpose::nontrans, mkl::diag::nonunit, nb, nrhs, 1.0, &D(0, k*nb), nb, &F(k*nb, 0), ldf, {event2}); event3.wait_and_throw(); } return info; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/block_lu_decomposition/dgeblttrf.cpp
//============================================================== // Copyright © 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= /* * * Content: * Function DGEBLTTRF for LU factorization of general block * tridiagonal matrix; * Function PTLDGETRF for partial LU factorization of general * rectangular matrix. ************************************************************************/ #include <cstdint> #include <sycl/sycl.hpp> #include "oneapi/mkl.hpp" using namespace oneapi; int64_t ptldgetrf(sycl::queue queue, int64_t m, int64_t n, int64_t k, double* a, int64_t lda, int64_t* ipiv); /************************************************************************ * Definition: * =========== * int64_t dgeblttrf(sycl::queue queue, int64_t n, int64_t nb, double* d, double* dl, double* du1, double* du2, int64_t* ipiv) { * * Purpose: * ======== * DGEBLTTRF computes LU factorization of general block tridiagonal * matrix * (D_1 C_1 ) * (B_1 D_2 C_2 ) * ( B_2 D_3 C_3 ) * ( ......... ) * ( B_N-2 D_N-1 C_N-1 ) * ( B_N-1 D_N ) * using elimination with partial pivoting and row interchanges. * The factorization has the form A = L*U, where L is a product of * permutation and unit lower bidiagonal block matrices and U is upper * triangular with nonzeroes in only the main block diagonal and first * two block superdiagonals. * This is a block version of LAPACK DGTTRF subroutine. * * Arguments: * ========== * QUEUE (input) sycl queue * The device queue * * N (input) int64_t * The number of block rows of the matrix A. N > 0. * * NB (input) int64_t * The size of blocks. NB > 0. * * D (input/output) double array, dimension (NB)*(N*NB) * On entry, the array stores N diagonal blocks (each of size NB by NB) * of the matrix to be factored. The blocks are stored * sequentially: first NB columns of D store block D_1, second NB * columns store block D_2,...,last NB columns store block D_N. * On exit, the array stores diagonal blocks of triangular factor L * and U. Diagonal blocks of lower triangular factor L replace * respective lower triangles of blocks D_j (1 <= j <= N). * Diagonal units are not stored. Diagonal blocks of upper * triangular factor U replace respective upper triangles of * blocks D_j. * * DL (input/output) double array, dimension (NB)*((N-1)*NB) * On entry, the array stores N-1 subdiagonal blocks (each of size * NB by NB) of the matrix to be factored. The blocks are stored * sequentially: first NB columns of DL store block B_1, second * NB columns store block B_2,...,last NB columns store block * B_N-1. * On exit, the array stores subdiagonal blocks of lower triangular * factor L. * * DU1 (input/output) double array, dimension (NB)*((N-1)*NB) * On entry, the array stores N-1 superdiagonal blocks (each of size * NB by NB) of the matrix to be factored. The blocks are stored * sequentially: first NB columns of DU1 store block C_1, second * NB columns store block C_2,...,last NB columns store block * C_N-1. * On exit, the array stores superdiagonal blocks of triangular * factor U. * * DU2 (output) double array, dimension (NB)*((N-2)*NB) * On exit, the array stores blocks of the second superdiagonal of * triangular factor U. * * IPIV (output) int64_t array, dimension (NB)*(N) * The pivot 'local' row indices ('local' means indices vary in the * range 1..2*NB. Global row index is IPIV(I,K) + (K-1)*NB ). * * INFO (return) int64_t * = 0: successful exit * = -1000 memory buffer could not be allocated * < 0: if INFO = -i, the i-th argument had an illegal value * > 0: if INFO = i, U(i,i) is exactly zero. The factorization * can be not completed. ***********************************************************************/ int64_t dgeblttrf(sycl::queue queue, int64_t n, int64_t nb, double* d, double* dl, double* du1, double* du2, int64_t* ipiv) { // Matrix accessors auto D = [=,&d] (int64_t i, int64_t j) -> double& { return d[i + j*nb]; }; auto DL = [=,&dl] (int64_t i, int64_t j) -> double& { return dl[i + j*nb]; }; auto DU1 = [=,&du1] (int64_t i, int64_t j) -> double& { return du1[i + j*nb]; }; auto DU2 = [=,&du2] (int64_t i, int64_t j) -> double& { return du2[i + j*nb]; }; auto IPIV = [=,&ipiv] (int64_t i, int64_t j) -> int64_t& { return ipiv[i + j*nb]; }; // Test the input arguments. int64_t info=0; if(n <= 0) info = -1; else if(nb <= 0) info = -2; if(info) return info; sycl::context context = queue.get_context(); sycl::device device = queue.get_device(); // Allocating a contiguous USM array for partial factorizations const int64_t lda = 2*nb; double* a = sycl::malloc_shared<double>(lda * 3*nb, device, context); auto A = [=,&a](int64_t i, int64_t j) -> double& { return a[i + j*lda]; }; if (!a) { info = -1000; goto cleanup; } for (int64_t k = 0; k < n-2; k++){ // Form a 2*NB x 3*NB submatrix // D_K C_K 0 // B_K D_K+1 C_K+1 for (int64_t j = 0; j < nb; j++) { auto event1 = mkl::blas::copy(queue, nb, &D(0,(k)*nb + j), 1, &A(0, j),1); auto event2 = mkl::blas::copy(queue, nb, &DL(0,(k)*nb + j), 1, &A(nb, j),1); auto event3 = mkl::blas::copy(queue, nb, &DU1(0,(k)*nb + j), 1, &A(0, nb+j),1); auto event4 = mkl::blas::copy(queue, nb, &D(0,(k+1)*nb + j), 1, &A(nb, nb+j),1); auto event5 = mkl::blas::copy(queue, nb, &DU1(0,(k+1)*nb + j), 1, &A(nb,2*nb+j),1); event1.wait_and_throw(); event2.wait_and_throw(); event3.wait_and_throw(); event4.wait_and_throw(); event5.wait_and_throw(); queue.submit([&](sycl::handler& cgh) { cgh.parallel_for(sycl::range<1>(nb), [=] (sycl::id<1> it) { const int64_t i = it[0]; a[i + (2*nb + j)*lda] = 0.0; }); }); queue.wait(); } // Partial factorization of the submatrix // (D_K C_K 0 ) (L_K,K ) (U_K,K U_K,K+1, U_K,K+2) // ( ) = P * ( ) * // (B_K D_K+1 C_K+1) (L_K+1,K+1) // // ( 0 0 0 ) // + ( ) // ( 0 D'_K+1 C'_K+1) info = ptldgetrf(queue, 2*nb, 3*nb, nb, &A(0,0), lda, &IPIV(0,k)); if (info > 0) { // INFO is equal to the 'global' index of the element u_ii of the factor // U which is equal to zero return info + k*nb; } // Factorization results to be copied back to arrays: // L_K,K, U_K,K, D'_K+1 -> D // L_K+1,K -> DL // U_K,K+1 -> DU1 // U_K,K+2 -> DU2 for(int64_t j = 0; j < nb; j++) { auto event1 = mkl::blas::copy(queue, nb, &A( 0, j), 1, &D(0,k*nb + j), 1); auto event2 = mkl::blas::copy(queue, nb, &A(nb, j), 1, &DL(0,k*nb + j), 1); auto event3 = mkl::blas::copy(queue, nb, &A( 0, nb+j), 1, &DU1(0,k*nb + j), 1); auto event4 = mkl::blas::copy(queue, nb, &A(nb, nb+j), 1, &D(0,(k+1)*nb + j), 1); auto event5 = mkl::blas::copy(queue, nb, &A( 0,2*nb+j), 1, &DU2(0,k*nb + j), 1); auto event6 = mkl::blas::copy(queue, nb, &A(nb,2*nb+j), 1, &DU1(0,(k+1)*nb + j), 1); event1.wait_and_throw(); event2.wait_and_throw(); event3.wait_and_throw(); event4.wait_and_throw(); event5.wait_and_throw(); event6.wait_and_throw(); } } // Out of loop factorization of the last 2*NBx2*NB submatrix // (D_N-1 C_N-1) (L_N-1,N-1 0) (U_N-1,N-1 U_N-1,N ) // ( ) = P_N-1* ( ) * ( ) // (B_N-1 D_N) ( L_N,N-1 L_N,N) ( 0 U_N,N ) for(int64_t j = 0; j < nb; j++) { auto event1 = mkl::blas::copy(queue, nb, &D(0, (n-2)*nb + j), 1, &A(0, j), 1); auto event2 = mkl::blas::copy(queue, nb, &DL(0, (n-2)*nb + j), 1, &A(nb+0, j), 1); auto event3 = mkl::blas::copy(queue, nb, &DU1(0, (n-2)*nb + j), 1, &A( 0, nb+j), 1); auto event4 = mkl::blas::copy(queue, nb, &D(0, (n-1)*nb + j), 1, &A(nb+0, nb+j), 1); event1.wait_and_throw(); event2.wait_and_throw(); event3.wait_and_throw(); event4.wait_and_throw(); } // Pivoting array for the last factorization has 2*NB elements stored in // two last columns of IPIV try { std::int64_t scratchpad_size = mkl::lapack::getrf_scratchpad_size<double>(queue, 2*nb, 2*nb, lda); double* scratchpad = sycl::malloc_shared<double>(scratchpad_size, device, context); if (!scratchpad) { info = -1000; goto cleanup; } auto event1 = mkl::lapack::getrf(queue, 2*nb, 2*nb, &A(0,0), lda, &IPIV(0,n-2), scratchpad, scratchpad_size ); event1.wait_and_throw(); sycl::free(scratchpad, context); } catch(mkl::lapack::exception const& e) { // Handle LAPACK related exceptions happened during synchronous call std::cout << "Unexpected exception caught during synchronous call to LAPACK API:\ninfo: " << e.info() << std::endl; if (e.info() > 0) { // INFO is equal to the 'global' index of the element u_ii of the factor // U which is equal to zero info = e.info() + (n-2)*nb; } return info; } // Copy the last result back to arrays: // L_N-1,N-1, L_N,N, U_N-1,N-1, U_N,N -> D // L_N,N-1 -> DL // U_N-1,N -> DU1 for (int64_t j = 0; j < nb; j++) { auto event1 = mkl::blas::copy(queue, nb, &A(0, j), 1, &D(0, (n-2)*nb + j), 1); auto event2 = mkl::blas::copy(queue, nb, &A(nb+0, j), 1, &DL(0, (n-2)*nb + j), 1); auto event3 = mkl::blas::copy(queue, nb, &A(0, nb + j), 1, &DU1(0, (n-2)*nb + j), 1); auto event4 = mkl::blas::copy(queue, nb, &A(nb+0, nb + j), 1, &D(0, (n-1)*nb + j), 1); event1.wait_and_throw(); event2.wait_and_throw(); event3.wait_and_throw(); event4.wait_and_throw(); } sycl::free(a, context); return info; cleanup: sycl::free(a, context); return info; } /********************************************************************** * Purpose: * ======== * PTLDGETRF computes partial (in a case K<min(M,N)) LU factorization * of matrix A = P*(L*U+A1) * * Arguments: * ========== * M (input) int64_t * The number of rows of the matrix A. M >= 0. * * N (input) int64_t * The number of columns of the matrix A. N >= 0. * * K (input) int64_t * The number of columns of the matrix A participating in * factorization. N >= K >= 0 * * A (input/output) double array, dimension (LDA)*(N) * On entry, the M-by-N matrix A to be factored. * On exit: * if K >= min(M,N), A is overwritten by details of its LU * factorization as returned by DGETRF. * if K < min(M,N), partial factorization A = P * (L * U + A1) * is performed where P is permutation matrix (pivoting); * L is M by K lower trapezoidal (with unit diagonal) matrix * stored in lower MxK trapezoid of A. Diagonal units * are not stored. * U is K by N upper trapezoidal matrix stored in upper * K by N trapezoid of A; * A1 is (M-K) by (N-K) residual stored in intersection * of last M-K rows and last N-K columns of A. * * LDA (input) int64_t * The leading dimension of the array A. LDA >= max(1,M). * * IPIV (output) int64_t array, dimension (min(M,K)) * The pivot indices; for 1 <= i <= min(M,K), row i of the * matrix was interchanged with row IPIV(i). * * INFO (return) int64_t * = 0: successful exit * < 0: if INFO = -i, the i-th argument had an illegal value * > 0: if INFO = i, U(i,i) is exactly zero. The factorization * can be not completed. ***********************************************************************/ #undef A #undef D #undef DL #undef DU1 #undef DU2 #undef IPIV int64_t ptldgetrf(sycl::queue queue, int64_t m, int64_t n, int64_t k, double* a, int64_t lda, int64_t* ipiv) { auto A = [=,&a](int64_t i, int64_t j) -> double& { return a[i + j*lda]; }; sycl::context context = queue.get_context(); sycl::device device = queue.get_device(); int64_t info=0; if(m < 0) info = -1; else if(n < 0) info = -2; else if( (k > n) || (k <0)) info = -3; else if(lda < m) info = -5; if(info) return info; if(k < std::min<int64_t>(m,n)) { // LU factorization of first K columns { try { std::int64_t scratchpad_size = mkl::lapack::getrf_scratchpad_size<double>(queue, m, k, lda); double* scratchpad = sycl::malloc_shared<double>(scratchpad_size, device, context); if (!scratchpad) { info = -1000; return info; } auto event1 = mkl::lapack::getrf(queue, m, k, &A(0,0), lda, &ipiv[0], scratchpad, scratchpad_size ); event1.wait_and_throw(); sycl::free(scratchpad, context); } catch(mkl::lapack::exception const& e) { // Handle LAPACK related exceptions happened during synchronous call std::cout << "Unexpected exception caught during synchronous call to LAPACK API:\ninfo: " << e.info() << std::endl; if (e.info() > 0) { info = e.info(); } return info; } } for (int64_t i = 0; i < k; i++) { if(ipiv[i] != i+1) { // Applying permutations returned by DGETRF to last N-K columns auto event1 = mkl::blas::swap(queue, n-k, &A(i,k), lda, &A(ipiv[i]-1, k), lda); event1.wait_and_throw(); } } // Updating A1 { auto event1 = mkl::blas::trsm(queue, mkl::side::left, mkl::uplo::lower, mkl::transpose::nontrans, mkl::diag::unit, k, n-k, 1.0, &A(0,0), lda, &A(0,k), lda); auto event2 = mkl::blas::gemm(queue, mkl::transpose::nontrans, mkl::transpose::nontrans, m-k, n-k, k, -1.0, &A(k,0), lda, &A(0,k), lda, 1.0, &A(k,k), lda, {event1}); event2.wait_and_throw(); } } else { std::int64_t scratchpad_size = mkl::lapack::getrf_scratchpad_size<double>(queue, m, n, lda); double* scratchpad = sycl::malloc_shared<double>(scratchpad_size, device, context); if (!scratchpad) { info = -1000; return info; } auto event1 = mkl::lapack::getrf(queue, m, n, &A(0,0), lda, &ipiv[0], scratchpad, scratchpad_size ); event1.wait_and_throw(); sycl::free(scratchpad, context); } return info; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/block_lu_decomposition/auxi.cpp
//============================================================== // Copyright © 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= /* * Content: * Auxiliary subroutines for: * - Computing ratio ||A-L*U||_F/||A||_F of Frobenius norm of the * residual to the Frobenius norm of the initial matrix. * - Calculating max_(i=1,...,NRHS){||AX(i)-F(i)||/||F(i)||} of * ratios of residuals to norms of RHS vectors for a system of * linear equations with tridiagonal coefficient matrix and * multiple RHS * */ #include <algorithm> #include <cmath> #include <cstdint> #include <iostream> #include <vector> #include "mkl.h" /*********************************************************************** * Definition: * =========== * double resid1( int64_t n, int64_t nb, double* dl, double* d, double* du1, double* du2, int64_t* ipiv, double* dlcpy, double* dcpy, double* du1cpy) { * * Purpose: * ======== * Given LU factorization of block tridiagonal matrix A function RESID1 * returns ratio ||A-L*U||_F/||A||_F of Frobenius norm of the residual * to the Frobenius norm of the initial matrix. The ratio provides info * on how good the factorization is. * * Arguments: * ========== * N (input) int64_t * The number of block rows of the matrix A. N > 0. * * NB (input) int64_t * The size of blocks. NB > 0. * * DL (input) double array, dimension (NB) * ((N-1)*NB) * The array stores N-1 subdiagonal blocks (each of size NB by NB) of * lower triangular factor L. The blocks are stored sequentially * block by block. * * D (input) double array, dimension (NB) * (N*NB) * The array stores N diagonal blocks (each of size NB by NB) * of triangular factors L and U. * * DU1 (input) double array, dimension (NB) * ((N-1)*NB) * The array stores N-1 superdiagonal blocks (each of size * NB by NB) of triangular factor U. * * DU2 (input) double array, dimension (NB) * ((N-2)*NB) * The array stores N-2 blocks of the second superdiagonal of * triangular factor U. * * IPIV (input) int64_t array, dimension (NB) * (N) * The array stores pivot 'local' row indices. * * DLCPY (input) double array, dimension (NB) * ((N-1)*NB) * The array stores N-1 subdiagonal blocks of initial block * tridiagonal matrix. * * DCPY (input) double array, dimension (NB) * (N*NB) * The array stores N diagonal blocks of initial block * tridiagonal matrix. * * DU1CPY (input) double array, dimension (NB) * ((N-1)*NB) * The array stores N-1 superdiagonal blocks of initial block * tridiagonal matrix. ***********************************************************************/ double resid1( int64_t n, int64_t nb, double* dl, double* d, double* du1, double* du2, int64_t* ipiv, double* dlcpy, double* dcpy, double* du1cpy) { // Matrix accessors auto D = [=,&d] (int64_t i, int64_t j) -> double& { return d[i + j*nb]; }; auto DCPY = [=,&dcpy] (int64_t i, int64_t j) -> double& { return dcpy[i + j*nb]; }; auto DL = [=,&dl] (int64_t i, int64_t j) -> double& { return dl[i + j*nb]; }; auto DLCPY = [=,&dlcpy] (int64_t i, int64_t j) -> double& { return dlcpy[i + j*nb]; }; auto DU1 = [=,&du1] (int64_t i, int64_t j) -> double& { return du1[i + j*nb]; }; auto DU1CPY = [=,&du1cpy] (int64_t i, int64_t j) -> double& { return du1cpy[i + j*nb]; }; auto DU2 = [=,&du2] (int64_t i, int64_t j) -> double& { return du2[i + j*nb]; }; auto IPIV = [=,&ipiv] (int64_t i, int64_t j) -> int64_t& { return ipiv[i + j*nb]; }; double s = 0.0; double norm = 0.0; s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb*n, dcpy, nb); norm = s*s; s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb*(n-1), dlcpy, nb); norm = norm + s*s; s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb*(n-1), du1cpy, nb); norm = sqrt(norm + s*s); const int64_t ldb = 2*nb; std::vector<double> b(ldb*3*nb); std::vector<double> b1(ldb*3*nb); auto B = [=,&b] (int64_t i, int64_t j) -> double& { return b[i + j*ldb]; }; auto B1 = [=,&b1](int64_t i, int64_t j) -> double& { return b1[i + j*ldb]; }; for(int64_t j = 0; j < 3*nb; j++ ) { for(int64_t i = 0; i < 2*nb; i++ ) { B(i,j) = 0.0; B1(i,j) = 0.0; } } const int64_t ldl = 2*nb; std::vector<double> l(ldl*nb); auto L = [=,&l] (int64_t i, int64_t j) -> double& { return l[i + j*ldl]; }; for(int64_t j = 0; j < nb; j++) { for(int64_t i = 0; i < 2*nb; i++) { L(i,j) = 0.0; } L(j,j) = 1.0; } const int64_t ldu = nb; std::vector<double> u(ldu*3*nb); auto U = [=,&u] (int64_t i, int64_t j) -> double& { return u[i + j*ldu]; }; for(int64_t j = 0; j < 3*nb; j++) { for(int64_t i = 0; i < nb; i++) { U(i,j) = 0.0; } } for(int64_t j = 0; j < nb; j++) { cblas_dcopy( j+1, &D( 0, (n-1)*nb + j), 1, &U( 0, j), 1); cblas_dcopy(nb-j-1, &D(j+1, (n-1)*nb + j), 1, &L(j+1, j), 1); } cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, nb, nb, nb, 1.0, l.data(), 2*nb, u.data(), nb, 1.0, &B1(nb, nb), 2*nb); for(int64_t j = 0; j < nb; j++) { cblas_dcopy(j+1, &D( 0, (n-2)*nb + j), 1, &U( 0, j), 1); cblas_dcopy(nb, &DU1(0, (n-2)*nb + j), 1, &U( 0, nb+j), 1); cblas_dcopy(nb-j-1,&D(j+1, (n-2)*nb + j), 1, &L(j+1, j), 1); cblas_dcopy(nb, &DL( 0, (n-2)*nb + j), 1, &L( nb, j), 1); } cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, 2*nb, 2*nb, nb, 1.0, l.data(), 2*nb, u.data(), nb, 1.0, b1.data(), 2*nb); for (int64_t i = nb -1; i >= 0; i--) { if(IPIV(i,n-1) != nb+i+1) { cblas_dswap(2*nb, &B1(nb+i, 0), 2*nb, &B1(IPIV(i,n-1)-1,0), 2*nb); } } for (int64_t i = nb-1; i >= 0; i--) { if (IPIV(i,n-2) != i+1) { cblas_dswap(2*nb, &B1(i, 0), 2*nb, &B1(IPIV(i,n-2)-1, 0), 2*nb); } } for(int64_t j = 0; j < nb; j++) { for(int64_t i = 0; i < nb; i++) { B1(nb+i, j) = B1(nb+i, j) - DLCPY(i, (n-2)*nb+j); B1(nb+i, nb+j) = B1(nb+i, nb+j) - DCPY(i, (n-1)*nb+j); } } s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb, &B1(nb, 0), 2*nb); double eps = s*s; s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb, &B1(nb, nb), 2*nb); eps = eps + s*s; for (int64_t k = n-3; k >= 0; k--) { for(int64_t j = 0; j < nb; j++) { cblas_dcopy( j+1, &D( 0,k*nb + j), 1, &U(0, j), 1); cblas_dcopy( nb, &DU1(0,k*nb + j), 1, &U(0, nb+j), 1); cblas_dcopy( nb, &DU2(0,k*nb + j), 1, &U(0,2*nb+j), 1); cblas_dcopy(nb-j-1, &D(j+1,k*nb + j), 1, &L(j+1, j), 1); cblas_dcopy( nb, &DL( 0,k*nb + j), 1, &L(nb, j), 1); } cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, 2*nb, 3*nb, nb, 1.0, l.data(), 2*nb, u.data(), nb, 0.0, b.data(), 2*nb); for (int64_t j = 0; j < 2*nb; j++) { for(int64_t i = 0; i < nb; i++) { B(nb+i, nb+j) = B(nb+i, nb+j) + B1(i, j); } } for (int64_t i = nb-1; i >= 0; i--) { if (IPIV(i,k) != i+1) { cblas_dswap(3*nb, &B(i, 0), 2*nb, &B(IPIV(i,k)-1, 0), 2*nb); } } for(int64_t j = 0; j < nb; j++) { for (int64_t i = 0; i < nb; i++) { B1(nb+i, j) = B(nb+i, j) - DLCPY(i,(k)*nb+j); B1(nb+i, nb+j) = B(nb+i, nb+j) - DCPY(i,(k+1)*nb+j); B1(nb+i,2*nb+j) = B(nb+i,2*nb+j) -DU1CPY(i,(k+1)*nb+j); } } s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb, &B(0,2*nb), 2*nb); eps = eps + s*s; s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb, &B1(nb, 0), 2*nb); eps = eps + s*s; s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb, &B1(nb, nb), 2*nb); eps = eps + s*s; s = LAPACKE_dlange(MKL_COL_MAJOR, 'F', nb, nb, &B1(nb, 2*nb), 2*nb); eps = eps + s*s; for ( int64_t j = 0; j < 2*nb; j++) { cblas_dcopy(nb, &B(0, j), 1, &B1(0, j), 1); } } double resid = sqrt(eps)/norm; return resid; } /************************************************************************ * Definition: * =========== * double resid2(int64_t n, int64_t nb, int64_t nrhs, double* dl, double* d, double* du1, double* x, int64_t ldx, double* b, int64_t ldb) * Purpose: * ======== * Given solution X to a system of linear equations AX=B with tridiagonal * coefficient matrix A and multiple right hand sides B function RESID2 * returns max_(i=1,...,NRHS){||AX(i)-B(i)||/||B(i)||}. This quantity * provides info on how good the solution is. * * Arguments: * ========== * N (input) int64_t * The number of block rows of the matrix A. N > 0. * * NB (input) int64_t * The size of blocks. NB > 0. * * NRHS (input) int64_t * The number of right hand sides. NRHS >0. * * DL (input) double array, dimension (NB) * ((N-1)*NB) * The array stores N-1 subdiagonal blocks (each of size NB by NB) of * the coefficient matrix. The blocks are stored sequentially * block by block. * * D (input) double array, dimension (NB) * (N*NB) * The array stores N diagonal blocks (each of size NB by NB) * of the coefficient matrix. The blocks are stored sequentially * block by block. * * DU1 (input) double array, dimension (NB) * ((N-1)*NB) * The array stores N-1 superdiagonal blocks (each of size NB by NB) * of the coefficient matrix. The blocks are stored sequentially * block by block. * * X (input) double array, dimension (LDX) * (NRHS). * The array stores components of the solution to be tested. * * LDX (input) int64_t * The leading dimension of the array X. LDX >= N*NB. * * B (input) double array, dimension (LDB) * (NRHS). * The array stores components of the right hand sides. * * LDB (input) int64_t * The leading dimension of the array B. LDB >= N*NB. ***********************************************************************/ double resid2(int64_t n, int64_t nb, int64_t nrhs, double* dl, double* d, double* du1, double* x, int64_t ldx, double* b, int64_t ldb) { auto DL = [=,&dl] (int64_t i, int64_t j) -> double& { return dl[i + j*nb]; }; auto D = [=,&d] (int64_t i, int64_t j) -> double& { return d[i + j*nb]; }; auto DU1 = [=,&du1] (int64_t i, int64_t j) -> double& { return du1[i + j*nb]; }; auto X = [=,&x] (int64_t i, int64_t j) -> double& { return x[i + j*ldx]; }; auto B = [=,&b] (int64_t i, int64_t j) -> double& { return b[i + j*ldb]; }; // Initializing return value std::vector<double> norms(nrhs); // Compute norms of RHS vectors for (int64_t i = 0; i < nrhs; i++) { norms[i] = cblas_dnrm2(nb*n, &B(0,i), 1); } // Computing B(1)-D(1)*X(1)-DU1(1)*X(2) out of loop cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, nb, nrhs, nb, -1.0, d, nb, x, ldx, 1.0, b, ldb); cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, nb, nrhs, nb, -1.0, du1, nb, &X(nb, 0), ldx, 1.0, b, ldb); // In the loop computing B(K)-DL(K-1)*X(K-1)-D(K)*X(K)-DU1(K)*X(K+1) for (int64_t k = 1; k < n-1; k++) { cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, nb, nrhs, nb, -1.0, &DL(0, (k-1)*nb), nb, &X((k-1)*nb, 0), ldx, 1.0, &B(k*nb, 0), ldb); cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, nb, nrhs, nb, -1.0, &D(0, k*nb), nb, &X( k*nb, 0), ldx, 1.0, &B(k*nb, 0), ldb); cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, nb, nrhs, nb, -1.0, &DU1(0, k*nb), nb, &X((k+1)*nb, 0), ldx, 1.0, &B(k*nb, 0), ldb); } // Computing B(N)-DL(N-1)*X(N-1)-D(N)*X(N) out of loop cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, nb, nrhs, nb, -1.0, &D(0, (n-1)*nb), nb, &X((n-1)*nb, 0), ldx, 1.0, &B((n-1)*nb, 0), ldb); cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, nb, nrhs, nb, -1.0, &DL(0, (n-2)*nb), nb, &X((n-2)*nb, 0), ldx, 1.0, &B((n-1)*nb, 0), ldb); // Compute norms of residual vectors divided by norms of RHS vectors double res = 0.0; for (int64_t i = 0; i < nrhs; i++) { double s = cblas_dnrm2(n*nb, &B(0,i), 1); res = std::max<double>(res, s/norms[i]); } return res; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/block_lu_decomposition/factor.cpp
//============================================================== // Copyright © 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= /* * * Content: * Example of LU factorization of general block tridiagonal matrix ************************************************************************ * Purpose: * ======== * Testing LU factorization of block tridiagonal matrix * (D_1 C_1 ) * (B_1 D_2 C_2 ) * ( B_2 D_3 C_3 ) * ( ......... ) * ( B_N-2 D_N-1 C_N-1 ) * ( B_N-1 D_N ) * provided by function dgeblttrf by calculating Frobenius norm of the * residual ||A-L*U||. Computation of the residual and its Frobenius norm * is done by function resid1 (for source see file auxi.cpp). * Input block tridiagonal matrix A is randomly generated. */ #include <cstdint> #include <iostream> #include <vector> #include <sycl/sycl.hpp> #include "oneapi/mkl.hpp" using namespace oneapi; int64_t dgeblttrf(sycl::queue queue, int64_t n, int64_t nb, double* d, double* dl, double* du1, double* du2, int64_t* ipiv); double resid1( int64_t n, int64_t nb, double* dl, double* d, double* du1, double* du2, int64_t* ipiv, double* dlcpy, double* dcpy, double* du1cpy); template<typename T> using allocator_t = sycl::usm_allocator<T, sycl::usm::alloc::shared>; int main(){ if (sizeof(MKL_INT) != sizeof(int64_t)) { std::cerr << "MKL_INT not 64bit" << std::endl; return -1; } int64_t n = 200; int64_t nb = 20; int64_t info = 0; // Asynchronous error handler auto error_handler = [&] (sycl::exception_list exceptions) { for (auto const& e : exceptions) { try { std::rethrow_exception(e); } catch(mkl::lapack::exception const& e) { // Handle LAPACK related exceptions happened during asynchronous call info = e.info(); std::cout << "Unexpected exception caught during asynchronous LAPACK operation:\ninfo: " << e.info() << std::endl; } catch(sycl::exception const& e) { // Handle not LAPACK related exceptions happened during asynchronous call std::cout << "Unexpected exception caught during asynchronous operation:\n" << e.what() << std::endl; info = -1; } } }; sycl::device device{sycl::default_selector{}}; sycl::queue queue(device, error_handler); sycl::context context = queue.get_context(); if (device.is_gpu() && device.get_platform().get_backend() != sycl::backend::ext_oneapi_level_zero) { std::cerr << "This sample requires Level Zero when running on GPUs." << std::endl; std::cerr << "Please check your system configuration." << std::endl; return 0; } if (device.get_info<sycl::info::device::double_fp_config>().empty()) { std::cerr << "This sample uses double precision, which is not supported" << std::endl; std::cerr << "by the selected device. Quitting." << std::endl; return 0; } allocator_t<double> allocator_d(context, device); allocator_t<int64_t> allocator_i(context, device); std::vector<double, allocator_t<double>> d(nb* n*nb, allocator_d); std::vector<double, allocator_t<double>> dl(nb* (n-1)*nb, allocator_d); std::vector<double, allocator_t<double>> du1(nb* (n-1)*nb, allocator_d); std::vector<double, allocator_t<double>> du2(nb* (n-2)*nb, allocator_d); std::vector<double, allocator_t<double>> dcpy(nb* n*nb, allocator_d); std::vector<double, allocator_t<double>> dlcpy(nb* (n-1)*nb, allocator_d); std::vector<double, allocator_t<double>> du1cpy(nb* (n-1)*nb, allocator_d); std::vector<int64_t, allocator_t<int64_t>> ipiv(nb* n, allocator_i); std::vector<MKL_INT> iseed = {9, 41, 11, 3}; std::cout << "Testing accuracy of LU factorization with pivoting" << std::endl; std::cout << "of randomly generated block tridiagonal matrix " << std::endl; std::cout << "by calculating norm of the residual matrix." << std::endl; // Initializing arrays randomly LAPACKE_dlarnv(2, iseed.data(), n*nb*nb, d.data()); LAPACKE_dlarnv(2, iseed.data(), (n-1)*nb*nb, dl.data()); LAPACKE_dlarnv(2, iseed.data(), (n-1)*nb*nb, du1.data()); // Copying arrays for testing purposes cblas_dcopy(n*nb*nb, d.data(), 1, dcpy.data(), 1); cblas_dcopy((n-1)*nb*nb, dl.data(), 1, dlcpy.data(), 1); cblas_dcopy((n-1)*nb*nb, du1.data(), 1, du1cpy.data(), 1); // Factoring the matrix try { info = dgeblttrf(queue, n, nb, d.data(), dl.data(), du1.data(), du2.data(), ipiv.data()); } catch(sycl::exception const& e) { // Handle not LAPACK related exceptions happened during synchronous call std::cout << "Unexpected exception caught during synchronous call to SYCL API:\n" << e.what() << std::endl; info = -1; } // Check the exit INFO for success if(info){ std::cout << "DGEBLTTRF returned nonzero INFOi = " << info << std::endl; return 1; } // Computing the ratio ||A - LU||_F/||A||_F double eps = resid1(n, nb, dl.data(), d.data(), du1.data(), du2.data(), ipiv.data(), dlcpy.data(), dcpy.data(), du1cpy.data()); std::cout << "||A - LU||_F/||A||_F = " << eps << std::endl; return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneMKL/block_lu_decomposition/solve.cpp
//============================================================== // Copyright © 2020 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= /* * * Content: * Example of solving a system of linear equations with general * block tridiagonal coefficient matrix ************************************************************************ * Purpose: * ======== * Testing solution of a linear system of equations with general block * tridiagonal matrix of coefficients * D(1)*X(1) + C(1)*X(2) = F(1) * B(1)*X(1) + D(2)*X(2) + C(2)*X(3) = F(2) * B(2)*X(2) + D(3)*X(3) + C(3)*X(4) = F(3) * ... * B(N-2)*X(N-2) + D(N-1)*X(N-1) + C(N-1)*X(N) = F(N-1) * B(N-1)*X(N-1) + D(N)*X(N) = F(N) * Here D(J),B(J),C(J) are NB by NB matrices - block matrix coefficients * X(J),F(J) are NB by NRHS-matrices - unknowns and RHS components * * Solving is done via LU factorization of the coefficient matrix * (call DGEBLTTRF) followed by call DGEBLTTRS to solve a system of * equations with coefficient matrix factored by DGEBLTTRF. * * Coefficients and right hand sides are randomly generated. * * Testing is done via calculating * max{||F(1)-D(1)*X(1)-C(1)*X(2)||, * ||F(2)-B(1)*X(1)-D(1)*X(1)-C(1)*X(2)||, * ... * ||F(N)-B(N-1)*X(N-1)-D(N)*X(N)||} * * ||.|| denotes Frobenius norm of a respective matrix */ #include <cstdint> #include <iostream> #include <vector> #include <sycl/sycl.hpp> #include "oneapi/mkl.hpp" using namespace oneapi; int64_t dgeblttrf(sycl::queue, int64_t n, int64_t nb, double* d, double* dl, double* du1, double* du2, int64_t* ipiv); int64_t dgeblttrs(sycl::queue, int64_t n, int64_t nb, int64_t nrhs, double* d, double* dl, double* du1, double* du2, int64_t* ipiv, double* f, int64_t ldf); double resid2(int64_t n, int64_t nb, int64_t nrhs, double* dl, double* d, double* du1, double* x, int64_t ldx, double* b, int64_t ldb); template<typename T> using allocator_t = sycl::usm_allocator<T, sycl::usm::alloc::shared>; int main() { if (sizeof(MKL_INT) != sizeof(int64_t)) { std::cerr << "MKL_INT not 64bit" << std::endl; return -1; } int64_t n = 200; int64_t nb = 20; int64_t nrhs = 10; int64_t ldf = nb*n; int64_t info = 0; // Asynchronous error handler auto error_handler = [&] (sycl::exception_list exceptions) { for (auto const& e : exceptions) { try { std::rethrow_exception(e); } catch(mkl::lapack::exception const& e) { // Handle LAPACK related exceptions happened during asynchronous call info = e.info(); std::cout << "Unexpected exception caught during asynchronous LAPACK operation:\ninfo: " << e.info() << std::endl; } catch(sycl::exception const& e) { // Handle not LAPACK related exceptions happened during asynchronous call std::cout << "Unexpected exception caught during asynchronous operation:\n" << e.what() << std::endl; info = -1; } } }; sycl::device device{sycl::default_selector{}}; sycl::queue queue(device, error_handler); sycl::context context = queue.get_context(); if (device.is_gpu() && device.get_platform().get_backend() != sycl::backend::ext_oneapi_level_zero) { std::cerr << "This sample requires Level Zero when running on GPUs." << std::endl; std::cerr << "Please check your system configuration." << std::endl; return 0; } if (device.get_info<sycl::info::device::double_fp_config>().empty()) { std::cerr << "This sample uses double precision, which is not supported" << std::endl; std::cerr << "by the selected device. Quitting." << std::endl; return 0; } allocator_t<double> allocator_d(context, device); allocator_t<int64_t> allocator_i(context, device); std::vector<double, allocator_t<double>> d(nb * n*nb, allocator_d); std::vector<double, allocator_t<double>> dl(nb * (n-1)*nb, allocator_d); std::vector<double, allocator_t<double>> du1(nb * (n-1)*nb, allocator_d); std::vector<double, allocator_t<double>> du2(nb * (n-2)*nb, allocator_d); std::vector<double, allocator_t<double>> f(ldf * nrhs, allocator_d); std::vector<double, allocator_t<double>> dcpy(nb * n*nb, allocator_d); std::vector<double, allocator_t<double>> dlcpy(nb * (n-1)*nb, allocator_d); std::vector<double, allocator_t<double>> du1cpy(nb * (n-1)*nb, allocator_d); std::vector<double, allocator_t<double>> fcpy(ldf * nrhs, allocator_d); std::vector<int64_t, allocator_t<int64_t>> ipiv(nb * n, allocator_i); std::vector<int64_t> iseed = {1, 4, 23, 77}; // Initializing arrays randomly LAPACKE_dlarnv(2, reinterpret_cast<MKL_INT*>(iseed.data()), n*nb*nb, d.data()); LAPACKE_dlarnv(2, reinterpret_cast<MKL_INT*>(iseed.data()), (n-1)*nb*nb, dl.data()); LAPACKE_dlarnv(2, reinterpret_cast<MKL_INT*>(iseed.data()), (n-1)*nb*nb, du1.data()); LAPACKE_dlarnv(2, reinterpret_cast<MKL_INT*>(iseed.data()), n*nb*nrhs, f.data()); // Copying arrays for testing purposes cblas_dcopy(n*nb*nb, d.data(), 1, dcpy.data(), 1); cblas_dcopy((n-1)*nb*nb, dl.data(), 1, dlcpy.data(), 1); cblas_dcopy((n-1)*nb*nb, du1.data(), 1, du1cpy.data(), 1); cblas_dcopy(n*nb*nrhs, f.data(), 1, fcpy.data(), 1); std::cout << "Testing accuracy of solution of linear equations system" << std::endl; std::cout << "with randomly generated block tridiagonal coefficient" << std::endl; std::cout << "matrix by calculating ratios of residuals" << std::endl; std::cout << "to RHS vectors' norms." << std::endl; // LU factorization of the coefficient matrix info = dgeblttrf(queue, n, nb, d.data(), dl.data(), du1.data(), du2.data(), ipiv.data()); if (info) { std::cout << "DGEBLTTRF returned nonzero INFO = " << info << std::endl; return 1; } // Solving the system of equations using factorized coefficient matrix info = dgeblttrs(queue, n, nb, nrhs, d.data(), dl.data(), du1.data(), du2.data(), ipiv.data(), f.data(), ldf); if (info) { std::cout << -info << "-th parameter in call of dgeblttrs has illegal value" << std::endl; return 1; } else { // computing the residual double eps = resid2(n, nb, nrhs, dlcpy.data(), dcpy.data(), du1cpy.data(), f.data(), ldf, fcpy.data(), ldf); std::cout << "max_(i=1,...,nrhs){||ax(i)-f(i)||/||f(i)||} = " << eps << std::endl; } return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneDPL/maxloc_reductions/maxloc_buffered.cpp
#include <oneapi/dpl/algorithm> #include <oneapi/dpl/execution> #include <oneapi/dpl/iterator> #include <iostream> // Using oneDPL max_element with SYCL buffers. int main() { std::vector<int> data{2, 2, 2, 4, 1, 1, 1}; sycl::buffer<int> data_buf(data); auto policy = oneapi::dpl::execution::dpcpp_default; auto maxloc = oneapi::dpl::max_element(policy, oneapi::dpl::begin(data_buf), oneapi::dpl::end(data_buf)); std::cout << "Run on " << policy.queue().get_device().template get_info<sycl::info::device::name>() << std::endl; std::cout << "Maximum value is at element " << oneapi::dpl::distance(oneapi::dpl::begin(data_buf), maxloc) << std::endl; return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneDPL/maxloc_reductions/maxloc_usm.cpp
#include <oneapi/dpl/algorithm> #include <oneapi/dpl/execution> #include <oneapi/dpl/iterator> #include <iostream> // Using oneDPL max_element with USM. int main() { auto policy = oneapi::dpl::execution::dpcpp_default; const size_t n = 7; auto data = sycl::malloc_shared<int>(n, policy.queue()); data[0] = 2; data[1] = 2; data[2] = 2; data[3] = 4; data[4] = 1; data[5] = 1; data[6] = 1; auto maxloc = oneapi::dpl::max_element(policy, data, data + n); std::cout << "Run on " << policy.queue().get_device().template get_info<sycl::info::device::name>() << std::endl; std::cout << "Maximum value is at element " << oneapi::dpl::distance(data, maxloc) << std::endl; sycl::free(data, policy.queue()); return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneDPL/maxloc_reductions/maxloc_operator.cpp
#include <iostream> #include <sycl/sycl.hpp> // Hand-coded SYCL maxloc reduction operator. template <typename T, typename I> struct pair { bool operator < (const pair& o) const { return val <= o.val || (val == o.val && idx <= o.idx); } T val; I idx; }; template <typename T, typename I> using maxloc = sycl::maximum<pair<T, I>>; const size_t L = 1; int main(int argc, char **argv) { sycl::queue Q(sycl::default_selector{}); const size_t n = 7; auto data = sycl::malloc_shared<int>(n, Q); data[0] = 2; data[1] = 2; data[2] = 2; data[3] = 4; data[4] = 1; data[5] = 1; data[6] = 1; pair<int, int>* max_res = sycl::malloc_shared<pair<int, int>>(1, Q); pair<int, int> max_identity = { std::numeric_limits<int>::min(), std::numeric_limits<int>::min() }; *max_res = max_identity; auto red_max = sycl::reduction(max_res, max_identity, maxloc<int, int>()); Q.submit([&](sycl::handler& h) { h.parallel_for(sycl::nd_range<1>{n, L}, red_max, [=](sycl::nd_item<1> item, auto& max_res) { int i = item.get_global_id(0); pair<int, int> partial = {data[i], i}; max_res.combine(partial); }); }).wait(); std::cout << "Run on " << Q.get_device().get_info<sycl::info::device::name>() << std::endl; std::cout << "Maximum value = " << max_res->val << " at location " << max_res->idx << std::endl; //Cleanup sycl::free(data, Q); sycl::free(max_res, Q); return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneDPL/maxloc_reductions/maxloc_implicit.cpp
#include <oneapi/dpl/algorithm> #include <oneapi/dpl/execution> #include <oneapi/dpl/iterator> #include <iostream> // Using oneDPL max_element with implicit host-device data transfer. int main() { std::vector<int> data{2, 2, 2, 4, 1, 1, 1}; auto policy = oneapi::dpl::execution::dpcpp_default; auto maxloc = oneapi::dpl::max_element(policy, data.cbegin(), data.cend()); std::cout << "Run on " << policy.queue().get_device().template get_info<sycl::info::device::name>() << std::endl; std::cout << "Maximum value is at element " << oneapi::dpl::distance(data.cbegin(), maxloc) << std::endl; return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneDPL/dynamic_selection/nstream/src/1_nstreams_sycl.cpp
/* Copyright 2023 Intel Corporation. All Rights Reserved. The source code contained or described herein and all documents related to the source code ("Material") are owned by Intel Corporation or its suppliers or licensors. Title to the Material remains with Intel Corporation or its suppliers and licensors. The Material is protected by worldwide copyright laws and treaty provisions. No part of the Material may be used, copied, reproduced, modified, published, uploaded, posted, transmitted, distributed, or disclosed in any way without Intel's prior express written permission. No license under any patent, copyright, trade secret or other intellectual property right is granted to or conferred upon you by disclosure or delivery of the Materials, either expressly, by implication, inducement, estoppel or otherwise. Any license under such intellectual property rights must be express and approved by Intel in */ #include <sycl/sycl.hpp> #include <iostream> #include <vector> #include <cmath> #include <iomanip> void verify(float nstream_time, size_t length, int iterations, float scalar, std::vector<float> &A) { float ar(0); float br(2); float cr(2); for(int i = 0; i <= iterations; i++) { ar += br + scalar * cr; } ar *= length; float asum(0); for (size_t i = 0; i < length; i++) { asum += abs(A[i]); } float epsilon(1.e-8); if (abs(ar-asum)/asum > epsilon) { std::cout << "Failed Validation on output array" << std::endl << std::setprecision(16) << "Expected checksum: " << ar << std::endl << "Observed checksum: " << asum << std::endl << "ERROR: solution did not validate." << std::endl; exit(1); } else { float avgtime = nstream_time/iterations; float nbytes = 4.0 * length * sizeof(float); std::cout << "\n Rate: larger better (MB/s): " << (1.e-6*nbytes)/(1.e-9*avgtime) << "\n Avg time: lower better (ns): " << avgtime << std::endl; } } void invokeSYCL(int length, sycl::queue u) { int iterations{40}; float scalar{3}; std::vector<float> A(length, 0.0); std::vector<float> B(length, 2.0); std::vector<float> C(length, 2.0); const size_t bytes = length * sizeof(float); // Start the timer auto begin = std::chrono::high_resolution_clock::now(); for (int i = 0; i <= iterations; ++i) { std::cout << u.get_device().get_info<sycl::info::device::name>() <<std::endl; float *d_A = sycl::malloc_device<float>(length, u); float *d_B = sycl::malloc_device<float>(length, u); float *d_C = sycl::malloc_device<float>(length, u); u.memcpy(d_A, A.data(), bytes).wait(); u.memcpy(d_B, B.data(), bytes).wait(); u.memcpy(d_C, C.data(), bytes).wait(); auto x = u.submit([&](sycl::handler& h) { h.parallel_for<class vector_add>(length, [=](auto I) { d_A[I] += d_B[I] + scalar * d_C[I]; }); }); x.wait(); u.memcpy(A.data(), d_A, bytes).wait(); sycl::free(d_C, u); sycl::free(d_B, u); sycl::free(d_A, u); } // End the timer auto end = std::chrono::high_resolution_clock::now(); // Calculate time elapsed auto nstream_time = std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count(); verify(nstream_time, length, iterations, scalar, A); } int main(int argc, char * argv[]) { int length{50}; if (argc >= 2) { int conv = std::atoi(argv[1]); if (conv <= 0 || conv > 50000) { std::cout << "Vector length must be an integer between 1 and 50000." << std::endl; return -1; } else { length = conv; } } sycl::queue q(sycl::cpu_selector_v); std::cout << "Iterating on default SYCL (CPU): " << length << std::endl; invokeSYCL(length, q); return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneDPL/dynamic_selection/nstream/src/2_nstreams_policies.cpp
/* Copyright 2023 Intel Corporation. All Rights Reserved. The source code contained or described herein and all documents related to the source code ("Material") are owned by Intel Corporation or its suppliers or licensors. Title to the Material remains with Intel Corporation or its suppliers and licensors. The Material is protected by worldwide copyright laws and treaty provisions. No part of the Material may be used, copied, reproduced, modified, published, uploaded, posted, transmitted, distributed, or disclosed in any way without Intel's prior express written permission. No license under any patent, copyright, trade secret or other intellectual property right is granted to or conferred upon you by disclosure or delivery of the Materials, either expressly, by implication, inducement, estoppel or otherwise. Any license under such intellectual property rights must be express and approved by Intel in */ #include <sycl/sycl.hpp> #include <iostream> #include <vector> #include <cmath> #include <iomanip> #include <oneapi/dpl/dynamic_selection> namespace ds = oneapi::dpl::experimental; constexpr int num_warmup_iterations = 10; void verify(float nstream_time, size_t length, int iterations, float scalar, std::vector<float> &A) { float ar(0); float br(2); float cr(2); for(int i = 0; i <= iterations; i++) { ar += br + scalar * cr; } ar *= length; float asum(0); for (size_t i = 0; i < length; i++) { asum += abs(A[i]); } float epsilon(1.e-8); if (abs(ar-asum)/asum > epsilon) { std::cout << "Failed Validation on output array" << std::endl << std::setprecision(16) << "Expected checksum: " << ar << std::endl << "Observed checksum: " << asum << std::endl << "ERROR: solution did not validate." << std::endl; exit(1); } else { float avgtime = nstream_time/(iterations-2); float nbytes = 4.0 * length * sizeof(float); std::cout << "\n Rate: larger better (MB/s): " << (1.e-6*nbytes)/(1.e-9*avgtime) << "\n Avg time: lower better (ns): " << avgtime << std::endl; } } template<typename T> void invokeDS(int length, std::vector<sycl::queue> &universe){ int iterations{1000}; float scalar{3}; std::vector<float> A(length, 0.0); std::vector<float> B(length, 2.0); std::vector<float> C(length, 2.0); const size_t bytes = length * sizeof(float); T policy(universe); // Start the timer auto begin = std::chrono::high_resolution_clock::now(); for (int i = 0; i <= iterations; ++i) { if (i == num_warmup_iterations) { begin = std::chrono::high_resolution_clock::now(); } ds::submit_and_wait(policy, [&](sycl::queue e) { if (i < num_warmup_iterations) { std::cout << e.get_device().get_info<sycl::info::device::name>() <<std::endl; } float *d_A = sycl::malloc_device<float>(length, e); float *d_B = sycl::malloc_device<float>(length, e); float *d_C = sycl::malloc_device<float>(length, e); e.memcpy(d_A, A.data(), bytes).wait(); e.memcpy(d_B, B.data(), bytes).wait(); e.memcpy(d_C, C.data(), bytes).wait(); auto x = e.submit([&](sycl::handler &h) { h.parallel_for(length, [=](auto I) { d_A[I] += d_B[I] + scalar * d_C[I]; }); }); x.wait(); e.memcpy(A.data(), d_A, bytes).wait(); sycl::free(d_C, e); sycl::free(d_B, e); sycl::free(d_A, e); // // Note: Dynamic Device Select requires a return event such as the following. // return x; }); } auto end = std::chrono::high_resolution_clock::now(); // Calculate time elapsed auto nstream_time = std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count(); verify(nstream_time, length, iterations, scalar, A); } void invokeSYCL(int length) { int iterations{40}; float scalar{3}; std::vector<float> A(length, 0.0); std::vector<float> B(length, 2.0); std::vector<float> C(length, 2.0); const size_t bytes = length * sizeof(float); sycl::queue u(sycl::cpu_selector_v); // Start the timer auto begin = std::chrono::high_resolution_clock::now(); for (int i = 0; i <= iterations; ++i) { float *d_A = sycl::malloc_device<float>(length, u); float *d_B = sycl::malloc_device<float>(length, u); float *d_C = sycl::malloc_device<float>(length, u); u.memcpy(d_A, A.data(), bytes).wait(); u.memcpy(d_B, B.data(), bytes).wait(); u.memcpy(d_C, C.data(), bytes).wait(); auto x = u.submit([&](sycl::handler& h) { h.parallel_for<class vector_add>(length, [=](auto I) { d_A[I] += d_B[I] + scalar * d_C[I]; }); }); x.wait(); u.memcpy(A.data(), d_A, bytes).wait(); sycl::free(d_C, u); sycl::free(d_B, u); sycl::free(d_A, u); } // End the timer auto end = std::chrono::high_resolution_clock::now(); // Calculate time elapsed auto nstream_time = std::chrono::duration_cast<std::chrono::nanoseconds>(end-begin).count(); verify(nstream_time, length, iterations, scalar, A); } void printUsage() { std::cout << "Application requires arguments. Usage: 2_nstream_policies <vector length> <policy>" << std::endl << "Vector length must be an integer between 1 and 50000." << std::endl << "Policy:" << std::endl << "1 - Fixed Resource Policy (CPU)" << std::endl << "2 - Fixed Resource Policy (GPU)" << std::endl << "3 - Round Robin Policy" << std::endl << "4 - Dynamic Load Policy" << std::endl << "5 - Auto Tune Policy" << std::endl; } int main(int argc, char * argv[]) { int length{50}, policy{2}; std::vector<sycl::queue> universe; if (argc >= 3) { int conv = std::atoi(argv[1]); if (conv <= 0 || conv > 50000) { std::cout << "Vector length must be an integer between 1 and 50000." << std::endl; return -1; } else { length = conv; } policy = std::atoi(argv[2]); } else { printUsage(); return -1; } auto prop_list = sycl::property_list{sycl::property::queue::enable_profiling()}; switch (policy) { case 1: std::cout << "Using Static Policy (CPU) to iterate on CPU device with vector length: " << length << std::endl; // Add CPUs to the universe of devices. universe.push_back(sycl::queue{sycl::cpu_selector_v}); invokeDS<ds::fixed_resource_policy<ds::sycl_backend>>(length, universe); break; case 2: std::cout << "Using Static Policy (GPU) to iterate on GPU device with vector length: " << length << std::endl; // Add GPUs to the universe of devices. universe.push_back(sycl::queue{sycl::gpu_selector_v}); invokeDS<ds::fixed_resource_policy<ds::sycl_backend>>(length, universe); break; case 3: std::cout << "Using Round Robin Policy to iterate across available devices with vector length: " << length << std::endl; // Add CPUs and GPUs to the universe of devices. universe.push_back(sycl::queue{sycl::cpu_selector_v}); universe.push_back(sycl::queue{sycl::gpu_selector_v}); invokeDS<ds::round_robin_policy<ds::sycl_backend>>(length, universe); break; case 4: std::cout << "Using Dynamic Load Policy to pick least loaded device: " << length << std::endl; universe.push_back(sycl::queue{sycl::gpu_selector_v}); universe.push_back(sycl::queue{sycl::cpu_selector_v}); invokeDS<ds::dynamic_load_policy<ds::sycl_backend>>(length, universe); break; case 5: std::cout << "Using Auto-Tune to pick the best performing device: " << length << std::endl; universe.push_back(sycl::queue{sycl::gpu_selector_v, prop_list}); universe.push_back(sycl::queue{sycl::cpu_selector_v, prop_list}); invokeDS<ds::auto_tune_policy<ds::sycl_backend>>(length, universe); break; default: std::cout << "Invalid policy." << std::endl; printUsage(); return -1; break; } return 0; }
cpp
oneAPI-samples
data/projects/oneAPI-samples/Libraries/oneDPL/dynamic_selection/sepia-filter-ds/src/2_sepia_policies.cpp
//============================================================== // Copyright © 2019 Intel Corporation // // SPDX-License-Identifier: MIT // ============================================================= #include <algorithm> #include <chrono> #include <cmath> #include <iostream> #include <sycl/sycl.hpp> #include <oneapi/dpl/dynamic_selection> // dpc_common.hpp can be found in the dev-utilities include folder. // e.g., $ONEAPI_ROOT/dev-utilities/<version>/include/dpc_common.hpp #include "dpc_common.hpp" // stb/*.h files can be found in the dev-utilities include folder. // e.g., $ONEAPI_ROOT/dev-utilities/<version>/include/stb/*.h #define STB_IMAGE_IMPLEMENTATION #include "stb/stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb/stb_image_write.h" using namespace std; using namespace sycl; namespace ex = oneapi::dpl::experimental; // Few useful acronyms. constexpr auto sycl_read = access::mode::read; constexpr auto sycl_write = access::mode::write; constexpr auto sycl_device = access::target::device; int g_num_images = 4; #if !WINDOWS const char *g_fnames[3] = { "../input/silver512.png", "../input/nahelam512.bmp", "../input/silverfalls1.png" }; #else const char* g_fnames[3] = { "../../input/silver512.png", "../../input/nahelam512.bmp", "../../input/silverfalls1.png" }; #endif int g_width[4] = {0, 0, 0, 0}; int g_height[4] = {0, 0, 0, 0}; int g_channels[4] = {0, 0, 0, 0}; void fillVectors(int mix, std::vector<size_t>& num_pixels, std::vector<sycl::buffer<uint8_t>>& input_buffers, std::vector<sycl::buffer<uint8_t>>& output_buffers) { if (mix > 3) g_num_images = 3; else g_num_images = 4; for (int i = 0; i < g_num_images; ++i) { int img_width, img_height, channels; int index = 0; switch (mix) { case 1: // 1 - Small images only index = i%2; break; case 2: // 2 - Large images only index = 2; break; case 3: // 3 - 2 small : 2 large index = std::min(i%4, 2); break; case 4: // 4 - 2 small : 1 large index = i%3; break; case 5: // 5 - 1 small : 2 large index = std::min(i%3+1, 2); break; } uint8_t *image = stbi_load(g_fnames[index], &img_width, &img_height, &channels, 0); if (image == NULL) { cout << "Error in loading the image " << g_fnames[index] << "\n"; exit(1); } g_width[i] = img_width; g_height[i] = img_height; g_channels[i] = channels; size_t npixels = img_width * img_height; size_t img_size = img_width * img_height * channels; input_buffers.push_back(sycl::buffer{image, sycl::range(img_size)}); num_pixels.push_back(npixels); uint8_t *out_data = new uint8_t[img_size]; memset(out_data, 0, img_size * sizeof(uint8_t)); output_buffers.push_back(sycl::buffer{out_data, sycl::range(img_size)}); } } void writeImages(std::vector<sycl::buffer<uint8_t>>& output_buffers) { const char *out_names[4] = { "out0.png", "out1.png", "out2.png", "out3.png" }; for (int i = 0; i < g_num_images; ++i) { stbi_write_png(out_names[i], g_width[i], g_height[i], g_channels[i], reinterpret_cast<uint8_t*>(output_buffers[i].get_host_access().get_pointer()), g_width[i] * g_channels[i]); } } // SYCL does not need any special mark-up for functions which are called from // SYCL kernel and defined in the same compilation unit. SYCL compiler must be // able to find the full call graph automatically. // always_inline as calls are expensive on Gen GPU. // Notes: // - coeffs can be declared outside of the function, but still must be constant // - SYCL compiler will automatically deduce the address space for the two // pointers; sycl::multi_ptr specialization for particular address space // can used for more control __attribute__((always_inline)) static void ApplyFilter(uint8_t *src_image, uint8_t *dst_image, int i) { i *= 3; float temp; temp = (0.393f * src_image[i]) + (0.769f * src_image[i + 1]) + (0.189f * src_image[i + 2]); dst_image[i] = temp > 255 ? 255 : temp; temp = (0.349f * src_image[i]) + (0.686f * src_image[i + 1]) + (0.168f * src_image[i + 2]); dst_image[i + 1] = temp > 255 ? 255 : temp; temp = (0.272f * src_image[i]) + (0.534f * src_image[i + 1]) + (0.131f * src_image[i + 2]); dst_image[i + 2] = temp > 255 ? 255 : temp; } template<typename T> int invokeDS(int num_offloads, std::vector<sycl::queue> &resources, std::vector<size_t>& npixels, std::vector<sycl::buffer<uint8_t>>& input_buffers, std::vector<sycl::buffer<uint8_t>>& output_buffers) { T p(resources); auto t_begin = std::chrono::high_resolution_clock::now(); for (int i = 0; i < num_offloads; ++i) { try { sycl::buffer<uint8_t>& in = input_buffers[i%g_num_images]; sycl::buffer<uint8_t>& out = output_buffers[i%g_num_images]; size_t num_pixels = npixels[i%g_num_images]; auto f1 = [&](sycl::queue q, size_t n) { // See what device was actually selected for this queue. cout << "Lambda running on " << q.get_device().get_info<info::device::name>() << "\n"; // Submit a command group for execution. Returns immediately, not waiting // for command group completion. return q.submit([&](auto &h) { // This lambda defines a "command group" - a set of commands for the // device sharing some state and executed in-order - i.e. creation of // accessors may lead to on-device memory allocation, only after that // the kernel will be enqueued. // A command group can contain at most one parallel_for, single_task or // parallel_for_workgroup construct. accessor image_acc(in, h, read_only); accessor image_exp_acc(out, h, write_only); // This is the simplest form sycl::handler::parallel_for - // - it specifies "flat" 1D ND range(num_pixels), runtime will select // local size // - kernel lambda accepts single sycl::id argument, which has very // limited API; see the spec for more complex forms // the lambda parameter of the parallel_for is the kernel, which // actually executes on device h.parallel_for(range<1>(num_pixels), [=](auto i) { ApplyFilter(image_acc.get_pointer(), image_exp_acc.get_pointer(), i); }); }); }; ex::submit_and_wait(p, f1, num_pixels); } catch (sycl::exception e) { // This catches only synchronous exceptions that happened in current thread // during execution. The asynchronous exceptions caused by execution of the // command group are caught by the asynchronous exception handler // registered. Synchronous exceptions are usually those which are thrown // from the SYCL runtime code, such as on invalid constructor arguments. An // example of asynchronous exceptions is error occurred during execution of // a kernel. Make sure sycl::exception is caught, not std::exception. cout << "SYCL exception caught: " << e.what() << "\n"; return 1; } } auto t_end = std::chrono::high_resolution_clock::now(); auto total_time = std::chrono::duration_cast<std::chrono::microseconds>(t_end-t_begin).count(); cout << "Total time == " << total_time << " us\n"; return 0; } void printUsage(char *exe_name) { std::cout << "Application requires arguments. Usage: " << exe_name << " <num_images> <mix> <policy>" << std::endl << "Mix:" << std::endl << "1 - Small images only" << std::endl << "2 - Large images only" << std::endl << "3 - 2 small : 2 large" << std::endl << "4 - 2 small : 1 large" << std::endl << "5 - 1 small : 2 large" << std::endl << std::endl << "Policy:" << std::endl << "1 - Fixed Resource Policy (CPU)" << std::endl << "2 - Fixed Resource Policy (GPU)" << std::endl << "3 - Round Robin Policy" << std::endl << "4 - Dynamic Load Policy" << std::endl << "5 - Auto Tune Policy" << std::endl; } void displayConfig(int mix, int policy, int num_offloads) { std::cout << "Processing " << num_offloads << " images\n"; switch (mix) { case 1: // 1 - Small images only std::cout << "Only small images\n"; break; case 2: // 2 - Large images only std::cout << "Only large images\n"; break; case 3: // 3 - 2 small : 2 large std::cout << "50/50 small images and large images\n"; break; case 4: // 4 - 2 small : 1 large std::cout << "2 small images for each large image\n"; break; case 5: // 5 - 1 small : 2 large std::cout << "2 large images for each small image\n"; break; } switch (policy) { case 1: std::cout << "Using fixed_resource_policy to always select the CPU\n"; break; case 2: std::cout << "Using fixed_resource_policy to always select the GPU\n"; break; case 3: std::cout << "Using round_robin_policy to alternate between CPU and GPU\n"; break; case 4: std::cout << "Using dynamic_load_policy to select least loaded device\n"; break; case 5: std::cout << "Using auto_tune_policy to select best device for each image size\n"; break; } std::cout << "\n"; } int main(int argc, char * argv[]) { int num_offloads{100}, mix{2}, policy{2}; std::vector<sycl::queue> resources; std::vector<sycl::buffer<uint8_t>> input_buffers; std::vector<size_t> num_pixels; std::vector<sycl::buffer<uint8_t>> output_buffers; if (argc < 4) { printUsage(argv[0]); return -1; } else { int n = std::atoi(argv[1]); if (n <= 0) { std::cout << "num offloads must be a positive integer." << std::endl; return -1; } else { num_offloads = n; } int m = std::atoi(argv[2]); if (m <= 0 || m > 5) { std::cout << "Improper mix choice.\n"; printUsage(argv[0]); return -1; } else { mix = m; } policy = std::atoi(argv[3]); } displayConfig(mix, policy, num_offloads); fillVectors(mix, num_pixels, input_buffers, output_buffers); switch (policy) { case 1: try { // Add CPUs to the universe of devices. resources.push_back(sycl::queue{sycl::cpu_selector_v}); } catch (...) { std::cout << "Unable to create CPU queue\n"; } invokeDS<ex::fixed_resource_policy<ex::sycl_backend>>(num_offloads, resources, num_pixels, input_buffers, output_buffers); break; case 2: try { // Add GPUs to the universe of devices. resources.push_back(sycl::queue{sycl::gpu_selector_v}); } catch (...) { std::cout << "Unable to create GPU queue\n"; } invokeDS<ex::fixed_resource_policy<ex::sycl_backend>>(num_offloads, resources, num_pixels, input_buffers, output_buffers); break; case 3: try { // Add CPUs and GPUs to the universe of devices. resources.push_back(sycl::queue{sycl::cpu_selector_v}); resources.push_back(sycl::queue{sycl::gpu_selector_v}); } catch (...) { std::cout << "Unable to create queues\n"; } invokeDS<ex::round_robin_policy<ex::sycl_backend>>(num_offloads, resources, num_pixels, input_buffers, output_buffers); break; case 4: try { resources.push_back(sycl::queue{sycl::gpu_selector_v}); resources.push_back(sycl::queue{sycl::cpu_selector_v}); } catch (...) { std::cout << "Unable to create queues\n"; } invokeDS<ex::dynamic_load_policy<ex::sycl_backend>>(num_offloads, resources, num_pixels, input_buffers, output_buffers); break; case 5: try { auto prop_list = sycl::property_list{sycl::property::queue::enable_profiling()}; resources.push_back(sycl::queue{sycl::gpu_selector_v, prop_list}); resources.push_back(sycl::queue{sycl::cpu_selector_v, prop_list}); } catch (...) { std::cout << "Unable to create queues\n"; } invokeDS<ex::auto_tune_policy<ex::sycl_backend,std::size_t>>(num_offloads, resources, num_pixels, input_buffers, output_buffers); break; default: std::cout << "Invalid policy." << std::endl; printUsage(argv[0]); return -1; break; } writeImages(output_buffers); return 0; }
cpp