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
4#include "glob_fn_each2.h" // Peize Lin add 2016-09-07
5#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, const std::string& TargetName, const bool restart = true, const bool ifwarn = true);
153
154//-------------------------------------------------------------
155// The `SCAN_LINE_BEGIN` function efficiently searches
156// text files for specified keywords while ignoring comment
157// lines and whitespace. It skips any line starting with '#'
158//-------------------------------------------------------------
159bool SCAN_LINE_BEGIN(std::ifstream& ifs, const std::string& TargetName, const bool restart = true, const bool ifwarn = true);
160
161void SCAN_END(std::ifstream& ifs, const std::string& TargetName, const bool ifwarn = true);
162
163template <class T>
164static inline void DCOPY(const T& a, T& b, const int& dim)
165{
166 for (int i = 0; i < dim; ++i)
167 {
168 b[i] = a[i];
169 }
170}
171
172template <typename T>
173inline void DCOPY(const T* a, T* b, const int& dim)
174{
175 for (int i = 0; i < dim; ++i)
176 {
177 b[i] = a[i];
178 }
179}
180
181template <typename T>
182inline void COPYARRAY(const T* a, T* b, const int dim);
183
184template <>
185inline void COPYARRAY(const std::complex<double>* a, std::complex<double>* b, const int dim)
186{
187 const int one = 1;
188 zcopy_(&dim, a, &one, b, &one);
189}
190
191template <>
192inline void COPYARRAY(const double* a, double* b, const int dim)
193{
194 const int one = 1;
195 dcopy_(&dim, a, &one, b, &one);
196}
197
198void BLOCK_HERE(const std::string& description);
199
200//==========================================================
201// GLOBAL FUNCTION :
202// NAME : VECTOR_TO_PTR
203// change std::vector to pointer
204// Peize Lin add 2016-02-25
205//==========================================================
206template <class T>
207static inline T* VECTOR_TO_PTR(std::vector<T>& v)
208{
209 return &(v[0]);
210}
211template <class T>
212static inline T* VECTOR_TO_PTR(std::valarray<T>& v)
213{
214 return &(v[0]);
215}
216
217template <class T>
218static inline const T* VECTOR_TO_PTR(const std::vector<T>& v)
219{
220 return &(v[0]);
221}
222template <class T>
223static inline const T* VECTOR_TO_PTR(const std::valarray<T>& v)
224{
225 return &(v[0]);
226}
227
228//==========================================================
229// GLOBAL FUNCTION :
230// NAME : TO_STRING
231// change number to std::string
232// example: 233 -> "233"
233// Peize Lin add 2016-07-18
234//==========================================================
235template <typename T>
236std::string TO_STRING(const T& t, const int n = 20) // n=20 since LDBL_EPSILON is 1E-16 or 1E-19
237{
238 std::stringstream newstr;
239 newstr << std::setprecision(n) << t;
240 return newstr.str();
241}
242
243//==========================================================
244// GLOBAL FUNCTION :
245// NAME : MAP_EXIST
246// find whether exists the std::map index
247// if exist return the ptr called, else return nullptr
248// example: map_exist(ms,i,j,k) -> try to find ms[i][j][k]
249// Peize Lin add 2018-07-16
250//==========================================================
251template <typename T_map, typename T_key1>
252inline void* MAP_EXIST(T_map& ms, const T_key1& key1)
253{
254 auto ms1 = ms.find(key1);
255 if (ms1 == ms.end())
256 {
257 return nullptr;
258 }
259 return static_cast<void*>(&ms1->second);
260}
261
262template <typename T_map, typename T_key1, typename... T_key_tail>
263inline void* MAP_EXIST(T_map& ms, const T_key1& key1, const T_key_tail&... key_tail)
264{
265 auto ms1 = ms.find(key1);
266 if (ms1 == ms.end())
267 {
268 return nullptr;
269 }
270 return MAP_EXIST(ms1->second, key_tail...);
271}
272
273template <typename T_map, typename T_key1>
274inline const void* MAP_EXIST(const T_map& ms, const T_key1& key1)
275{
276 auto ms1 = ms.find(key1);
277 if (ms1 == ms.end())
278 {
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 {
290 return nullptr;
291 }
292 return MAP_EXIST(ms1->second, key_tail...);
293}
294
295//==========================================================
296// GLOBAL FUNCTION :
297// NAME : MemAvailable
298// read /proc/meminfo
299// unit: kB
300// Peize Lin add 2019-12-21
301//==========================================================
302size_t MemAvailable();
303
304//==========================================================
305// GLOBAL FUNCTION :
306// NAME : DELETE_MUL_PTR
307// delete Multi-dimensional array pointer
308// example:
309// int*** v;
310// DELETE_MUL_PTR(v,N1,N2);
311// -> for(int i1=0; i1<N1; ++i1){
312// for(int i2=0; i2<N2; ++i2){
313// delete[] v[i1][i2]; v[i1][i2]=nullptr; }
314// delete[] v[i1]; v[i1]=nullptr; }
315// delete[] v; v=nullptr;
316// Peize Lin add 2021-05-09
317//==========================================================
318template <typename T_element>
319static inline void DELETE_MUL_PTR(T_element* v)
320{
321 delete[] v;
322 v = nullptr;
323}
324template <typename T_element, typename T_N_first, typename... T_N_tail>
325static inline void DELETE_MUL_PTR(T_element* v, const T_N_first N_first, const T_N_tail... N_tail)
326{
327 for (T_N_first i = 0; i < N_first; ++i)
328 {
329 DELETE_MUL_PTR(v[i], N_tail...);
330 }
331 delete[] v;
332 v = nullptr;
333}
334
335//==========================================================
336// GLOBAL FUNCTION :
337// NAME : FREE_MUL_PTR
338// delete Multi-dimensional array pointer
339// example:
340// int*** v;
341// DELETE_MUL_PTR(v,N1,N2);
342// -> for(int i1=0; i1<N1; ++i1){
343// for(int i2=0; i2<N2; ++i2){
344// free(v[i1][i2]); v[i1][i2]=nullptr; }
345// free(v[i1]); v[i1]=nullptr; }
346// free(v); v=nullptr;
347// Peize Lin add 2021-05-09
348//==========================================================
349template <typename T_element>
350static inline void FREE_MUL_PTR(T_element* v)
351{
352 free(v);
353 v = nullptr;
354}
355template <typename T_element, typename T_N_first, typename... T_N_tail>
356static inline void FREE_MUL_PTR(T_element* v, const T_N_first N_first, const T_N_tail... N_tail)
357{
358 for (T_N_first i = 0; i < N_first; ++i)
359 {
360 FREE_MUL_PTR(v[i], N_tail...);
361 }
362 free(v);
363 v = nullptr;
364}
365
366template <typename T>
367T ddot_real(const int& dim, const std::complex<T>* psi_L, const std::complex<T>* psi_R, const bool reduce = true);
368
369//==========================================================
370// GLOBAL FUNCTION :
371// NAME : IS_COLUMN_MAJOR_KS_SOLVER
372// check ks_solver requires column major or not
373//==========================================================
374static inline bool IS_COLUMN_MAJOR_KS_SOLVER(std::string ks_solver)
375{
376 return ks_solver == "genelpa" || ks_solver == "elpa" || ks_solver == "scalapack_gvx" || ks_solver == "cusolver"
377 || ks_solver == "cusolvermp" || ks_solver == "pexsi" || ks_solver == "lapack";
378}
379
380} // namespace GlobalFunc
381} // namespace ModuleBase
382
383#endif
void zcopy_(const int *n, const std::complex< double > *a, const int *incx, std::complex< double > *b, int const *incy)
void dcopy_(const int *n, const double *a, const int *incx, double *b, int const *incy)
#define T
Definition exp.cpp:237
std::ofstream ofs_warning
Definition global_variable.cpp:39
Definition glob_fn_ddotreal.cpp:8
void MAKE_DIR(const std::string &fn)
Definition global_function.cpp:64
void OUTP(std::ofstream &ofs, const std::string &name, const T &a, const std::string &explanation="")
Definition global_function.h:64
void COPYARRAY(const T *a, T *b, const int dim)
void NOTE(const std::string &words)
Definition global_function.cpp:27
bool SCAN_LINE_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart, const bool ifwarn)
Definition global_function.cpp:137
void DONE(std::ofstream &ofs, const std::string &description, const bool only_rank0)
Definition global_function.cpp:78
bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart, const bool ifwarn)
Definition global_function.cpp:107
void OUT_TIME(const std::string &name, time_t &start, time_t &end)
Definition global_function.cpp:223
void SCAN_END(std::ifstream &ifs, const std::string &TargetName, const bool ifwarn)
Definition global_function.cpp:181
void * MAP_EXIST(T_map &ms, const T_key1 &key1)
Definition global_function.h:252
template double ddot_real(const int &dim, const std::complex< double > *psi_L, const std::complex< double > *psi_R, const bool reduce)
void TEST_LEVEL(const std::string &name, bool disable)
std::string TO_STRING(const T &t, const int n=20)
Definition global_function.h:236
void ZEROS(std::complex< T > *u, const TI n)
Definition global_function.h:109
size_t MemAvailable()
Definition global_function.cpp:241
void NEW_PART(const std::string &words)
Definition global_function.cpp:42
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:54
void BLOCK_HERE(const std::string &description)
Definition global_function.cpp:192
int4 i[880]
Definition sincos.cpp:28
Definition clebsch_gordan_coeff.cpp:8
file(GLOB ATen_CORE_SRCS "*.cpp") set(ATen_CPU_SRCS $
Definition CMakeLists.txt:1
iclock::time_point start
Definition test_partition.cpp:22