RepastHPC  2.3.1
ReducibleDataSource.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  * ReducibleDataSource.h
36  *
37  * Created on: Aug 23, 2010
38  * Author: nick
39  */
40 
41 #ifndef REDUCEABLEDATASOURCE_H_
42 #define REDUCEABLEDATASOURCE_H_
43 
44 #include <vector>
45 #include <boost/mpi.hpp>
46 
47 #include "TDataSource.h"
48 #include "SVDataSource.h"
49 #include "Variable.h"
50 #include "RepastProcess.h"
51 
52 namespace repast {
53 
58 template <typename Op, typename T>
60 
61 private:
62  bool dirty;
63 
64 protected:
65  Op _op;
66  std::vector<T> data;
67  TDataSource<T>* _dataSource;
68  int rank;
69 
70 public:
71  ReducibleDataSource(std::string name, TDataSource<T>* dataSource, Op op);
73 
74  virtual void record();
75  virtual void write(Variable* var);
76  virtual SVDataSource::DataType type() const {
78  }
79 };
80 
81 template<typename Op, typename T>
82 ReducibleDataSource<Op, T>::ReducibleDataSource(std::string name, TDataSource<T>* dataSource, Op op) : SVDataSource(name), dirty(false), _op(op),
83 _dataSource(dataSource) {
84  rank = RepastProcess::instance()->rank();
85 };
86 
87 template<typename Op, typename T>
88 ReducibleDataSource<Op, T>::~ReducibleDataSource() {
89  delete _dataSource;
90 }
91 
92 template<typename Op, typename T>
93 void ReducibleDataSource<Op, T>::record() {
94  data.push_back(_dataSource->getData());
95 }
96 
97 template<typename Op, typename T>
98 void ReducibleDataSource<Op, T>::write(Variable* var) {
99  boost::mpi::communicator* comm = RepastProcess::instance()->getCommunicator();
100  if (rank == 0) {
101  size_t size = data.size();
102  T* results = new T[size];
103  reduce(*comm, &data[0], size, results, _op, 0);
104  var->insert(results, size);
105  delete[] results;
106  } else {
107  reduce(*comm, &data[0], data.size(), _op, 0);
108  }
109  data.clear();
110 }
111 
112 }
113 
114 #endif /* REDUCEABLEDATASOURCE_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::Variable
Used in SVDataSet to manage and store the data.
Definition: Variable.h:52
repast::data_type_traits
Base class for specialized int and double type classes.
Definition: SVDataSource.h:78
repast::SVDataSource
Data source for data to be written into separated-value data sets.
Definition: SVDataSource.h:54
repast::RepastProcess::instance
static RepastProcess * instance()
Gets this RepastProcess.
Definition: RepastProcess.cpp:126
repast::ReducibleDataSource
Source of data and a reduction operation.
Definition: ReducibleDataSource.h:59