DisCostiC v2.0.0
Distributed Cost in Cluster: a cross-architecture simulator for massively parallel MPI programs
Loading...
Searching...
No Matches
NetworkConfigParser.hpp
Go to the documentation of this file.
1
7
8#ifndef NETWORKCONFIGPARSER_HPP
9#define NETWORKCONFIGPARSER_HPP
10
11#pragma once
12
13#include <sstream>
14#include <fstream>
15#include <map>
16#include <typeinfo>
17#include <climits>
18#include <cstdlib>
19#include <mpi.h>
20#include "macro.hpp"
21#include "DataStruct.hpp"
22
26namespace UserInterface
27{
28
33 {
34 private:
35 int dataCounter = 0;
36 double networkFileData[1000][8] = {0};
37 std::map<std::string, std::string> data;
38 std::string fileName;
39
43 void removeComment(std::string &line) const
44 {
45 if (line.find('#') != line.npos)
46 line.erase(line.find('#'));
47 }
48
52 bool whitespace(const std::string &line) const
53 {
54 return (line.find_first_not_of(' ') == line.npos);
55 }
56
60 void parseLine(const std::string &line, size_t const lineNum)
61 {
62 std::string temp = line;
63 // std::cout << line << std::endl;
64
65 size_t pos = 0;
66 std::string token;
67
68 size_t index = 0;
69
70 while ((pos = temp.find(",")) != std::string::npos)
71 {
72 token = temp.substr(0, pos);
73 networkFileData[dataCounter][index] = stod(token);
74 // std::cout << "Token : "<<token << std::endl;
75 ++index;
76 temp.erase(0, pos + std::string(",").length());
77 }
78 networkFileData[dataCounter][index] = stod(temp);
79
81 // for (int i = 0; i < dataCounter; ++i)
82 // {
83 // for (int j = 0; j < 8; ++j)
84 // {
85 // std::cout << networkFileData[i][j] << std::endl;
86 // }
87 // }
88 }
89
90 public:
98 NetworkConfigParser(const std::string &fileName)
99 {
100
101 std::ifstream file;
102 fileopen(file, fileName);
103
104 std::string line;
105 size_t lineNum = 0;
106 while (std::getline(file, line))
107 {
108 lineNum++;
109 std::string temp = line;
110 if (temp.empty())
111 continue;
112
113 removeComment(temp);
114
115 if (whitespace(temp))
116 continue;
117
118 parseLine(temp, lineNum);
119 }
120
121 fileclose(file, fileName);
122 }
123
124 void readData(const std::string &fileName)
125 {
126
127 std::ifstream file;
128 fileopen(file, fileName);
129
130 std::string line;
131 size_t lineNum = 0;
132 while (std::getline(file, line))
133 {
134 lineNum++;
135 std::string temp = line;
136 if (temp.empty())
137 continue;
138
139 removeComment(temp);
140
141 if (whitespace(temp))
142 continue;
143
144 parseLine(temp, lineNum);
145 }
146
147 fileclose(file, fileName);
148 }
149
150 void setData(const int numberOfConcurrentConnection, const int buffersize, DisCosTiC::Networktype &latency, DisCosTiC::Networktype &inverse_bandwidth, DisCosTiC::Networktype &o, DisCosTiC::Networktype &O, DisCosTiC::Networktype &g, DisCosTiC::Networktype &G, DisCosTiC_Datatype &eagerLimit, const int mode)
151 {
152 int index = 0;
153 int bestIndex = 0;
154 int minDiff = INT_MAX;
155 bool foundConnectionMatch = false;
156
157 //find entries matching the connection count and get the closest message size
158 for (int i = 0; i < dataCounter; ++i)
159 {
160 if (networkFileData[i][0] >= numberOfConcurrentConnection)
161 {
162 foundConnectionMatch = true;
163
164 //exact match for message size
165 if (networkFileData[i][1] == buffersize)
166 {
167 index = i;
168 break;
169 }
170
171 //track closest message size
172 int diff = abs((int)(networkFileData[i][1] - buffersize));
173 if (diff < minDiff)
174 {
175 minDiff = diff;
176 bestIndex = i;
177 }
178 }
179 }
180
181 //use closest match if no exact match found
182 if (!foundConnectionMatch)
183 {
184 //no connection match. use first entry
185 index = 0;
186 }
187 else if (networkFileData[index][1] != buffersize)
188 {
189 // no exact match, use closest message size
190 index = bestIndex;
191 }
192
193 latency[mode] = networkFileData[index][2];
194 inverse_bandwidth[mode] = networkFileData[index][3];
195 eagerLimit = networkFileData[index][4];
196 G[mode] = networkFileData[index][5];
197 o[mode] = networkFileData[index][6];
198 g[mode] = networkFileData[index][7];
199 O[mode] = networkFileData[index][8];
200
201 // data.insert(std::pair<std::string, std::string>("latency_in_ns", std::to_string(networkFileData[index][2])));
202 // data.insert(std::pair<std::string, std::string>("G_in_ns", std::to_string(networkFileData[index][3])));
203 // data.insert(std::pair<std::string, std::string>("inverse_bandwidth", std::to_string(networkFileData[index][3])));
204 // data.insert(std::pair<std::string, std::string>("comm_o_in_ns", std::to_string(networkFileData[index][5])));
205 // data.insert(std::pair<std::string, std::string>("comm_O_in_ns", std::to_string(networkFileData[index][7])));
206 // data.insert(std::pair<std::string, std::string>("comm_g_in_ns", std::to_string(networkFileData[index][6])));
207 // data.insert(std::pair<std::string, std::string>("comm_eagerlimit_in_bytes", std::to_string(networkFileData[index][4])));
208 }
209 bool getKey(const std::string &key) const
210 {
211 return data.find(key) != data.end();
212 }
216 template <typename scalarT>
217 scalarT getValue(const std::string &key, scalarT const &defaultValue = scalarT()) const
218 {
219 if (!getKey(key))
220 return defaultValue;
221 return Conversion::stringTOScalarT<scalarT>(data.find(key)->second);
222 }
223
224 };
225}
226#endif
std::size_t DisCosTiC_Datatype
Definition DataType.hpp:52
static T stringTOScalarT(std::string const &val)
a wrapper class which contains functions for parsing the configuration file end class configParser
bool getKey(const std::string &key) const
std::map< std::string, std::string > data
which will hold pairs of key-value
scalarT getValue(const std::string &key, scalarT const &defaultValue=scalarT()) const
a function that retrieves the value of a specific key
bool whitespace(const std::string &line) const
a function that returns false if a non-space character was found, true otherwise. The function is "co...
void setData(const int numberOfConcurrentConnection, const int buffersize, DisCosTiC::Networktype &latency, DisCosTiC::Networktype &inverse_bandwidth, DisCosTiC::Networktype &o, DisCosTiC::Networktype &O, DisCosTiC::Networktype &g, DisCosTiC::Networktype &G, DisCosTiC_Datatype &eagerLimit, const int mode)
NetworkConfigParser()
a class to set the name of the configuration file and extracts and parses the data
void parseLine(const std::string &line, size_t const lineNum)
a function that parse the line by calling above mentioed functions
void removeComment(std::string &line) const
a function that removes everything from the semicolon (including it) to the end of the line.
NetworkConfigParser(const std::string &fileName)
std::string fileName
As member variables, we will only have a std::string, which will hold the name of the configuration f...
void readData(const std::string &fileName)
#define fileopen(myfile, filename)
open an open file
Definition macro.hpp:233
#define fileclose(myfile, filename)
close an open file
Definition macro.hpp:251
std::array< DisCosTiC_Timetype, 4 > Networktype
it parses the user-defined configuration file (.cfg)