Skip to content

Commit 9939b55

Browse files
Merge include-dpctl-tensor into move_dpctl_ext_to_dpnp_tensor
2 parents 9ed637e + 851628b commit 9939b55

2 files changed

Lines changed: 316 additions & 0 deletions

File tree

dpctl_ext/CMakeLists.txt

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# -*- coding: utf-8 -*-
2+
# *****************************************************************************
3+
# Copyright (c) 2026, Intel Corporation
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
# - Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
# - Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
# - Neither the name of the copyright holder nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software
15+
# without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27+
# THE POSSIBILITY OF SUCH DAMAGE.
28+
# *****************************************************************************
29+
30+
# TODO: rework this logic to remove current duplication
31+
if(WIN32)
32+
string(
33+
CONCAT WARNING_FLAGS
34+
"-Wall "
35+
"-Wextra "
36+
"-Winit-self "
37+
"-Wunused-function "
38+
"-Wuninitialized "
39+
"-Wmissing-declarations "
40+
"-Wstrict-prototypes "
41+
"-Wno-unused-parameter "
42+
)
43+
string(CONCAT SDL_FLAGS "/GS " "/DynamicBase ")
44+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Ox ${WARNING_FLAGS} ${SDL_FLAGS}")
45+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Ox ${WARNING_FLAGS} ${SDL_FLAGS}")
46+
set(CMAKE_C_FLAGS_DEBUG
47+
"${CMAKE_C_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O0 -g1 -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\""
48+
)
49+
set(CMAKE_CXX_FLAGS_DEBUG
50+
"${CMAKE_CXX_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O0 -g1 -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\""
51+
)
52+
set(CMAKE_C_FLAGS_COVERAGE
53+
"${CMAKE_C_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O1 -g1 -DDEBUG"
54+
)
55+
set(CMAKE_CXX_FLAGS_COVERAGE
56+
"${CMAKE_CXX_FLAGS_DEBUG} ${WARNING_FLAGS} ${SDL_FLAGS} -O1 -g1 -DDEBUG"
57+
)
58+
set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "${CMAKE_MODULE_LINKER_FLAGS_DEBUG}")
59+
set(DPCTL_LDFLAGS "/NXCompat;/DynamicBase")
60+
mark_as_advanced(
61+
CMAKE_CXX_FLAGS_COVERAGE
62+
CMAKE_C_FLAGS_COVERAGE
63+
CMAKE_MODULE_LINKER_FLAGS_COVERAGE
64+
)
65+
elseif(UNIX)
66+
string(
67+
CONCAT WARNING_FLAGS
68+
"-Wall "
69+
"-Wextra "
70+
"-Winit-self "
71+
"-Wunused-function "
72+
"-Wuninitialized "
73+
"-Wmissing-declarations "
74+
"-Wstrict-prototypes "
75+
"-Wno-unused-parameter "
76+
"-fdiagnostics-color=auto "
77+
)
78+
string(
79+
CONCAT SDL_FLAGS
80+
"-fstack-protector "
81+
"-fstack-protector-all "
82+
"-fpic "
83+
"-fPIC "
84+
"-D_FORTIFY_SOURCE=2 "
85+
"-Wformat "
86+
"-Wformat-security "
87+
# "-fno-strict-overflow " # no-strict-overflow is implied by -fwrapv
88+
"-fno-delete-null-pointer-checks "
89+
"-fwrapv "
90+
)
91+
string(CONCAT CFLAGS "${WARNING_FLAGS}" "${SDL_FLAGS}")
92+
string(CONCAT CXXFLAGS "${WARNING_FLAGS}" "${SDL_FLAGS}")
93+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 ${CFLAGS}")
94+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 ${CXXFLAGS}")
95+
set(CMAKE_C_FLAGS_DEBUG
96+
"${CMAKE_C_FLAGS_DEBUG} ${CFLAGS} -O0 -g -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\""
97+
)
98+
set(CMAKE_CXX_FLAGS_DEBUG
99+
"${CMAKE_CXX_FLAGS_DEBUG} ${CXXFLAGS} -O0 -g -DDEBUG -Xsycl-target-frontend=spir64 \"-g0\""
100+
)
101+
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_DEBUG} ${CFLAGS} -O1 -g1 -DDEBUG")
102+
set(CMAKE_CXX_FLAGS_COVERAGE "${CMAKE_CXX_FLAGS_DEBUG} ${CXXFLAGS} -O1 -g1 -DDEBUG")
103+
set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "${CMAKE_MODULE_LINKER_FLAGS_DEBUG}")
104+
set(DPCTL_LDFLAGS "-z,noexecstack,-z,relro,-z,now")
105+
mark_as_advanced(
106+
CMAKE_CXX_FLAGS_COVERAGE
107+
CMAKE_C_FLAGS_COVERAGE
108+
CMAKE_MODULE_LINKER_FLAGS_COVERAGE
109+
)
110+
else()
111+
message(FATAL_ERROR "Unsupported system.")
112+
endif()
113+
114+
# at build time create include/ directory and copy header files over
115+
set(DPCTL_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
116+
117+
set(CMAKE_INSTALL_RPATH "$ORIGIN")
118+
119+
function(build_dpctl_ext _trgt _src _dest)
120+
set(options SYCL)
121+
cmake_parse_arguments(BUILD_DPCTL_EXT "${options}" "RELATIVE_PATH" "" ${ARGN})
122+
add_cython_target(${_trgt} ${_src} CXX OUTPUT_VAR _generated_src)
123+
set(_cythonize_trgt "${_trgt}_cythonize_pyx")
124+
python_add_library(${_trgt} MODULE WITH_SOABI ${_generated_src})
125+
if(BUILD_DPCTL_EXT_SYCL)
126+
add_sycl_to_target(TARGET ${_trgt} SOURCES ${_generated_src})
127+
target_compile_options(${_trgt} PRIVATE -fno-sycl-id-queries-fit-in-int)
128+
target_link_options(${_trgt} PRIVATE -fsycl-device-code-split=per_kernel)
129+
if(DPCTL_OFFLOAD_COMPRESS)
130+
target_link_options(${_trgt} PRIVATE --offload-compress)
131+
endif()
132+
if(_dpctl_sycl_targets)
133+
# make fat binary
134+
target_compile_options(
135+
${_trgt}
136+
PRIVATE ${_dpctl_sycl_target_compile_options}
137+
)
138+
target_link_options(${_trgt} PRIVATE ${_dpctl_sycl_target_link_options})
139+
endif()
140+
endif()
141+
target_link_libraries(${_trgt} PRIVATE Python::NumPy)
142+
if(DPCTL_GENERATE_COVERAGE)
143+
target_compile_definitions(${_trgt} PRIVATE CYTHON_TRACE=1 CYTHON_TRACE_NOGIL=1)
144+
if(BUILD_DPCTL_EXT_SYCL)
145+
target_compile_options(${_trgt} PRIVATE -fno-sycl-use-footer)
146+
endif()
147+
endif()
148+
# Dpctl
149+
target_include_directories(${_trgt} PRIVATE ${Dpctl_INCLUDE_DIR})
150+
target_link_directories(${_trgt} PRIVATE ${Dpctl_INCLUDE_DIR}/..)
151+
target_link_libraries(${_trgt} PRIVATE DPCTLSyclInterface)
152+
set(_linker_options "LINKER:${DPCTL_LDFLAGS}")
153+
target_link_options(${_trgt} PRIVATE ${_linker_options})
154+
get_filename_component(_name_wle ${_generated_src} NAME_WLE)
155+
get_filename_component(_generated_src_dir ${_generated_src} DIRECTORY)
156+
set(_generated_public_h "${_generated_src_dir}/${_name_wle}.h")
157+
set(_generated_api_h "${_generated_src_dir}/${_name_wle}_api.h")
158+
159+
# TODO: create separate folder inside build folder that contains only
160+
# headers related to this target and appropriate folder structure to
161+
# eliminate shadow dependencies
162+
# Go up two levels to build root for "dpctl_ext/tensor/_usmarray.h" resolution
163+
get_filename_component(_parent_dir ${_generated_src_dir} DIRECTORY)
164+
get_filename_component(_build_root ${_parent_dir} DIRECTORY)
165+
# TODO: do not set directory if we did not generate header
166+
target_include_directories(${_trgt} INTERFACE ${_build_root})
167+
set(_rpath_value "$ORIGIN")
168+
if(BUILD_DPCTL_EXT_RELATIVE_PATH)
169+
set(_rpath_value "${_rpath_value}/${BUILD_DPCTL_EXT_RELATIVE_PATH}")
170+
endif()
171+
if(DPCTL_WITH_REDIST)
172+
set(_rpath_value "${_rpath_value}:${_rpath_value}/../../..")
173+
endif()
174+
set_target_properties(${_trgt} PROPERTIES INSTALL_RPATH ${_rpath_value})
175+
176+
install(TARGETS ${_trgt} LIBRARY DESTINATION ${_dest})
177+
install(
178+
FILES ${_generated_api_h}
179+
DESTINATION ${CMAKE_INSTALL_PREFIX}/dpctl_ext/include/${_dest}
180+
OPTIONAL
181+
)
182+
install(
183+
FILES ${_generated_public_h}
184+
DESTINATION ${CMAKE_INSTALL_PREFIX}/dpctl_ext/include/${_dest}
185+
OPTIONAL
186+
)
187+
if(DPCTL_GENERATE_COVERAGE)
188+
get_filename_component(_original_src_dir ${_src} DIRECTORY)
189+
file(RELATIVE_PATH _rel_dir ${CMAKE_SOURCE_DIR} ${_original_src_dir})
190+
install(FILES ${_generated_src} DESTINATION ${CMAKE_INSTALL_PREFIX}/${_rel_dir})
191+
endif()
192+
193+
# Create target with headers only, because python is managing all the
194+
# library imports at runtime
195+
set(_trgt_headers ${_trgt}_headers)
196+
add_library(${_trgt_headers} INTERFACE)
197+
add_dependencies(${_trgt_headers} ${_trgt})
198+
get_target_property(_trgt_headers_dir ${_trgt} INTERFACE_INCLUDE_DIRECTORIES)
199+
target_include_directories(${_trgt_headers} INTERFACE ${_trgt_headers_dir})
200+
endfunction()
201+
202+
# Install dpctl_ext C-API headers (similar to dpctl's C-API installation)
203+
install(
204+
DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/apis/include/
205+
DESTINATION ${CMAKE_INSTALL_PREFIX}/dpctl_ext/include
206+
FILES_MATCHING
207+
REGEX "\\.h(pp)?$"
208+
)
209+
210+
add_subdirectory(tensor)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2026, 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+
// - Neither the name of the copyright holder nor the names of its contributors
13+
// may be used to endorse or promote products derived from this software
14+
// without specific prior written permission.
15+
//
16+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26+
// THE POSSIBILITY OF SUCH DAMAGE.
27+
//*****************************************************************************
28+
//
29+
//===---------------------------------------------------------------------===//
30+
///
31+
/// \file
32+
/// This file provides access to dpctl_ext's C-API, including:
33+
/// - dpctl C-API (from external dpctl package - SYCL interface)
34+
/// - dpctl_ext tensor C-API (usm_ndarray)
35+
//===---------------------------------------------------------------------===//
36+
37+
#pragma once
38+
39+
// Include dpctl C-API headers explicitly from external dpctl package (SYCL
40+
// interface)
41+
// TODO: Once dpctl removes its tensor module and stabilizes dpctl_capi.h,
42+
// we can simplify to just: #include "dpctl_capi.h"
43+
// For now, explicit includes ensure we only get SYCL interface without tensor.
44+
45+
#include "syclinterface/dpctl_sycl_extension_interface.h"
46+
#include "syclinterface/dpctl_sycl_types.h"
47+
48+
#ifdef __cplusplus
49+
#define CYTHON_EXTERN_C extern "C"
50+
#else
51+
#define CYTHON_EXTERN_C
52+
#endif
53+
54+
#include "dpctl/_sycl_context.h"
55+
#include "dpctl/_sycl_context_api.h"
56+
#include "dpctl/_sycl_device.h"
57+
#include "dpctl/_sycl_device_api.h"
58+
#include "dpctl/_sycl_event.h"
59+
#include "dpctl/_sycl_event_api.h"
60+
#include "dpctl/_sycl_queue.h"
61+
#include "dpctl/_sycl_queue_api.h"
62+
#include "dpctl/memory/_memory.h"
63+
#include "dpctl/memory/_memory_api.h"
64+
#include "dpctl/program/_program.h"
65+
#include "dpctl/program/_program_api.h"
66+
67+
// Include the generated Cython C-API headers for usm_ndarray
68+
// These headers are generated during build and placed in the build directory
69+
#include "dpctl_ext/tensor/_usmarray.h"
70+
#include "dpctl_ext/tensor/_usmarray_api.h"
71+
72+
/*
73+
* Function to import dpctl_ext C-API and make it available.
74+
* This imports both:
75+
* - dpctl C-API (from external dpctl package - SYCL interface)
76+
* - dpctl_ext C-API (tensor interface - usm_ndarray)
77+
*
78+
* C functions can use dpctl_ext's C-API functions without linking to
79+
* shared objects defining these symbols, if they call `import_dpctl_ext()`
80+
* prior to using those symbols.
81+
*
82+
* It is declared inline to allow multiple definitions in
83+
* different translation units.
84+
*
85+
* TODO: When dpctl_ext is renamed to dpctl.tensor:
86+
* - Rename this file: dpctl_ext_capi.h → dpctl/tensor/tensor_capi.h
87+
* (Use tensor_capi.h, NOT dpctl_capi.h, to avoid conflict with external
88+
* dpctl)
89+
* - Rename this function: import_dpctl_ext() → import_dpctl_tensor()
90+
* - Include external dpctl_capi.h and simplify imports to use import_dpctl()
91+
*/
92+
static inline void import_dpctl_ext(void)
93+
{
94+
// Import dpctl SYCL interface
95+
// TODO: Once dpctl removes its tensor module and stabilizes dpctl_capi.h,
96+
// we can simplify to just: import_dpctl()
97+
import_dpctl___sycl_device();
98+
import_dpctl___sycl_context();
99+
import_dpctl___sycl_event();
100+
import_dpctl___sycl_queue();
101+
import_dpctl__memory___memory();
102+
import_dpctl__program___program();
103+
// Import dpctl_ext tensor interface
104+
import_dpctl_ext__tensor___usmarray();
105+
return;
106+
}

0 commit comments

Comments
 (0)