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_dataset.h"
5 #include "teca_metadata.h"
6 #include "teca_shared_object.h"
7 #include "teca_threaded_algorithm.h"
8 
9 #include <vector>
10 
11 TECA_SHARED_OBJECT_FORWARD_DECL(teca_index_reduce)
12 
13 /// Base class for MPI + threads map reduce reduction over an index.
14 /**
15  * The available indices are partitioned across MPI ranks and threads. One can
16  * restrict operation to a range of time steps by setting first and last
17  * indices to process.
18  *
19  * metadata keys:
20  *
21  * requires:
22  *
23  * index_initializer_key -- holds the name of the key that tells how
24  * many indices are available. the named key
25  * must also be present and should contain the
26  * number of indices available
27  *
28  * index_request_key -- holds the name of the key used to request
29  * a specific index. request are generated with this
30  * name set to a specific index to be processed some
31  * upstream algorithm is expected to produce the
32  * data associated with the given index
33  *
34  * consumes:
35  *
36  * The key named by index_request_key
37  */
39 {
40 public:
41  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_index_reduce)
42  virtual ~teca_index_reduce(){}
43 
44  // report/initialize to/from Boost program options
45  // objects.
46  TECA_GET_ALGORITHM_PROPERTIES_DESCRIPTION()
47  TECA_SET_ALGORITHM_PROPERTIES()
48 
49  // set the range of time steps to process.
50  // setting first_step=0 and last_step=-1 results
51  // in processing all available steps. this is
52  // the default.
53  TECA_ALGORITHM_PROPERTY(long, start_index)
54  TECA_ALGORITHM_PROPERTY(long, end_index)
55 
56 protected:
58 
59 protected:
60  // override that implements the reduction. given two datasets
61  // a left and right, reduce into a single dataset and return.
62  virtual p_teca_dataset reduce(const const_p_teca_dataset &left,
63  const const_p_teca_dataset &right) = 0;
64 
65  // override that is called when the reduction is complete.
66  // the default implementation passes data through.
67  virtual p_teca_dataset finalize(const const_p_teca_dataset &ds)
68  {
69  return std::const_pointer_cast<teca_dataset>(ds);
70  }
71 
72  // override that allows derived classes to generate upstream
73  // requests that will be applied over all time steps. derived
74  // classes implement this method instead of get_upstream_request,
75  // which here is already implemented to handle the application
76  // of requests over all timesteps.
77  virtual std::vector<teca_metadata> initialize_upstream_request(
78  unsigned int port, const std::vector<teca_metadata> &input_md,
79  const teca_metadata &request) = 0;
80 
81  // override that allows derived classes to report what they can
82  // produce. this will be called from get_output_metadata which
83  // will strip out time and partition time across MPI ranks.
84  virtual teca_metadata initialize_output_metadata(unsigned int port,
85  const std::vector<teca_metadata> &input_md) = 0;
86 
87 protected:
88 // customized pipeline behavior and parallel code.
89 // most derived classes won't need to override these.
90 
91  // generates an upstream request for each timestep. will
92  // call initialize_upstream_request and apply the results to
93  // all time steps.
94  std::vector<teca_metadata> get_upstream_request(
95  unsigned int port, const std::vector<teca_metadata> &input_md,
96  const teca_metadata &request) override;
97 
98  // uses MPI communication to collect remote data for
99  // required for the reduction. calls "reduce" with
100  // each pair of datasets until the datasets across
101  // all threads and ranks are reduced into a single
102  // dataset, which is returned.
103  const_p_teca_dataset execute(unsigned int port,
104  const std::vector<const_p_teca_dataset> &input_data,
105  const teca_metadata &request, int streaming) override;
106 
107  // consumes time metadata, partitions time's across
108  // MPI ranks.
109  teca_metadata get_output_metadata(unsigned int port,
110  const std::vector<teca_metadata> &input_md) override;
111 
112 private:
113  // drivers for reducing the local and remote datasets.
114  // calls reduce override as needed.
115  const_p_teca_dataset reduce_local(
116  std::vector<const_p_teca_dataset> local_data);
117 
118  const_p_teca_dataset reduce_remote(const_p_teca_dataset local_data);
119 
120 private:
121  long start_index;
122  long end_index;
123 };
124 
125 #endif
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:18
teca_shared_object.h
teca_geometry::left
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:17
teca_index_reduce
Base class for MPI + threads map reduce reduction over an index.
Definition: teca_index_reduce.h:38
teca_threaded_algorithm
This is the base class defining a threaded algorithm.
Definition: teca_threaded_algorithm.h:46