Skip to content

Commit 5c4d4ce

Browse files
authored
Merge 331d857 into 96a2e41
2 parents 96a2e41 + 331d857 commit 5c4d4ce

File tree

14 files changed

+1294
-243
lines changed

14 files changed

+1294
-243
lines changed

doc/reference/fft.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ Helper routines
6363
.. dpnp.fft.config.set_cufft_gpus
6464
.. dpnp.fft.config.get_plan_cache
6565
.. dpnp.fft.config.show_plan_cache_info
66+
67+
.. automodule:: dpnp.fft

dpnp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ endfunction()
5757
build_dpnp_cython_ext_with_backend(dparray ${CMAKE_CURRENT_SOURCE_DIR}/dparray.pyx dpnp)
5858
add_subdirectory(backend)
5959
add_subdirectory(backend/extensions/blas)
60+
add_subdirectory(backend/extensions/fft)
6061
add_subdirectory(backend/extensions/lapack)
6162
add_subdirectory(backend/extensions/vm)
6263
add_subdirectory(backend/extensions/sycl_ext)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# *****************************************************************************
2+
# Copyright (c) 2024, Intel Corporation
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions are met:
7+
# - Redistributions of source code must retain the above copyright notice,
8+
# this list of conditions and the following disclaimer.
9+
# - Redistributions in binary form must reproduce the above copyright notice,
10+
# this list of conditions and the following disclaimer in the documentation
11+
# and/or other materials provided with the distribution.
12+
#
13+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
# THE POSSIBILITY OF SUCH DAMAGE.
24+
# *****************************************************************************
25+
26+
27+
set(python_module_name _fft_impl)
28+
set(_module_src
29+
${CMAKE_CURRENT_SOURCE_DIR}/fft_py.cpp
30+
${CMAKE_CURRENT_SOURCE_DIR}/c2c.cpp
31+
)
32+
33+
pybind11_add_module(${python_module_name} MODULE ${_module_src})
34+
add_sycl_to_target(TARGET ${python_module_name} SOURCES ${_module_src})
35+
36+
if (WIN32)
37+
if (${CMAKE_VERSION} VERSION_LESS "3.27")
38+
# this is a work-around for target_link_options inserting option after -link option, cause
39+
# linker to ignore it.
40+
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -fsycl-device-code-split=per_kernel")
41+
endif()
42+
endif()
43+
44+
set_target_properties(${python_module_name} PROPERTIES CMAKE_POSITION_INDEPENDENT_CODE ON)
45+
46+
target_include_directories(${python_module_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
47+
target_include_directories(${python_module_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../src)
48+
49+
target_include_directories(${python_module_name} PUBLIC ${Dpctl_INCLUDE_DIRS})
50+
target_include_directories(${python_module_name} PUBLIC ${Dpctl_TENSOR_INCLUDE_DIR})
51+
52+
if (WIN32)
53+
target_compile_options(${python_module_name} PRIVATE
54+
/clang:-fno-approx-func
55+
/clang:-fno-finite-math-only
56+
)
57+
else()
58+
target_compile_options(${python_module_name} PRIVATE
59+
-fno-approx-func
60+
-fno-finite-math-only
61+
)
62+
endif()
63+
64+
target_link_options(${python_module_name} PUBLIC -fsycl-device-code-split=per_kernel)
65+
66+
if (DPNP_GENERATE_COVERAGE)
67+
target_link_options(${python_module_name} PRIVATE -fprofile-instr-generate -fcoverage-mapping)
68+
endif()
69+
70+
if (MKL_VERSION_2024)
71+
target_link_libraries(${python_module_name} PUBLIC MKL::MKL_SYCL::DFT)
72+
else()
73+
target_link_libraries(${python_module_name} PUBLIC MKL::MKL_DPCPP)
74+
endif()
75+
76+
install(TARGETS ${python_module_name}
77+
DESTINATION "dpnp/backend/extensions/fft"
78+
)

dpnp/backend/extensions/fft/c2c.cpp

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2024, Intel Corporation
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
// - Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// - Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
// THE POSSIBILITY OF SUCH DAMAGE.
24+
//*****************************************************************************
25+
26+
#include <oneapi/mkl.hpp>
27+
#include <sycl/sycl.hpp>
28+
29+
#include <dpctl4pybind11.hpp>
30+
31+
#include "c2c.hpp"
32+
#include "fft_utils.hpp"
33+
// dpctl tensor headers
34+
#include "utils/memory_overlap.hpp"
35+
#include "utils/output_validation.hpp"
36+
37+
namespace dpnp::extensions::fft
38+
{
39+
namespace mkl_dft = oneapi::mkl::dft;
40+
namespace py = pybind11;
41+
42+
template <mkl_dft::precision prec>
43+
std::pair<sycl::event, sycl::event>
44+
compute_fft(ComplexDescriptorWrapper<prec> &descr,
45+
const dpctl::tensor::usm_ndarray &in,
46+
const dpctl::tensor::usm_ndarray &out,
47+
const bool is_forward,
48+
const std::vector<sycl::event> &depends)
49+
{
50+
// TODO: activate in MKL=2024.2
51+
// bool committed = descr.is_committed();
52+
// if (!committed) {
53+
// throw py::value_error("Descriptor is not committed");
54+
//}
55+
56+
const bool in_place = descr.get_in_place();
57+
if (in_place) {
58+
throw py::value_error(
59+
"Descriptor is defined for in-place FFT while this function is set "
60+
"to compute out-of-place FFT.");
61+
}
62+
63+
const int in_nd = in.get_ndim();
64+
const int out_nd = out.get_ndim();
65+
if ((in_nd != out_nd)) {
66+
throw py::value_error(
67+
"The input and output arrays must have the same dimension.");
68+
}
69+
70+
auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
71+
if (overlap(in, out)) {
72+
throw py::value_error("The input and output arrays are overlapping "
73+
"segments of memory");
74+
}
75+
76+
sycl::queue exec_q = descr.get_queue();
77+
if (!dpctl::utils::queues_are_compatible(exec_q,
78+
{in.get_queue(), out.get_queue()}))
79+
{
80+
throw py::value_error(
81+
"USM allocations are not compatible with the execution queue.");
82+
}
83+
84+
py::ssize_t in_size = in.get_size();
85+
py::ssize_t out_size = out.get_size();
86+
if (in_size != out_size) {
87+
throw py::value_error("The size of the input vector must be "
88+
"equal to the size of the output vector.");
89+
}
90+
91+
size_t src_nelems = in_size;
92+
dpctl::tensor::validation::CheckWritable::throw_if_not_writable(out);
93+
dpctl::tensor::validation::AmpleMemory::throw_if_not_ample(out, src_nelems);
94+
95+
using ScaleT = typename ScaleType<prec>::value_type;
96+
std::complex<ScaleT> *in_ptr = in.get_data<std::complex<ScaleT>>();
97+
std::complex<ScaleT> *out_ptr = out.get_data<std::complex<ScaleT>>();
98+
99+
sycl::event fft_event = {};
100+
std::stringstream error_msg;
101+
bool is_exception_caught = false;
102+
103+
try {
104+
if (is_forward) {
105+
fft_event = oneapi::mkl::dft::compute_forward(
106+
descr.get_descriptor(), in_ptr, out_ptr, depends);
107+
}
108+
else {
109+
fft_event = oneapi::mkl::dft::compute_backward(
110+
descr.get_descriptor(), in_ptr, out_ptr, depends);
111+
}
112+
} catch (oneapi::mkl::exception const &e) {
113+
error_msg
114+
<< "Unexpected MKL exception caught during FFT() call:\nreason: "
115+
<< e.what();
116+
is_exception_caught = true;
117+
} catch (sycl::exception const &e) {
118+
error_msg << "Unexpected SYCL exception caught during FFT() call:\n"
119+
<< e.what();
120+
is_exception_caught = true;
121+
}
122+
if (is_exception_caught) {
123+
throw std::runtime_error(error_msg.str());
124+
}
125+
126+
sycl::event args_ev =
127+
dpctl::utils::keep_args_alive(exec_q, {in, out}, {fft_event});
128+
129+
return std::make_pair(fft_event, args_ev);
130+
}
131+
132+
// Explicit instantiations
133+
template std::pair<sycl::event, sycl::event>
134+
compute_fft(ComplexDescriptorWrapper<mkl_dft::precision::SINGLE> &descr,
135+
const dpctl::tensor::usm_ndarray &in,
136+
const dpctl::tensor::usm_ndarray &out,
137+
const bool is_forward,
138+
const std::vector<sycl::event> &depends);
139+
140+
template std::pair<sycl::event, sycl::event>
141+
compute_fft(ComplexDescriptorWrapper<mkl_dft::precision::DOUBLE> &descr,
142+
const dpctl::tensor::usm_ndarray &in,
143+
const dpctl::tensor::usm_ndarray &out,
144+
const bool is_forward,
145+
const std::vector<sycl::event> &depends);
146+
147+
} // namespace dpnp::extensions::fft

0 commit comments

Comments
 (0)