Skip to content

Commit

Permalink
Remove CLHEP dependency when building without Geant4
Browse files Browse the repository at this point in the history
The intent of building G4HepEm without Geant4 is to remove any
external dependency, leaving it to the consumer to implement the
core random number engine and wrapper functions.

Remove CLHEP dependency when building without Geant4, removing the
CPU-side implementation file with G4HepEmRandomEngine's flat and
flatArray member function definitions.

Link g4HepEmRun shared library with dynamic_lookup of undefined symbols
on Darwin only. Document reason for this and potential issues.

Update documentation of G4HepEmRandomEngine noting requirements for
users on implementing the host-device engine(s) and member functions.
  • Loading branch information
drbenmorgan committed Jul 26, 2023
1 parent 6a4e309 commit c187112
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
5 changes: 1 addition & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ if(G4HepEm_GEANT4_BUILD)

set(CMAKE_CXX_STANDARD ${Geant4_CXX_STANDARD}) # use value from Geant4Config.cmake
else()
# Still currently need CLHEP for G4HepEmRun's random engine
find_package(CLHEP REQUIRED)

# And need workaround for G4VERSION_NUM G4HepEmRun...
# Need workaround for G4VERSION_NUM G4HepEmRun...
# ... done in that component for now.
endif()

Expand Down
21 changes: 18 additions & 3 deletions G4HepEm/G4HepEmRun/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ set(G4HEPEmRun_impl_headers
include/G4HepEmPositronInteractionAnnihilation.icc
include/G4HepEmRunUtils.icc
)
set(G4HEPEmRun_sources
src/G4HepEmRandomEngine.cc
)

if(G4HepEm_GEANT4_BUILD)
set(G4HEPEmRun_sources
src/G4HepEmRandomEngine.cc
)
endif()

# See Issue #11 and discussion...
set_source_files_properties(${G4HEPEmRun_impl_headers} PROPERTIES LANGUAGE CXX)
Expand All @@ -66,6 +69,18 @@ if(BUILD_SHARED_LIBS)
elseif(TARGET CLHEP::CLHEP)
target_link_libraries(g4HepEmRun PUBLIC CLHEP::CLHEP)
endif()

# Without Geant4 and for Clang we must allow undefined symbols because we don't compile
# in implementations for G4HepEmRandomEngine::flat/flatArray (to be supplied by consumer)
# Note: use of '-undefined dynamic_lookup' rather than "-U,<symname>' is a sledgehammer, but
# for our use case should be o.k. as it only applies to build of *this* library and *only* when
# we build without Geant4. The "require fully defined" case is tested by the build with Geant4.
# For some further info on this problem, see:
# - https://gitlab.kitware.com/cmake/cmake/-/issues/18536
# - https://gitlab.kitware.com/vtk/vtk/-/issues/17214
if((CMAKE_SYSTEM_NAME STREQUAL "Darwin") AND NOT G4HepEm_GEANT4_BUILD)
target_link_options(g4HepEmRun PRIVATE -undefined dynamic_lookup)
endif()
endif()
if(BUILD_STATIC_LIBS)
set_target_properties(g4HepEmRun-static PROPERTIES COMPILE_FLAGS "-x c++ ${CMAKE_CXX_FLAGS}")
Expand Down
20 changes: 18 additions & 2 deletions G4HepEm/G4HepEmRun/include/G4HepEmRandomEngine.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,33 @@
*
* A simple abstraction for a random number engine.
*
* Holds a reference to the real engine and two function pointers to generate
* one random number or fill an array with a given size, respectively.
* Holds a reference to the real engine and two member functions to use the
* engine to generate one random number or fill an array with a given size, respectively.
*
* When G4HepEm is compiled with support for Geant4, the reference must be a pointer
* to an instance of `CLHEP::HepRandomEngine` for host-side use. For device-side use,
* the user must implement a suitable reference _and_ compile/link `__device__` implementations
* for the `flat` and `flatArray` member functions.
*
* For G4HepEm built in standalone mode without Geant4 support, the user must compile and
* link in both host- and device- side implementations for the engine and member functions.
*/
class G4HepEmRandomEngine final {
public:
G4HepEmHostDevice
G4HepEmRandomEngine(void *object)
: fObject(object), fIsGauss(false), fGauss(0.) { }

/** Return a random number uniformly distributed between 0 and 1.
*/
G4HepEmHostDevice
double flat();
/** Fill elements of array with random numbers uniformly distributed between 0 and 1.
*
* @param [in] size Number of elements in `vect` input array
* @param [in][out] vect Array to fill with random numbers
* @pre `size` must be less than or equal to the number of elements in `vect`
*/
G4HepEmHostDevice
void flatArray(const int size, double* vect);

Expand Down
2 changes: 0 additions & 2 deletions cmake/G4HepEmConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ include(CMakeFindDependencyMacro)
set(G4HepEm_geant4_FOUND @G4HepEm_GEANT4_BUILD@)
if(G4HepEm_geant4_FOUND)
find_dependency(Geant4 @Geant4_VERSION@ REQUIRED)
else()
find_dependency(CLHEP @CLHEP_VERSION@ REQUIRED)
endif()

# Direct CUDA deps to be determined, but should be handled by
Expand Down

0 comments on commit c187112

Please sign in to comment.