Skip to content

Commit dc6676e

Browse files
tomflindaTejaX-Alaghari
authored andcommitted
Behaviour test for dpct_helper_add_mkl_to_target helper macro
Signed-off-by: TejaX-Alaghari <teja.alaghari@intel.com>
1 parent 91170a0 commit dc6676e

File tree

4 files changed

+178
-0
lines changed

4 files changed

+178
-0
lines changed

behavior_tests/behavior_tests.xml

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
<test testName="bt-no-dpct-helper-function" configFile="config/TEMPLATE_behavior_tests.xml" />
168168
<test testName="cmp-cmds-linker-entry-src-files" configFile="config/TEMPLATE_behavior_tests_lin.xml" />
169169
<test testName="cmake_dpct_helper_compile_sycl_code" configFile="config/TEMPLATE_behavior_tests.xml" />
170+
<test testName="cmake_dpct_helper_add_mkl_to_target" configFile="config/TEMPLATE_behavior_tests.xml" />
170171
</tests>
171172

172173
</suite>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#==---- CMakeLists.txt ---------------------------- cmake script file ----==//
2+
#
3+
# Copyright (C) Intel Corporation
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
# See https://llvm.org/LICENSE.txt for license information.
6+
#
7+
#===----------------------------------------------------------------------===//
8+
9+
cmake_minimum_required(VERSION 3.10)
10+
11+
project(cmake_add_mkl LANGUAGES CXX)
12+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl")
13+
find_program(
14+
dpct_bin_path
15+
NAMES dpct
16+
PATHS)
17+
get_filename_component(bin_path_of_dpct ${dpct_bin_path} DIRECTORY)
18+
set(dpct_cmake_file_path "${bin_path_of_dpct}/../cmake/dpct.cmake")
19+
include(${dpct_cmake_file_path})
20+
21+
find_package(IntelSYCL REQUIRED)
22+
23+
set(CUDA_SOURCES
24+
fft.dp.cpp
25+
)
26+
27+
add_executable(app.run ${CUDA_SOURCES})
28+
29+
dpct_helper_add_mkl_to_target(app.run)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# ====------ do_test.py---------- *- Python -* ----===##
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
#
8+
# ===----------------------------------------------------------------------===#
9+
import subprocess
10+
import platform
11+
import os
12+
import sys
13+
from test_config import CT_TOOL
14+
15+
from test_utils import *
16+
17+
def setup_test():
18+
change_dir(test_config.current_test)
19+
return True
20+
21+
def migrate_test():
22+
# clean previous migration output
23+
if (os.path.exists("build")):
24+
shutil.rmtree("build")
25+
26+
ret = call_subprocess("mkdir build")
27+
if not ret:
28+
print("Error to create build folder:", test_config.command_output)
29+
30+
ret = change_dir("build")
31+
if not ret:
32+
print("Error to go to build folder:", test_config.command_output)
33+
34+
ret = call_subprocess("cmake -G \"Unix Makefiles\" -DCMAKE_CXX_COMPILER=icpx ../")
35+
if not ret:
36+
print("Error to run cmake configure:", test_config.command_output)
37+
38+
ret = call_subprocess("make")
39+
if not ret:
40+
print("Error to run build process:", test_config.command_output)
41+
42+
return os.path.exists("app.run")
43+
def build_test():
44+
return True
45+
def run_test():
46+
return call_subprocess("./app.run")
47+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include <sycl/sycl.hpp>
2+
#include <dpct/dpct.hpp>
3+
#include <iostream>
4+
#include <dpct/fft_utils.hpp>
5+
6+
#include <cmath>
7+
8+
// Function to compare two floating-point numbers with a tolerance
9+
int almostEqual(float a, float b, float tolerance) {
10+
return fabs(a - b) < tolerance;
11+
}
12+
13+
// Function to compare two cufftComplex variables
14+
int compareCufftComplex(sycl::float2 a, sycl::float2 b, float tolerance) {
15+
return almostEqual(a.x(), b.x(), tolerance) &&
16+
almostEqual(a.y(), b.y(), tolerance);
17+
}
18+
19+
std::string strigifyCufftComplex(sycl::float2 a) {
20+
return std::string("(" + std::to_string(a.x()) + ", " +
21+
std::to_string(a.y()) + ")");
22+
}
23+
24+
int main() {
25+
dpct::device_ext &dev_ct1 = dpct::get_current_device();
26+
sycl::queue &q_ct1 = dev_ct1.in_order_queue();
27+
const int n = 8; // Size of the input array
28+
29+
// Allocate memory on the host for input and output arrays
30+
sycl::float2 *h_input = (sycl::float2 *)malloc(sizeof(sycl::float2) * n);
31+
sycl::float2 *h_output = (sycl::float2 *)malloc(sizeof(sycl::float2) * n);
32+
33+
// Initialize the input array with random values
34+
for (int i = 0; i < n; ++i) {
35+
h_input[i].x() = static_cast<float>(rand()) / RAND_MAX;
36+
h_input[i].y() = static_cast<float>(rand()) / RAND_MAX;
37+
}
38+
39+
// Allocate memory on the host for reference output array
40+
sycl::float2 *h_ref_output =
41+
(sycl::float2 *)malloc(sizeof(sycl::float2) * n);
42+
43+
// Initialize h_ref_output with expected output
44+
h_ref_output[0] = sycl::float2(4.94234f, 4.77104f);
45+
h_ref_output[1] = sycl::float2(0.914293f, -0.261793f);
46+
h_ref_output[2] = sycl::float2(-0.415583f, 0.264357f);
47+
h_ref_output[3] = sycl::float2(0.241085f, 0.382871f);
48+
h_ref_output[4] = sycl::float2(-0.153554f, -1.45243f);
49+
h_ref_output[5] = sycl::float2(-0.421167f, -1.15111f);
50+
h_ref_output[6] = sycl::float2(0.0986443f, 0.210444f);
51+
h_ref_output[7] = sycl::float2(1.51544f, 0.391681f);
52+
53+
// Allocate memory on the device (GPU)
54+
sycl::float2 *d_input, *d_output;
55+
d_input = sycl::malloc_device<sycl::float2>(n, q_ct1);
56+
d_output = sycl::malloc_device<sycl::float2>(n, q_ct1);
57+
58+
// Copy input data from host to device
59+
q_ct1.memcpy(d_input, h_input, sizeof(sycl::float2) * n).wait();
60+
61+
// Create a cuFFT plan
62+
dpct::fft::fft_engine_ptr plan;
63+
plan = dpct::fft::fft_engine::create(
64+
&q_ct1, n, dpct::fft::fft_type::complex_float_to_complex_float, 1);
65+
66+
// Perform forward FFT
67+
plan->compute<sycl::float2, sycl::float2>(
68+
d_input, d_output, dpct::fft::fft_direction::forward);
69+
70+
// Copy output data from device to host
71+
q_ct1.memcpy(h_output, d_output, sizeof(sycl::float2) * n).wait();
72+
73+
// Verify the result
74+
bool failed = false;
75+
76+
float tolerance = 5e-6f;
77+
for (int i = 0; i < n; ++i) {
78+
if (!compareCufftComplex(h_output[i], h_ref_output[i], tolerance)) {
79+
std::cout << "Failed: at index - " << i;
80+
std::cout << ": " << strigifyCufftComplex(h_output[i]);
81+
std::cout << " != " << strigifyCufftComplex(h_ref_output[i]);
82+
std::cout << std::endl;
83+
failed = true;
84+
break;
85+
}
86+
}
87+
88+
if(!failed) std::cout << "Verification successful" << std::endl;
89+
90+
// Destroy the cuFFT plan and free allocated memory
91+
dpct::fft::fft_engine::destroy(plan);
92+
sycl::free(d_input, q_ct1);
93+
sycl::free(d_output, q_ct1);
94+
free(h_input);
95+
free(h_output);
96+
97+
if(failed)
98+
return 1;
99+
100+
return 0;
101+
}

0 commit comments

Comments
 (0)