Skip to content

Commit

Permalink
Made ConfigParser static
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrazzle committed Feb 13, 2024
1 parent 80df587 commit d5bb767
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 31 deletions.
14 changes: 6 additions & 8 deletions src/Memory/MemoryFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ std::unique_ptr<SubArray>
makeSimpleSubArray(const NVM::Simulation::Config& conf) {
SimpleConfigs configs;

ConfigParser parser;
parser.registerValue<double>("PIMFaultRate", 0.0, &configs.pimFaultRate);
parser.registerValue<int>("NumTries", 1, &configs.numTries);
parser.registerValue<int>("NumCorrectableFaults", 0,
&configs.numCorrectableFaults);
parser.registerValue<int>("WordSize", 64, &configs.wordSize);

parser.parse(conf);
ConfigParser::registerValue<double>("PIMFaultRate", 0.0,
&configs.pimFaultRate);
ConfigParser::registerValue<int>("NumTries", 1, &configs.numTries);
ConfigParser::registerValue<int>("NumCorrectableFaults", 0,
&configs.numCorrectableFaults);
ConfigParser::registerValue<int>("WordSize", 64, &configs.wordSize);

FaultModel model(configs.pimFaultRate, configs.wordSize,
configs.numCorrectableFaults);
Expand Down
3 changes: 2 additions & 1 deletion src/Utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
set(Utils_SOURCES
Address.cpp
RowData.cpp)
RowData.cpp
ConfigParser.cpp)

add_library(RTSim_Utils ${Utils_SOURCES})
target_include_directories(RTSim_Utils PUBLIC ${RTSIM_DIR}/include PRIVATE ${RTSIM_DIR}/src)
Expand Down
8 changes: 8 additions & 0 deletions src/Utils/ConfigParser.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "Utils/ConfigParser.h"

using namespace NVM::Memory;

bool ConfigParser::hasConfig = false;
NVM::Simulation::Config ConfigParser::config;
std::vector<std::function<void(const NVM::Simulation::Config&)>>
ConfigParser::parsers;
24 changes: 18 additions & 6 deletions src/Utils/ConfigParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class ConfigParser {
ConfigParser() {}

template<typename T>
void registerValue(std::string key, T defaultValue, T* destination) {
static void registerValue(std::string key, T defaultValue, T* destination) {
using namespace NVM::Logging;

log() << LogLevel::DEBUG << "Registering config value " << key
<< ", default is " << defaultValue << '\n';

parsers.push_back([key, defaultValue,
destination](const NVM::Simulation::Config& conf) {
auto parseFunc = [key, defaultValue,
destination](const NVM::Simulation::Config& conf) {
try {
*destination = conf.get<T>(key);
log() << LogLevel::STAT << "Read config value " << key << " as "
Expand All @@ -31,15 +31,27 @@ class ConfigParser {
<< key << " (" << defaultValue << ")\n";
*destination = defaultValue;
}
});
};

if (hasConfig) {
parseFunc(config);
} else {
parsers.push_back(parseFunc);
}
}

void parse(const NVM::Simulation::Config& conf) {
static void setConfig(const NVM::Simulation::Config& conf) {
config = conf;
for (auto p : parsers) p(conf);
hasConfig = true;
}

private:
std::vector<std::function<void(const NVM::Simulation::Config&)>> parsers;
static std::vector<std::function<void(const NVM::Simulation::Config&)>>
parsers;

static NVM::Simulation::Config config;
static bool hasConfig;
};

} // namespace NVM::Memory
26 changes: 10 additions & 16 deletions src/main/simpleMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,24 @@ ncycle_t getMaxCycles(char* arg) {
}

void setAddressScheme(const NVM::Simulation::Config& conf) {
ConfigParser parser;
ComponentCounts counts;

parser.registerValue<unsigned int>("DBCS", 64, &counts.rows);
parser.registerValue<unsigned int>("DOMAINS", 512, &counts.cols);
parser.registerValue<unsigned int>("RANKS", 1, &counts.ranks);
parser.registerValue<unsigned int>("BANKS", 1, &counts.banks);
parser.registerValue<unsigned int>("CHANNELS", 1, &counts.channels);
ConfigParser::registerValue<unsigned int>("DBCS", 64, &counts.rows);
ConfigParser::registerValue<unsigned int>("DOMAINS", 512, &counts.cols);
ConfigParser::registerValue<unsigned int>("RANKS", 1, &counts.ranks);
ConfigParser::registerValue<unsigned int>("BANKS", 1, &counts.banks);
ConfigParser::registerValue<unsigned int>("CHANNELS", 1, &counts.channels);

std::string addressScheme;
parser.registerValue<std::string>("AddressMappingScheme", "RK:BK:CH:R:C",
&addressScheme);

parser.parse(conf);
ConfigParser::registerValue<std::string>("AddressMappingScheme",
"RK:BK:CH:R:C", &addressScheme);

setScheme(addressScheme, counts);
}

void setLogLevel(const NVM::Simulation::Config& conf) {
ConfigParser parser;
std::string logLevel;
parser.registerValue<std::string>("LogLevel", "STAT", &logLevel);
parser.parse(conf);
ConfigParser::registerValue<std::string>("LogLevel", "STAT", &logLevel);

if (logLevel == "DEBUG") {
log().setGlobalLevel(LogLevel::DEBUG);
Expand Down Expand Up @@ -77,6 +72,7 @@ int main(int argc, char* argv[]) {
ncycle_t simulateCycles = getMaxCycles((argc > 3) ? argv[3] : nullptr);

NVM::Simulation::Config conf = readConfig(argv[1]);
ConfigParser::setConfig(conf);

for (int i = 4; i < argc; i++) {
conf.override(argv[i]);
Expand All @@ -88,10 +84,8 @@ int main(int argc, char* argv[]) {
// Build RTSystem
std::unique_ptr<MemorySystem> memory;

ConfigParser parser;
std::string memType;
parser.registerValue<std::string>("MemType", "Simple", &memType);
parser.parse(conf);
ConfigParser::registerValue<std::string>("MemType", "Simple", &memType);

if (memType == "Simple") {
memory = makeSimpleSystem(conf);
Expand Down

0 comments on commit d5bb767

Please sign in to comment.