Common.cxx
1 // Copyright (C) 2001-2012 Vivien Mallet
2 //
3 // This file is part of the linear-algebra library Seldon,
4 // http://seldon.sourceforge.net/.
5 //
6 // Seldon is free software; you can redistribute it and/or modify it under the
7 // terms of the GNU Lesser General Public License as published by the Free
8 // Software Foundation; either version 2.1 of the License, or (at your option)
9 // any later version.
10 //
11 // Seldon is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
14 // more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with Seldon. If not, see http://www.gnu.org/licenses/.
18 
19 
20 #ifndef SELDON_FILE_COMMON_CXX
21 
22 #include "Common.hxx"
23 
24 #ifdef SELDON_WITH_SLEPC
25 #include <slepceps.h>
26 #endif
27 
28 namespace Seldon
29 {
31  string GetExtension(const string& nom)
32  {
33  size_t index = nom.find_last_of('.');
34  if (index == string::npos)
35  return string("");
36 
37  string extension = nom.substr(index+1, nom.size()-index);
38  return extension;
39  }
40 
41 
43  string GetBaseString(const string& nom)
44  {
45  size_t index = nom.find_last_of('.');
46  if (index == string::npos)
47  return nom;
48 
49  string base = nom.substr(0, index);
50  return base;
51  }
52 
53 
54 #ifdef SELDON_WITH_HDF5
55 
60  template <class T>
61  hid_t GetH5Type(T& input)
62  {
63  double d;
64  float f;
65  int i;
66  long l;
67  char c;
68  unsigned char uc;
69  long long ll;
70  unsigned int ui;
71  unsigned short us;
72  unsigned long ul;
73  unsigned long long ull;
74 
75  if (typeid(input) == typeid(d))
76  return H5T_NATIVE_DOUBLE;
77  if (typeid(input) == typeid(f))
78  return H5T_NATIVE_FLOAT;
79  if (typeid(input) == typeid(i))
80  return H5T_NATIVE_INT;
81  if (typeid(input) == typeid(l))
82  return H5T_NATIVE_LONG;
83  if (typeid(input) == typeid(c))
84  return H5T_NATIVE_CHAR;
85  if (typeid(input) == typeid(uc))
86  return H5T_NATIVE_UCHAR;
87  if (typeid(input) == typeid(ll))
88  return H5T_NATIVE_LLONG;
89  if (typeid(input) == typeid(ui))
90  return H5T_NATIVE_UINT;
91  if (typeid(input) == typeid(us))
92  return H5T_NATIVE_USHORT;
93  if (typeid(input) == typeid(ul))
94  return H5T_NATIVE_ULONG;
95  if (typeid(input) == typeid(ull))
96  return H5T_NATIVE_ULLONG;
97  else
98  throw Error("hid_t GetH5Type(T& input)",
99  "Type has no corresponding native HDF5 datatype.");
100  }
101 #endif
102 
103 
105  void InitSeldon(int argc, char** argv)
106  {
107  // double precision for writing
108  cout.precision(16);
109 
110  // initialization of MPI
111 #ifdef SELDON_WITH_MPI
112 
113 #ifdef MONTJOIE_WITHOUT_THREAD
114  MPI_Init(&argc, &argv);
115 #else
116  int required = MPI_THREAD_MULTIPLE;
117  int provided = -1;
118  int rank, print_level = 1;
119  MPI_Init_thread(&argc, &argv, required, &provided);
120  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
121  if ((rank == 0) && (print_level > 0))
122  {
123  switch (provided)
124  {
125  case MPI_THREAD_SINGLE:
126  cout << "MPI_Init_thread level = MPI_THREAD_SINGLE" << endl;
127  break;
128  case MPI_THREAD_FUNNELED:
129  cout << "MPI_Init_thread level = MPI_THREAD_FUNNELED" << endl;
130  break;
131  case MPI_THREAD_SERIALIZED:
132  cout << "MPI_Init_thread level = MPI_THREAD_SERIALIZED" << endl;
133  break;
134  case MPI_THREAD_MULTIPLE:
135  cout << "MPI_Init_thread level = MPI_THREAD_MULTIPLE" << endl;
136  break;
137  default:
138  cout << "MPI_Init_thread level = ???" << endl;
139  }
140  }
141 #endif
142 
143 #ifdef SELDON_WITH_SLEPC
144  string help("Slepc interface");
145  SlepcInitialize(&argc, &argv, (char*)0, help.data());
146 #endif
147 
148 #endif
149 
150  // initialization of random generator
151  // (in order to have the same init for all processors)
152  srand(0);
153  }
154 
155 
156  int FinalizeSeldon()
157  {
158 #ifdef SELDON_WITH_SLEPC
159  SlepcFinalize();
160 #endif
161 
162 #ifdef SELDON_WITH_MPI
163  MPI_Finalize();
164 #endif
165 
166  return 0;
167  }
168 
169 } // namespace Seldon.
170 
171 #define SELDON_FILE_COMMON_CXX
172 #endif
Seldon::GetBaseString
string GetBaseString(const string &nom)
returns base of a string
Definition: Common.cxx:43
Seldon::InitSeldon
void InitSeldon(int argc, char **argv)
Function to call in parallel to initialize MPI.
Definition: Common.cxx:105
Seldon::GetExtension
string GetExtension(const string &nom)
returns extension of a string
Definition: Common.cxx:31
Seldon
Seldon namespace.
Definition: Array.cxx:24