-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Behavioural test for dpct_add_mkl_to_target helper macro
- Loading branch information
1 parent
ec4afae
commit e490d7b
Showing
4 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
behavior_tests/src/cmake_dpct_add_mkl_to_target/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#==---- CMakeLists.txt ---------------------------- cmake script file ----==// | ||
# | ||
# Copyright (C) Intel Corporation | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# | ||
#===----------------------------------------------------------------------===// | ||
|
||
cmake_minimum_required(VERSION 3.10) | ||
|
||
project(cmake_add_mkl LANGUAGES CXX) | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsycl") | ||
find_program( | ||
dpct_bin_path | ||
NAMES dpct | ||
PATHS) | ||
get_filename_component(bin_path_of_dpct ${dpct_bin_path} DIRECTORY) | ||
set(dpct_cmake_file_path "${bin_path_of_dpct}/../cmake/dpct.cmake") | ||
include(${dpct_cmake_file_path}) | ||
|
||
find_package(IntelSYCL REQUIRED) | ||
|
||
set(CUDA_SOURCES | ||
fft.dp.cpp | ||
) | ||
|
||
add_executable(app.run ${CUDA_SOURCES}) | ||
|
||
dpct_add_mkl_to_target(app.run) |
47 changes: 47 additions & 0 deletions
47
behavior_tests/src/cmake_dpct_add_mkl_to_target/do_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# ====------ do_test.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 subprocess | ||
import platform | ||
import os | ||
import sys | ||
from test_config import CT_TOOL | ||
|
||
from test_utils import * | ||
|
||
def setup_test(): | ||
change_dir(test_config.current_test) | ||
return True | ||
|
||
def migrate_test(): | ||
# clean previous migration output | ||
if (os.path.exists("build")): | ||
shutil.rmtree("build") | ||
|
||
ret = call_subprocess("mkdir build") | ||
if not ret: | ||
print("Error to create build folder:", test_config.command_output) | ||
|
||
ret = change_dir("build") | ||
if not ret: | ||
print("Error to go to build folder:", test_config.command_output) | ||
|
||
ret = call_subprocess("cmake -G \"Unix Makefiles\" -DCMAKE_CXX_COMPILER=icpx ../") | ||
if not ret: | ||
print("Error to run cmake configure:", test_config.command_output) | ||
|
||
ret = call_subprocess("make") | ||
if not ret: | ||
print("Error to run build process:", test_config.command_output) | ||
|
||
return os.path.exists("app.run") | ||
def build_test(): | ||
return True | ||
def run_test(): | ||
return call_subprocess("./app.run") | ||
|
101 changes: 101 additions & 0 deletions
101
behavior_tests/src/cmake_dpct_add_mkl_to_target/fft.dp.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include <sycl/sycl.hpp> | ||
#include <dpct/dpct.hpp> | ||
#include <iostream> | ||
#include <dpct/fft_utils.hpp> | ||
|
||
#include <cmath> | ||
|
||
// Function to compare two floating-point numbers with a tolerance | ||
int almostEqual(float a, float b, float tolerance) { | ||
return fabs(a - b) < tolerance; | ||
} | ||
|
||
// Function to compare two cufftComplex variables | ||
int compareCufftComplex(sycl::float2 a, sycl::float2 b, float tolerance) { | ||
return almostEqual(a.x(), b.x(), tolerance) && | ||
almostEqual(a.y(), b.y(), tolerance); | ||
} | ||
|
||
std::string strigifyCufftComplex(sycl::float2 a) { | ||
return std::string("(" + std::to_string(a.x()) + ", " + | ||
std::to_string(a.y()) + ")"); | ||
} | ||
|
||
int main() { | ||
dpct::device_ext &dev_ct1 = dpct::get_current_device(); | ||
sycl::queue &q_ct1 = dev_ct1.in_order_queue(); | ||
const int n = 8; // Size of the input array | ||
|
||
// Allocate memory on the host for input and output arrays | ||
sycl::float2 *h_input = (sycl::float2 *)malloc(sizeof(sycl::float2) * n); | ||
sycl::float2 *h_output = (sycl::float2 *)malloc(sizeof(sycl::float2) * n); | ||
|
||
// Initialize the input array with random values | ||
for (int i = 0; i < n; ++i) { | ||
h_input[i].x() = static_cast<float>(rand()) / RAND_MAX; | ||
h_input[i].y() = static_cast<float>(rand()) / RAND_MAX; | ||
} | ||
|
||
// Allocate memory on the host for reference output array | ||
sycl::float2 *h_ref_output = | ||
(sycl::float2 *)malloc(sizeof(sycl::float2) * n); | ||
|
||
// Initialize h_ref_output with expected output | ||
h_ref_output[0] = sycl::float2(4.94234f, 4.77104f); | ||
h_ref_output[1] = sycl::float2(0.914293f, -0.261793f); | ||
h_ref_output[2] = sycl::float2(-0.415583f, 0.264357f); | ||
h_ref_output[3] = sycl::float2(0.241085f, 0.382871f); | ||
h_ref_output[4] = sycl::float2(-0.153554f, -1.45243f); | ||
h_ref_output[5] = sycl::float2(-0.421167f, -1.15111f); | ||
h_ref_output[6] = sycl::float2(0.0986443f, 0.210444f); | ||
h_ref_output[7] = sycl::float2(1.51544f, 0.391681f); | ||
|
||
// Allocate memory on the device (GPU) | ||
sycl::float2 *d_input, *d_output; | ||
d_input = sycl::malloc_device<sycl::float2>(n, q_ct1); | ||
d_output = sycl::malloc_device<sycl::float2>(n, q_ct1); | ||
|
||
// Copy input data from host to device | ||
q_ct1.memcpy(d_input, h_input, sizeof(sycl::float2) * n).wait(); | ||
|
||
// Create a cuFFT plan | ||
dpct::fft::fft_engine_ptr plan; | ||
plan = dpct::fft::fft_engine::create( | ||
&q_ct1, n, dpct::fft::fft_type::complex_float_to_complex_float, 1); | ||
|
||
// Perform forward FFT | ||
plan->compute<sycl::float2, sycl::float2>( | ||
d_input, d_output, dpct::fft::fft_direction::forward); | ||
|
||
// Copy output data from device to host | ||
q_ct1.memcpy(h_output, d_output, sizeof(sycl::float2) * n).wait(); | ||
|
||
// Verify the result | ||
bool failed = false; | ||
|
||
float tolerance = 5e-6f; | ||
for (int i = 0; i < n; ++i) { | ||
if (!compareCufftComplex(h_output[i], h_ref_output[i], tolerance)) { | ||
std::cout << "Failed: at index - " << i; | ||
std::cout << ": " << strigifyCufftComplex(h_output[i]); | ||
std::cout << " != " << strigifyCufftComplex(h_ref_output[i]); | ||
std::cout << std::endl; | ||
failed = true; | ||
break; | ||
} | ||
} | ||
|
||
if(!failed) std::cout << "Verification successful" << std::endl; | ||
|
||
// Destroy the cuFFT plan and free allocated memory | ||
dpct::fft::fft_engine::destroy(plan); | ||
sycl::free(d_input, q_ct1); | ||
sycl::free(d_output, q_ct1); | ||
free(h_input); | ||
free(h_output); | ||
|
||
if(failed) | ||
return 1; | ||
|
||
return 0; | ||
} |