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 // FIXME Always use complex data for pexsi and ppexsi.
87 #define _USE_COMPLEX_
88 
89 // The verbose level of debugging information
90 #ifdef DEBUG
91 #define _DEBUGlevel_ DEBUG
92 #endif
93 
94 // Release mode. For speed up the calculation and reduce verbose level.
95 // Note that RELEASE overwrites DEBUG level.
96 #ifdef RELEASE
97 #define _RELEASE_
98 #define _DEBUGlevel -1
99 #endif
100 
101 /***********************************************************************
102  * Data types and constants
103  **********************************************************************/
104 
107 
108 namespace PEXSI{
109 
110 // Basic data types
111 
112 #ifndef Add_
113 #define FORTRAN(name) name
114 #define BLAS(name) name
115 #define LAPACK(name) name
116 #else
117 #define FORTRAN(name) name##_
118 #define BLAS(name) name##_
119 #define LAPACK(name) name##_
120 #endif
121 typedef int Int;
122 typedef int64_t LongInt;
123 typedef double Real;
124 typedef std::complex<double> Complex; // Must use elemental form of complex
125 #ifdef _USE_COMPLEX_
126 typedef std::complex<double> Scalar; // Must use elemental form of complex
127 #else
128 typedef double Scalar;
129 #endif
130 
131 // IO
132 extern std::ofstream statusOFS;
133 
134 // *********************************************************************
135 // Define constants
136 // *********************************************************************
137 // Commonly used
138 const Int I_ZERO = 0;
139 const Int I_ONE = 1;
140 const Int I_MINUS_ONE = -1;
141 const Real D_ZERO = 0.0;
142 const Real D_ONE = 1.0;
143 const Real D_MINUS_ONE = -1.0;
144 const Complex Z_ZERO = Complex(0.0, 0.0);
145 const Complex Z_ONE = Complex(1.0, 0.0);
146 const Complex Z_MINUS_ONE = Complex(-1.0, 0.0);
147 const Complex Z_I = Complex(0.0, 1.0);
148 const Complex Z_MINUS_I = Complex(0.0, -1.0);
149 const Scalar SCALAR_ZERO = static_cast<Scalar>(0.0);
150 const Scalar SCALAR_ONE = static_cast<Scalar>(1.0);
151 const Scalar SCALAR_MINUS_ONE = static_cast<Scalar>(-1.0);
152 
153 template<typename T>
154 const T ZERO(){ return static_cast<T>(0.0);};
155 template<typename T>
156 const T ONE(){ return static_cast<T>(1.0);};
157 template<typename T>
158 const T MINUS_ONE(){ return static_cast<T>(-1.0);};
159 
160 const char UPPER = 'U';
161 const char LOWER = 'L';
162 
163 // Physical constants
164 
165 const Real au2K = 315774.67;
166 const Real PI = 3.141592653589793;
167 
168 } // namespace PEXSI
169 
170 /***********************************************************************
171  * Error handling
172  **********************************************************************/
173 
174 namespace PEXSI{
175 
176 
177 #ifndef _RELEASE_
178  void PushCallStack( std::string s );
179  void PopCallStack();
180  void DumpCallStack();
181 #endif // ifndef _RELEASE_
182 
183  // We define an output stream that does nothing. This is done so that the
184  // root process can be used to print data to a file's ostream while all other
185  // processes use a null ostream.
186  struct NullStream : std::ostream
187  {
188  struct NullStreamBuffer : std::streambuf
189  {
190  Int overflow( Int c ) { return traits_type::not_eof(c); }
191  } nullStreamBuffer_;
192 
193  NullStream()
194  : std::ios(&nullStreamBuffer_), std::ostream(&nullStreamBuffer_)
195  { }
196  };
197 
199 
201  {
202  public:
204  {
205  void * array[25];
206  int nSize = backtrace(array, 25);
207  char ** symbols = backtrace_symbols(array, nSize);
208 
209  for (int i = 0; i < nSize; i++)
210  {
211  std::cout << symbols[i] << std::endl;
212  }
213 
214  free(symbols);
215  }
216  };
217 
218  // *********************************************************************
219  // Global utility functions
220  // These utility functions do not depend on local definitions
221  // *********************************************************************
222  // Return the closest integer to a real number
223  inline Int iround(Real a){
224  Int b = 0;
225  if(a>0) b = (a-Int(a)<0.5)?Int(a):(Int(a)+1);
226  else b = (Int(a)-a<0.5)?Int(a):(Int(a)-1);
227  return b;
228  }
229 
230  // Read the options from command line
231  inline void OptionsCreate(Int argc, char** argv, std::map<std::string,std::string>& options){
232  options.clear();
233  for(Int k=1; k<argc; k=k+2) {
234  options[ std::string(argv[k]) ] = std::string(argv[k+1]);
235  }
236  }
237 
238  // Size information.
239  // Like sstm.str().length() but without making the copy
240  inline Int Size( std::stringstream& sstm ){
241  Int length;
242  sstm.seekg (0, std::ios::end);
243  length = sstm.tellg();
244  sstm.seekg (0, std::ios::beg);
245  return length;
246  }
247 
248 
249 } // namespace PEXSI
250 
251 
252 #endif // _PEXSI_ENVIRONMENT_HPP_
Definition: environment.hpp:188
Definition: environment.hpp:200
Definition: environment.hpp:186