TECA
The Toolkit for Extreme Climate Analysis
teca_index_reduce.h
1 #ifndef teca_index_reduce_h
2 #define teca_index_reduce_h
3 
4 #include "teca_config.h"
5 #include "teca_dataset.h"
6 #include "teca_metadata.h"
7 #include "teca_shared_object.h"
8 #include "teca_threaded_algorithm.h"
9 
10 #include <vector>
11 
12 TECA_SHARED_OBJECT_FORWARD_DECL(teca_index_reduce)
13 
14 /// Base class for MPI+threads+GPUs map reduce reduction over a set of indices.
15 /**
16  * The available indices are partitioned across MPI ranks and threads. Threads
17  * are assigned to service GPUs or CPU cores. One can restrict operation to a
18  * range of time steps by setting first and last indices to process.
19  *
20  * ### metadata keys:
21  *
22  * | key | description |
23  * | ---- | ----------- |
24  * | index_initializer_key | Holds the name of the key that tells how many |
25  * | | indices are available. The named key must also |
26  * | | be present and should contain the number of |
27  * | | indices available. |
28  * | index_request_key | Holds the name of the key used to request a |
29  * | | specific index. Requests are generated with this |
30  * | | name set to a specific index to be processed |
31  * | | some upstream algorithm is expected to produce |
32  * | | the data associated with the given index. |
33  */
35 {
36 public:
37  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_index_reduce)
38  virtual ~teca_index_reduce(){}
39 
40  // report/initialize to/from Boost program options
41  // objects.
42  TECA_GET_ALGORITHM_PROPERTIES_DESCRIPTION()
43  TECA_SET_ALGORITHM_PROPERTIES()
44 
45  /** @name start_index
46  * set the first index to process. Indices go from 0 to n-1. The default
47  * start_index is 0.
48  */
49  ///@{
50  TECA_ALGORITHM_PROPERTY(long, start_index)
51  ///@}
52 
53  /** @name end_index
54  * set the last index to process. Indices go from 0 to n-1. The default
55  * end_index is -1, which is used to indicate all indices should be
56  * processed.
57  */
58  ///@{
59  TECA_ALGORITHM_PROPERTY(long, end_index)
60  ///@}
61 
62 protected:
64 
65 protected:
66 
67  /** An override that implements the reduction. given two datasets a left
68  * and right, reduce into a single dataset and return.
69  *
70  * @param[in] device_id The device that should be used for the reduction.
71  * A value of -1 indicates the CPU should be used.
72  * @param[in] left a dataset to reduce
73  * @param[in] right a dataset to reduce
74  *
75  * @returns the reduced dataset
76  */
77  virtual p_teca_dataset reduce(int device_id,
78  const const_p_teca_dataset &left,
79  const const_p_teca_dataset &right) = 0;
80 
81  /** An override that is called when the reduction is complete. The default
82  * implementation passes data through. This might be used for instance to
83  * complete an averaging operation where the ::reduce override sums the
84  * data and the ::finalize override scales by 1/N, where N is the number of
85  * datasets summed.
86  *
87  * @param[in] device_id The device that should be used for the reduction.
88  * A value of -1 indicates the CPU should be used.
89  * @param[in] ds the reduced dataset
90  *
91  * @returns a dataset that has been finalized.
92  */
93  virtual p_teca_dataset finalize(int device_id,
94  const const_p_teca_dataset &ds)
95  {
96  (void) device_id;
97  return std::const_pointer_cast<teca_dataset>(ds);
98  }
99 
100  /** An override that allows derived classes to generate upstream requests
101  * that will be applied over all time steps. derived classes implement this
102  * method instead of ::get_upstream_request, which here is already
103  * implemented to handle the application of requests over the index set.
104  */
105  virtual std::vector<teca_metadata> initialize_upstream_request(
106  unsigned int port, const std::vector<teca_metadata> &input_md,
107  const teca_metadata &request) = 0;
108 
109  /** An override that allows derived classes to report what they can
110  * produce. this will be called from ::get_output_metadata which will strip
111  * out time and partition time across MPI ranks.
112  */
113  virtual teca_metadata initialize_output_metadata(unsigned int port,
114  const std::vector<teca_metadata> &input_md) = 0;
115 
116 protected:
117 // customized pipeline behavior and parallel code.
118 // most derived classes won't need to override these.
119 
121  using teca_threaded_algorithm::execute;
122 
123  /** Generates an upstream request for each index. will call
124  * initialize_upstream_request and apply the results to all time steps.
125  */
126  std::vector<teca_metadata> get_upstream_request(
127  unsigned int port, const std::vector<teca_metadata> &input_md,
128  const teca_metadata &request) override;
129 
130  /** Uses MPI communication to collect remote data for required for the
131  * reduction. Calls ::reduce with each pair of datasets until the datasets
132  * across all threads and ranks are reduced into a single dataset, which is
133  * returned.
134  */
135  const_p_teca_dataset execute(unsigned int port,
136  const std::vector<const_p_teca_dataset> &input_data,
137  const teca_metadata &request, int streaming) override;
138 
139  /// consumes index metadata, and partitions indices across MPI ranks.
140  teca_metadata get_output_metadata(unsigned int port,
141  const std::vector<teca_metadata> &input_md) override;
142 
143 private:
144  /** The driver for reducing the local datasets. Calls the ::reduce override as
145  * needed.
146  */
147  const_p_teca_dataset reduce_local(int device_id,
148  std::vector<const_p_teca_dataset> local_data);
149 
150  /** The driver for reducing the remote datasets. Calls the ::reduce override
151  * as needed.
152  */
153  const_p_teca_dataset reduce_remote(int device_id,
154  const_p_teca_dataset local_data);
155 
156 private:
157  long start_index;
158  long end_index;
159 };
160 
161 #endif
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_algorithm::get_output_metadata
virtual teca_metadata get_output_metadata(unsigned int port, const std::vector< teca_metadata > &input_md)
teca_shared_object.h
teca_geometry::left
TECA_EXPORT bool left(n_t e0x, n_t e0y, n_t e1x, n_t e1y, n_t px, n_t py)
tests if a point is Left|On|Right of an infinite line.
Definition: teca_geometry.h:19
teca_error::TECA_EXPORT
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
teca_index_reduce
Base class for MPI+threads+GPUs map reduce reduction over a set of indices.
Definition: teca_index_reduce.h:34
teca_threaded_algorithm
This is the base class defining a threaded algorithm.
Definition: teca_threaded_algorithm.h:60