PEXSI
 All Classes Namespaces Files Functions Variables Typedefs Pages
environment.hpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2012 The Regents of the University of California,
3  through Lawrence Berkeley National Laboratory.
4 
5  Author: Lin Lin
6 
7  This file is part of PEXSI. All rights reserved.
8 
9  Redistribution and use in source and binary forms, with or without
10  modification, are permitted provided that the following conditions are met:
11 
12  (1) Redistributions of source code must retain the above copyright notice, this
13  list of conditions and the following disclaimer.
14  (2) Redistributions in binary form must reproduce the above copyright notice,
15  this list of conditions and the following disclaimer in the documentation
16  and/or other materials provided with the distribution.
17  (3) Neither the name of the University of California, Lawrence Berkeley
18  National Laboratory, U.S. Dept. of Energy nor the names of its contributors may
19  be used to endorse or promote products derived from this software without
20  specific prior written permission.
21 
22  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
23  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33  You are under no obligation whatsoever to provide any bug fixes, patches, or
34  upgrades to the features, functionality or performance of the source code
35  ("Enhancements") to anyone; however, if you choose to make your Enhancements
36  available either publicly, or directly to Lawrence Berkeley National
37  Laboratory, without imposing a separate written license agreement for such
38  Enhancements, then you hereby grant the following license: a non-exclusive,
39  royalty-free perpetual license to install, use, modify, prepare derivative
40  works, incorporate into other computer software, distribute, and sublicense
41  such enhancements or derivative works thereof, in binary and source code form.
42 */
46 #ifndef _PEXSI_ENVIRONMENT_HPP_
47 #define _PEXSI_ENVIRONMENT_HPP_
48 
49 // STL libraries
50 #include <iostream>
51 #include <iomanip>
52 #include <fstream>
53 #include <sstream>
54 #include <unistd.h>
55 
56 #include <cfloat>
57 #include <complex>
58 #include <string>
59 
60 #include <set>
61 #include <map>
62 #include <stack>
63 #include <vector>
64 
65 #include <algorithm>
66 #include <cmath>
67 
68 #include <cassert>
69 #include <stdexcept>
70 #include <execinfo.h>
71 //#include <signal.h>
72 #include <exception>
73 
74 // For 32-bit and 64-bit integers
75 #include <stdint.h>
76 
77 // MPI
78 #include <mpi.h>
79 
80 
81 
82 // *********************************************************************
83 // Redefine the global macros
84 // *********************************************************************
85 
86 
87 // FIXME Always use complex data for pexsi and ppexsi.
88 #define _USE_COMPLEX_
89 
90 // The verbose level of debugging information
91 #ifdef DEBUG
92 #define _DEBUGlevel_ DEBUG
93 #endif
94 
95 // Release mode. For speed up the calculation and reduce verbose level.
96 // Note that RELEASE overwrites DEBUG level.
97 #ifdef RELEASE
98 #define _RELEASE_
99 #define _DEBUGlevel -1
100 #endif
101 
102 /***********************************************************************
103  * Data types and constants
104  **********************************************************************/
105 
108 
109 namespace PEXSI{
110 
111 // Basic data types
112 
113 #ifndef Add_
114 #define FORTRAN(name) name
115 #define BLAS(name) name
116 #define LAPACK(name) name
117 #else
118 #define FORTRAN(name) name##_
119 #define BLAS(name) name##_
120 #define LAPACK(name) name##_
121 #endif
122 typedef int Int;
123 typedef int64_t LongInt;
124 typedef double Real;
125 typedef std::complex<double> Complex; // Must use elemental form of complex
126 #ifdef _USE_COMPLEX_
127 typedef std::complex<double> Scalar; // Must use elemental form of complex
128 #else
129 typedef double Scalar;
130 #endif
131 
132 // IO
133 extern std::ofstream statusOFS;
134 #ifdef GEMM_PROFILE
135 extern std::ofstream statOFS;
136 #include <deque>
137 extern std::deque<int > gemm_stat;
138 #endif
139 
140 #ifdef COMM_PROFILE
141 extern std::ofstream commOFS;
142 #include <deque>
143 extern std::deque<int > comm_stat;
144 
145 #define PROFILE_COMM(sender,receiver,tag,size)\
146 do{\
147  comm_stat.push_back(sender);\
148  comm_stat.push_back(receiver);\
149  comm_stat.push_back(tag);\
150  comm_stat.push_back(size);\
151 }while(0)
152 
153 #define HEADER_COMM "Sender\tReceiver\tTag\tSize"
154 #define LINE_COMM(it) *it<<"\t"<<*(it+1)<<"\t"<<*(it+2)<<"\t"<<*(it+3)
155 
156 #else
157 
158 #define PROFILE_COMM(sender,receiver,tag,size)
159 
160 #endif
161 
162 
163 // *********************************************************************
164 // Define constants
165 // *********************************************************************
166 // Commonly used
167 const Int I_ZERO = 0;
168 const Int I_ONE = 1;
169 const Int I_MINUS_ONE = -1;
170 const Real D_ZERO = 0.0;
171 const Real D_ONE = 1.0;
172 const Real D_MINUS_ONE = -1.0;
173 const Complex Z_ZERO = Complex(0.0, 0.0);
174 const Complex Z_ONE = Complex(1.0, 0.0);
175 const Complex Z_MINUS_ONE = Complex(-1.0, 0.0);
176 const Complex Z_I = Complex(0.0, 1.0);
177 const Complex Z_MINUS_I = Complex(0.0, -1.0);
178 const Scalar SCALAR_ZERO = static_cast<Scalar>(0.0);
179 const Scalar SCALAR_ONE = static_cast<Scalar>(1.0);
180 const Scalar SCALAR_MINUS_ONE = static_cast<Scalar>(-1.0);
181 
182 template<typename T>
183 const T ZERO(){ return static_cast<T>(0.0);};
184 template<typename T>
185 const T ONE(){ return static_cast<T>(1.0);};
186 template<typename T>
187 const T MINUS_ONE(){ return static_cast<T>(-1.0);};
188 
189 const char UPPER = 'U';
190 const char LOWER = 'L';
191 
192 // Physical constants
193 
194 const Real au2K = 315774.67;
195 const Real PI = 3.141592653589793;
196 
197 } // namespace PEXSI
198 
199 /***********************************************************************
200  * Error handling
201  **********************************************************************/
202 
203 namespace PEXSI{
204 
205 
206 
207 
208 
209 
210  inline void gdb_lock(){
211  volatile int lock = 1;
212  statusOFS<<"LOCKED"<<std::endl;
213  while (lock == 1){ }
214  }
215 
216 
217 
218 
219 
220 
221 
222 #ifndef _RELEASE_
223  void PushCallStack( std::string s );
224  void PopCallStack();
225  void DumpCallStack();
226 #endif // ifndef _RELEASE_
227 
228  // We define an output stream that does nothing. This is done so that the
229  // root process can be used to print data to a file's ostream while all other
230  // processes use a null ostream.
231  struct NullStream : std::ostream
232  {
233  struct NullStreamBuffer : std::streambuf
234  {
235  Int overflow( Int c ) { return traits_type::not_eof(c); }
236  } nullStreamBuffer_;
237 
238  NullStream()
239  : std::ios(&nullStreamBuffer_), std::ostream(&nullStreamBuffer_)
240  { }
241  };
242 
244 
246  {
247  public:
249  {
250  void * array[25];
251  int nSize = backtrace(array, 25);
252  char ** symbols = backtrace_symbols(array, nSize);
253 
254  for (int i = 0; i < nSize; i++)
255  {
256  std::cout << symbols[i] << std::endl;
257  }
258 
259  free(symbols);
260  }
261  };
262 
263  // *********************************************************************
264  // Global utility functions
265  // These utility functions do not depend on local definitions
266  // *********************************************************************
267  // Return the closest integer to a real number
268  inline Int iround(Real a){
269  Int b = 0;
270  if(a>0) b = (a-Int(a)<0.5)?Int(a):(Int(a)+1);
271  else b = (Int(a)-a<0.5)?Int(a):(Int(a)-1);
272  return b;
273  }
274 
275  // Read the options from command line
276  inline void OptionsCreate(Int argc, char** argv, std::map<std::string,std::string>& options){
277  options.clear();
278  for(Int k=1; k<argc; k=k+2) {
279  options[ std::string(argv[k]) ] = std::string(argv[k+1]);
280  }
281  }
282 
283  // Size information.
284  // Like sstm.str().length() but without making the copy
285  inline Int Size( std::stringstream& sstm ){
286  Int length;
287  sstm.seekg (0, std::ios::end);
288  length = sstm.tellg();
289  sstm.seekg (0, std::ios::beg);
290  return length;
291  }
292 
293 
294 } // namespace PEXSI
295 
296 
297 #endif // _PEXSI_ENVIRONMENT_HPP_
Definition: environment.hpp:233
Definition: environment.hpp:245
Definition: environment.hpp:231