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 // Google coredumper for debugging
81 #ifdef COREDUMPER
82 #define _COREDUMPER_
83 #endif
84 
85 
86 // *********************************************************************
87 // Redefine the global macros
88 // *********************************************************************
89 
90 
91 // FIXME Always use complex data for pexsi and ppexsi.
92 #define _USE_COMPLEX_
93 
94 // The verbose level of debugging information
95 #ifdef DEBUG
96 #define _DEBUGlevel_ DEBUG
97 #endif
98 
99 // Release mode. For speed up the calculation and reduce verbose level.
100 // Note that RELEASE overwrites DEBUG level.
101 #ifdef RELEASE
102 #define _RELEASE_
103 #define _DEBUGlevel -1
104 #endif
105 
106 /***********************************************************************
107  * Data types and constants
108  **********************************************************************/
109 
112 
113 namespace PEXSI{
114 
115 // Basic data types
116 
117 #ifndef Add_
118 #define FORTRAN(name) name
119 #define BLAS(name) name
120 #define LAPACK(name) name
121 #else
122 #define FORTRAN(name) name##_
123 #define BLAS(name) name##_
124 #define LAPACK(name) name##_
125 #endif
126 typedef int Int;
127 typedef int64_t LongInt;
128 typedef double Real;
129 typedef std::complex<double> Complex; // Must use elemental form of complex
130 #ifdef _USE_COMPLEX_
131 typedef std::complex<double> Scalar; // Must use elemental form of complex
132 #else
133 typedef double Scalar;
134 #endif
135 
136 // IO
137 extern std::ofstream statusOFS;
138 #ifdef GEMM_PROFILE
139 extern std::ofstream statOFS;
140 #include <deque>
141 extern std::deque<int > gemm_stat;
142 #endif
143 
144 #if defined(COMM_PROFILE) || defined(COMM_PROFILE_BCAST)
145 extern std::ofstream commOFS;
146 #include <deque>
147 extern std::deque<int > comm_stat;
148 
149 #define PROFILE_COMM(sender,receiver,tag,size)\
150 do{\
151  comm_stat.push_back(sender);\
152  comm_stat.push_back(receiver);\
153  comm_stat.push_back(tag);\
154  comm_stat.push_back(size);\
155 }while(0)
156 
157 #define HEADER_COMM "Sender\tReceiver\tTag\tSize"
158 #define LINE_COMM(it) *it<<"\t"<<*(it+1)<<"\t"<<*(it+2)<<"\t"<<*(it+3)
159 
160 #else
161 
162 #define PROFILE_COMM(sender,receiver,tag,size)
163 
164 #endif
165 
166 
167 // *********************************************************************
168 // Define constants
169 // *********************************************************************
170 // Commonly used
171 const Int I_ZERO = 0;
172 const Int I_ONE = 1;
173 const Int I_MINUS_ONE = -1;
174 const Real D_ZERO = 0.0;
175 const Real D_ONE = 1.0;
176 const Real D_MINUS_ONE = -1.0;
177 const Complex Z_ZERO = Complex(0.0, 0.0);
178 const Complex Z_ONE = Complex(1.0, 0.0);
179 const Complex Z_MINUS_ONE = Complex(-1.0, 0.0);
180 const Complex Z_I = Complex(0.0, 1.0);
181 const Complex Z_MINUS_I = Complex(0.0, -1.0);
182 const Scalar SCALAR_ZERO = static_cast<Scalar>(0.0);
183 const Scalar SCALAR_ONE = static_cast<Scalar>(1.0);
184 const Scalar SCALAR_MINUS_ONE = static_cast<Scalar>(-1.0);
185 
186 template<typename T>
187 const T ZERO(){ return static_cast<T>(0.0);};
188 template<typename T>
189 const T ONE(){ return static_cast<T>(1.0);};
190 template<typename T>
191 const T MINUS_ONE(){ return static_cast<T>(-1.0);};
192 
193 const char UPPER = 'U';
194 const char LOWER = 'L';
195 
196 // Physical constants
197 
198 const Real au2K = 315774.67;
199 const Real PI = 3.141592653589793;
200 
201 } // namespace PEXSI
202 
203 /***********************************************************************
204  * Error handling
205  **********************************************************************/
206 
207 namespace PEXSI{
208 
209 
210 
211 
212 
213 
214  inline void gdb_lock(){
215  static volatile int count=0;
216  static volatile int enabled=0;
217  count++;
218  pid_t pid = getpid();
219 // std::cout<<pid<<" is locked "<<count<<std::endl;
220  statusOFS<<pid<<" is locked "<<count<<std::endl;
221  volatile int lock = 1;
222  while (lock == 1 && enabled == 1){ }
223 // std::cout<<pid<<" is unlocked "<<count<<std::endl;
224  statusOFS<<pid<<" is unlocked "<<count<<std::endl;
225  }
226 
227 
228 
229 
230  inline void gdb_truelock(){
231  pid_t pid = getpid();
232  std::cout<<pid<<" is locked "<<std::endl;
233  volatile int lock = 1;
234  while (lock == 1){ }
235  std::cout<<pid<<" is unlocked "<<std::endl;
236  }
237 
238 
239 
240 
241 
242 
243 
244 void ErrorHandling( const char * msg );
245 
246 // We define an output stream that does nothing. This is done so that the
247 // root process can be used to print data to a file's ostream while all other
248 // processes use a null ostream.
249 struct NullStream : std::ostream
250 {
251  struct NullStreamBuffer : std::streambuf
252  {
253  Int overflow( Int c ) { return traits_type::not_eof(c); }
254  } nullStreamBuffer_;
255 
256  NullStream()
257  : std::ios(&nullStreamBuffer_), std::ostream(&nullStreamBuffer_)
258  { }
259 };
260 
262 
264 {
265 public:
267  {
268  void * array[25];
269  int nSize = backtrace(array, 25);
270  char ** symbols = backtrace_symbols(array, nSize);
271 
272  for (int i = 0; i < nSize; i++)
273  {
274  std::cout << symbols[i] << std::endl;
275  }
276 
277  free(symbols);
278  }
279 };
280 
281 // *********************************************************************
282 // Global utility functions
283 // These utility functions do not depend on local definitions
284 // *********************************************************************
285 // Return the closest integer to a real number
286 inline Int iround(Real a){
287  Int b = 0;
288  if(a>0) b = (a-Int(a)<0.5)?Int(a):(Int(a)+1);
289  else b = (Int(a)-a<0.5)?Int(a):(Int(a)-1);
290  return b;
291 }
292 
293 // Read the options from command line
294 inline void OptionsCreate(Int argc, char** argv, std::map<std::string,std::string>& options){
295  options.clear();
296  for(Int k=1; k<argc; k=k+2) {
297  options[ std::string(argv[k]) ] = std::string(argv[k+1]);
298  }
299 }
300 
301 // Size information.
302 // Like sstm.str().length() but without making the copy
303 inline Int Size( std::stringstream& sstm ){
304  Int length;
305  sstm.seekg (0, std::ios::end);
306  length = sstm.tellg();
307  sstm.seekg (0, std::ios::beg);
308  return length;
309 }
310 
311 
312 } // namespace PEXSI
313 
314 
315 #endif // _PEXSI_ENVIRONMENT_HPP_
Definition: environment.hpp:251
Definition: environment.hpp:263
Definition: environment.hpp:249