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