From 87ad9bd04fd4793790f96dc28a6914a5377f239a Mon Sep 17 00:00:00 2001 From: Tobias Ribizel Date: Mon, 31 Jul 2023 19:27:28 +0200 Subject: [PATCH] clean up dependency structure --- CMakeLists.txt | 1 - cmake/template_instantiation.cmake | 34 ++++++++++++++++++------------ common/CMakeLists.txt | 1 - common/unified/CMakeLists.txt | 6 +----- dpcpp/CMakeLists.txt | 3 +++ omp/CMakeLists.txt | 2 +- 6 files changed, 26 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4107b76cb0f..dfa853299cf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/cmake/template_instantiation.cmake b/cmake/template_instantiation.cmake index af5c395279c..bc37d895537 100644 --- a/cmake/template_instantiation.cmake +++ b/cmake/template_instantiation.cmake @@ -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 ";" "" 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) @@ -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) @@ -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 "" ";" 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() diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 8512e05d07a..77bdd7230b9 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -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) diff --git a/common/unified/CMakeLists.txt b/common/unified/CMakeLists.txt index a9f45c63f13..5a37eb022f9 100644 --- a/common/unified/CMakeLists.txt +++ b/common/unified/CMakeLists.txt @@ -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 @@ -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) \ No newline at end of file +set(GKO_UNIFIED_COMMON_SOURCES ${UNIFIED_SOURCES} PARENT_SCOPE) \ No newline at end of file diff --git a/dpcpp/CMakeLists.txt b/dpcpp/CMakeLists.txt index 31b5e0543ba..b33b63d4af9 100644 --- a/dpcpp/CMakeLists.txt +++ b/dpcpp/CMakeLists.txt @@ -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_sources(ginkgo_dpcpp PRIVATE @@ -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 diff --git a/omp/CMakeLists.txt b/omp/CMakeLists.txt index 74e5e5b8806..50f46cd23cd 100644 --- a/omp/CMakeLists.txt +++ b/omp/CMakeLists.txt @@ -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)