forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
502 lines (442 loc) · 9.01 KB
/
memory.cpp
File metadata and controls
502 lines (442 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//==========================================================
// AUTHOR : mohan
// DATE : 2008-11-18
//==========================================================
#include <cassert>
#include "memory.h"
#include "global_variable.h"
#include "source_base/parallel_reduce.h"
namespace ModuleBase
{
// 8 bit = 1 Byte
// 1024 Byte = 1 KB
// 1024 KB = 1 MB
// 1024 MB = 1 GB
double Memory::total = 0.0;
int Memory::complex_matrix_memory = 2*sizeof(double); // 16 byte
int Memory::double_memory = sizeof(double); // 8 byte
int Memory::int_memory = sizeof(int); // 4.0 Byte
int Memory::bool_memory = sizeof(bool); // 1.0 Byte
int Memory::float_memory = sizeof(float); // 4.0 Byte
int Memory::short_memory = sizeof(short); // 2.0 Byte
int Memory::n_memory = 1000;
int Memory::n_now = 0;
bool Memory::init_flag = false;
#if defined(__CUDA) || defined(__ROCM)
double Memory::total_gpu = 0.0;
int Memory::n_now_gpu = 0;
bool Memory::init_flag_gpu = false;
std::string *Memory::name_gpu;
std::string *Memory::class_name_gpu;
double *Memory::consume_gpu;
#endif
std::string *Memory::name;
std::string *Memory::class_name;
double *Memory::consume;
Memory::Memory()
{
}
Memory::~Memory()
{
}
double Memory::calculate_mem(const long &n_in,const std::string &type)
{
double n = static_cast<double>(n_in);
double mem = 0.0;
double factor = 1.0/1024.0/1024.0;
double complex_matrix_mem = complex_matrix_memory * factor;
double double_mem = double_memory * factor;
double int_mem = int_memory * factor;
double bool_mem = bool_memory * factor;
double float_mem = float_memory * factor;
double short_mem = short_memory * factor;
if(type=="ModuleBase::ComplexMatrix" || type=="complexmatrix" || type=="cdouble")
{
mem = complex_matrix_mem;
}
else if(type=="real" || type=="double")
{
mem = double_mem;
}
else if(type=="int")
{
mem = int_mem;
}
else if(type=="bool")
{
mem = bool_mem;
}
else if(type=="short")
{
mem = short_mem;
}
else if(type=="float")
{
mem = float_mem;
}
else if(type=="AtomLink")
{
mem = int_mem * 2 + double_mem * 3;
}
else if(type=="ModuleBase::Vector3<double>")
{
mem = 3 * double_mem;
}
else
{
std::cout<<"not this type in memory storage : "<<type << std::endl;
}
total += n * mem;
return n*mem;
}
double Memory::record
(
const std::string &class_name_in,
const std::string &name_in,
const long &n_in,
const std::string &type,
const bool accumulate
)
{
if(!Memory::init_flag)
{
name = new std::string[n_memory];
class_name = new std::string[n_memory];
consume = new double[n_memory];
for(int i=0;i<n_memory;i++)
{
consume[i] = 0.0;
}
Memory::init_flag = true;
}
int find = 0;
for(find = 0; find < n_now; find++)
{
if( name_in == name[find] )
{
break;
}
}
// find == n_now : found a new record.
if(find == n_now)
{
n_now++;
name[find] = name_in;
class_name[find] = class_name_in;
}
if(n_now >= n_memory)
{
std::cout<<" Error! Too many memories required.";
return 0.0;
}
consume[find] = Memory::calculate_mem(n_in,type);
if(consume[find] > 5)
{
print(find);
}
return consume[find];
}
void Memory::record
(
const std::string &name_in,
const size_t &n_in,
const bool accumulate
)
{
if(!Memory::init_flag)
{
name = new std::string[n_memory];
class_name = new std::string[n_memory];
consume = new double[n_memory];
for(int i=0;i<n_memory;i++)
{
consume[i] = 0.0;
}
Memory::init_flag = true;
}
int find = 0;
for(find = 0; find < n_now; find++)
{
if( name_in == name[find] )
{
break;
}
}
// find == n_now : found a new record.
if(find == n_now)
{
n_now++;
name[find] = name_in;
class_name[find] = "";
}
if(n_now >= n_memory)
{
std::cout<<" Error! Too many memories has been recorded.";
return;
}
const double factor = 1.0/1024.0/1024.0;
double size_mb = n_in * factor;
if(accumulate)
{
consume[find] += size_mb;
Memory::total += size_mb;
}
else
{
if(consume[find] < size_mb)
{
Memory::total += size_mb - consume[find];
consume[find] = size_mb;
if(consume[find] > 5)
{
print(find);
}
}
}
return;
}
#if defined(__CUDA) || defined(__ROCM)
double Memory::record_gpu
(
const std::string &class_name_in,
const std::string &name_in,
const long &n_in,
const std::string &type,
const bool accumulate
)
{
if(!Memory::init_flag_gpu)
{
name_gpu = new std::string[n_memory];
class_name_gpu = new std::string[n_memory];
consume_gpu = new double[n_memory];
for(int i=0;i<n_memory;i++)
{
consume_gpu[i] = 0.0;
}
Memory::init_flag_gpu = true;
}
int find = 0;
for(find = 0; find < n_now_gpu; find++)
{
if( name_in == name_gpu[find] )
{
break;
}
}
// find == n_now : found a new record.
if(find == n_now_gpu)
{
n_now_gpu++;
name_gpu[find] = name_in;
class_name_gpu[find] = class_name_in;
}
if(n_now_gpu >= n_memory)
{
std::cout<<" Error! Too many gpu memories required.";
return 0.0;
}
consume_gpu[find] = Memory::calculate_mem(n_in,type);
if(consume_gpu[find] > 5)
{
print(find);
}
return consume_gpu[find];
}
void Memory::record_gpu
(
const std::string &name_in,
const size_t &n_in,
const bool accumulate
)
{
if(!Memory::init_flag_gpu)
{
name_gpu = new std::string[n_memory];
class_name_gpu = new std::string[n_memory];
consume_gpu = new double[n_memory];
for(int i=0;i<n_memory;i++)
{
consume_gpu[i] = 0.0;
}
Memory::init_flag_gpu = true;
}
int find = 0;
for(find = 0; find < n_now_gpu; find++)
{
if( name_in == name_gpu[find] )
{
break;
}
}
// find == n_now : found a new record.
if(find == n_now_gpu)
{
n_now_gpu++;
name_gpu[find] = name_in;
class_name_gpu[find] = "";
}
if(n_now_gpu >= n_memory)
{
std::cout<<" Error! Too many gpu memories has been recorded.";
return;
}
const double factor = 1.0/1024.0/1024.0;
double size_mb = n_in * factor;
if(accumulate)
{
consume_gpu[find] += size_mb;
Memory::total_gpu += size_mb;
}
else
{
if(consume_gpu[find] < size_mb)
{
Memory::total_gpu += size_mb - consume_gpu[find];
consume_gpu[find] = size_mb;
if(consume_gpu[find] > 5)
{
print(find);
}
}
}
return;
}
#endif
void Memory::print(const int find)
{
GlobalV::ofs_running <<"\n Warning_Memory_Consuming allocated: "
<<" "<<name[find]<<" "<<consume[find]<<" MB" << std::endl;
return;
}
void Memory::finish(std::ofstream &ofs)
{
print_all(ofs);
if(init_flag)
{
delete[] name;
delete[] class_name;
delete[] consume;
init_flag = false;
}
#if defined(__CUDA) || defined(__ROCM)
if(init_flag_gpu)
{
delete[] name_gpu;
delete[] class_name_gpu;
delete[] consume_gpu;
init_flag_gpu = false;
}
#endif
return;
}
void Memory::print_all(std::ofstream &ofs)
{
if(!init_flag)
{
return;
}
const double small = 1.0; // unit is MB
#ifdef __MPI
Parallel_Reduce::reduce_all(Memory::total);
#if defined(__CUDA) || defined(__ROCM)
Parallel_Reduce::reduce_all(Memory::total_gpu);
#endif
#endif
ofs <<"\n NAME-------------------------|MEMORY(MB)------------------" << std::endl;
ofs << std::right;
ofs << std::setw(30)<< "total" << std::setw(15) <<std::setprecision(4)<< Memory::total << std::endl;
assert(n_memory>0);
bool *print_flag = new bool[n_memory];
for(int i=0; i<n_memory; i++)
{
print_flag[i] = false;
}
for (int i=0; i<n_memory; i++)
{
#ifdef __MPI
Parallel_Reduce::reduce_all(consume[i]);
#endif
}
for (int i=0; i<n_memory; i++) // Xiaoyang fix memory record sum bug 2023/10/25
{
int k = 0;
double tmp = -1.0;
for(int j=0; j<n_memory; j++)
{
if(print_flag[j])
{
continue;
}
else if(tmp < consume[j])
{
k = j;
tmp = consume[j];
}
}
print_flag[k] = true;
if ( consume[k] < small )
{
continue;
}
else
{
ofs << std::setw(30) << name[k]
<< std::setw(15) << consume[k] << std::endl;
}
}
#if defined(__CUDA) || defined(__ROCM)
if(!init_flag_gpu)
{
delete[] print_flag;
return;
}
ofs <<"\n NAME-------------------------|GPU MEMORY(MB)--------------" << std::endl;
ofs <<std::setw(30)<< "total" << std::setw(15) <<std::setprecision(4)<< Memory::total_gpu << std::endl;
assert(n_memory>0);
bool *print_flag_gpu = new bool[n_memory];
for(int i=0; i<n_memory; i++)
{
print_flag_gpu[i] = false;
}
for (int i=0; i<n_memory; i++)
{
#ifdef __MPI
Parallel_Reduce::reduce_all(consume_gpu[i]);
#endif
}
for (int i=0; i<n_memory; i++) // Xiaoyang fix memory record sum bug 2023/10/25
{
int k = 0;
double tmp = -1.0;
for(int j=0; j<n_memory; j++)
{
if(print_flag_gpu[j])
{
continue;
}
else if(tmp < consume_gpu[j])
{
k = j;
tmp = consume_gpu[j];
}
}
print_flag_gpu[k] = true;
if ( consume_gpu[k] < small )
{
continue;
}
else
{
ofs << std::setw(30) << name_gpu[k]
<< std::setw(15) << consume_gpu[k] << std::endl;
}
}
delete[] print_flag_gpu;
#endif
ofs<<" ------------- < 1.0 MB has been ignored ----------------"<<std::endl;
ofs<<" ----------------------------------------------------------"<<std::endl;
delete[] print_flag; //mohan fix by valgrind at 2012-04-02
return;
}
}