From 7444f8c9b5ccf8cad2b634ab75e4fa0c50c02512 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Wed, 7 Feb 2024 11:06:06 +0100 Subject: [PATCH] Move cuda instantiations in a plugin (#132) * Introduce CUDA extension * Move cuda instantiations in an plugin * Change folder hierarchy according to the guidelines --- CMakeLists.txt | 14 +--- .../CUDA/BeamAdapter.CUDAConfig.cmake.in | 9 +++ extensions/CUDA/CMakeLists.txt | 28 ++++++++ .../BeamAdapter/CUDA}/CudaInstantiations.cpp | 37 +++++----- .../CUDA/src/BeamAdapter/CUDA/config.h.in | 37 ++++++++++ extensions/CUDA/src/BeamAdapter/CUDA/init.cpp | 67 +++++++++++++++++++ extensions/CUDA/src/BeamAdapter/CUDA/init.h | 29 ++++++++ 7 files changed, 191 insertions(+), 30 deletions(-) create mode 100644 extensions/CUDA/BeamAdapter.CUDAConfig.cmake.in create mode 100644 extensions/CUDA/CMakeLists.txt rename {src/BeamAdapter/gpu/cuda => extensions/CUDA/src/BeamAdapter/CUDA}/CudaInstantiations.cpp (83%) create mode 100644 extensions/CUDA/src/BeamAdapter/CUDA/config.h.in create mode 100644 extensions/CUDA/src/BeamAdapter/CUDA/init.cpp create mode 100644 extensions/CUDA/src/BeamAdapter/CUDA/init.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d476908a2..f940cd090 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,6 @@ sofa_find_package(Sofa.Component.Constraint.Projective REQUIRED) sofa_find_package(Sofa.Component.Constraint.Lagrangian REQUIRED) sofa_find_package(SofaImplicitField QUIET) sofa_find_package(SofaAdvancedConstraint QUIET) -sofa_find_package(SofaCUDA QUIET) set(BEAMADAPTER_SRC "src/${PROJECT_NAME}") @@ -138,14 +137,6 @@ if(SofaAdvancedConstraint_FOUND) #cuda_add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${CUDA_SOURCES}) endif() - -if(SofaCUDA_FOUND) - list (APPEND SOURCE_FILES - ${BEAMADAPTER_SRC}/gpu/cuda/CudaInstantiations.cpp - ) - message(STATUS "BeamAdapter: SofaCUDA has been enabled, instantiations for CudaTypes will be enabled.") -endif() - add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES} ${README_FILES}) target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Simulation.Core) @@ -158,9 +149,6 @@ endif() if(SofaAdvancedConstraint_FOUND) target_link_libraries(${PROJECT_NAME} PUBLIC SofaAdvancedConstraint) endif() -if(SofaCUDA_FOUND) - target_link_libraries(${PROJECT_NAME} PUBLIC SofaCUDA) -endif() sofa_create_package_with_targets( PACKAGE_NAME ${PROJECT_NAME} @@ -196,4 +184,6 @@ SET_PROPERTY(TARGET ${PROJECT_NAME} PROPERTY FOLDER "plugins") # Config files and install rules for pythons scripts sofa_install_pythonscripts(PLUGIN_NAME ${PROJECT_NAME} PYTHONSCRIPTS_SOURCE_DIR "python") +sofa_add_subdirectory(plugin extensions/CUDA BeamAdapter.CUDA) + include(cmake/packaging.cmake) diff --git a/extensions/CUDA/BeamAdapter.CUDAConfig.cmake.in b/extensions/CUDA/BeamAdapter.CUDAConfig.cmake.in new file mode 100644 index 000000000..27f99f793 --- /dev/null +++ b/extensions/CUDA/BeamAdapter.CUDAConfig.cmake.in @@ -0,0 +1,9 @@ +# CMake package configuration file for the BeamAdapter.CUDA library + +@PACKAGE_GUARD@ +@PACKAGE_INIT@ + +find_package(BeamAdapter QUIET REQUIRED) +find_package(SofaCUDA QUIET REQUIRED) + +check_required_components(BeamAdapter.CUDA) diff --git a/extensions/CUDA/CMakeLists.txt b/extensions/CUDA/CMakeLists.txt new file mode 100644 index 000000000..51ffef0dc --- /dev/null +++ b/extensions/CUDA/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.12) +project(BeamAdapter.CUDA) + +set(HEADER_FILES + src/BeamAdapter/CUDA/init.h + src/BeamAdapter/CUDA/config.h.in +) + +set(SOURCE_FILES + src/BeamAdapter/CUDA/init.cpp + src/BeamAdapter/CUDA/CudaInstantiations.cpp +) + +sofa_find_package(BeamAdapter REQUIRED) +sofa_find_package(SofaCUDA REQUIRED) + +add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES}) +target_link_libraries(${PROJECT_NAME} BeamAdapter) +target_link_libraries(${PROJECT_NAME} SofaCUDA) + +sofa_create_package_with_targets( + PACKAGE_NAME ${PROJECT_NAME} + PACKAGE_VERSION ${Sofa_VERSION} + TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES + INCLUDE_SOURCE_DIR "src" + INCLUDE_INSTALL_DIR "${PROJECT_NAME}" + RELOCATABLE "plugins" +) diff --git a/src/BeamAdapter/gpu/cuda/CudaInstantiations.cpp b/extensions/CUDA/src/BeamAdapter/CUDA/CudaInstantiations.cpp similarity index 83% rename from src/BeamAdapter/gpu/cuda/CudaInstantiations.cpp rename to extensions/CUDA/src/BeamAdapter/CUDA/CudaInstantiations.cpp index 94079918e..3c56f451b 100644 --- a/src/BeamAdapter/gpu/cuda/CudaInstantiations.cpp +++ b/extensions/CUDA/src/BeamAdapter/CUDA/CudaInstantiations.cpp @@ -19,6 +19,7 @@ * * * Contact information: contact@sofa-framework.org * ******************************************************************************/ +#include #include #include @@ -43,62 +44,62 @@ using namespace sofa::gpu::cuda; namespace sofa::component::fem::_beaminterpolation_ { - // template class SOFA_BEAMADAPTER_API BeamInterpolation; + // template class SOFA_BEAMADAPTER_CUDA_API BeamInterpolation; #ifdef SOFA_GPU_CUDA_DOUBLE - template class SOFA_BEAMADAPTER_API BeamInterpolation; + template class SOFA_BEAMADAPTER_CUDA_API BeamInterpolation; #endif } // namespace sofa::component::fem::_beaminterpolation_ namespace sofa::component::fem::_wirebeaminterpolation_ { - // template class SOFA_BEAMADAPTER_API WireBeamInterpolation; + // template class SOFA_BEAMADAPTER_CUDA_API WireBeamInterpolation; #ifdef SOFA_GPU_CUDA_DOUBLE - template class SOFA_BEAMADAPTER_API WireBeamInterpolation; + template class SOFA_BEAMADAPTER_CUDA_API WireBeamInterpolation; #endif } // namespace sofa::component::fem::_beaminterpolation_ namespace sofa::component::engine::_wirerestshape_ { - template class SOFA_BEAMADAPTER_API WireRestShape; + template class SOFA_BEAMADAPTER_CUDA_API WireRestShape; #ifdef SOFA_GPU_CUDA_DOUBLE - template class SOFA_BEAMADAPTER_API WireRestShape; + template class SOFA_BEAMADAPTER_CUDA_API WireRestShape; #endif } // namespace sofa::component::engine::_wirerestshape_ namespace sofa::component::controller::_interventionalradiologycontroller_ { - template class SOFA_BEAMADAPTER_API InterventionalRadiologyController; + template class SOFA_BEAMADAPTER_CUDA_API InterventionalRadiologyController; #ifdef SOFA_GPU_CUDA_DOUBLE - template class SOFA_BEAMADAPTER_API InterventionalRadiologyController; + template class SOFA_BEAMADAPTER_CUDA_API InterventionalRadiologyController; #endif } // namespace sofa::component::controller::_interventionalradiologycontroller_ namespace sofa::component::mapping::_adaptivebeammapping_ { - template class SOFA_BEAMADAPTER_API AdaptiveBeamMapping; + template class SOFA_BEAMADAPTER_CUDA_API AdaptiveBeamMapping; #ifdef SOFA_GPU_CUDA_DOUBLE - template class SOFA_BEAMADAPTER_API AdaptiveBeamMapping; + template class SOFA_BEAMADAPTER_CUDA_API AdaptiveBeamMapping; #endif } // namespace sofa::component::mapping::_adaptivebeammapping_ namespace sofa::component::mapping { - template class SOFA_BEAMADAPTER_API MultiAdaptiveBeamMapping; + template class SOFA_BEAMADAPTER_CUDA_API MultiAdaptiveBeamMapping; #ifdef SOFA_GPU_CUDA_DOUBLE - template class SOFA_BEAMADAPTER_API MultiAdaptiveBeamMapping; + template class SOFA_BEAMADAPTER_CUDA_API MultiAdaptiveBeamMapping; #endif } // namespace sofa::component::mapping namespace sofa::beamadapter { - template class SOFA_BEAMADAPTER_API RodMeshSection; - template class SOFA_BEAMADAPTER_API RodSpireSection; - template class SOFA_BEAMADAPTER_API RodStraightSection; + template class SOFA_BEAMADAPTER_CUDA_API RodMeshSection; + template class SOFA_BEAMADAPTER_CUDA_API RodSpireSection; + template class SOFA_BEAMADAPTER_CUDA_API RodStraightSection; #ifdef SOFA_GPU_CUDA_DOUBLE - template class SOFA_BEAMADAPTER_API RodMeshSection; - template class SOFA_BEAMADAPTER_API RodSpireSection; - template class SOFA_BEAMADAPTER_API RodStraightSection; + template class SOFA_BEAMADAPTER_CUDA_API RodMeshSection; + template class SOFA_BEAMADAPTER_CUDA_API RodSpireSection; + template class SOFA_BEAMADAPTER_CUDA_API RodStraightSection; #endif } // namespace sofa::beamadapter diff --git a/extensions/CUDA/src/BeamAdapter/CUDA/config.h.in b/extensions/CUDA/src/BeamAdapter/CUDA/config.h.in new file mode 100644 index 000000000..90a08534f --- /dev/null +++ b/extensions/CUDA/src/BeamAdapter/CUDA/config.h.in @@ -0,0 +1,37 @@ +/****************************************************************************** +* BeamAdapter plugin * +* (c) 2006 Inria, University of Lille, CNRS * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: see Authors.md * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include + +#ifdef SOFA_BUILD_BEAMADAPTER_CUDA +# define SOFA_TARGET @PROJECT_NAME@ +# define SOFA_BEAMADAPTER_CUDA_API SOFA_EXPORT_DYNAMIC_LIBRARY +#else +# define SOFA_BEAMADAPTER_CUDA_API SOFA_IMPORT_DYNAMIC_LIBRARY +#endif + +namespace beamadapter::cuda +{ + constexpr const char* MODULE_NAME = "@PROJECT_NAME@"; + constexpr const char* MODULE_VERSION = "@PROJECT_VERSION@"; +} // namespace beamadapter::cuda diff --git a/extensions/CUDA/src/BeamAdapter/CUDA/init.cpp b/extensions/CUDA/src/BeamAdapter/CUDA/init.cpp new file mode 100644 index 000000000..0de54cd21 --- /dev/null +++ b/extensions/CUDA/src/BeamAdapter/CUDA/init.cpp @@ -0,0 +1,67 @@ +/****************************************************************************** +* BeamAdapter plugin * +* (c) 2006 Inria, University of Lille, CNRS * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: see Authors.md * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#include +#include +#include +namespace beamadapter::cuda +{ + +extern "C" { + SOFA_EXPORT_DYNAMIC_LIBRARY void initExternalModule(); + SOFA_EXPORT_DYNAMIC_LIBRARY const char* getModuleName(); + SOFA_EXPORT_DYNAMIC_LIBRARY const char* getModuleVersion(); + SOFA_EXPORT_DYNAMIC_LIBRARY const char* getModuleComponentList(); +} + +void initExternalModule() +{ + init(); +} + +const char* getModuleName() +{ + return MODULE_NAME; +} + +const char* getModuleVersion() +{ + return MODULE_VERSION; +} + +void init() +{ + static bool first = true; + if (first) + { + sofa::component::initBeamAdapter(); + first = false; + } +} + +const char* getModuleComponentList() +{ + /// string containing the names of the classes provided by the plugin + static std::string classes = sofa::core::ObjectFactory::getInstance()->listClassesFromTarget(MODULE_NAME); + return classes.c_str(); +} + +} // namespace beamadapter::cuda diff --git a/extensions/CUDA/src/BeamAdapter/CUDA/init.h b/extensions/CUDA/src/BeamAdapter/CUDA/init.h new file mode 100644 index 000000000..8ca6aa1d6 --- /dev/null +++ b/extensions/CUDA/src/BeamAdapter/CUDA/init.h @@ -0,0 +1,29 @@ +/****************************************************************************** +* BeamAdapter plugin * +* (c) 2006 Inria, University of Lille, CNRS * +* * +* This program is free software; you can redistribute it and/or modify it * +* under the terms of the GNU Lesser General Public License as published by * +* the Free Software Foundation; either version 2.1 of the License, or (at * +* your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, but WITHOUT * +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * +* for more details. * +* * +* You should have received a copy of the GNU Lesser General Public License * +* along with this program. If not, see . * +******************************************************************************* +* Authors: see Authors.md * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include + +namespace beamadapter::cuda +{ +SOFA_BEAMADAPTER_CUDA_API void init(); +} // namespace beamadapter::cuda