1 #ifndef PROTO_MEMINFO_H 2 #define PROTO_MEMINFO_H 8 #include<Proto_MemType.H> 10 #include <sys/resource.h> 37 typedef std::pair<size_t,size_t> pairType;
46 if(m_maxSize < a_currentSize)
47 m_maxSize = a_currentSize;
48 pairType tracePoint = std::make_pair (a_currentSize, m_maxSize);
49 m_info.push_back(tracePoint);
60 for(
auto &it : m_info)
62 file << it.first <<
" " << it.second << std::endl;
77 std::vector<pairType> m_info;
81 template<MemType MEMTYPE=MEMTYPE_DEFAULT>
101 std::pair<std::string,unsigned int> position = std::make_pair(file,line);
102 auto elem = m_info.find(position);
103 if(elem != m_info.end())
105 auto& item = elem->second;
106 double& mem = item.dataSize;
107 long unsigned int&
count = item.count;
116 m_info[position] = item;
130 auto elem = m_info.find(a_position);
131 if(elem != m_info.end())
133 auto& item = elem->second;
138 m_info[a_position] = a_elem;
152 protoMemGetInfo(&tmp_free,&tmp_tot);
153 assert(tmp_tot > tmp_free);
154 size_t tmp_current_size = tmp_tot - tmp_free;
155 m_trace.addElem(tmp_current_size);
165 int who = RUSAGE_SELF;
167 getrusage(who, &usage);
168 size_t tmp_current_size = usage.ru_maxrss * 1000;
169 m_trace.addElem(tmp_current_size);
178 std::ifstream ifile(filename);
179 return !(ifile.fail());
195 std::cout <<
" print mem Info " << std::endl << std::endl;
196 for(
auto& it : m_info)
198 auto& position = it.first;
199 std::string file = position.first;
200 unsigned int line = position.second;
201 double dataSize = it.second.dataSize;
202 long unsigned int count = it.second.count;
203 std::cout <<
" In " << file <<
" line " << line <<
" we allocated " 204 << dataSize * 1E-9 <<
" GB, in " << count <<
" allocation(s)" 209 std::string baseline =
"trace-rank-" + std::to_string(rank);
211 std::string baseline =
"trace";
213 std::string filename = baseline +
".txt";
215 while(fexists(filename))
217 filename = baseline +
"-" + std::to_string(i) +
".txt";
220 std::cout <<
" write memory trace in " << filename << std::endl << std::endl;
221 std::ofstream file(filename);
222 m_trace.writeFile(file);
237 void mpiCollectInfo()
241 if(size == 1)
return;
243 std::vector<int> info_sizes;
246 info_sizes.resize(size);
248 size_t mySize = m_info.size();
256 Proto_MPI<void>::comm);
261 std::vector<std::pair<std::pair<std::string,unsigned int>,
describeInfo>> toSend;
264 auto item = std::make_pair(it.first,it.second);
265 toSend.push_back(item);
267 size_t nBytes =
sizeof(std::pair<std::pair<std::string,unsigned int>,
describeInfo>)*mySize;
274 Proto_MPI<void>::comm);
278 for(
size_t i = 0 ; i < size ; i++) std::cout <<
" rank: " << rank <<
" size "<< info_sizes[i] << std::endl;
280 for(
size_t otherRank = 1 ; otherRank < size ; otherRank++)
282 std::vector<std::pair<std::pair<std::string,unsigned int>,
describeInfo>> toRecv;
283 size_t otherSize = info_sizes[otherRank];
284 toRecv.resize(otherSize);
285 size_t nBytes =
sizeof(std::pair<std::pair<std::string,unsigned int>,
describeInfo>)*otherSize;
297 for(
auto it : toRecv)
299 enterInfos(it.first,it.second);
308 std::map<std::pair<std::string,unsigned int>,
describeInfo> m_info;
319 static void PRINT_MEMORY_INFO()
321 #ifdef memoryFootPrint 323 std::cout <<
" -------------- device ----------- " << std::endl << std::endl;
326 std::cout <<
" --------------- host ------------ " << std::endl << std::endl;
331 std::cout <<
" The memory footprint option is not activated. flag: -DmemoryFootPrint (performance overhead if activated)" << std::endl;
338 #ifdef memoryFootPrint 340 #define countMallocDevice(cond) \ 343 Proto::memInfo<Proto::DEVICE>& tmpInfo = Proto::memInfo<Proto::DEVICE>::getMemInfo();\ 344 tmpInfo.addTracePoint();\ 347 #define countMallocHost(cond) \ 350 Proto::memInfo<Proto::HOST>& tmpInfo = Proto::memInfo<Proto::HOST>::getMemInfo();\ 351 tmpInfo.addTracePointHost();\ 354 #define storeMemInfo(MEM,B) \ 357 using Proto::DEVICE;\ 358 Proto::memInfo<MEM>& tmpInfo = Proto::memInfo<MEM>::getMemInfo();\ 359 tmpInfo.enterInfo(__FILE__,__LINE__,B);\ 362 #define countMallocDevice(cond) cond 363 #define countMallocHost(cond) cond 364 #define storeMemInfo(MEMTYPE,BYTES) 366 #endif //PROTO_MEMINFO_H describeInfo & operator+=(describeInfo &in)
describe operator+
Definition: Proto_MemInfo.H:22
unsigned int numProc()
number of parallel processes
Definition: Proto_SPMD.H:72
long unsigned int count
number of malloc for a given position
Definition: Proto_MemInfo.H:30
size_t size()
accessor function
Definition: Proto_MemInfo.H:70
void enterInfo(std::string file, unsigned int line, unsigned long int dataSize)
insert function
Definition: Proto_MemInfo.H:99
double dataSize
size of accumulated memory in bytes
Definition: Proto_MemInfo.H:29
void writeFile(std::ofstream &file)
output function
Definition: Proto_MemInfo.H:58
void addElem(size_t a_currentSize)
insert function
Definition: Proto_MemInfo.H:44
int procID()
local process ID
Definition: Proto_SPMD.H:52
void addTracePoint()
insert function
Definition: Proto_MemInfo.H:147
memInfo()
constructor
Definition: Proto_MemInfo.H:89
void printInfo()
Definition: Proto_MemInfo.H:186
void addTracePointHost()
insert function
Definition: Proto_MemInfo.H:163
Definition: Proto_MemInfo.H:82
Definition: Proto_Box.H:11
static memInfo< MEMTYPE > & getMemInfo()
Definition: Proto_MemInfo.H:228
Definition: Proto_MemInfo.H:14
bool fexists(std::string filename)
check function
Definition: Proto_MemInfo.H:176
void enterInfos(std::pair< std::string, unsigned int > a_position, describeInfo &a_elem)
insert function
Definition: Proto_MemInfo.H:128
Definition: Proto_MemInfo.H:34