KCL-BMEIS / niftyreg

This project contains command line tools to perform rigid, affine and non-linear registration of nifti or analyse images as well as utilities
BSD 3-Clause "New" or "Revised" License
141 stars 42 forks source link

CUDA position independent code #66

Closed rijobro closed 2 years ago

rijobro commented 4 years ago

CUDA parts of NiftyReg are static libraries. In SIRF, some libraries that link against these CUDA NiftyReg libraries need to be static. This causes problems, as per issue https://github.com/CCPPETMR/SIRF/issues/551.

The solution is to add the following to the NVCC compiler options: --compiler-options -fPIC.

Would you have any problem with me making a PR to add this in? Should it be added by default, or would you like me to create an option, which when set to ON, appends the above string? It should be a 2-minute job, so let me know what you prefer.

rijobro commented 4 years ago

proposition 1:

set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} --compiler-options -fPIC")

proposition 2:

if (CMAKE_POSITION_INDEPENDENT_CODE)
    set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} --compiler-options -fPIC")
endif()

proposition 3:

if (CMAKE_POSITION_INDEPENDENT_CODE)
    if("${CMAKE_SYSTEM}" MATCHES "Linux")
        set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} --compiler-options -fPIC")
    endif()
endif()
KrisThielemans commented 4 years ago

All of these are dangerous as each compiler might need a different flag. proposition 4:

 if(CMAKE_POSITION_INDEPENDENT_CODE AND DEFINED CMAKE_C_COMPILE_OPTIONS_PIC)
       # add (undocumented) CMake flag that should tell the host compiler to generate position independent code
        set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} --compiler-options ${CMAKE_C_COMPILE_OPTIONS_PIC}")
endif()
KrisThielemans commented 4 years ago

I wouldn't do this by default for sure. position-independent code can be less efficient on some architectures.

I see no need for a new option. CMake has CMAKE_POSITION_INDEPENDENT_CODE already. If anyone wnats to build NiftyReg with this, they can just set CMAKE_POSITION_INDEPENDENT_CODE=ON