TECA
The Toolkit for Extreme Climate Analysis
teca_programmable_reduce.h
1 #ifndef teca_programmable_reduce_h
2 #define teca_programmable_reduce_h
3 
4 #include "teca_dataset.h"
5 #include "teca_metadata.h"
6 #include "teca_shared_object.h"
7 #include "teca_programmable_algorithm.h"
8 #include "teca_index_reduce.h"
9 
10 #include <string>
11 #include <vector>
12 #include <functional>
13 
14 TECA_SHARED_OBJECT_FORWARD_DECL(teca_programmable_reduce)
15 
16 #ifdef SWIG
17 typedef void* reduce_callback_t;
18 typedef void* finalize_callback_t;
19 #else
20 /// A callable that can reduce two datasets into one.
21 using reduce_callback_t = std::function<p_teca_dataset(
22  const const_p_teca_dataset &, const const_p_teca_dataset &)>;
23 
24 /// A callable that can finalize the reduction.
25 using finalize_callback_t = std::function<p_teca_dataset(
26  const const_p_teca_dataset &)>;
27 #endif
28 
29 /// Callbacks implement a user defined reduction over time steps.
30 /**
31  * Callbacks implement a reduction on teca_datasets over time steps.
32  * User provides reduce callable that takes 2 datasets and produces
33  * a third reduced dataset. Callbacks should be threadsafe as this is
34  * a parallel operation. See teca_index_reduce for details of
35  * parallelization.
36  */
38 {
39 public:
40  TECA_ALGORITHM_STATIC_NEW(teca_programmable_reduce)
41  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_programmable_reduce)
43 
44  // set the implementation name, this is used in logging to
45  // identify the specific instance of programmable reduce
46  int set_name(const std::string &name);
47 
48  const char *get_class_name() const override
49  { return this->class_name; }
50 
51  // set the callback that initializes the output metadata during
52  // report phase of the pipeline. The callback must be a callable
53  // with the signature:
54  //
55  // teca_metadata report_callback(unsigned int port,
56  // const std::vector<teca_metadata> &input_md);
57  //
58  // the default implementation forwards downstream
59  TECA_ALGORITHM_CALLBACK_PROPERTY(report_callback_t, report_callback)
60 
61  // set the callback that initializes the upstream request.
62  // The callback must be a callable with the signature:
63  //
64  // std::vector<teca_metadata> request(
65  // unsigned int port, const std::vector<teca_metadata> &input_md,
66  // const teca_metadata &request) override;
67  //
68  // the default implementation forwards upstream
69  TECA_ALGORITHM_CALLBACK_PROPERTY(request_callback_t, request_callback)
70 
71  // set the callback that performs the reduction on 2 datasets
72  // returning the reduced dataset. The callback must be a callable
73  // with the signature:
74  //
75  // p_teca_dataset reduce(const const_p_teca_dataset &left,
76  // const const_p_teca_dataset &right);
77  //
78  // the default implementation returns a nullptr
79  TECA_ALGORITHM_CALLBACK_PROPERTY(reduce_callback_t, reduce_callback)
80 
81  // set the callback that finalizes the reduction.
82  // The callback must be a callable with the signature:
83  //
84  // p_teca_dataset reduce(const const_p_teca_dataset &ds);
85  //
86  // the default implementation passes the input dataset
87  // through
88  TECA_ALGORITHM_CALLBACK_PROPERTY(finalize_callback_t, finalize_callback)
89 
90 protected:
92 
93  // overrides
94  p_teca_dataset reduce(const const_p_teca_dataset &left,
95  const const_p_teca_dataset &right) override;
96 
97  p_teca_dataset finalize(const const_p_teca_dataset &input) override;
98 
99  std::vector<teca_metadata> initialize_upstream_request(
100  unsigned int port, const std::vector<teca_metadata> &input_md,
101  const teca_metadata &request) override;
102 
103  teca_metadata initialize_output_metadata(unsigned int port,
104  const std::vector<teca_metadata> &input_md) override;
105 
106 private:
107  reduce_callback_t reduce_callback;
108  finalize_callback_t finalize_callback;
109  request_callback_t request_callback;
110  report_callback_t report_callback;
111  char class_name[64];
112 };
113 
114 #endif
teca_programmable_reduce
Callbacks implement a user defined reduction over time steps.
Definition: teca_programmable_reduce.h:37
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_programmable_reduce::get_class_name
const char * get_class_name() const override
return the name of the class.
Definition: teca_programmable_reduce.h:48