ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
global_function.h
Go to the documentation of this file.
1#ifndef GLOBAL_FUNCTION_H
2#define GLOBAL_FUNCTION_H
3
5#include "global_function-func_each_2.h" // Peize Lin add 2016-09-07
6#include "global_variable.h"
7#include "tool_check.h" // mohan add 2021-05-08
8#include "tool_quit.h" // mohan add 2021-05-07
9#include "tool_title.h" // mohan add 2021-05-05
10
11#include <cassert>
12#include <complex>
13#include <fstream>
14#include <iomanip>
15#include <iostream>
16#include <sstream>
17#include <string>
18#include <valarray>
19#include <vector>
20
21namespace ModuleBase
22{
23namespace GlobalFunc
24{
25
26void NOTE(const std::string& words);
27void NEW_PART(const std::string& words);
28
29//==========================================================
30// GLOBAL FUNCTION :
31// NAME : OUT( output date for checking )
32// NAME : OUT( output date into file "ofs")
33// NAME : OUTP( output parameters )
34//==========================================================
35void OUT(std::ofstream& ofs, const std::string& name);
36
37template <class T>
38void OUT(std::ofstream& ofs, const std::string& name, const T& a)
39{
40 std::stringstream name2;
41 name2 << name;
42 ofs << " " << std::setw(40) << name2.str() << " = " << a << std::endl;
43 // ofs << " " << name << a << std::endl;
44 return;
45}
46
47template <class T>
48void OUT(std::ofstream& ofs, const std::string& name, const T& x, const T& y)
49{
50 ofs << " " << std::setw(40) << name << " = [ " << x << ", " << y << " ]" << std::endl;
51 // ofs << " " << name << a << std::endl;
52 return;
53}
54
55template <class T>
56void OUT(std::ofstream& ofs, const std::string& name, const T& x, const T& y, const T& z)
57{
58 ofs << " " << std::setw(40) << name << " = [ " << x << ", " << y << ", " << z << " ]" << std::endl;
59 return;
60}
61
62// output parameters and explanations
63template <class T>
64void OUTP(std::ofstream& ofs, const std::string& name, const T& a, const std::string& explanation = "")
65{
66 ofs << std::setw(30) << name << " " << a << " #" << explanation << std::endl;
67}
68
69template <class T>
70void OUT(const std::string& name, const T& a)
71{
72 std::cout << " " << std::setw(40) << name << " = " << a << std::endl;
73 // std::cout << " " << name << a << std::endl;
74 return;
75}
76
77void OUT_TIME(const std::string& name, time_t& start, time_t& end);
78
79//==========================================================
80// GLOBAL FUNCTION :
81// NAME : MAKE_DIR( make dir ,using system function)
82//==========================================================
83void MAKE_DIR(const std::string& file);
84
85//==========================================================
86// GLOBAL FUNCTION :
87// NAME : AUTO_SET( auto_set variables )
88//==========================================================
89template <class T>
90void AUTO_SET(const std::string& name, const T& a)
91{
92 GlobalV::ofs_warning << " AUTO_SET " << name << " to " << a << std::endl;
93 return;
94}
95
96//==========================================================
97// GLOBAL FUNCTION :
98// NAME : DONE( ouput information(time) on screen and log)
99// we can regard it as a milestone.
100//==========================================================
101void DONE(std::ofstream& ofs, const std::string& description, bool only_rank0 = false);
102
103//==========================================================
104// GLOBAL FUNCTION :
105// NAME : ZEROS
106// set elements of u as zero which u is 1_d std::complex array
107//==========================================================
108template <class T, class TI>
109inline void ZEROS(std::complex<T>* u, const TI n) // Peize Lin change int to TI at 2020.03.03
110{
111 assert(n >= 0);
112 for (TI i = 0; i < n; i++)
113 {
114 u[i] = std::complex<T>(0.0, 0.0);
115 }
116 return;
117}
118
119template <class T, class TI>
120inline void ZEROS(T* u, const TI n) // Peize Lin change int to TI at 2020.03.03
121{
122 assert(n >= 0);
123 for (TI i = 0; i < n; i++)
124 {
125 u[i] = 0;
126 }
127}
128
129//==========================================================
130// GLOBAL FUNCTION :
131// NAME : TEST_LEVEL
132// control the test_level
133//==========================================================
134void TEST_LEVEL(const std::string& name, bool disable);
135
136//==========================================================
137// GLOBAL FUNCTION :
138//==========================================================
139template <class T>
140static void READ_VALUE(std::ifstream& ifs, T& v)
141{
142 ifs >> v;
143 std::string line;
144 getline(ifs, line);
145 return;
146}
147
148//-------------------------------------------------------------
151//-------------------------------------------------------------
152bool SCAN_BEGIN(std::ifstream& ifs,
153 const std::string& TargetName,
154 const bool restart = true,
155 const bool ifwarn = true);
156
157//-------------------------------------------------------------
158// The `SCAN_LINE_BEGIN` function efficiently searches
159// text files for specified keywords while ignoring comment
160// lines and whitespace. It skips any line starting with '#'
161//-------------------------------------------------------------
162bool SCAN_LINE_BEGIN(std::ifstream& ifs,
163 const std::string& TargetName,
164 const bool restart = true,
165 const bool ifwarn = true);
166
167void SCAN_END(std::ifstream& ifs, const std::string& TargetName, const bool ifwarn = true);
168
169template <class T>
170static inline void DCOPY(const T& a, T& b, const int& dim)
171{
172 for (int i = 0; i < dim; ++i) {
173 b[i] = a[i];
174 }
175}
176
177template <typename T>
178inline void DCOPY(const T* a, T* b, const int& dim) {
179 for (int i = 0; i < dim; ++i) {
180 b[i] = a[i];
181 }
182}
183
184template <typename T>
185inline void COPYARRAY(const T* a, T* b, const long dim);
186
187template <>
188inline void COPYARRAY(const std::complex<double>* a, std::complex<double>* b, const long dim)
189{
190 const int one = 1;
191 zcopy_(&dim, a, &one, b, &one);
192}
193
194template <>
195inline void COPYARRAY(const double* a, double* b, const long dim)
196{
197 const int one = 1;
198 dcopy_(&dim, a, &one, b, &one);
199}
200
201void BLOCK_HERE(const std::string& description);
202
203//==========================================================
204// GLOBAL FUNCTION :
205// NAME : VECTOR_TO_PTR
206// change std::vector to pointer
207// Peize Lin add 2016-02-25
208//==========================================================
209template <class T>
210static inline T* VECTOR_TO_PTR(std::vector<T>& v)
211{
212 return &(v[0]);
213}
214template <class T>
215static inline T* VECTOR_TO_PTR(std::valarray<T>& v)
216{
217 return &(v[0]);
218}
219
220template <class T>
221static inline const T* VECTOR_TO_PTR(const std::vector<T>& v)
222{
223 return &(v[0]);
224}
225template <class T>
226static inline const T* VECTOR_TO_PTR(const std::valarray<T>& v)
227{
228 return &(v[0]);
229}
230
231//==========================================================
232// GLOBAL FUNCTION :
233// NAME : TO_STRING
234// change number to std::string
235// example: 233 -> "233"
236// Peize Lin add 2016-07-18
237//==========================================================
238template <typename T>
239std::string TO_STRING(const T& t, const int n=20) // n=20 since LDBL_EPSILON is 1E-16 or 1E-19
240{
241 std::stringstream newstr;
242 newstr << std::setprecision(n) << t;
243 return newstr.str();
244}
245
246//==========================================================
247// GLOBAL FUNCTION :
248// NAME : MAP_EXIST
249// find whether exists the std::map index
250// if exist return the ptr called, else return nullptr
251// example: map_exist(ms,i,j,k) -> try to find ms[i][j][k]
252// Peize Lin add 2018-07-16
253//==========================================================
254template <typename T_map, typename T_key1>
255inline void* MAP_EXIST(T_map& ms, const T_key1& key1)
256{
257 auto ms1 = ms.find(key1);
258 if (ms1 == ms.end()) {
259 return nullptr;
260 }
261 return static_cast<void*>(&ms1->second);
262}
263
264template <typename T_map, typename T_key1, typename... T_key_tail>
265inline void* MAP_EXIST(T_map& ms, const T_key1& key1, const T_key_tail&... key_tail)
266{
267 auto ms1 = ms.find(key1);
268 if (ms1 == ms.end()) {
269 return nullptr;
270 }
271 return MAP_EXIST(ms1->second, key_tail...);
272}
273
274template <typename T_map, typename T_key1>
275inline const void* MAP_EXIST(const T_map& ms, const T_key1& key1)
276{
277 auto ms1 = ms.find(key1);
278 if (ms1 == ms.end()) {
279 return nullptr;
280 }
281 return static_cast<const void*>(&ms1->second);
282}
283
284template <typename T_map, typename T_key1, typename... T_key_tail>
285inline const void* MAP_EXIST(const T_map& ms, const T_key1& key1, const T_key_tail&... key_tail)
286{
287 auto ms1 = ms.find(key1);
288 if (ms1 == ms.end()) {
289 return nullptr;
290 }
291 return MAP_EXIST(ms1->second, key_tail...);
292}
293
294//==========================================================
295// GLOBAL FUNCTION :
296// NAME : MemAvailable
297// read /proc/meminfo
298// unit: kB
299// Peize Lin add 2019-12-21
300//==========================================================
301size_t MemAvailable();
302
303//==========================================================
304// GLOBAL FUNCTION :
305// NAME : DELETE_MUL_PTR
306// delete Multi-dimensional array pointer
307// example:
308// int*** v;
309// DELETE_MUL_PTR(v,N1,N2);
310// -> for(int i1=0; i1<N1; ++i1){
311// for(int i2=0; i2<N2; ++i2){
312// delete[] v[i1][i2]; v[i1][i2]=nullptr; }
313// delete[] v[i1]; v[i1]=nullptr; }
314// delete[] v; v=nullptr;
315// Peize Lin add 2021-05-09
316//==========================================================
317template <typename T_element>
318static inline void DELETE_MUL_PTR(T_element* v)
319{
320 delete[] v;
321 v = nullptr;
322}
323template <typename T_element, typename T_N_first, typename... T_N_tail>
324static inline void DELETE_MUL_PTR(T_element* v, const T_N_first N_first, const T_N_tail... N_tail)
325{
326 for (T_N_first i = 0; i < N_first; ++i) {
327 DELETE_MUL_PTR(v[i], N_tail...);
328 }
329 delete[] v;
330 v = nullptr;
331}
332
333//==========================================================
334// GLOBAL FUNCTION :
335// NAME : FREE_MUL_PTR
336// delete Multi-dimensional array pointer
337// example:
338// int*** v;
339// DELETE_MUL_PTR(v,N1,N2);
340// -> for(int i1=0; i1<N1; ++i1){
341// for(int i2=0; i2<N2; ++i2){
342// free(v[i1][i2]); v[i1][i2]=nullptr; }
343// free(v[i1]); v[i1]=nullptr; }
344// free(v); v=nullptr;
345// Peize Lin add 2021-05-09
346//==========================================================
347template <typename T_element>
348static inline void FREE_MUL_PTR(T_element* v)
349{
350 free(v);
351 v = nullptr;
352}
353template <typename T_element, typename T_N_first, typename... T_N_tail>
354static inline void FREE_MUL_PTR(T_element* v, const T_N_first N_first, const T_N_tail... N_tail)
355{
356 for (T_N_first i = 0; i < N_first; ++i) {
357 FREE_MUL_PTR(v[i], N_tail...);
358 }
359 free(v);
360 v = nullptr;
361}
362
363template <typename T>
364T ddot_real(const int& dim, const std::complex<T>* psi_L, const std::complex<T>* psi_R, const bool reduce = true);
365
366//==========================================================
367// GLOBAL FUNCTION :
368// NAME : IS_COLUMN_MAJOR_KS_SOLVER
369// check ks_solver requires column major or not
370//==========================================================
371static inline bool IS_COLUMN_MAJOR_KS_SOLVER(std::string ks_solver)
372{
373 return ks_solver == "genelpa" || ks_solver == "elpa" || ks_solver == "scalapack_gvx" || ks_solver == "cusolver"
374 || ks_solver == "cusolvermp" || ks_solver == "cg_in_lcao" || ks_solver == "pexsi" || ks_solver == "lapack";
375}
376
377} // namespace GlobalFunc
378} // namespace ModuleBase
379
380#endif
void zcopy_(long const *n, const std::complex< double > *a, int const *incx, std::complex< double > *b, int const *incy)
void dcopy_(long const *n, const double *a, int const *incx, double *b, int const *incy)
#define T
Definition exp.cpp:237
std::ofstream ofs_warning
Definition global_variable.cpp:39
void MAKE_DIR(const std::string &fn)
Definition global_function.cpp:61
void OUTP(std::ofstream &ofs, const std::string &name, const T &a, const std::string &explanation="")
Definition global_function.h:64
void NOTE(const std::string &words)
Definition global_function.cpp:24
bool SCAN_LINE_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart, const bool ifwarn)
Definition global_function.cpp:140
void DONE(std::ofstream &ofs, const std::string &description, const bool only_rank0)
Definition global_function.cpp:81
bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart, const bool ifwarn)
Definition global_function.cpp:110
void OUT_TIME(const std::string &name, time_t &start, time_t &end)
Definition global_function.cpp:226
void SCAN_END(std::ifstream &ifs, const std::string &TargetName, const bool ifwarn)
Definition global_function.cpp:184
void * MAP_EXIST(T_map &ms, const T_key1 &key1)
Definition global_function.h:255
void COPYARRAY(const T *a, T *b, const long dim)
void TEST_LEVEL(const std::string &name, bool disable)
T ddot_real(const int &dim, const std::complex< T > *psi_L, const std::complex< T > *psi_R, const bool reduce=true)
Definition global_function_ddotreal.cpp:20
std::string TO_STRING(const T &t, const int n=20)
Definition global_function.h:239
void ZEROS(std::complex< T > *u, const TI n)
Definition global_function.h:109
size_t MemAvailable()
Definition global_function.cpp:244
void NEW_PART(const std::string &words)
Definition global_function.cpp:39
void AUTO_SET(const std::string &name, const T &a)
Definition global_function.h:90
void OUT(std::ofstream &ofs, const std::string &name)
Definition global_function.cpp:51
void BLOCK_HERE(const std::string &description)
Definition global_function.cpp:195
int4 i[880]
Definition sincos.cpp:28
Definition array_pool.h:6
file(GLOB ATen_CORE_SRCS "*.cpp") set(ATen_CPU_SRCS $
Definition CMakeLists.txt:1
iclock::time_point start
Definition test_partition.cpp:22