OP  0.1
OP is a optimization solver plugin package
 All Classes Namespaces Functions Variables Typedefs Enumerations Friends
op_debug.hpp
1 #pragma once
2 
3 #include "op.hpp"
4 #include "op_mpi.hpp"
5 #include "op_utility.hpp"
6 #include <fstream>
7 #include <sstream>
8 
9 namespace op {
11  namespace debug {
12 
20  template <typename VectorType>
21  void writeVectorToDisk(const std::vector<VectorType> & vector, int local_rank, std::string local_vector_string)
22  {
23  std::stringstream local_vector_file;
24  local_vector_file << local_vector_string << "." << local_rank;
25  std::ofstream local_vector(local_vector_file.str());
26  for (auto v : vector) {
27  local_vector << v << std::endl;
28  }
29  local_vector.close();
30  }
31 
32 
40  template <typename VectorType>
41  void writeVariablesToDisk(const op::Vector<VectorType> & op_variables, int local_rank, std::string local_variable_string = "local_variables")
42  {
43  writeVectorToDisk(op_variables.data(), local_rank, local_variable_string);
44  }
45 
53  template <typename VectorType>
54  void readVectorFromDisk(std::vector<VectorType> & vector, int local_rank, std::string local_vector_string)
55  {
56  std::stringstream local_vector_file;
57  local_vector_file << local_vector_string << "." << local_rank;
58  std::ifstream local_vector(local_vector_file.str());
59  while (local_vector.good() && !local_vector.eof()) {
60  VectorType v;
61  local_vector >> v;
62  vector.push_back(v);
63  }
64  local_vector.close();
65  }
66 
75  template <typename VectorIndexType>
76  void writeCommPatternToDisk(op::utility::CommPattern<VectorIndexType> & comm_pattern, int local_rank, std::string comm_pattern_string = "pattern")
77  {
78  std::stringstream comm_pattern_file_name;
79  comm_pattern_file_name << comm_pattern_string << "." << local_rank;
80  std::ofstream comm_pattern_file(comm_pattern_file_name.str());
81 
82  auto rank_comm = comm_pattern.rank_communication;
83 
84  comm_pattern_file << "rank communication:" << std::endl;
85  comm_pattern_file << "send:" << std::endl;
86  for (auto [rank, values] : rank_comm.send) {
87  comm_pattern_file << rank << std::endl << " : " << std::endl;
88  for (auto v : values) {
89  comm_pattern_file << v << std::endl;
90  }
91  comm_pattern_file << std::endl;
92  }
93 
94  comm_pattern_file << "recv:" << std::endl;
95  for (auto [rank, values] : rank_comm.recv) {
96  comm_pattern_file << rank << std::endl << " : " << std::endl;
97  for (auto v : values) {
98  comm_pattern_file << v << std::endl;
99  }
100  comm_pattern_file << std::endl;
101  }
102 
103  comm_pattern_file << "owned_variable_list:" << std::endl;
104  for (auto v : comm_pattern.owned_variable_list) {
105  comm_pattern_file << v << std::endl;
106  }
107  comm_pattern_file << std::endl;
108 
109  comm_pattern_file << "local_variable_list:" << std::endl;
110  for (auto v : comm_pattern.local_variable_list) {
111  comm_pattern_file << v << std::endl;
112  }
113  comm_pattern_file << std::endl;
114  comm_pattern_file.close();
115  }
116 
125  template <typename VectorIndexType>
126  op::utility::CommPattern<VectorIndexType> readCommPatternFromDisk(int local_rank, std::string comm_pattern_string = "pattern")
127  {
129 
130  std::stringstream comm_pattern_file_name;
131  comm_pattern_file_name << comm_pattern_string << "." << local_rank;
132  std::ifstream comm_pattern_file(comm_pattern_file_name.str());
133 
134  auto rank_comm = comm_pattern.rank_communication;
135 
136  std::string buffer;
137 
138  // rank communication:
139  std::getline(comm_pattern_file, buffer);
140  buffer.clear();
141  // Check if send map exists
142  std::getline(comm_pattern_file, buffer);
143 
144  if (buffer == "send:") {
145  // Read in send map
146  while(comm_pattern_file.good()) {
147  buffer.clear();
148  std::getline(comm_pattern_file, buffer);
149  if (buffer == "recv:") break;
150  int rank = std::stoi(buffer);
151  // skip " : "
152  std::getline(comm_pattern_file, buffer);
153  buffer.clear();
154  std::getline(comm_pattern_file, buffer);
155  // read until we reach \n \n
156  VectorIndexType indices;
157  while (comm_pattern_file.good() && buffer.length() > 0) {
158  std::size_t index = std::stoul(buffer);
159  indices.push_back(index);
160  buffer.clear();
161  std::getline(comm_pattern_file, buffer);
162  }
163  // add to map
164  comm_pattern.rank_communication.send[rank] = indices;
165  }
166  }
167 
168  if (buffer == "recv:") {
169  // Read in recv map
170  while(comm_pattern_file.good()) {
171  buffer.clear();
172  std::getline(comm_pattern_file, buffer);
173  if (buffer == "owned_variable_list:") break;
174  int rank = std::stoi(buffer);
175  // skip " : "
176  std::getline(comm_pattern_file, buffer);
177  buffer.clear();
178  std::getline(comm_pattern_file, buffer);
179  // read until we reach \n \n
180  VectorIndexType indices;
181  while (comm_pattern_file.good() && buffer.length() > 0) {
182  std::size_t index = std::stoul(buffer);
183  indices.push_back(index);
184  buffer.clear();
185  std::getline(comm_pattern_file, buffer);
186  }
187  // add to map
188  comm_pattern.rank_communication.recv[rank] = indices;
189  }
190  }
191 
192  // Read in owned_variable_list
193  buffer.clear();
194  std::getline(comm_pattern_file, buffer);
195  while(comm_pattern_file.good() && buffer.length() > 0) {
196  if constexpr (std::is_same_v<typename VectorIndexType::value_type, int>) {
197  comm_pattern.owned_variable_list.push_back(std::stoi(buffer));
198  } else if constexpr(std::is_same_v<typename VectorIndexType::value_type, std::size_t>){
199  comm_pattern.owned_variable_list.push_back(std::stoul(buffer));
200  }
201  buffer.clear();
202  std::getline(comm_pattern_file, buffer);
203  }
204 
205  // Read in local_variable_list
206  buffer.clear();
207  std::getline(comm_pattern_file, buffer); // skip local_variable_list: string
208  buffer.clear();
209  std::getline(comm_pattern_file, buffer);
210  while(comm_pattern_file.good() && buffer.length() > 0) {
211  if constexpr (std::is_same_v<typename VectorIndexType::value_type, int>) {
212  comm_pattern.local_variable_list.push_back(std::stoi(buffer));
213  } else if constexpr(std::is_same_v<typename VectorIndexType::value_type, std::size_t>){
214  comm_pattern.local_variable_list.push_back(std::stoul(buffer));
215  }
216  buffer.clear();
217  std::getline(comm_pattern_file, buffer);
218  }
219 
220  comm_pattern_file.close();
221  return comm_pattern;
222  }
223 
224  } // debug namespace
225 } // op namespace
Abstracted Optimization Vector container.
Definition: op.hpp:80
VectorType & data()
Get the underlying data.
Definition: op.hpp:96
void writeCommPatternToDisk(op::utility::CommPattern< VectorIndexType > &comm_pattern, int local_rank, std::string comm_pattern_string="pattern")
Write comm_pattern to disk. Files are named comm_pattern_string.rank.
Definition: op_debug.hpp:76
Complete Op communication pattern information.
Definition: op_utility.hpp:27
void writeVectorToDisk(const std::vector< VectorType > &vector, int local_rank, std::string local_vector_string)
Write vector to disk. Files are named vector_string.rank.
Definition: op_debug.hpp:21
void writeVariablesToDisk(const op::Vector< VectorType > &op_variables, int local_rank, std::string local_variable_string="local_variables")
Write local variables to disk. Files are named local_variable_string.rank.
Definition: op_debug.hpp:41
void readVectorFromDisk(std::vector< VectorType > &vector, int local_rank, std::string local_vector_string)
Read vector to disk. Files are named vector_string.rank.
Definition: op_debug.hpp:54
op::utility::CommPattern< VectorIndexType > readCommPatternFromDisk(int local_rank, std::string comm_pattern_string="pattern")
Read comm_pattern to disk. Files are named comm_pattern_string.rank.
Definition: op_debug.hpp:126