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