00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00038 #ifndef MAT_MATINCLUDE
00039 #define MAT_MATINCLUDE
00040 #include <iostream>
00041 #include <vector>
00042 #include <fstream>
00043 #include <ios>
00044 #include <cassert>
00045 #include <ctime>
00046 #include <limits>
00047 #include <stdexcept>
00048
00049 #ifdef _OPENMP
00050 #include <omp.h>
00051 #endif
00052
00053
00054 #include "config.h"
00055
00056 #include "template_blas_num_limits.h"
00057
00058 #include "Failure.h"
00059 #include "DebugPolicies.h"
00060 #include "SizesAndBlocks.h"
00061 #include "Memory_buffer_thread.h"
00062
00063 #ifdef _OPENMP
00064 #define MAT_OMP_INIT enum omp_failType {noFail = 0, standardFail, runtimeFail, matFail}; \
00065 volatile omp_failType omp_fail = noFail; \
00066 std::exception omp_exce; \
00067 std::runtime_error omp_runtime(""); \
00068 Failure omp_matFail; \
00069 omp_set_nested(true);
00070
00071 #define MAT_OMP_START try {
00072 #define MAT_OMP_END } \
00073 catch(Failure & omp_fail_caught) { \
00074 omp_fail = matFail; omp_matFail = omp_fail_caught; } \
00075 catch(std::runtime_error & omp_runtime_caught) { \
00076 omp_fail = runtimeFail; omp_runtime = omp_runtime_caught; } \
00077 catch(std::exception & omp_exce_caught) { \
00078 omp_fail = standardFail; omp_exce = omp_exce_caught; \
00079 }
00080 #define MAT_OMP_FINALIZE if(omp_fail) \
00081 { std::cerr<<"Exception was thrown in OpenMP parallel region\n"; \
00082 switch (omp_fail) { \
00083 case standardFail: throw omp_exce; break; \
00084 case runtimeFail: throw omp_runtime; break; \
00085 case matFail: throw omp_matFail; break; \
00086 default: throw Failure("Odd error in omp parallel loop\n");} \
00087 }
00088 #else
00089 #define MAT_OMP_INIT
00090 #define MAT_OMP_START
00091 #define MAT_OMP_END
00092 #define MAT_OMP_FINALIZE
00093 #endif
00094
00095 namespace mat{
00096 class Params {
00097 protected:
00098 #ifdef _OPENMP
00099 static unsigned int nProcs;
00100 static unsigned int matrixParallelLevel;
00101 #endif
00102 public:
00103 static unsigned int getNProcs() {
00104 #ifdef _OPENMP
00105 if (nProcs == 0)
00106 throw Failure("mat::Params::getNProcs(): nProcs == 0 Forgot to call setNProcs()?");
00107 return nProcs;
00108 #else
00109 return 1;
00110 #endif
00111 }
00112 static void setNProcs(unsigned int const nP) {
00113 #ifdef _OPENMP
00114 nProcs = nP;
00115 #ifdef USE_SSE_INTRINSICS
00116 Memory_buffer_thread::instance().init_buffers(nProcs);
00117 #endif
00118 #endif
00119 }
00120 static unsigned int getMatrixParallelLevel() {
00121 #ifdef _OPENMP
00122 if (matrixParallelLevel == 0)
00123 throw Failure("mat::Params::getMatrixParallelLevel(): matrixParallelLevel == 0 Forgot to call setMatrixParallelLevel()?");
00124 return matrixParallelLevel;
00125 #else
00126 return 0;
00127 #endif
00128 }
00129 static void setMatrixParallelLevel(unsigned int const mPL) {
00130 #ifdef _OPENMP
00131 matrixParallelLevel = mPL;
00132 #endif
00133 }
00134 };
00135
00136
00137
00138 enum property {zero, ful};
00139 enum normType {frobNorm, euclNorm, mixedNorm};
00140 normType getNormType(const char* normStr);
00141 std::string getNormTypeString(normType nType);
00142
00143
00144
00145
00146 template<typename Treal>
00147 inline static Treal getMachineEpsilon() {
00148 return template_blas_get_machine_epsilon<Treal>();
00149 }
00150
00151 template<typename Treal>
00152 inline static Treal getRelPrecision() {
00153 return getMachineEpsilon<Treal>();
00154 }
00155
00156 class Time {
00157 static double get_wall_seconds();
00158 double ticTime;
00159 public:
00160 Time();
00161 void tic();
00162 float toc();
00163 };
00164
00165 class MemUsage {
00166 private:
00167 static int getNumberFromBuffer(const char* buffer, const char* s);
00168 public:
00169 struct Values {
00170 float res;
00171 float virt;
00172 float peak;
00173 Values() : res(0), virt(0), peak(0) { }
00174 };
00175 static void getMemUsage(Values & values);
00176 };
00177
00178 }
00179 #endif