cloudy  trunk
mpi_utilities.h
Go to the documentation of this file.
1 /* This file is part of Cloudy and is copyright (C)1978-2013 by Gary J. Ferland and
2  * others. For conditions of distribution and use see copyright notice in license.txt */
3 
4 #ifndef MPI_UTILITIES_H_
5 #define MPI_UTILITIES_H_
6 
7 #ifdef MPI_ENABLED
8 
9 namespace MPI
10 {
38 
39  inline const Datatype& type(bool) { return BOOL; }
40  inline const Datatype& type(const bool*) { return BOOL; }
41  inline const Datatype& type(char) { return CHAR; }
42  inline const Datatype& type(const char*) { return CHAR; }
43  inline const Datatype& type(unsigned char) { return UNSIGNED_CHAR; }
44  inline const Datatype& type(const unsigned char*) { return UNSIGNED_CHAR; }
45  inline const Datatype& type(short int) { return SHORT; }
46  inline const Datatype& type(const short int*) { return SHORT; }
47  inline const Datatype& type(unsigned short int) { return UNSIGNED_SHORT; }
48  inline const Datatype& type(const unsigned short int*) { return UNSIGNED_SHORT; }
49  inline const Datatype& type(int) { return INT; }
50  inline const Datatype& type(const int*) { return INT; }
51  inline const Datatype& type(unsigned int) { return UNSIGNED; }
52  inline const Datatype& type(const unsigned int*) { return UNSIGNED; }
53  inline const Datatype& type(long) { return LONG_INT; }
54  inline const Datatype& type(const long*) { return LONG_INT; }
55  inline const Datatype& type(unsigned long) { return UNSIGNED_LONG; }
56  inline const Datatype& type(const unsigned long*) { return UNSIGNED_LONG; }
57  inline const Datatype& type(sys_float) { return FLOAT; }
58  inline const Datatype& type(const sys_float*) { return FLOAT; }
59  inline const Datatype& type(double) { return DOUBLE; }
60  inline const Datatype& type(const double*) { return DOUBLE; }
61  inline const Datatype& type(complex<sys_float>) { return COMPLEX; }
62  inline const Datatype& type(const complex<sys_float>*) { return COMPLEX; }
63  inline const Datatype& type(complex<double>) { return DOUBLE_COMPLEX; }
64  inline const Datatype& type(const complex<double>*) { return DOUBLE_COMPLEX; }
65 }
66 
67 #else /* MPI_ENABLED */
68 
69 namespace MPI
70 {
71  // This global struct is needed so that we can #define away the arguments of
72  // calls to MPI routines, which allows us to reduce the number of stubs needed.
73  // Since it contains no real data and only an inline function, the fact that
74  // it is global creates no problems (it stores nothing in memory). Some compilers
75  // (like g++) don't even require this struct to be allocated.
76  struct t_MPI
77  {
78  int total_insanity() { return TotalInsanityAsStub<int>(); }
79  };
80  extern t_MPI COMM_WORLD;
81 }
82 
83 // define MPI stubs here, so that we don't get endless #ifdef MPI_ENBLED in the code...
84 // this way we can use if( cpu.i().lgMPI() ) { .... } instead
85 #define Barrier() total_insanity()
86 #define Bcast(W,X,Y,Z) total_insanity()
87 #define Finalize() COMM_WORLD.total_insanity()
88 #define Get_size() total_insanity()
89 #define Get_rank() total_insanity()
90 #define Init(Y,Z) COMM_WORLD.total_insanity()
91 #define Reduce(U,V,W,X,Y,Z) total_insanity()
92 
93 #endif /* MPI_ENABLED */
94 
96 {
97  vector<int> p_jobs;
98  unsigned int p_ptr;
99  void p_clear0()
100  {
101  p_jobs.clear();
102  }
103  void p_clear1()
104  {
105  p_ptr = 0;
106  }
107 public:
109  {
110  p_clear1();
111  }
112  explicit load_balance( int nJobs )
113  {
114  p_clear1();
115  init( nJobs );
116  }
118  {
119  p_clear0();
120  }
121  void clear()
122  {
123  p_clear0();
124  p_clear1();
125  }
126  void init( int nJobs );
127  int next_job()
128  {
129  if( p_ptr < p_jobs.size() )
130  {
131  int res = p_jobs[p_ptr];
132  if( cpu.i().lgMPI() )
133  p_ptr += MPI::COMM_WORLD.Get_size();
134  else
135  p_ptr++;
136  return res;
137  }
138  else
139  return -1;
140  }
141  void finalize()
142  {
143  // wait for all jobs to finish
144  if( cpu.i().lgMPI() )
145  MPI::COMM_WORLD.Barrier();
146  }
147 };
148 
150 inline string GridPointPrefix(int n)
151 {
152  ostringstream oss;
153  oss << "grid" << setfill( '0' ) << setw(9) << n << "_";
154  return oss.str();
155 }
156 
158 void process_output();
159 
161 void append_file( FILE*, const char* );
162 
163 #endif /* _MPI_UTILITIES_H_ */
load_balance::~load_balance
~load_balance()
Definition: mpi_utilities.h:117
load_balance::clear
void clear()
Definition: mpi_utilities.h:121
load_balance::p_clear0
void p_clear0()
Definition: mpi_utilities.h:99
load_balance::init
void init(int nJobs)
Definition: mpi_utilities.cpp:20
load_balance::p_ptr
unsigned int p_ptr
Definition: mpi_utilities.h:98
MPI::t_MPI
Definition: mpi_utilities.h:76
cpu
static t_cpu cpu
Definition: cpu.h:355
process_output
void process_output()
Definition: mpi_utilities.cpp:55
MPI::COMM_WORLD
t_MPI COMM_WORLD
Definition: mpi_utilities.cpp:13
t_cpu::i
t_cpu_i & i()
Definition: cpu.h:347
MPI
Definition: mpi_utilities.cpp:11
load_balance::finalize
void finalize()
Definition: mpi_utilities.h:141
GridPointPrefix
string GridPointPrefix(int n)
Definition: mpi_utilities.h:150
load_balance::p_jobs
vector< int > p_jobs
Definition: mpi_utilities.h:97
t_cpu_i::lgMPI
bool lgMPI() const
Definition: cpu.h:322
load_balance::p_clear1
void p_clear1()
Definition: mpi_utilities.h:103
sys_float
float sys_float
Definition: cddefines.h:106
load_balance::load_balance
load_balance(int nJobs)
Definition: mpi_utilities.h:112
load_balance
Definition: mpi_utilities.h:95
MPI::t_MPI::total_insanity
int total_insanity()
Definition: mpi_utilities.h:78
load_balance::next_job
int next_job()
Definition: mpi_utilities.h:127
append_file
void append_file(FILE *, const char *)
Definition: mpi_utilities.cpp:218
load_balance::load_balance
load_balance()
Definition: mpi_utilities.h:108