-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make alpaka_add_executable
obsolete
#1793
Comments
The definition of |
I checked already if it is possible via |
This is the central comment for me:
|
We need to tag files which language should be used. This is what |
I quickly tried the idea presented here to run a cmake function after all targets have been created and find the user's targets which link against alpaka, so I can change the source file language of these targets. It seem however, that the solution on stackoverflow only works with generator expressions, because the |
We discussed this in the alpaka VC today: we could add a separate function to annotate the source files of a given target and then use the plain |
I think it could looks like: add_executeable(mainTgt)
add_sources(mainTgt PRIVATE main.cpp)
# link with internal target, other libraries ...
target_link_library(mainTgt PRIVATE utils)
alpaka_enable_cuda(mainTgt) |
@SimeonEhrig You missed to link an alpaka target. If we add an target for any backend anyway I thought you snipped should looks like:
|
I would have called it something that does not involve the name CUDA, like Also, we would only need to declare source files which can contain device/kernel code. I think we added the host/device API separation at some point, so people could still use and link alpaka, but not use any device functionality. So maybe the function should be called
Same argument here.
Please discuss having targets for specific backends in a separate ticket, or continue at: #919. |
@bernhardmgruber I am mixing the discussion from #919 and this because IMO these are not orthogonal topics. The problem is that the current alpaka CMake is based on old CMake workflows mixed with targets. If we talk about modernizing what removing
As I know, currently alpaka is not providing a way to include only host API or device API functionality.. |
@psychocoderHPC In my opinion, your implementation is to complicated for the default use case. I suggest the following implementation: enable a specific backend, e.g. CUDA: function(alpaka_enable_cuda)
# foreach source in target, mark as CUDA file
# ...
target_link_library(tgt PRIVATE alpaka::alpaka)
target_link_library(tgt PRIVATE alpaka::cuda)
# ...
endfunction() add_executeable(mainTgt)
add_sources(mainTgt PRIVATE main.cpp)
# link with internal target, other libraries ...
target_link_library(mainTgt PRIVATE utils)
alpaka_enable_cuda(mainTgt) add all enabled backends, like the current behavior: function(alpaka_add_enabled_backends)
# ...
if(alpaka_ACC_GPU_CUDA_ENABLED)
alpaka_enable_cuda(tgt)
endif()
if(alpaka_ACC_GPU_HIP_ENABLED)
# ...
endif()
endfunction() add_executeable(mainTgt)
add_sources(mainTgt PRIVATE main.cpp)
# link with internal target, other libraries ...
target_link_library(mainTgt PRIVATE utils)
alpaka_add_enabled_backends(mainTgt) |
@SimeonEhrig You missed that we need to annotate source files and not the target. How to link alpaka to the target does not require a new function, we have already |
No, if you have the target, you can query it for all the source files and annotate those. It is actually the API I would prefer. This is not about how to link against alpaka, which, as you said, is solved by |
Ok then I understand it wrong, I thought selecting the files from a target is creating problems. Is
equal to
? |
Ah, now I get your point! The two examples make a difference, because But both approaches do not solve my original issue to have a standard cmake file without anything alpaka specific. So my conclusion for now is that this cannot be solved in the near future. |
I developed a prototype: alpaka.cmake option(alpaka_ACC_SERIAL_CPU "serial cpu accelerator" OFF)
option(alpaka_ACC_CUDA "serial cpu accelerator" OFF)
set(alpaka_active_acc "")
if(alpaka_ACC_SERIAL_CPU)
add_library(alpaka_acc_serial_cpu INTERFACE)
target_compile_definitions(alpaka_acc_serial_cpu INTERFACE "ALPAKA_SERIAL_CPU")
add_library(alpaka::acc_serial_cpu ALIAS alpaka_acc_serial_cpu)
list(APPEND alpaka_active_acc alpaka::acc_serial_cpu)
endif()
if(alpaka_ACC_CUDA)
enable_language(CUDA)
add_library(alpaka_acc_cuda INTERFACE)
target_compile_definitions(alpaka_acc_cuda INTERFACE "ALPAKA_ACC_CUDA")
add_library(alpaka::acc_cuda ALIAS alpaka_acc_cuda)
list(APPEND alpaka_active_acc alpaka::acc_cuda)
endif()
function(alpaka_link_library)
set(tgt ${ARGV0})
set(alpaka_tgt ${ARGV1})
if(NOT TARGET ${alpaka_tgt})
message(FATAL_ERROR "target ${alpaka_tgt} is not enabled")
endif()
if(${alpaka_tgt} STREQUAL "alpaka::acc_serial_cpu")
message(DEBUG "alpaka::acc_serial_cpu used")
endif()
if(${alpaka_tgt} STREQUAL "alpaka::acc_cuda")
message(DEBUG "alpaka::acc_cuda used")
get_target_property(cuda_sources ${tgt} SOURCES)
list(FILTER cuda_sources INCLUDE REGEX "(\\.cpp|\\.cxx)$")
if("${cuda_sources}" STREQUAL "")
message(WARNING "alpaka_link_library(${tgt} ${alpaka_tgt}): source list of target ${tgt} is empty")
endif()
foreach(cuda_source in ${cuda_sources})
SET_SOURCE_FILES_PROPERTIES(${cuda_source} PROPERTIES LANGUAGE CUDA)
endforeach()
endif()
target_link_libraries(${tgt} PUBLIC ${alpaka_tgt})
endfunction()
function(alpaka_link_enabled_accs)
cmake_parse_arguments(PARSE_ARGV 1
"ARG" # prefix
"" #options
"" # singlevalue
"EXCLUDE" # multivalue -> exclude target manually
)
set(local_alpaka_active_acc ${alpaka_active_acc})
if(NOT "${ARG_EXCLUDE}" STREQUAL "")
message(DEBUG "exclude tragets: ${ARG_EXCLUDE}")
list(REMOVE_ITEM local_alpaka_active_acc ${ARG_EXCLUDE})
endif()
foreach(acc ${local_alpaka_active_acc})
alpaka_link_library(${ARGV0} ${acc})
endforeach()
endfunction() CMakeLists.txt cmake_minimum_required(VERSION 3.18)
# set project name and meta information
project(main
VERSION 1.0
LANGUAGES CXX)
set(CMAKE_CUDA_ARCHITECTURES 60)
include(alpaka.cmake)
###########################################################
# automatic select, which acc is uesed
###########################################################
add_executable(auto_all)
target_sources(auto_all PRIVATE main.cpp hello_cpu.cpp hello_cuda.cpp)
alpaka_link_enabled_accs(auto_all)
add_executable(auto_exclude_cuda)
target_sources(auto_exclude_cuda PRIVATE main.cpp hello_cpu.cpp)
alpaka_link_enabled_accs(auto_exclude_cuda EXCLUDE alpaka::acc_cuda)
add_executable(auto_exclude_serial_cpu)
target_sources(auto_exclude_serial_cpu PRIVATE main.cpp hello_cuda.cpp)
alpaka_link_enabled_accs(auto_exclude_serial_cpu EXCLUDE alpaka::acc_serial_cpu)
add_executable(auto_exclude_all)
target_sources(auto_exclude_all PRIVATE main.cpp hello_cpu.cpp hello_cuda.cpp)
alpaka_link_enabled_accs(auto_all EXCLUDE alpaka::acc_serial_cpu alpaka::acc_cuda)
###########################################################
# compile source file for specific acc's
###########################################################
if(alpaka_ACC_SERIAL_CPU)
add_library(hello_cpu OBJECT)
target_sources(hello_cpu PRIVATE hello_cpu.cpp)
alpaka_link_library(hello_cpu alpaka::acc_serial_cpu)
endif()
if(alpaka_ACC_CUDA)
add_library(hello_cuda OBJECT)
target_sources(hello_cuda PRIVATE hello_cuda.cpp)
alpaka_link_library(hello_cuda alpaka::acc_cuda)
endif()
###########################################################
# manuall select, which acc is uesed
###########################################################
if(alpaka_ACC_SERIAL_CPU)
add_executable(manuell_cpu main.cpp)
target_link_libraries(manuell_cpu PRIVATE hello_cpu)
endif()
if(alpaka_ACC_CUDA)
add_executable(manuell_cuda main.cpp)
target_link_libraries(manuell_cuda PRIVATE hello_cuda)
endif()
if(alpaka_ACC_SERIAL_CPU AND alpaka_ACC_CUDA)
add_executable(manuell_cpu_cuda main.cpp)
target_link_libraries(manuell_cpu_cuda PRIVATE hello_cpu hello_cuda)
endif() full project: cmake_prototyp.zip |
Although taking the burden from the user is great. i found that compilation times can be reduced within a target, by explicitly marking only the files, that make use of alpaka. function(alpaka_target In_Name)
if(alpaka_ACC_GPU_CUDA_ENABLE)
enable_language(CUDA)
# We have to set this here since CUDA_SEPARABLE_COMPILATION is not propagated by the alpaka::alpaka target.
if(alpaka_RELOCATABLE_DEVICE_CODE STREQUAL ON)
set_property(TARGET ${In_Name} PROPERTY CUDA_SEPARABLE_COMPILATION ON)
elseif(alpaka_RELOCATABLE_DEVICE_CODE STREQUAL OFF)
set_property(TARGET ${In_Name} PROPERTY CUDA_SEPARABLE_COMPILATION OFF)
endif()
endif()
endfunction()
function(alpaka_source)
if(alpaka_ACC_GPU_CUDA_ENABLE)
enable_language(CUDA)
foreach(_file ${ARGN})
if((${_file} MATCHES "\\.cpp$") OR
(${_file} MATCHES "\\.cxx$") OR
(${_file} MATCHES "\\.cu$")
)
set_source_files_properties(${_file} PROPERTIES LANGUAGE CUDA)
endif()
endforeach()
endif()
endfunction()
# usage:
add_executable(my_tgt ${all_my_sources})
alpaka_target(my_tgt)
alpaka_source(${my_sources_that_use_alpaka}) |
For building executables using alpaka, we need to use
alpaka_add_executable
instead ofadd_executable
. AFAIK, the reason is that in case the CUDA backend is active, we need to mark all source files as CUDA files instead of CXX files for CMake. While this is understandable, it still puts a burden on users and I wonder whether we cannot just make the normaladd_executable
work.This issue has been mentioned a few times already in #919 and also briefly in #1779.
The text was updated successfully, but these errors were encountered: