ergo
matInclude.h
Go to the documentation of this file.
1 /* Ergo, version 3.3, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2013 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 
36 #ifndef MAT_MATINCLUDE
37 #define MAT_MATINCLUDE
38 #include <iostream>
39 #include <vector>
40 #include <fstream>
41 #include <ios>
42 #include <cassert>
43 #include <ctime>
44 #include <limits>
45 #include <stdexcept>
46 
47 #ifdef _OPENMP
48 #include <omp.h>
49 #endif
50 
51 /* We need to include config.h to get the USE_SSE_INTRINSICS flag. */
52 #include "config.h"
53 
54 #include "Failure.h"
55 #include "DebugPolicies.h"
56 #include "SizesAndBlocks.h"
57 #include "Memory_buffer_thread.h"
58 
59 #ifdef _OPENMP
60 #define MAT_OMP_INIT enum omp_failType {noFail = 0, standardFail, runtimeFail, matFail}; \
61  volatile omp_failType omp_fail = noFail; \
62  std::exception omp_exce; \
63  std::runtime_error omp_runtime(""); \
64  Failure omp_matFail; \
65  omp_set_nested(true);
66 // if (omp_fail == noFail) {
67 #define MAT_OMP_START try {
68 #define MAT_OMP_END } \
69  catch(Failure & omp_fail_caught) { \
70  omp_fail = matFail; omp_matFail = omp_fail_caught; } \
71  catch(std::runtime_error & omp_runtime_caught) { \
72  omp_fail = runtimeFail; omp_runtime = omp_runtime_caught; } \
73  catch(std::exception & omp_exce_caught) { \
74  omp_fail = standardFail; omp_exce = omp_exce_caught; \
75 }
76 #define MAT_OMP_FINALIZE if(omp_fail) \
77  { std::cerr<<"Exception was thrown in OpenMP parallel region\n"; \
78  switch (omp_fail) { \
79  case standardFail: throw omp_exce; break; \
80  case runtimeFail: throw omp_runtime; break; \
81  case matFail: throw omp_matFail; break; \
82  default: throw Failure("Odd error in omp parallel loop\n");} \
83  }
84 #else
85 #define MAT_OMP_INIT
86 #define MAT_OMP_START
87 #define MAT_OMP_END
88 #define MAT_OMP_FINALIZE
89 #endif
90 
91 namespace mat{
92  class Params {
93  protected:
94 #ifdef _OPENMP
95  static unsigned int nProcs;
96  static unsigned int matrixParallelLevel;
97 #endif
98  public:
99  static unsigned int getNProcs() {
100 #ifdef _OPENMP
101  if (nProcs == 0)
102  throw Failure("mat::Params::getNProcs(): nProcs == 0 Forgot to call setNProcs()?");
103  return nProcs;
104 #else
105  return 1;
106 #endif
107  }
108  static void setNProcs(unsigned int const nP) {
109 #ifdef _OPENMP
110  nProcs = nP;
111 #ifdef USE_SSE_INTRINSICS
112  Memory_buffer_thread::instance().init_buffers(nProcs);
113 #endif
114 #endif
115  }
116  static unsigned int getMatrixParallelLevel() {
117 #ifdef _OPENMP
118  if (matrixParallelLevel == 0)
119  throw Failure("mat::Params::getMatrixParallelLevel(): matrixParallelLevel == 0 Forgot to call setMatrixParallelLevel()?");
120  return matrixParallelLevel;
121 #else
122  return 0;
123 #endif
124  }
125  static void setMatrixParallelLevel(unsigned int const mPL) {
126 #ifdef _OPENMP
127  matrixParallelLevel = mPL;
128 #endif
129  }
130  };
131 
132 
133 
134  enum property {zero, ful};
136  normType getNormType(const char* normStr);
137  std::string getNormTypeString(normType nType);
138 
139 
140  template<typename Treal>
141  inline static Treal getRelPrecision() {
142  throw Failure("getPrecision() : The used type is not supported by"
143  " getPrecision() ");
144  }
145  template<>
146  inline long double getRelPrecision<long double>() {
147  return std::numeric_limits<long double>::epsilon();
148  }
149  template<>
150  inline double getRelPrecision<double>() {
151  return std::numeric_limits<double>::epsilon();
152  }
153  template<>
154  inline float getRelPrecision<float>() {
155  return std::numeric_limits<float>::epsilon();
156  }
157 
158  class Time {
159  static double get_wall_seconds();
160  double ticTime;
161  public:
162  Time();
163  void tic();
164  float toc();
165  };
166 
167  class MemUsage {
168  private:
169  static int getNumberFromBuffer(const char* buffer, const char* s);
170  public:
171  struct Values {
172  float res;
173  float virt;
174  float peak;
175  Values() : res(0), virt(0), peak(0) { }
176  };
177  static void getMemUsage(Values & values);
178  };
179 
180 } /* end namespace mat */
181 #endif
static Treal getRelPrecision()
Definition: matInclude.h:141
static void setMatrixParallelLevel(unsigned int const mPL)
Definition: matInclude.h:125
std::string getNormTypeString(normType nType)
Definition: matInclude.cc:55
static void setNProcs(unsigned int const nP)
Definition: matInclude.h:108
float peak
Definition: matInclude.h:174
Definition: matInclude.h:167
double ticTime
Definition: matInclude.h:160
void tic()
Definition: matInclude.cc:77
Values()
Definition: matInclude.h:175
Classes describing debug policies with different debug levels.
double getRelPrecision< double >()
Definition: matInclude.h:150
Definition: matInclude.h:92
Definition: allocate.cc:30
Definition: matInclude.h:158
float toc()
Definition: matInclude.cc:81
Definition: matInclude.h:134
float res
Definition: matInclude.h:172
long double getRelPrecision< long double >()
Definition: matInclude.h:146
Definition: matInclude.h:135
float getRelPrecision< float >()
Definition: matInclude.h:154
float virt
Definition: matInclude.h:173
static void getMemUsage(Values &values)
Definition: matInclude.cc:127
Definition: matInclude.h:135
static unsigned int getMatrixParallelLevel()
Definition: matInclude.h:116
Definition: matInclude.h:134
static int getNumberFromBuffer(const char *buffer, const char *s)
Definition: matInclude.cc:89
static double get_wall_seconds()
Definition: matInclude.cc:67
static unsigned int getNProcs()
Definition: matInclude.h:99
Time()
Definition: matInclude.cc:75
Definition: matInclude.h:135
Definition: Failure.h:47
Definition: matInclude.h:171
normType getNormType(const char *normStr)
Definition: matInclude.cc:45
normType
Definition: matInclude.h:135