Simpatico  v1.10
MpiLoader.h
1 #ifndef UTIL_MPI_LOADER_H
2 #define UTIL_MPI_LOADER_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/containers/DArray.h> // template argument
12 #include <util/containers/FArray.h> // template argument
13 #include <util/containers/DMatrix.h> // template argument
14 #include <util/mpi/MpiFileIo.h> // used in template implementation
15 #include <util/mpi/MpiTraits.h> // used in template implementation
16 #include <util/mpi/MpiSendRecv.h> // used in template implementation
17 #include <util/global.h>
18 
19 
20 namespace Util
21 {
22 
42  template <class IArchive>
43  class MpiLoader
44  {
45 
46  public:
47 
54  MpiLoader(MpiFileIo& mpiFileIo, IArchive& archive);
55 
61  template <typename Data>
62  void load(Data &value);
63 
70  template <typename Data>
71  void load(Data *value, int n);
72 
79  template <typename Data>
80  void load(DArray<Data>& array, int n);
81 
87  template <typename Data, int N>
88  void load(FArray<Data, N >& array);
89 
100  template <typename Data> void
101  load(Data* value, int m, int n, int np);
102 
110  template <typename Data>
111  void load(DMatrix<Data>& matrix, int m, int n);
112 
113  private:
114 
115  // Pointer to associated MpiFileIo (passed to constructor).
116  MpiFileIo* mpiFileIoPtr_;
117 
118  // Pointer to associated input archive (passed to constructor).
119  IArchive* archivePtr_;
120 
121  };
122 
123  /*
124  * Constructor
125  */
126  template <typename IArchive>
127  MpiLoader<IArchive>::MpiLoader(MpiFileIo& mpiFileIo, IArchive& archive)
128  : mpiFileIoPtr_(&mpiFileIo),
129  archivePtr_(&archive)
130  {}
131 
132  /*
133  * Load and broadcast a single Data value.
134  */
135  template <typename IArchive>
136  template <typename Data>
137  void MpiLoader<IArchive>::load(Data &value)
138  {
139  if (mpiFileIoPtr_->isIoProcessor()) {
140  *archivePtr_ >> value;
141  }
142  #ifdef UTIL_MPI
143  if (mpiFileIoPtr_->hasIoCommunicator()) {
144  bcast<Data>(mpiFileIoPtr_->ioCommunicator(), value, 0);
145  }
146  #endif
147  }
148 
149  /*
150  * Add a C array parameter, and load its elements.
151  */
152  template <typename IArchive>
153  template <typename Data>
154  void MpiLoader<IArchive>::load(Data* value, int n)
155  {
156  if (mpiFileIoPtr_->isIoProcessor()) {
157  for (int i = 0; i < n; ++i) {
158  *archivePtr_ >> value[i];
159  }
160  }
161  #ifdef UTIL_MPI
162  if (mpiFileIoPtr_->hasIoCommunicator()) {
163  bcast<Data>(mpiFileIoPtr_->ioCommunicator(), value, n, 0);
164  }
165  #endif
166  }
167 
168  /*
169  * Load a DArray < Data > container.
170  */
171  template <typename IArchive>
172  template <typename Data>
173  void
175  {
176  if (mpiFileIoPtr_->isIoProcessor()) {
177  *archivePtr_ >> array;
178  if (array.capacity() < n) {
179  UTIL_THROW("Error: DArray capacity < n");
180  }
181  }
182  #ifdef UTIL_MPI
183  if (mpiFileIoPtr_->hasIoCommunicator()) {
184  bcast<Data>(mpiFileIoPtr_->ioCommunicator(), array, n, 0);
185  }
186  #endif
187  }
188 
189  /*
190  * Load an FArray < Data, N > fixed-size 1D array container.
191  */
192  template <typename IArchive>
193  template <typename Data, int N>
195  {
196  if (mpiFileIoPtr_->isIoProcessor()) {
197  for (int i = 0; i < N; ++i) {
198  *archivePtr_ >> array[i];
199  }
200  }
201  #ifdef UTIL_MPI
202  if (mpiFileIoPtr_->hasIoCommunicator()) {
203  bcast<Data>(mpiFileIoPtr_->ioCommunicator(), &(array[0]), N, 0);
204  }
205  #endif
206  }
207 
208  /*
209  * Load a CArray2DParam < Data > C two-dimensional array parameter.
210  */
211  template <typename IArchive>
212  template <typename Data>
213  void
214  MpiLoader<IArchive>::load(Data *array, int m, int n, int np)
215  {
216  if (mpiFileIoPtr_->isIoProcessor()) {
217  int i, j;
218  for (i = 0; i < m; ++i) {
219  for (j = 0; j < n; ++j) {
220  *archivePtr_ >> array[i*np + j];
221  }
222  }
223  }
224  #ifdef UTIL_MPI
225  if (mpiFileIoPtr_->hasIoCommunicator()) {
226  // Broadcast block of m rows of np elements each.
227  bcast<Data>(mpiFileIoPtr_->ioCommunicator(), &(array[0]), m*np, 0);
228  }
229  #endif
230  }
231 
232  /*
233  * Add and load a DMatrix < Data > C two-dimensional matrix parameter.
234  */
235  template <typename IArchive>
236  template <typename Data>
237  void MpiLoader<IArchive>::load(DMatrix<Data>& matrix, int m, int n)
238  {
239  if (mpiFileIoPtr_->isIoProcessor()) {
240  *archivePtr_ >> matrix;
241  }
242  #ifdef UTIL_MPI
243  if (mpiFileIoPtr_->hasIoCommunicator()) {
244  bcast<Data>(mpiFileIoPtr_->ioCommunicator(), matrix, m, n, 0);
245  }
246  #endif
247  }
248 
249 }
250 #endif
MpiLoader(MpiFileIo &mpiFileIo, IArchive &archive)
Constructor.
Definition: MpiLoader.h:127
File containing preprocessor macros for error handling.
Identifies whether this processor may do file I/O.
Definition: MpiFileIo.h:33
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
MPI::Intracomm & ioCommunicator() const
Get the MPI communicator by reference.
Definition: MpiFileIo.h:105
Dynamically allocated Matrix.
Definition: DMatrix.h:24
Utility classes for scientific computation.
Definition: accumulators.mod:1
void load(Data &value)
Load and broadcast a single Data value.
Definition: MpiLoader.h:137
bool isIoProcessor() const
Can this processor do file I/O ?
Definition: MpiFileIo.h:92
A fixed size (static) contiguous array template.
Definition: FArray.h:46
bool hasIoCommunicator() const
Does this object have an associated MPI communicator?
Definition: MpiFileIo.h:99
Dynamically allocatable contiguous array template.
Definition: DArray.h:31
Provides methods for MPI-aware loading of data from input archive.
Definition: MpiLoader.h:43
This file contains templates for global functions send<T>, recv<T> and bcast<T>.