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