33// DATE : 2008-11-18
44// ==========================================================
55#include " memory.h"
6+ #include " global_variable.h"
7+ #include " src_parallel/parallel_reduce.h"
68
79namespace ModuleBase
810{
@@ -18,7 +20,7 @@ int Memory::bool_memory = sizeof(bool); // 1.0 Byte
1820int Memory::float_memory = sizeof (float ); // 4.0 Byte
1921int Memory::short_memory = sizeof (short ); // 2.0 Byte
2022
21- int Memory::n_memory = 500 ;
23+ int Memory::n_memory = 1000 ;
2224int Memory::n_now = 0 ;
2325bool Memory::init_flag = false ;
2426
@@ -140,10 +142,75 @@ double Memory::record
140142 return consume[find];
141143}
142144
145+ void Memory::record
146+ (
147+ const std::string &name_in,
148+ const size_t &n_in,
149+ const bool accumulate
150+ )
151+ {
152+ if (!Memory::init_flag)
153+ {
154+ name = new std::string[n_memory];
155+ class_name = new std::string[n_memory];
156+ consume = new double [n_memory];
157+ for (int i=0 ;i<n_memory;i++)
158+ {
159+ consume[i] = 0.0 ;
160+ }
161+ Memory::init_flag = true ;
162+ }
163+
164+ int find = 0 ;
165+ for (find = 0 ; find < n_now; find++)
166+ {
167+ if ( name_in == name[find] )
168+ {
169+ break ;
170+ }
171+ }
172+
173+ // find == n_now : found a new record.
174+ if (find == n_now)
175+ {
176+ n_now++;
177+ name[find] = name_in;
178+ class_name[find] = " " ;
179+ }
180+ if (n_now >= n_memory)
181+ {
182+ std::cout<<" Error! Too many memories has been recorded." ;
183+ return ;
184+ }
185+
186+ const double factor = 1.0 /1024.0 /1024.0 ;
187+ double size_mb = n_in * factor;
188+
189+ if (accumulate)
190+ {
191+ consume[find] += size_mb;
192+ Memory::total += size_mb;
193+ }
194+ else
195+ {
196+ if (consume[find] < size_mb)
197+ {
198+ Memory::total += size_mb - consume[find];
199+ consume[find] = size_mb;
200+ if (consume[find] > 5 )
201+ {
202+ print (find);
203+ }
204+ }
205+ }
206+
207+ return ;
208+ }
209+
143210void Memory::print (const int find)
144211{
145- // std::cout <<"\n Warning_Memory_Consuming : "
146- // <<class_name[find] <<" "<<name[find]<<" "<<consume[find]<<" MB" << std::endl;
212+ GlobalV::ofs_running <<" \n Warning_Memory_Consuming allocated : "
213+ <<" " <<name[find]<<" " <<consume[find]<<" MB" << std::endl;
147214 return ;
148215}
149216
@@ -167,10 +234,12 @@ void Memory::print_all(std::ofstream &ofs)
167234 if (!init_flag) return ;
168235
169236 const double small = 1.0 ;
170- // std::cout<<"\n CLASS_NAME---------|NAME---------------|MEMORY(MB)--------";
171- ofs <<" \n CLASS_NAME---------|NAME---------------|MEMORY(MB)--------" << std::endl;
237+ #ifdef __MPI
238+ Parallel_Reduce::reduce_double_all (Memory::total);
239+ #endif
240+ ofs <<" \n NAME---------------|MEMORY(MB)--------" << std::endl;
172241// std::cout<<"\n"<<std::setw(41)<< " " <<std::setprecision(4)<<total;
173- ofs <<std::setw (41 )<< " " <<std::setprecision (4 )<<total << std::endl;
242+ ofs <<std::setw (20 )<< " total " << std::setw ( 15 ) << std:: setprecision (4 )<< Memory:: total << std::endl;
174243
175244 bool *print_flag = new bool [n_memory];
176245 for (int i=0 ; i<n_memory; i++) print_flag[i] = false ;
@@ -192,16 +261,16 @@ void Memory::print_all(std::ofstream &ofs)
192261 }
193262 }
194263 print_flag[k] = true ;
195-
264+ #ifdef __MPI
265+ Parallel_Reduce::reduce_double_all (consume[k]);
266+ #endif
196267 if ( consume[k] < small )
197268 {
198269 continue ;
199270 }
200271 else
201272 {
202- ofs << " "
203- << std::setw (20 ) << class_name[k]
204- << std::setw (20 ) << name[k]
273+ ofs << std::setw (20 ) << name[k]
205274 << std::setw (15 ) << consume[k] << std::endl;
206275
207276// std::cout << "\n "
@@ -211,6 +280,7 @@ void Memory::print_all(std::ofstream &ofs)
211280 }
212281 }
213282// std::cout<<"\n ----------------------------------------------------------"<<std::endl;
283+ ofs<<" ------------- < 1.0 MB has been ignored ----------------" <<std::endl;
214284 ofs<<" ----------------------------------------------------------" <<std::endl;
215285 delete[] print_flag; // mohan fix by valgrind at 2012-04-02
216286 return ;
0 commit comments