Skip to content

Commit

Permalink
clean up dependency structure
Browse files Browse the repository at this point in the history
  • Loading branch information
upsj committed Jul 31, 2023
1 parent 0dbcbca commit 87ad9bd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 21 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,6 @@ configure_file(${Ginkgo_SOURCE_DIR}/include/ginkgo/config.hpp.in
# propagated to the other parts of Ginkgo in case of building as static libraries
add_subdirectory(devices) # Basic device functionalities. Always compiled.
add_subdirectory(common) # Import list of unified kernel source files
set_source_files_properties(${GKO_UNIFIED_COMMON_INSTANTIATE_SOURCES} PROPERTIES GENERATED 1)
message("${GKO_UNIFIED_COMMON_INSTANTIATE_SOURCES}")
if(GINKGO_BUILD_CUDA)
add_subdirectory(cuda) # High-performance kernels for NVIDIA GPUs
Expand Down
34 changes: 21 additions & 13 deletions cmake/template_instantiation.cmake
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
cmake_minimum_required(VERSION 3.13)
function(add_instantiation_files source_file output_files_var)
file(READ "${source_file}" file_contents)
function(add_instantiation_files source_dir source_file output_files_var)
# read full file into variable
set(source_path "${source_dir}/${source_file}")
file(READ "${source_path}" file_contents)
# escape semicolons and use them for line separation
string(REPLACE ";" "<semicolon>" file_contents "${file_contents}")
string(REGEX REPLACE "[\r\n]" ";" file_contents "${file_contents}")
# find location of // begin|split|end comments
set(begin_location)
set(end_location)
set(split_locations)
Expand All @@ -24,6 +27,7 @@ function(add_instantiation_files source_file output_files_var)
if (begin_location GREATER_EQUAL end_location)
message(FATAL_ERROR "Incorrect begin/end order")
endif()
# determine which lines belong to the header and footer
set(range_begins ${begin_location} ${split_locations})
set(range_ends ${split_locations} ${end_location})
list(LENGTH begin_locations range_count)
Expand All @@ -34,27 +38,31 @@ function(add_instantiation_files source_file output_files_var)
list(SUBLIST file_contents 0 ${length_header} header)
list(SUBLIST file_contents ${end_location_past} ${length_footer} footer)
set(output_files)
# for each range between // begin|split|end pairs
foreach(range RANGE 0 ${range_count_minus_one})
set(filename "${source_file}.${range}.cpp")
list(APPEND output_files "${filename}")
# create an output filename
string(REGEX REPLACE "(\.hip\.cpp|\.dp\.cpp|\.cpp|\.cu)$" ".${range}\\1" target_file "${source_file}")
set(target_path "${CMAKE_CURRENT_BINARY_DIR}/${target_file}")
list(APPEND output_files "${target_path}")
# extract the range between the comments
list(GET range_begins ${range} begin)
list(GET range_ends ${range} end)
math(EXPR begin "${begin} + 1")
math(EXPR length "${end} - ${begin}")
list(SUBLIST file_contents ${begin} ${length} content)
# concatenate header, content and footer and turn semicolons into newlines
string(REPLACE ";" "\n" content "${header};${content};${footer}")
# and escaped semicolons into regular semicolons again
string(REPLACE "<semicolon>" ";" content "${content}")
# create a .tmp file, but only copy it over if source file changed
# this way, we don't rebuild unnecessarily
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${filename}.tmp" "${content}")
file(WRITE "${target_path}.tmp" "${content}")
add_custom_command(
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${filename}"
COMMAND ${CMAKE_COMMAND}
-E copy "${CMAKE_CURRENT_BINARY_DIR}/${filename}.tmp"
"${CMAKE_CURRENT_BINARY_DIR}/${filename}"
MAIN_DEPENDENCY "${source_file}")
OUTPUT "${target_path}"
COMMAND ${CMAKE_COMMAND} -E copy "${target_path}.tmp" "${target_path}"
MAIN_DEPENDENCY "${source_path}")
endforeach()
# lazy workaround to make cmake generation depend on the source file
configure_file("${source_file}", "${source_file}.tmp" COPYONLY)
# make sure cmake gets called when the source file was updated
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${source_path}")
set(${output_files_var} ${output_files} PARENT_SCOPE)
endfunction()
1 change: 0 additions & 1 deletion common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
add_subdirectory(unified)
set(GKO_UNIFIED_COMMON_SOURCES ${GKO_UNIFIED_COMMON_SOURCES} PARENT_SCOPE)
set(GKO_UNIFIED_COMMON_INSTANTIATE_SOURCES ${GKO_UNIFIED_COMMON_INSTANTIATE_SOURCES} PARENT_SCOPE)
6 changes: 1 addition & 5 deletions common/unified/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
include(../../cmake/template_instantiation.cmake)
add_instantiation_files(matrix/dense_kernels.instantiate.cpp UNIFIED_INSTANTIATE_SOURCES)
set(UNIFIED_SOURCES
base/device_matrix_data_kernels.cpp
base/index_set_kernels.cpp
Expand Down Expand Up @@ -29,6 +27,4 @@ set(UNIFIED_SOURCES
solver/ir_kernels.cpp
)
list(TRANSFORM UNIFIED_SOURCES PREPEND ${CMAKE_CURRENT_SOURCE_DIR}/)
list(TRANSFORM UNIFIED_INSTANTIATE_SOURCES PREPEND ${CMAKE_CURRENT_BINARY_DIR}/)
set(GKO_UNIFIED_COMMON_SOURCES ${UNIFIED_SOURCES} PARENT_SCOPE)
set(GKO_UNIFIED_COMMON_INSTANTIATE_SOURCES ${UNIFIED_INSTANTIATE_SOURCES} PARENT_SCOPE)
set(GKO_UNIFIED_COMMON_SOURCES ${UNIFIED_SOURCES} PARENT_SCOPE)
3 changes: 3 additions & 0 deletions dpcpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ set(GINKGO_MKL_ROOT "${MKL_ROOT}" PARENT_SCOPE)
find_package(oneDPL REQUIRED HINTS "$ENV{DPL_ROOT}")
set(GINKGO_DPL_ROOT "${DPL_ROOT}" PARENT_SCOPE)

include(${PROJECT_SOURCE_DIR}/cmake/template_instantiation.cmake)
add_instantiation_files(${PROJECT_SOURCE_DIR}/common/unified matrix/dense_kernels.instantiate.cpp DENSE_INSTANTIATE)
add_library(ginkgo_dpcpp $<TARGET_OBJECTS:ginkgo_dpcpp_device> "")
target_sources(ginkgo_dpcpp
PRIVATE
Expand Down Expand Up @@ -55,6 +57,7 @@ target_sources(ginkgo_dpcpp
stop/criterion_kernels.dp.cpp
stop/residual_norm_kernels.dp.cpp
${GKO_UNIFIED_COMMON_SOURCES}
${DENSE_INSTANTIATE}
)

# TODO: adjust it when dpcpp jacobi supports more block size
Expand Down
2 changes: 1 addition & 1 deletion omp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ target_sources(ginkgo_omp
stop/criterion_kernels.cpp
stop/residual_norm_kernels.cpp
${GKO_UNIFIED_COMMON_SOURCES}
${GKO_UNIFIED_COMMON_INSTANTIATE_SOURCES}
${PROJECT_SOURCE_DIR}/common/unified/matrix/dense_kernels.instantiate.cpp
)

ginkgo_compile_features(ginkgo_omp)
Expand Down

0 comments on commit 87ad9bd

Please sign in to comment.