forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_function.cpp
More file actions
266 lines (245 loc) · 7.88 KB
/
global_function.cpp
File metadata and controls
266 lines (245 loc) · 7.88 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
//==========================================================
// AUTHOR : mohan
//==========================================================
#include "global_function.h"
#include "source_base/parallel_common.h"
#include "global_file.h"
//==========================================================
// USE FILE timer.h
// ONLY : output time after quit.
//==========================================================
#include "memory.h"
#include "timer.h"
#include <fstream>
#include <iostream>
#include <string>
namespace ModuleBase
{
namespace GlobalFunc
{
void NOTE(const std::string &words)
{
return;
if (GlobalV::ofs_running)
{
// GlobalV::ofs_running << " *********************************************************************************"
// << std::endl;
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
<< std::endl;
GlobalV::ofs_running << " " << words << std::endl;
GlobalV::ofs_running << " !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
<< std::endl;
}
}
void NEW_PART(const std::string &words)
{
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><" << std::endl;
GlobalV::ofs_running << "\n " << words << std::endl;
GlobalV::ofs_running << "\n ><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><\n" << std::endl;
return;
}
//==========================================================
// GLOBAL FUNCTION :
// NAME : OUT( output date for checking )
//==========================================================
void OUT(std::ofstream &ofs, const std::string &name)
{
ofs << "\n" << std::setw(18) << name << std::endl;
return;
}
//==========================================================
// GLOBAL FUNCTION :
// NAME : MAKE_DIR( make dir ,using system function)
//==========================================================
void MAKE_DIR(const std::string &fn)
{
// ModuleBase::TITLE("global_function","MAKE_DIR");
#ifndef __SW
if (GlobalV::MY_RANK == 0)
{
std::stringstream ss;
ss << " test -d " << fn << " || mkdir " << fn;
//----------------------------------------------------------
// EXPLAIN : 'system' function return '0' if success
//----------------------------------------------------------
if (system(ss.str().c_str()))
{
ModuleBase::WARNING_QUIT("MAKE_DIR", fn);
}
}
#endif
return;
}
void DONE(std::ofstream &ofs, const std::string &description, const bool only_rank0)
{
if (only_rank0)
{
if (GlobalV::MY_RANK == 0)
{
// ofs << " ---------------------------------------------------------------------------------\n";
ofs << " DONE : " << description;
ofs << " Time : " << ModuleBase::timer::print_until_now() << " (SEC)";
ofs << std::endl << std::endl;
// ofs << "\n ---------------------------------------------------------------------------------\n";
}
}
else
{
// ofs << " ---------------------------------------------------------------------------------\n";
ofs << " DONE : " << description;
ofs << " Time : " << ModuleBase::timer::print_until_now() << " (SEC)";
ofs << std::endl << std::endl;
// ofs << "\n ---------------------------------------------------------------------------------\n";
}
// std::cout << "\n---------------------------------------------------------------------------------\n";
std::cout << " DONE(" << std::setw(10) << ModuleBase::timer::print_until_now() << " SEC) : " << description
<< std::endl;
// std::cout << "\n---------------------------------------------------------------------------------\n";
return;
}
bool SCAN_BEGIN(std::ifstream &ifs,
const std::string &TargetName,
const bool restart,
const bool ifwarn)
{
std::string SearchName;
bool find = false;
if (restart)
{
ifs.clear();
ifs.seekg(0);
}
ifs.rdstate();
while (ifs.good())
{
ifs >> SearchName;
if (SearchName == TargetName)
{
find = true;
break;
}
}
if (!find && ifwarn)
{
GlobalV::ofs_warning << " In SCAN_BEGIN, can't find: " << TargetName << " block." << std::endl;
}
return find;
}
bool SCAN_LINE_BEGIN(std::ifstream &ifs,
const std::string &TargetName,
const bool restart,
const bool ifwarn)
{
bool find = false;
if (restart)
{
ifs.clear();
ifs.seekg(0);
}
ifs.rdstate();
std::string line;
while (std::getline(ifs,line))
{
//! obtain the first character, should not be #
size_t first_char_pos = line.find_first_not_of(" \t");
if (first_char_pos != std::string::npos && line[first_char_pos] == '#')
{
continue;
}
//! search in each line
std::istringstream iss(line);
std::string SearchName;
while (iss >> SearchName)
{
if (SearchName == TargetName)
{
find = true;
//std::cout << " search name = " << SearchName << std::endl;
return find;
}
}
}
if (!find && ifwarn)
{
GlobalV::ofs_warning << " In SCAN_LINE_BEGIN, can't find: " << TargetName << " block." << std::endl;
}
return find;
}
void SCAN_END(std::ifstream &ifs, const std::string &TargetName, const bool ifwarn)
{
std::string SearchName;
ifs >> SearchName;
if (SearchName != TargetName && ifwarn)
{
GlobalV::ofs_warning << " In SCAN_END, can't find: " << TargetName << " block." << std::endl;
}
return;
}
void BLOCK_HERE(const std::string &description)
{
// return;
std::cout << "\n********************************************";
std::cout << "\n Here is a Block, 1: go on 0: quit";
std::cout << "\n " << description;
std::cout << "\n********************************************" << std::endl;
bool go_on = false;
if (GlobalV::MY_RANK == 0)
{
std::cin >> go_on;
}
#ifdef __MPI
int swap = go_on;
if (GlobalV::MY_RANK == 0)
swap = go_on;
MPI_Bcast(&swap, 1, MPI_INT, 0, MPI_COMM_WORLD);
if (GlobalV::MY_RANK != 0)
go_on = static_cast<bool>(swap);
#endif
if (go_on)
{
return;
}
else
{
ModuleBase::QUIT();
}
}
void OUT_TIME(const std::string &name, time_t &start, time_t &end)
{
double mini = difftime(end, start) / 60.0;
if (mini > 0.1)
{
if(GlobalV::ofs_warning)
{
GlobalV::ofs_warning << std::setprecision(2);
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
GlobalV::ofs_warning << " NAME < " << name << " > = " << std::endl;
GlobalV::ofs_warning << " -> " << ctime(&start) << " -> " << ctime(&end);
GlobalV::ofs_warning << " TIME = " << mini << " [Minutes]" << std::endl;
GlobalV::ofs_warning << " -------------------------------------------------------" << std::endl;
GlobalV::ofs_warning << std::setprecision(6);
}
}
}
size_t MemAvailable()
{
size_t mem_sum = 0;
int i = 0;
std::ifstream ifs("/proc/meminfo");
while (ifs.good())
{
std::string label, size, kB;
ifs >> label >> size >> kB;
if (label == "MemAvailable:")
return std::stol(size);
else if (label == "MemFree:" || label == "Buffers:" || label == "Cached:")
{
mem_sum += std::stol(size);
++i;
}
if (i == 3) return mem_sum;
}
throw std::runtime_error("read /proc/meminfo error in " + TO_STRING(__FILE__) + " line " + TO_STRING(__LINE__));
}
} // namespace GlobalFunc
} // namespace ModuleBase