forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_file.cpp
More file actions
406 lines (373 loc) · 10.8 KB
/
global_file.cpp
File metadata and controls
406 lines (373 loc) · 10.8 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
//==========================================================
// AUTHOR : mohan
// DATE : 2008-11-10
//==========================================================
#include "global_file.h"
#ifdef __MPI
#include <mpi.h>
#endif
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cerrno>
#include <sstream>
#include "global_function.h"
#include "global_variable.h"
#include "source_base/parallel_common.h"
#include "source_base/parallel_reduce.h"
//----------------------------------------------------------
// EXPLAIN : Be Called in input.cpp
//----------------------------------------------------------
namespace ModuleBase
{
void ModuleBase::Global_File::make_dir_out(
const std::string &suffix,
const std::string &calculation,
const bool &out_dir,
const bool &out_wfc_dir,
const int rank,
const bool &restart,
const bool out_alllog,
const std::string &global_out_dir,
const std::string &global_stru_dir,
const std::string &global_matrix_dir,
const std::string &global_wfc_dir,
const std::string &global_mlkedf_descriptor_dir,
const std::string &global_deepks_label_elec_dir,
const std::string &log_file,
const bool of_ml_gene_data,
const bool deepks_out_freq_elec)
{
//----------------------------------------------------------
// USE STL FUNCTION
// NAME : system
//----------------------------------------------------------
#ifdef __MPI
MPI_Barrier(MPI_COMM_WORLD);
#endif
int make_dir = 0;
// mohan update 2011-05-03
int times = 0;
while(times<GlobalV::NPROC)
{
if(rank==times)
{
int ret = mkdir(global_out_dir.c_str(), 0755);
if ( ret == 0 || errno == EEXIST )
{
std::cout << " MAKE THE DIR : " << global_out_dir << std::endl;
make_dir = 1;
}
else
{
std::cout << " PROC " << rank << " Please delete the file named by OUT.suffix !!! " << std::endl;
make_dir = 0;
}
}
#ifdef __MPI
Parallel_Reduce::reduce_all(make_dir);
#endif
if(make_dir>0) {break;
}
++times;
}
#ifdef __MPI
if(make_dir==0)
{
std::cout << " CAN NOT MAKE THE OUT DIR......." << std::endl;
ModuleBase::QUIT();
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
if(calculation == "md")
{
int make_dir_stru = 0;
times = 0;
while(times<GlobalV::NPROC)
{
if(rank==times)
{
int ret = mkdir(global_stru_dir.c_str(), 0755);
if ( ret == 0 || errno == EEXIST )
{
std::cout << " MAKE THE STRU DIR : " << global_stru_dir << std::endl;
make_dir_stru = 1;
}
else
{
std::cout << " PROC " << rank << " CAN NOT MAKE THE STRU DIR !!! " << std::endl;
make_dir_stru = 0;
}
}
#ifdef __MPI
Parallel_Reduce::reduce_all(make_dir_stru);
#endif
if(make_dir_stru>0) { break;
}
++times;
}
#ifdef __MPI
if(make_dir_stru==0)
{
std::cout << " CAN NOT MAKE THE STRU DIR......." << std::endl;
ModuleBase::QUIT();
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
// make dir for HS matrix output in md calculation
if((out_dir) && calculation == "md")
{
int make_dir_matrix = 0;
times = 0;
while(times<GlobalV::NPROC)
{
if(rank==times)
{
int ret = mkdir(global_matrix_dir.c_str(), 0755);
if ( ret == 0 || errno == EEXIST )
{
std::cout << " MAKE THE MATRIX DIR : " << global_matrix_dir << std::endl;
make_dir_matrix = 1;
}
else
{
std::cout << " PROC " << rank << " CAN NOT MAKE THE MATRIX DIR !!! " << std::endl;
make_dir_matrix = 0;
}
}
#ifdef __MPI
Parallel_Reduce::reduce_all(make_dir_matrix);
#endif
if(make_dir_matrix>0) { break;
}
++times;
}
#ifdef __MPI
if(make_dir_matrix==0)
{
std::cout << " CAN NOT MAKE THE MATRIX DIR......." << std::endl;
ModuleBase::QUIT();
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
if(out_wfc_dir)
{
int make_dir_wfc = 0;
times = 0;
while(times<GlobalV::NPROC)
{
if(rank==times)
{
int ret = mkdir(global_wfc_dir.c_str(), 0755);
if ( ret == 0 || errno == EEXIST )
{
std::cout << " MAKE THE WFC DIR : " << global_wfc_dir << std::endl;
make_dir_wfc = 1;
}
else
{
std::cout << " PROC " << rank << " CAN NOT MAKE THE WFC DIR !!! " << std::endl;
make_dir_wfc = 0;
}
}
#ifdef __MPI
Parallel_Reduce::reduce_all(make_dir_wfc);
#endif
if(make_dir_wfc>0) { break;
}
++times;
}
#ifdef __MPI
if(make_dir_wfc==0)
{
std::cout << " CAN NOT MAKE THE WFC DIR......." << std::endl;
ModuleBase::QUIT();
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
if(of_ml_gene_data)
{
int make_dir_descrip = 0;
times = 0;
while(times<GlobalV::NPROC)
{
if(rank==times)
{
int ret = mkdir(global_mlkedf_descriptor_dir.c_str(), 0755);
if ( ret == 0 || errno == EEXIST )
{
std::cout << " MAKE THE MLKEDF DESCRIPTOR DIR : " << global_mlkedf_descriptor_dir << std::endl;
make_dir_descrip = 1;
}
else
{
std::cout << " PROC " << rank << " CAN NOT MAKE THE MLKEDF DESCRIPTOR DIR !!! " << std::endl;
make_dir_descrip = 0;
}
}
#ifdef __MPI
Parallel_Reduce::reduce_all(make_dir_descrip);
#endif
if(make_dir_descrip > 0)
{
break;
}
++times;
}
#ifdef __MPI
if(make_dir_descrip == 0)
{
std::cout << " CAN NOT MAKE THE MLKEDF DESCRIPTOR DIR......." << std::endl;
ModuleBase::QUIT();
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
if(deepks_out_freq_elec)
{
int make_dir_deepks_elec = 0;
times = 0;
while(times<GlobalV::NPROC)
{
if(rank==times)
{
int ret = mkdir(global_deepks_label_elec_dir.c_str(), 0755);
if ( ret == 0 || errno == EEXIST )
{
std::cout << " MAKE THE DEEPKS LABELS (ELEC) DIR : " << global_deepks_label_elec_dir << std::endl;
make_dir_deepks_elec = 1;
}
else
{
std::cout << " PROC " << rank << " CAN NOT MAKE THE DEEPKS LABELS (ELEC) DIR !!! " << std::endl;
make_dir_deepks_elec = 0;
}
}
#ifdef __MPI
Parallel_Reduce::reduce_all(make_dir_deepks_elec);
#endif
if(make_dir_deepks_elec > 0)
{
break;
}
++times;
}
#ifdef __MPI
if(make_dir_deepks_elec == 0)
{
std::cout << " CAN NOT MAKE THE DEEPKS LABELS (ELEC) DIR......." << std::endl;
ModuleBase::QUIT();
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
// mohan add 2010-09-12
if(out_alllog)
{
open_log(GlobalV::ofs_running, log_file, calculation, restart, global_out_dir);
#if defined(__CUDA) || defined(__ROCM)
open_log(GlobalV::ofs_device, "device" + std::to_string(rank) + ".log", calculation, restart, global_out_dir);
#endif
}
else
{
if(rank==0)
{
open_log(GlobalV::ofs_running, log_file, calculation, restart, global_out_dir);
#if defined(__CUDA) || defined(__ROCM)
open_log(GlobalV::ofs_device, "device.log", calculation, restart, global_out_dir);
#endif
}
}
if(rank==0)
{
open_log(GlobalV::ofs_warning, "warning.log", calculation, restart, global_out_dir);
}
#ifdef GATHER_INFO
open_log(GlobalV::ofs_info, "math_info_" + std::to_string(rank) + ".log", calculation, restart, global_out_dir);
#endif
return;
}
void ModuleBase::Global_File::make_dir_atom(const std::string &label, const std::string &global_out_dir)
{
//----------------------------------------------------------
// EXPLAIN : generate atom dir for each type of atom
//----------------------------------------------------------
std::stringstream ss;
ss << global_out_dir << label << "/";
ModuleBase::GlobalFunc::MAKE_DIR(ss.str());
return;
}
void ModuleBase::Global_File::open_log(std::ofstream &ofs, const std::string &fn, const std::string &calculation, const bool &restart, const std::string &global_out_dir)
{
//----------------------------------------------------------
// USE GLOBAL VARIABLE :
// global_out_dir : (default dir to store "*.log" file)
//----------------------------------------------------------
std::stringstream ss;
ss << global_out_dir << fn;
if(calculation == "md" && restart)
{
ofs.open(ss.str(), std::ios::app);
}
else
{
ofs.open( ss.str() );
}
// ofs << " WELCOME TO MESIA PROGRAM." << std::endl;
// ofs << " OPEN "<<fn<<".log"<<" DONE."<<std::endl;
return;
}
void ModuleBase::Global_File::close_log( std::ofstream &ofs,const std::string &fn)
{
if(ofs)
{
ofs.close();
}
//ofs << "CLOSE "<<fn<<".log"<<" DONE."<<std::endl;
return;
}
void ModuleBase::Global_File::close_all_log(const int rank, const bool out_alllog,const std::string &calculation)
{
//----------------------------------------------------------
// USE GLOBAL VARIABLES :
// NAME : GlobalV::ofs_running
// NAME : GlobalV::ofs_warning
// NAME : ofs_recon
// NAME : ofs_sph_proj
// NAME : ofs_build
//----------------------------------------------------------
// mohan update 2011-01-13
std::stringstream ss;
if(out_alllog)
{
ss << "running_" << calculation << "_cpu" << rank << ".log";
close_log(GlobalV::ofs_running,ss.str());
#if defined(__CUDA) || defined(__ROCM)
close_log(GlobalV::ofs_device, "device" + std::to_string(rank));
#endif
}
else
{
if(rank==0)
{
ss << "running_" << calculation << ".log";
close_log(GlobalV::ofs_running,ss.str());
#if defined(__CUDA) || defined(__ROCM)
close_log(GlobalV::ofs_device, "device");
#endif
}
}
if (rank==0)
{
close_log(GlobalV::ofs_warning, "warning.log");
}
#ifdef GATHER_INFO
close_log(GlobalV::ofs_info, "math_info");
#endif
return;
}
}