RepastHPC  2.3.1
NCReducibleDataSource.h
1 /*
2  * Repast for High Performance Computing (Repast HPC)
3  *
4  * Copyright (c) 2010 Argonne National Laboratory
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with
8  * or without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice,
12  * this list of conditions and the following disclaimer.
13  *
14  * Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * Neither the name of the Argonne National Laboratory nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TRUSTEES OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * NCReducibleDataSource.h
36  *
37  * Created on: Oct 14, 2010
38  * Author: nick
39  */
40 
41 #ifndef NCREDUCIBLEDATASOURCE_H_
42 #define NCREDUCIBLEDATASOURCE_H_
43 
44 #include "NCDataSource.h"
45 #include "RepastProcess.h"
46 #include "TDataSource.h"
47 
48 #include <boost/mpi.hpp>
49 #include <vector>
50 #include <netcdfcpp.h>
51 
52 namespace repast {
53 
58 template <typename Op, typename T>
60 
61 protected:
62  Op op_;
63  std::vector<T> data;
64  TDataSource<T>* dataSource_;
65  int rank, start;
66 
67 public:
68  NCReducibleDataSource(std::string name, TDataSource<T>* dataSource, Op op);
70 
71  virtual NcType ncType();
72 
73  virtual void record();
74  virtual void write(NcVar* var);
75 
76 };
77 
78 template<typename Op, typename T>
79 NCReducibleDataSource<Op, T>::NCReducibleDataSource(std::string name, TDataSource<T>* dataSource, Op op) : NCDataSource(name), op_(op),
80 dataSource_(dataSource), start(0) {
81  rank = RepastProcess::instance()->rank();
82 };
83 
84 template<typename Op, typename T>
85 NCReducibleDataSource<Op, T>::~NCReducibleDataSource() {
86  delete dataSource_;
87 }
88 template<typename Op, typename T>
89 NcType NCReducibleDataSource<Op, T>::ncType() {
90  return NcTypeTrait<T>::type;
91 }
92 
93 template<typename Op, typename T>
94 void NCReducibleDataSource<Op, T>::record() {
95  data.push_back(dataSource_->getData());
96 }
97 
98 template<typename Op, typename T>
99 void NCReducibleDataSource<Op, T>::write(NcVar* var) {
100  boost::mpi::communicator* comm = RepastProcess::instance()->getCommunicator();
101  if (rank == 0) {
102  size_t size = data.size();
103  T* results = new T[size];
104  reduce(*comm, &data[0], size, results, op_, 0);
105 
106  var->set_cur(start, 0);
107  // writing results along the tick dimension
108  // and run dimension -- each result is indexed by the
109  // the tick values of the tick dimension and the single run dimension
110  var->put(results, size, 1);
111  start += size;
112 
113  delete[] results;
114  } else {
115  reduce(*comm, &data[0], data.size(), op_, 0);
116  }
117  data.clear();
118 }
119 
120 
121 
122 }
123 
124 
125 #endif /* NCREDUCIBLEDATASOURCE_H_ */
repast::TDataSource
Interface for class that act as datasoures for DataSets.
Definition: TDataSource.h:53
repast::RepastProcess::rank
int rank() const
Gets the rank of this process.
Definition: RepastProcess.h:378
repast::NCReducibleDataSource
Source of data and a reduction operation.
Definition: NCReducibleDataSource.h:59
repast::NCDataSource
Data source used internally by NCDataSets.
Definition: NCDataSource.h:49
repast::RepastProcess::instance
static RepastProcess * instance()
Gets this RepastProcess.
Definition: RepastProcess.cpp:126