1 #ifndef PROTO_MEMINFO_H 2 #define PROTO_MEMINFO_H 9 #include <sys/resource.h> 48 if(m_maxSize < a_currentSize)
49 m_maxSize = a_currentSize;
50 pairType tracePoint = std::make_pair (a_currentSize, m_maxSize);
51 m_info.push_back(tracePoint);
62 for(
auto &it : m_info)
64 file << it.first <<
" " << it.second << std::endl;
83 template<MemType MEMTYPE=MEMTYPE_DEFAULT>
103 std::pair<std::string,unsigned int> position = std::make_pair(file,line);
104 auto elem = m_info.find(position);
105 if(elem != m_info.end())
107 auto& item = elem->second;
108 double& mem = item.dataSize;
109 long unsigned int&
count = item.count;
118 m_info[position] = item;
132 auto elem = m_info.find(a_position);
133 if(elem != m_info.end())
135 auto& item = elem->second;
140 m_info[a_position] = a_elem;
154 protoMemGetInfo(&tmp_free,&tmp_tot);
155 assert(tmp_tot > tmp_free);
156 size_t tmp_current_size = tmp_tot - tmp_free;
157 m_trace.addElem(tmp_current_size);
167 int who = RUSAGE_SELF;
169 getrusage(who, &usage);
170 size_t tmp_current_size = usage.ru_maxrss * 1000;
171 m_trace.addElem(tmp_current_size);
180 std::ifstream ifile(filename);
181 return !(ifile.fail());
197 std::cout <<
" print mem Info " << std::endl << std::endl;
198 for(
auto& it : m_info)
200 auto& position = it.first;
201 std::string file = position.first;
202 unsigned int line = position.second;
203 double dataSize = it.second.dataSize;
204 long unsigned int count = it.second.count;
205 std::cout <<
" In " << file <<
" line " << line <<
" we allocated " 206 << dataSize * 1E-9 <<
" GB, in " << count <<
" allocation(s)" 211 std::string baseline =
"trace-rank-" + std::to_string(rank);
213 std::string baseline =
"trace";
215 std::string filename = baseline +
".txt";
217 while(fexists(filename))
219 filename = baseline +
"-" + std::to_string(i) +
".txt";
222 std::cout <<
" write memory trace in " << filename << std::endl << std::endl;
223 std::ofstream file(filename);
224 m_trace.writeFile(file);
239 void mpiCollectInfo()
243 if(size == 1)
return;
245 std::vector<int> info_sizes;
248 info_sizes.resize(size);
250 size_t mySize = m_info.size();
258 Proto_MPI<void>::comm);
263 std::vector<std::pair<std::pair<std::string,unsigned int>,
describeInfo>> toSend;
266 auto item = std::make_pair(it.first,it.second);
267 toSend.push_back(item);
269 size_t nBytes =
sizeof(std::pair<std::pair<std::string,unsigned int>,
describeInfo>)*mySize;
276 Proto_MPI<void>::comm);
280 for(
size_t i = 0 ; i < size ; i++) std::cout <<
" rank: " << rank <<
" size "<< info_sizes[i] << std::endl;
282 for(
size_t otherRank = 1 ; otherRank < size ; otherRank++)
284 std::vector<std::pair<std::pair<std::string,unsigned int>,
describeInfo>> toRecv;
285 size_t otherSize = info_sizes[otherRank];
286 toRecv.resize(otherSize);
287 size_t nBytes =
sizeof(std::pair<std::pair<std::string,unsigned int>,
describeInfo>)*otherSize;
299 for(
auto it : toRecv)
301 enterInfos(it.first,it.second);
321 static void PRINT_MEMORY_INFO()
323 #ifdef memoryFootPrint 325 std::cout <<
" -------------- device ----------- " << std::endl << std::endl;
328 std::cout <<
" --------------- host ------------ " << std::endl << std::endl;
333 std::cout <<
" The memory footprint option is not activated. flag: -DmemoryFootPrint (performance overhead if activated)" << std::endl;
340 #ifdef memoryFootPrint 342 #define countMallocDevice(cond) \ 345 Proto::memInfo<Proto::DEVICE>& tmpInfo = Proto::memInfo<Proto::DEVICE>::getMemInfo();\ 346 tmpInfo.addTracePoint();\ 349 #define countMallocHost(cond) \ 352 Proto::memInfo<Proto::HOST>& tmpInfo = Proto::memInfo<Proto::HOST>::getMemInfo();\ 353 tmpInfo.addTracePointHost();\ 356 #define storeMemInfo(MEM,B) \ 359 using Proto::DEVICE;\ 360 Proto::memInfo<MEM>& tmpInfo = Proto::memInfo<MEM>::getMemInfo();\ 361 tmpInfo.enterInfo(__FILE__,__LINE__,B);\ 364 #define countMallocDevice(cond) cond 365 #define countMallocHost(cond) cond 366 #define storeMemInfo(MEMTYPE,BYTES) 368 #endif //PROTO_MEMINFO_H describeInfo & operator+=(describeInfo &in)
describe operator+
Definition: Proto_MemInfo.H:24
std::vector< pairType > m_info
store a vector of current and maximum memory usage
Definition: Proto_MemInfo.H:79
traceInfo m_trace
store the current and maximum memory footprint for each malloc
Definition: Proto_MemInfo.H:311
unsigned int numProc()
Get Number of Ranks.
Definition: Proto_SPMD.H:59
long unsigned int count
number of malloc for a given position
Definition: Proto_MemInfo.H:32
size_t size()
accessor function
Definition: Proto_MemInfo.H:72
constexpr int line
Definition: Proto_Reduction.H:18
void enterInfo(std::string file, unsigned int line, unsigned long int dataSize)
insert function
Definition: Proto_MemInfo.H:101
double dataSize
size of accumulated memory in bytes
Definition: Proto_MemInfo.H:31
void writeFile(std::ofstream &file)
output function
Definition: Proto_MemInfo.H:60
void addElem(size_t a_currentSize)
insert function
Definition: Proto_MemInfo.H:46
int procID()
Get Local Process ID.
Definition: Proto_SPMD.H:39
void addTracePoint()
insert function
Definition: Proto_MemInfo.H:149
memInfo()
constructor
Definition: Proto_MemInfo.H:91
void printInfo()
Definition: Proto_MemInfo.H:188
void addTracePointHost()
insert function
Definition: Proto_MemInfo.H:165
Definition: Proto_MemInfo.H:84
std::map< std::pair< std::string, unsigned int >, describeInfo > m_info
accumulate data size allocations for each malloc position in the code
Definition: Proto_MemInfo.H:310
std::pair< size_t, size_t > pairType
Definition: Proto_MemInfo.H:39
Definition: Proto_Array.H:17
static memInfo< MEMTYPE > & getMemInfo()
Definition: Proto_MemInfo.H:230
Definition: Proto_MemInfo.H:16
bool fexists(std::string filename)
check function
Definition: Proto_MemInfo.H:178
void enterInfos(std::pair< std::string, unsigned int > a_position, describeInfo &a_elem)
insert function
Definition: Proto_MemInfo.H:130
Definition: Proto_MemInfo.H:36