From 63a1441667421a89ac8e7796d3743688535014b9 Mon Sep 17 00:00:00 2001 From: "Jiang, Zhiwei" Date: Thu, 7 Mar 2024 13:37:07 +0800 Subject: [PATCH] [SYCLomatic #1760] Compare the random number generated from CPU and GPU (#644) Signed-off-by: Jiang, Zhiwei --- .../config/TEMPLATE_multi_devices.xml | 14 +++++ multi_devices/multi_devices.xml | 7 +++ multi_devices/src/rng.cpp | 48 +++++++++++++++++ multi_devices/test_drivers.xml | 6 +++ multi_devices/test_multi_devices.py | 54 +++++++++++++++++++ test_suite_list.xml | 1 + 6 files changed, 130 insertions(+) create mode 100644 multi_devices/config/TEMPLATE_multi_devices.xml create mode 100644 multi_devices/multi_devices.xml create mode 100644 multi_devices/src/rng.cpp create mode 100644 multi_devices/test_drivers.xml create mode 100644 multi_devices/test_multi_devices.py diff --git a/multi_devices/config/TEMPLATE_multi_devices.xml b/multi_devices/config/TEMPLATE_multi_devices.xml new file mode 100644 index 000000000000..01dda41c1c2f --- /dev/null +++ b/multi_devices/config/TEMPLATE_multi_devices.xml @@ -0,0 +1,14 @@ + + + + WARNING: DON'T UPDATE THIS FILE MANUALLY!!! + This is auto-generated accessors configuration file which affects all tests in ported gcc suites + If you have any issue with this file please contact Compiler QA team + + + + + + + + diff --git a/multi_devices/multi_devices.xml b/multi_devices/multi_devices.xml new file mode 100644 index 000000000000..4dc0aa0fddbd --- /dev/null +++ b/multi_devices/multi_devices.xml @@ -0,0 +1,7 @@ + + + multi devices test + + + + diff --git a/multi_devices/src/rng.cpp b/multi_devices/src/rng.cpp new file mode 100644 index 000000000000..5af1cf3d0904 --- /dev/null +++ b/multi_devices/src/rng.cpp @@ -0,0 +1,48 @@ +// ===------- rng.cpp ------------------------------------- *- C++ -* ----=== // +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +// ===--------------------------------------------------------------------=== // + +#include +#include +#include + +int main() { + sycl::queue cpu_q(sycl::cpu_selector_v, sycl::property::queue::in_order()); + sycl::queue gpu_q(sycl::gpu_selector_v, sycl::property::queue::in_order()); + + float *h_data = (float *)std::malloc(sizeof(float) * 10); + float *d_data; + d_data = sycl::malloc_shared(10, gpu_q); + + dpct::rng::host_rng_ptr h_rng; + dpct::rng::host_rng_ptr d_rng; + + h_rng = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203, cpu_q); + d_rng = dpct::rng::create_host_rng(dpct::rng::random_engine_type::mt2203, gpu_q); + h_rng->set_engine_idx(1); + d_rng->set_engine_idx(1); + + h_rng->generate_gaussian(h_data, 10, 0, 1); + d_rng->generate_gaussian(d_data, 10, 0, 1); + + cpu_q.wait(); + gpu_q.wait(); + + for (int i = 0; i < 10; i++) + printf("%f, ", h_data[i]); + printf("\n"); + for (int i = 0; i < 10; i++) + printf("%f, ", d_data[i]); + printf("\n"); + + for (int i = 0; i < 10; i++) { + if (std::abs(h_data[i] - d_data[i]) > 0.01f) + return -1; + } + + return 0; +} diff --git a/multi_devices/test_drivers.xml b/multi_devices/test_drivers.xml new file mode 100644 index 000000000000..1f2bf01d6f7b --- /dev/null +++ b/multi_devices/test_drivers.xml @@ -0,0 +1,6 @@ + + + + Test driver for multi_devices test of DPCT + + diff --git a/multi_devices/test_multi_devices.py b/multi_devices/test_multi_devices.py new file mode 100644 index 000000000000..4bfe23b9124d --- /dev/null +++ b/multi_devices/test_multi_devices.py @@ -0,0 +1,54 @@ +# ===------ test_multi_devices.py ------------------------ *- Python -* ----===# +# +# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +# +# ===-----------------------------------------------------------------------===# + +import os +import re +import sys +from pathlib import Path +parent = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +sys.path.append(parent) + +from test_utils import * + +def setup_test(): + return True + +def migrate_test(): + return True + +def build_test(): + if (os.path.exists(test_config.current_test)): + os.chdir(test_config.current_test) + test_config.out_root = os.getcwd() + + mkl_related = ["rng"] + + srcs = [] + cmp_opts = [] + link_opts = [] + objects = [] + + for dirpath, dirnames, filenames in os.walk(test_config.out_root): + srcs.append(os.path.abspath(os.path.join(dirpath, test_config.current_test + ".cpp"))) + + if test_config.current_test in mkl_related: + mkl_opts = [] + if platform.system() == "Linux": + mkl_opts = test_config.mkl_link_opt_lin + else: + mkl_opts = test_config.mkl_link_opt_win + link_opts += mkl_opts + cmp_opts.append("-DMKL_ILP64") + + ret = compile_and_link(srcs, cmp_opts, objects, link_opts) + return ret + +def run_test(): + args = [] + ret = run_binary_with_args(args) + return ret diff --git a/test_suite_list.xml b/test_suite_list.xml index 1573549865da..54026b531f5b 100644 --- a/test_suite_list.xml +++ b/test_suite_list.xml @@ -11,5 +11,6 @@ +