ergo
matInclude.h
Go to the documentation of this file.
1 /* Ergo, version 3.2, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2012 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 
46 #ifdef _OPENMP
47 #include <omp.h>
48 #endif
49 
50 #include "Failure.h"
51 #include "DebugPolicies.h"
52 #include "SizesAndBlocks.h"
53 
54 #ifdef _OPENMP
55 #define MAT_OMP_INIT enum omp_failType {noFail = 0, standardFail, matFail}; \
56  volatile omp_failType omp_fail = noFail; \
57  std::exception omp_exce; \
58  Failure omp_matFail; \
59  omp_set_nested(true);
60 // if (omp_fail == noFail) {
61 #define MAT_OMP_START try {
62 #define MAT_OMP_END } \
63  catch(Failure & omp_fail_caught) { \
64  omp_fail = matFail; omp_matFail = omp_fail_caught; } \
65  catch(std::exception & omp_exce_caught) { \
66  omp_fail = standardFail; omp_exce = omp_exce_caught; }
67 #define MAT_OMP_FINALIZE if(omp_fail) \
68  { std::cerr<<"Exception was thrown in OpenMP parallel region\n"; \
69  switch (omp_fail) { \
70  case standardFail: throw omp_exce; break; \
71  case matFail: throw omp_matFail; break; \
72  default: throw Failure("Odd error in omp parallel loop\n");} \
73  }
74 #else
75 #define MAT_OMP_INIT
76 #define MAT_OMP_START
77 #define MAT_OMP_END
78 #define MAT_OMP_FINALIZE
79 #endif
80 
81 namespace mat{
82  class Params {
83  protected:
84 #ifdef _OPENMP
85  static unsigned int nProcs;
86  static unsigned int matrixParallelLevel;
87 #endif
88  public:
89  static unsigned int getNProcs() {
90 #ifdef _OPENMP
91  if (nProcs == 0)
92  throw Failure("mat::Params::getNProcs(): nProcs == 0 Forgot to call setNProcs()?");
93  return nProcs;
94 #else
95  return 1;
96 #endif
97  }
98  static void setNProcs(unsigned int const nP) {
99 #ifdef _OPENMP
100  nProcs = nP;
101 #endif
102  }
103  static unsigned int getMatrixParallelLevel() {
104 #ifdef _OPENMP
105  if (matrixParallelLevel == 0)
106  throw Failure("mat::Params::getMatrixParallelLevel(): matrixParallelLevel == 0 Forgot to call setMatrixParallelLevel()?");
107  return matrixParallelLevel;
108 #else
109  return 0;
110 #endif
111  }
112  static void setMatrixParallelLevel(unsigned int const mPL) {
113 #ifdef _OPENMP
114  matrixParallelLevel = mPL;
115 #endif
116  }
117  };
118 
119 
120 
121  enum property {zero, ful};
123  normType getNormType(const char* normStr);
124  std::string getNormTypeString(normType nType);
125 
126 
127  template<typename Treal>
128  inline static Treal getRelPrecision() {
129  throw Failure("getPrecision() : The used type is not supported by"
130  " getPrecision() ");
131  }
132  template<>
133  inline long double getRelPrecision<long double>() {
134  return std::numeric_limits<long double>::epsilon();
135  }
136  template<>
137  inline double getRelPrecision<double>() {
138  return std::numeric_limits<double>::epsilon();
139  }
140  template<>
141  inline float getRelPrecision<float>() {
142  return std::numeric_limits<float>::epsilon();
143  }
144 
145  class Time {
146  static double get_wall_seconds();
147  double ticTime;
148  public:
149  Time();
150  void tic();
151  float toc();
152  };
153 
154  class MemUsage {
155  private:
156  static int getNumberFromBuffer(const char* buffer, const char* s);
157  public:
158  struct Values {
159  float res;
160  float virt;
161  float peak;
162  Values() : res(0), virt(0), peak(0) { }
163  };
164  static void getMemUsage(Values & values);
165  };
166 
167 } /* end namespace mat */
168 #endif