Skip to content

Commit

Permalink
[SYCLomatic oneapi-src#1760] Compare the random number generated from…
Browse files Browse the repository at this point in the history
… CPU and GPU (oneapi-src#644)

Signed-off-by: Jiang, Zhiwei <[email protected]>
  • Loading branch information
zhiweij1 authored Mar 7, 2024
1 parent b71f100 commit 63a1441
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
14 changes: 14 additions & 0 deletions multi_devices/config/TEMPLATE_multi_devices.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<test driverID="test_multi_devices" name="TEMPLATE">
<description>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</description>
<files>
<file path="src/${testName}.cpp"/>
</files>
<rules>
<optlevelRule GPUFeature="NOT double" excludeOptlevelNameString="gpu"/>
<optlevelRule excludeOptlevelNameString="cuda" />
</rules>
</test>
7 changes: 7 additions & 0 deletions multi_devices/multi_devices.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<suite>
<description>multi devices test</description>
<tests>
<test testName="rng" configFile="config/TEMPLATE_multi_devices.xml" splitGroup="double"/>
</tests>
</suite>
48 changes: 48 additions & 0 deletions multi_devices/src/rng.cpp
Original file line number Diff line number Diff line change
@@ -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 <sycl/sycl.hpp>
#include <dpct/rng_utils.hpp>
#include <cstdio>

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<float>(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;
}
6 changes: 6 additions & 0 deletions multi_devices/test_drivers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<testDrivers>
<testDriver driverID="test_multi_devices" file="test_multi_devices.py" title="test_multi_devices">
<description>Test driver for multi_devices test of DPCT</description>
</testDriver>
</testDrivers>
54 changes: 54 additions & 0 deletions multi_devices/test_multi_devices.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test_suite_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
<suite name="api_coverage" dir="api_coverage" opts="option_cpu, option_gpu, option_usmnone_cpu, option_usmnone_gpu"/>
<suite name="behavior_tests" dir="behavior_tests" opts="option_cpu, option_gpu, option_usmnone_cpu, option_usmnone_gpu"/>
<suite name="applications" dir="applications" opts="option_cpu, option_gpu, option_usmnone_cpu, option_usmnone_gpu"/>
<suite name="multi_devices" dir="multi_devices" opts="option_gpu"/>
</suites>
</test_suite>

0 comments on commit 63a1441

Please sign in to comment.