Skip to content
Snippets Groups Projects
Commit 8881f7ed authored by Benjamin Beichler's avatar Benjamin Beichler
Browse files

Merge branch 'dev-bb' into 'master'

reworked random seed and random engine and more

See merge request panorama.systemc.group/app4mc.sim!52
parents e8299c72 8df248fa
No related branches found
No related tags found
No related merge requests found
Showing
with 166 additions and 129 deletions
......@@ -16,3 +16,6 @@
[submodule "libs/CLIUtils"]
path = libs/CLIUtils
url = https://github.com/CLIUtils/CLI11.git
[submodule "libs/pcg-cpp"]
path = libs/pcg-cpp
url = https://github.com/imneme/pcg-cpp.git
......@@ -11,12 +11,13 @@ add_library (app4mc.sim_lib STATIC
"RandomScheduler.cpp"
"PriorityScheduler.cpp"
"MappingModel.cpp"
"Deviations.cpp"
"Deviation.cpp"
"BTFTracer.cpp"
"Tracer.cpp"
"SimParamParser.cpp"
)
target_link_libraries(app4mc.sim_lib SystemC::systemc effolkronium_random easyloggingpp gcem polymorphic_value::polymorphic_value CLI11)
target_link_libraries(app4mc.sim_lib SystemC::systemc effolkronium_random easyloggingpp gcem polymorphic_value::polymorphic_value CLI11 pcg)
#supress warning of sc_vector iterator definition
target_compile_definitions(app4mc.sim_lib PUBLIC _SILENCE_CXX17_ITERATOR_BASE_CLASS_DEPRECATION_WARNING=1)
......
......@@ -15,32 +15,6 @@
*/
#include "Deviation.h"
std::seed_seq& app4mcsim_seed::operator()()
{
auto seed = getSeed();
std::cout << "Simulation Random Seed: " << "0x" << std::hex << seed.seed1 << " 0x" << seed.seed2 << " 0x" << seed.seed3 << " 0x" << seed.seed4 << std::endl;
std::cout << "reuse seed with: app4mcsim_seed::setSeed(0x"<< std::hex << seed.seed1 << ",0x" << seed.seed2 << ",0x"<< seed.seed3 << ",0x" << seed.seed4 <<");" << std::endl;
return seq;
}
app4mcsim_seed::seed_t app4mcsim_seed::getSeed(std::optional<seed_t> userinit)
{
static seed_t seedSingelton = { std::random_device{ }( ),std::random_device{ }( ),std::random_device{ }( ),std::random_device{ }( ) };
if (userinit){
seedSingelton = *userinit;
}
return seedSingelton;
}
void app4mcsim_seed::setSeed(std::random_device::result_type seed1,std::random_device::result_type seed2,std::random_device::result_type seed3,std::random_device::result_type seed4)
{
seed_t userinit = {seed1,seed2,seed3,seed4} ;
getSeed(std::make_optional<seed_t>(userinit));
}
template class ValueWeibullDistribution<ELong>;
template class ValueWeibullDistribution<double>;
template class ValueWeibullDistribution<Time>;
......@@ -59,4 +33,33 @@ template class ValueBetaDistribution<double>;
template class ValueBoundaries<Time>;
template class ValueBoundaries<ELong>;
template class ValueBoundaries<double>;
\ No newline at end of file
template class ValueBoundaries<double>;
uint64_t app4mcsim_seed::operator()() {
auto seed = getSeed();
std::cout << "Simulation Random Seed: "
<< "0x" << std::hex << seed << std::endl;
std::cout << "reuse seed with: --seed 0x" << seed << std::endl;
return seed;
}
uint64_t& app4mcsim_seed::getSeed() {
static uint64_t seed = []() {
//seed 64bit with at least 64bit from the random device
//although random_device may always return 32bit number, this is not garuanteed by the std, so we calculate the number of random_device calls
//we shift in all rand_device values for the seed, which might throw away some random bits(but typically it exactly fits)
constexpr auto countRandCalls = integerCeilDiv(std::numeric_limits<uint64_t>::digits, std::numeric_limits<std::random_device::result_type>::digits);
std::random_device rnd;
uint64_t rv = 0;
for (int i = 0; i < countRandCalls; i++) {
rv = rv << static_cast<unsigned int>(std::numeric_limits<std::random_device::result_type>::digits);
rv |= rnd();
}
return rv;
}();
return seed;
}
void app4mcsim_seed::setSeed(uint64_t seed) {
getSeed() = seed;
}
/**
********************************************************************************
* Copyright (c) 2021 University of Rostock and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* - Benjamin Beichler <Benjamin.Beichler@uni-rostock.de> - initial contribution
********************************************************************************
*/
#include "SimParamParser.h"
int SimParamParser::parse(int argc, char** argv, SimParam& args)
{
CLI::App app("APP4MCsim: abstract timing simulation");
app.add_option("-o,--output", args.traceDirectory, "Trace output directory");
app.add_option("-t,--time", args.simulationTimeInMs, "simulation time ");
app.add_option("-r,--tracer", args.tracerNames, TraceManager::getCliHelpText())->delimiter(',');
app.add_flag("-p, --TracefileTimePrefix", args.useTraceFileTimePrefix, "add \"%Y-%m-%d-%H-%M-%S\" prefix to tracefiles to not override old files");
auto* seedArg = app.add_option("-s,--seed", args.simulationSeed, "seed for the underlying pcg pseudo random number generator; any non-negative 64bit value is possible");
seedArg->check(CLI::TypeValidator<uint64_t>());
CLI11_PARSE(app, argc, argv);
if (!seedArg->empty()) {
app4mcsim_seed::setSeed(args.simulationSeed);
}
if (args.useTraceFileTimePrefix) {
TraceManager::enableTimePrefix();
}
if (!args.traceDirectory.empty()) {
TraceManager::setTraceDirectory(args.traceDirectory);
}
for (auto& tracer : args.tracerNames) {
// remove any leading whitespace
tracer.erase(0, tracer.find_first_not_of(" \t\n\r\f\v"));
}
// code to keep old magic numbers option
for (auto& entry : args.tracerNames) {
int number = -1;
std::string_view entrySv = entry;
// we need pointer arithmetic here, otherwise from_chars is not working
auto rv = std::from_chars(&(*entrySv.begin()), &(*entrySv.begin()) + entrySv.size(), number); // NOLINT cppcoreguidelines-pro-bounds-pointer-arithmetic
if (rv.ec == std::errc()) {
switch (number) {
case BTF:
entry = BtfTracer::tracerName();
break;
case BTF_AGI:
entry = BtfTracerWithActivityGraphItemTracing::tracerName();
break;
case BTF_AGI_BRANCH:
entry = BtfTracerWithActivityGraphItemTracingHierarchy::tracerName();
break;
case BTF_SIGNAL:
entry = BtfTracerWithSignalTracing::tracerName();
default:
// do nothing, the string is ignored in TraceManager
break;
}
args.tracerSelect = number;
}
}
// rework list of Tracernames, that every Tracer appear only once
std::sort(args.tracerNames.begin(), args.tracerNames.end());
auto last = std::unique(args.tracerNames.begin(), args.tracerNames.end());
args.tracerNames.erase(last, args.tracerNames.end());
// add default if nothing is given
if (args.tracerNames.empty()) {
args.tracerNames.emplace_back(std::string(BtfTracer::tracerName()));
}
TraceManager::createTracerFromList(args.tracerNames);
VLOG(0) << "app4mcsim called with" << std::endl << "\t-t " << args.simulationTimeInMs << std::endl << "\t-o " << args.traceDirectory << std::endl;
return 0;
}
......@@ -78,7 +78,7 @@ ExecutionItem Task::getNextExecItem(const std::shared_ptr<ProcessingUnit> &pu) {
//create exec stack
if (execStack.isEmpty()) {
auto terminationCallback = [this, &pu]() {
auto terminationCallback = [this, pu]() {
scheduler->taskTerminate(shared_from_this(), pu);
terminate();
};
......
......@@ -17,6 +17,7 @@
#include "Common.h"
#include "effolkronium/random.hpp"
#include <cstdint>
#include <random>
#include <utility>
......@@ -24,28 +25,22 @@
#include <type_traits>
#include <optional>
#include "gcem.hpp"
#include <pcg_random.hpp>
/**
* This singleton is used to set a seed for a Simulation
*
**/
struct app4mcsim_seed {
struct seed_t{
std::random_device::result_type seed1;
std::random_device::result_type seed2;
std::random_device::result_type seed3;
std::random_device::result_type seed4;
};
/// return seed sequence
std::seed_seq& operator() ();
private:
static seed_t getSeed(std::optional<seed_t> userinit = std::nullopt);
std::seed_seq seq = {getSeed().seed1,getSeed().seed2,getSeed().seed3,getSeed().seed4 };
public:
static void setSeed(std::random_device::result_type seed1,std::random_device::result_type seed2,std::random_device::result_type seed3,std::random_device::result_type seed4);
static constexpr int integerCeilDiv(int nom, int den) {
return (nom + den - 1) / den;
}
uint64_t operator()();
private:
static uint64_t& getSeed();
public:
static void setSeed(uint64_t seed);
};
using app4mcsim_rand = effolkronium::basic_random_static<std::mt19937,app4mcsim_seed>;
using app4mcsim_rand = effolkronium::basic_random_static<pcg32, app4mcsim_seed>;
constexpr int numberOfResampleBoundedDeviation = 10;
template<typename T, typename sampleFun>
......
......@@ -13,6 +13,7 @@
********************************************************************************
*/
#pragma once
#include <cstdint>
#include <algorithm>
#include "BTFTracer.h"
#include "Tracer.h"
......@@ -21,8 +22,10 @@
#include "TracerFactory.h"
#include "easylogging++.h"
#include <charconv>
#include <stdexcept>
#include <string>
#include <string_view>
#include <Deviation.h>
struct SimParam
......@@ -32,6 +35,7 @@ struct SimParam
int simulationTimeInMs = 1000;
std::vector<std::string> tracerNames;
bool useTraceFileTimePrefix = false;
uint64_t simulationSeed;
//legacy, only for compability
int tracerSelect = 0;
......@@ -41,69 +45,5 @@ class SimParamParser {
private:
SimParamParser() = default;
public:
static int parse(int argc, char** argv, SimParam& args)
{
CLI::App app("APP4MCsim: abstract timing simulation");
app.add_option("-o,--output", args.traceDirectory, "Trace output directory");
app.add_option("-t,--time", args.simulationTimeInMs, "simulation time ");
app.add_option("-r,--tracer", args.tracerNames, TraceManager::getCliHelpText())->delimiter(',');
app.add_flag("-p, --TracefileTimePrefix", args.useTraceFileTimePrefix, "add \"%Y-%m-%d-%H-%M-%S\" prefix to tracefiles to not override old files");
CLI11_PARSE(app, argc, argv);
if (args.useTraceFileTimePrefix) {
TraceManager::enableTimePrefix();
}
if (!args.traceDirectory.empty()){
TraceManager::setTraceDirectory(args.traceDirectory);
}
for (auto& tracer : args.tracerNames) {
// remove any leading whitespace
tracer.erase(0, tracer.find_first_not_of(" \t\n\r\f\v"));
}
// code to keep old magic numbers option
for (auto& entry : args.tracerNames) {
int number = -1;
std::string_view entrySv = entry;
//we need pointer arithmetic here, otherwise from_chars is not working
auto rv = std::from_chars(&(*entrySv.begin()), &(*entrySv.begin())+entrySv.size(), number); //NOLINT cppcoreguidelines-pro-bounds-pointer-arithmetic
if (rv.ec == std::errc()) {
switch (number) {
case BTF:
entry = BtfTracer::tracerName();
break;
case BTF_AGI:
entry = BtfTracerWithActivityGraphItemTracing::tracerName();
break;
case BTF_AGI_BRANCH:
entry = BtfTracerWithActivityGraphItemTracingHierarchy::tracerName();
break;
case BTF_SIGNAL:
entry = BtfTracerWithSignalTracing::tracerName();
default:
// do nothing, the string is ignored in TraceManager
break;
}
args.tracerSelect = number;
}
}
// rework list of Tracernames, that every Tracer appear only once
std::sort(args.tracerNames.begin(), args.tracerNames.end());
auto last = std::unique(args.tracerNames.begin(), args.tracerNames.end());
args.tracerNames.erase(last, args.tracerNames.end());
// add default if nothing is given
if (args.tracerNames.empty()) {
args.tracerNames.emplace_back(std::string(BtfTracer::tracerName()));
}
TraceManager::createTracerFromList(args.tracerNames);
VLOG(0)<<"app4mcsim called with"<<std::endl<<"\t-t "<<args.simulationTimeInMs<<std::endl<<"\t-o "<<args.traceDirectory<<std::endl;
return 0;
}
static int parse(int argc, char** argv, SimParam& args);
};
\ No newline at end of file
......@@ -2,7 +2,7 @@
add_executable (activityGraphItemTracingFromTask "ActivityGraphItemTracingFromTask.cpp")
target_link_libraries(activityGraphItemTracingFromTask app4mc.sim_lib)
target_precompile_headers(activityGraphItemTracingFromTask REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_activityGraphItemTracingFromTask COMMAND activityGraphItemTracingFromTask)
add_test(NAME Run_activityGraphItemTracingFromTask COMMAND activityGraphItemTracingFromTask ${ADDITIONAL_TEST_ARGS})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "install default path set to <cmakeroot>/install" FORCE)
#message(" >> CMAKE_INSTALL_PREFIX = " ${CMAKE_INSTALL_PREFIX} )
......
......@@ -7,6 +7,10 @@ if(MSVC)
else(MSVC)
add_compile_options(-Wall -Wextra -pedantic)
endif(MSVC)
# use all tracer for tests
set (ADDITIONAL_TEST_ARGS --tracer BtfTracer,BtfTracerWithActivityGraphItemTracing,BtfTracerWithActivityGraphItemTracingHierarchy,BtfTracerWithSignalTracing)
add_subdirectory("HWOnlyExample_hier")
add_subdirectory("ChannelExample")
add_subdirectory("ChannelExamplePriorities")
......
......@@ -3,7 +3,7 @@ add_executable (ChannelExample "ChannelExample.cpp")
target_link_libraries(ChannelExample app4mc.sim_lib)
target_precompile_headers(ChannelExample REUSE_FROM app4mc.sim_lib)
add_test(NAME RunChannelExample COMMAND ChannelExample)
add_test(NAME RunChannelExample COMMAND ChannelExample ${ADDITIONAL_TEST_ARGS})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "install default path set to <cmakeroot>/install" FORCE)
#message(" >> CMAKE_INSTALL_PREFIX = " ${CMAKE_INSTALL_PREFIX} )
......
......@@ -3,7 +3,7 @@ add_executable (ChannelExamplePriorities "ChannelExamplePriorities.cpp")
target_link_libraries(ChannelExamplePriorities app4mc.sim_lib)
target_precompile_headers(ChannelExamplePriorities REUSE_FROM app4mc.sim_lib)
add_test(NAME RunChannelExamplePriorities COMMAND ChannelExamplePriorities)
add_test(NAME RunChannelExamplePriorities COMMAND ChannelExamplePriorities ${ADDITIONAL_TEST_ARGS})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "install default path set to <cmakeroot>/install" FORCE)
#message(" >> CMAKE_INSTALL_PREFIX = " ${CMAKE_INSTALL_PREFIX} )
......
......@@ -2,7 +2,7 @@
add_executable (executionConditions "ExecutionConditions.cpp")
target_link_libraries(executionConditions app4mc.sim_lib)
target_precompile_headers(executionConditions REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_executionConditions COMMAND executionConditions)
add_test(NAME Run_executionConditions COMMAND executionConditions ${ADDITIONAL_TEST_ARGS})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "install default path set to <cmakeroot>/install" FORCE)
#message(" >> CMAKE_INSTALL_PREFIX = " ${CMAKE_INSTALL_PREFIX} )
......
......@@ -3,4 +3,4 @@ add_executable (hw_example_hier "HWOnlyExample_hier.cpp")
target_link_libraries(hw_example_hier app4mc.sim_lib)
target_precompile_headers(hw_example_hier REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_hw_example_hier COMMAND hw_example_hier)
\ No newline at end of file
add_test(NAME Run_hw_example_hier COMMAND hw_example_hier ${ADDITIONAL_TEST_ARGS})
\ No newline at end of file
......@@ -4,7 +4,7 @@ target_link_libraries(InterprocessTrigger_example app4mc.sim_lib)
target_include_directories(InterprocessTrigger_example PUBLIC "../exampleHardware")
target_precompile_headers(InterprocessTrigger_example REUSE_FROM app4mc.sim_lib)
add_test(NAME RunInterprocessTrigger_example COMMAND InterprocessTrigger_example)
add_test(NAME RunInterprocessTrigger_example COMMAND InterprocessTrigger_example ${ADDITIONAL_TEST_ARGS})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "install default path set to <cmakeroot>/install" FORCE)
#message(" >> CMAKE_INSTALL_PREFIX = " ${CMAKE_INSTALL_PREFIX} )
......
......@@ -2,23 +2,23 @@
add_executable (modeSwitchesLiteralValues "ModeSwitchesLiteralValues.cpp")
target_link_libraries(modeSwitchesLiteralValues app4mc.sim_lib)
target_precompile_headers(modeSwitchesLiteralValues REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_modeSwitchesLiteralValues COMMAND modeSwitchesLiteralValues)
add_test(NAME Run_modeSwitchesLiteralValues COMMAND modeSwitchesLiteralValues ${ADDITIONAL_TEST_ARGS})
add_executable (modeSwitchesLiteralLabels "ModeSwitchesLiteralLabels.cpp")
target_link_libraries(modeSwitchesLiteralLabels app4mc.sim_lib)
target_precompile_headers(modeSwitchesLiteralLabels REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_modeSwitchesLiteralLabels COMMAND modeSwitchesLiteralLabels)
add_test(NAME Run_modeSwitchesLiteralLabels COMMAND modeSwitchesLiteralLabels ${ADDITIONAL_TEST_ARGS})
add_executable (modeSwitchesNumericValues "ModeSwitchesNumericValues.cpp")
target_link_libraries(modeSwitchesNumericValues app4mc.sim_lib)
target_precompile_headers(modeSwitchesNumericValues REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_modeSwitchesNumericValues COMMAND modeSwitchesNumericValues)
add_test(NAME Run_modeSwitchesNumericValues COMMAND modeSwitchesNumericValues ${ADDITIONAL_TEST_ARGS})
add_executable (modeSwitchesNumericLabels "ModeSwitchesNumericLabels.cpp")
target_link_libraries(modeSwitchesNumericLabels app4mc.sim_lib)
target_precompile_headers(modeSwitchesNumericLabels REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_modeSwitchesNumericLabels COMMAND modeSwitchesNumericLabels)
add_test(NAME Run_modeSwitchesNumericLabels COMMAND modeSwitchesNumericLabels ${ADDITIONAL_TEST_ARGS})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "install default path set to <cmakeroot>/install" FORCE)
#message(" >> CMAKE_INSTALL_PREFIX = " ${CMAKE_INSTALL_PREFIX} )
......
......@@ -3,7 +3,7 @@ add_executable (os_events_example "OsEvents.cpp")
target_link_libraries(os_events_example app4mc.sim_lib)
target_precompile_headers(os_events_example REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_os_events_example COMMAND os_events_example)
add_test(NAME Run_os_events_example COMMAND os_events_example ${ADDITIONAL_TEST_ARGS})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "install default path set to <cmakeroot>/install" FORCE)
......
add_executable (semaphore "Semaphore.cpp")
target_link_libraries(semaphore app4mc.sim_lib)
target_precompile_headers(semaphore REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_semaphore COMMAND semaphore)
add_test(NAME Run_semaphore COMMAND semaphore ${ADDITIONAL_TEST_ARGS})
install(
TARGETS semaphore
......
......@@ -2,7 +2,7 @@
add_executable (WhileLoop "WhileLoop.cpp")
target_link_libraries(WhileLoop app4mc.sim_lib)
target_precompile_headers(WhileLoop REUSE_FROM app4mc.sim_lib)
add_test(NAME Run_WhileLoop COMMAND WhileLoop)
add_test(NAME Run_WhileLoop COMMAND WhileLoop ${ADDITIONAL_TEST_ARGS})
install(
TARGETS WhileLoop
......
......@@ -3,7 +3,7 @@ add_executable (budget "budget.cpp")
target_link_libraries(budget app4mc.sim_lib)
target_precompile_headers(budget REUSE_FROM app4mc.sim_lib)
add_test(NAME RunBudgetExample COMMAND budget)
add_test(NAME RunBudgetExample COMMAND budget ${ADDITIONAL_TEST_ARGS})
SET(CMAKE_INSTALL_PREFIX ${CMAKE_SOURCE_DIR}/install CACHE PATH "install default path set to <cmakeroot>/install" FORCE)
#message(" >> CMAKE_INSTALL_PREFIX = " ${CMAKE_INSTALL_PREFIX} )
......
......@@ -6,4 +6,4 @@ target_link_libraries(${EXP_NAME} app4mc.sim_lib)
target_include_directories(${EXP_NAME} PUBLIC "../exampleHardware")
target_precompile_headers(${EXP_NAME} REUSE_FROM app4mc.sim_lib)
add_test(NAME "Run${EXP_NAME}" COMMAND ${EXP_NAME})
\ No newline at end of file
add_test(NAME "Run${EXP_NAME}" COMMAND ${EXP_NAME} ${ADDITIONAL_TEST_ARGS})
\ No newline at end of file
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