IAP GITLAB

Skip to content
Snippets Groups Projects
Commit d88c1634 authored by Maximilian Reininghaus's avatar Maximilian Reininghaus :vulcan:
Browse files

helper module to set up RNG-function injection

parent c7e9acf0
No related branches found
No related tags found
1 merge request!572Resolve "Random number generators in modules: linkage, missing symbols, etc."
......@@ -251,6 +251,7 @@ add_subdirectory (documentation)
#
set (CORSIKA_DATA_WITH_TEST ON) # we want to run the corsika-data unit test
add_subdirectory (modules/data) # this is corsika-data (submodule)
add_subdirectory (modules/common)
add_subdirectory (modules/pythia8)
add_subdirectory (modules/sibyll)
add_subdirectory (modules/sophia)
......
add_library (modules_common INTERFACE)
target_include_directories (
modules_common
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include/corsika_modules/common>
)
install (
FILES
rng_decl.hpp rng_impl.gpp
DESTINATION include/corsika_modules/common
)
install (
TARGETS modules_common
EXPORT CORSIKA8PublicTargets
ARCHIVE DESTINATION lib/corsika
LIBRARY DESTINATION lib/corsika # just for cmake 3.10.x (ubuntu 18)
)
# add sibyll to corsika8 build
add_dependencies (CORSIKA8 modules_common)
target_link_libraries (CORSIKA8 INTERFACE modules_common)
/*
* (c) Copyright 2023 CORSIKA Project, corsika-project@lists.kit.edu
*
* This software is distributed under the terms of the GNU General Public
* Licence version 3 (GPL Version 3). See file LICENSE for a full version of
* the license.
*/
#pragma once
#include <functional>
#define DECLARE_RNG(NAME) \
namespace NAME { \
void set_rng_function(std::function<void(double*, std::size_t)>); \
} \
extern "C" void set_##NAME##_rng_function(void (*rng_function)(double*, std::size_t));
/*
* (c) Copyright 2023 CORSIKA Project, corsika-project@lists.kit.edu
*
* This software is distributed under the terms of the GNU General Public
* Licence version 3 (GPL Version 3). See file LICENSE for a full version of
* the license.
*/
#pragma once
#include <array>
#include <functional>
#include <iterator>
#include <iostream>
alignas(64) static std::array<double, 16> random_buffer{}; // aligned to fill two cache lines
static std::function<void(double*, size_t)> rng_ptr = nullptr;
static double const* next_rand = std::next(random_buffer.data(), random_buffer.size());
#define IMPLEMENT_RNG(NAME) \
namespace NAME { \
void set_rng_function(std::function<void(double*, size_t)> rng_function) { \
rng_ptr = rng_function; \
} \
} \
extern "C" void set_##NAME##_rng_function(void (*rng_function)(double*, size_t)) { \
NAME::set_rng_function(std::function{rng_function}); \
}
static double draw_std_rnd() {
if (next_rand == std::next(random_buffer.data(), random_buffer.size())) {
// no more unused values in buffer, refill via injected RNG function
rng_ptr(random_buffer.data(), random_buffer.size());
next_rand = random_buffer.data();
}
return *next_rand++;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment