TECA
teca_algorithm.h
1 #ifndef teca_algorithm_h
2 #define teca_algorithm_h
3 
4 #include "teca_config.h"
5 
6 // forward delcaration of ref counted types
7 #include "teca_dataset_fwd.h"
8 #include "teca_algorithm_fwd.h"
9 #include "teca_algorithm_executive_fwd.h"
11 
12 // for types used in the api
13 #include "teca_metadata.h"
14 #include "teca_algorithm_output_port.h"
15 #include "teca_program_options.h"
16 #include "teca_mpi.h"
17 
18 #include <vector>
19 #include <utility>
20 #include <iosfwd>
21 
22 // interface to teca pipeline architecture. all sources/readers
23 // filters, sinks/writers will implement this interface
24 class teca_algorithm : public std::enable_shared_from_this<teca_algorithm>
25 {
26 public:
27  // construct/destruct
28  virtual ~teca_algorithm() noexcept;
29 
30  TECA_ALGORITHM_DELETE_COPY_ASSIGN(teca_algorithm)
31 
32  // return the name of the class.
33  virtual const char *get_class_name() const = 0;
34 
35  // set/get the communicator to use at this stage of the pipeline this has
36  // no influence on other stages. We duplicate the passed in communicator
37  // providing an isolated communication space for subsequent operations. By
38  // default the communicator is initialized to MPI_COMM_WORLD, here it is not
39  // duplicated. Thus to put an algorithm into a unique communication space
40  // one should explicitly set a communicator. When an algorithm should not
41  // use MPI, for instance when it is in a nested pipeline, one may set the
42  // communicator to MPI_COMM_SELF.
43  void set_communicator(MPI_Comm comm);
44  MPI_Comm get_communicator();
45 
46 #if defined(TECA_HAS_BOOST)
47  // initialize the given options description
48  // with algorithm's properties
49  virtual void get_properties_description(const std::string &, options_description &)
50  {}
51 
52  // initialize the algorithm from the given options
53  // variable map.
54  virtual void set_properties(const std::string &, variables_map &)
55  {}
56 #endif
57 
58  // get an output port from the algorithm. to be used
59  // during pipeline building
60  virtual
61  teca_algorithm_output_port get_output_port(unsigned int port = 0);
62 
63  // set an input to this algorithm
64  void set_input_connection(const teca_algorithm_output_port &port)
65  { this->set_input_connection(0, port); }
66 
67  virtual
68  void set_input_connection(unsigned int id,
69  const teca_algorithm_output_port &port);
70 
71  // remove input connections
72  virtual
73  void remove_input_connection(unsigned int id);
74 
75  // remove all input connections
76  void clear_input_connections();
77 
78  // access the cached data produced by this algorithm. when no
79  // request is specified the dataset on the top(most recent) of
80  // the cache is returned. When a request is specified it may
81  // optionally be filtered by the implementations cache key filter.
82  // see also get_cache_key (threadsafe)
83  const_p_teca_dataset get_output_data(unsigned int port = 0);
84 
85  // remove a dataset from the top/bottom of the cache. the
86  // top of the cache has the most recently created dataset.
87  // top or bottom is selected via the boolean argument.
88  // (threadsafe)
89  void pop_cache(unsigned int port = 0, int top = 0);
90 
91  // set the cache size. the default is 1. (threadsafe)
92  void set_cache_size(unsigned int n);
93 
94  // execute the pipeline from this instance up.
95  virtual int update();
96  virtual int update(unsigned int port);
97 
98  // get meta data considering this instance up.
99  virtual teca_metadata update_metadata(unsigned int port = 0);
100 
101  // set the executive
102  void set_executive(p_teca_algorithm_executive exe);
103  p_teca_algorithm_executive get_executive();
104 
105  // serialize the configuration to a stream. this should
106  // store the public user modifiable properties so that
107  // runtime configuration may be saved and restored..
108  virtual void to_stream(std::ostream &s) const;
109  virtual void from_stream(std::istream &s);
110 
111 protected:
112  teca_algorithm();
113 
114  // implementations should call this from their constructors
115  // to setup the internal caches and data structures required
116  // for execution.
117  void set_number_of_input_connections(unsigned int n);
118  void set_number_of_output_ports(unsigned int n);
119 
120  // set the modified flag on the given output port's cache.
121  // should be called when user modifies properties on the
122  // object that require the output to be regenerated.
123  virtual void set_modified();
124  void set_modified(unsigned int port);
125 
126 protected:
127 // this section contains methods that developers
128 // typically need to override when implementing
129 // teca_algorithm's such as reader, filters, and
130 // writers.
131 
132  // implementations must override this method to provide
133  // information to downstream consumers about what data
134  // will be produced on each output port. The port to
135  // provide information about is named in the first argument
136  // the second argument contains a list of the metadata
137  // describing data on all of the inputs.
138  virtual
139  teca_metadata get_output_metadata(unsigned int port,
140  const std::vector<teca_metadata> &input_md);
141 
142  // implementations must override this method and
143  // generate a set of requests describing the data
144  // required on the inputs to produce data for the
145  // named output port, given the upstream meta data
146  // and request. If no data is needed on an input
147  // then the list should contain a null request.
148  virtual
149  std::vector<teca_metadata> get_upstream_request(
150  unsigned int port, const std::vector<teca_metadata> &input_md,
151  const teca_metadata &request);
152 
153  // implementations must override this method and
154  // produce the output dataset for the port named
155  // in the first argument. The second argument is
156  // a list of all of the input datasets. See also
157  // get_request. The third argument contains a request
158  // from the consumer which can spcify information
159  // such as arrays, subset region, timestep etc.
160  // The implementation is free to handle the request
161  // as it sees fit.
162  virtual
163  const_p_teca_dataset execute(unsigned int port,
164  const std::vector<const_p_teca_dataset> &input_data,
165  const teca_metadata &request);
166 
167  // implementations may choose to override this method
168  // to gain control of keys used in the cache. By default
169  // the passed in request is used as the key. This overide
170  // gives implementor the chance to filter the passed in
171  // request.
172  virtual
173  teca_metadata get_cache_key(unsigned int port,
174  const teca_metadata &request) const;
175 
176 protected:
177 // this section contains methods that control the
178 // pipeline's behavior. these would typically only
179 // need to be overriden when designing a new class
180 // of algorithms.
181 
182  // driver function that manage meta data reporting phase
183  // of pipeline execution.
184  virtual
185  teca_metadata get_output_metadata(
186  teca_algorithm_output_port &current);
187 
188  // driver function that manages execution of the given
189  // requst on the named port
190  virtual
191  const_p_teca_dataset request_data(
192  teca_algorithm_output_port &port,
193  const teca_metadata &request);
194 
195  // driver function that clears the output data cache
196  // where modified flag has been set from the current
197  // port upstream.
198  virtual
199  int validate_cache(teca_algorithm_output_port &current);
200 
201  // driver function that clears the modified flag on the
202  // named port and all of it's upstream connections.
203  virtual
204  void clear_modified(teca_algorithm_output_port current);
205 
206 protected:
207 // api exposing internals for use in driver methods
208 
209  // search the given port's cache for the dataset associated
210  // with the given request. see also get_cache_key. (threadsafe)
211  const_p_teca_dataset get_output_data(unsigned int port,
212  const teca_metadata &request);
213 
214  // add or update the given request , dataset pair in the cache.
215  // see also get_cache_key. (threadsafe)
216  int cache_output_data(unsigned int port,
217  const teca_metadata &request, const_p_teca_dataset &data);
218 
219  // clear the cache on the given output port
220  void clear_cache(unsigned int port);
221 
222  // get the number of input connections
223  unsigned int get_number_of_input_connections();
224 
225  // get the output port associated with this algorithm's
226  // i'th input connection.
227  teca_algorithm_output_port &get_input_connection(unsigned int i);
228 
229  // clear the modified flag on the i'th output
230  void clear_modified(unsigned int port);
231 
232  // return the output port's modified flag value
233  int get_modified(unsigned int port) const;
234 
235 private:
236  teca_algorithm_internals *internals;
237 
238  friend class teca_threaded_algorithm;
239  friend class teca_data_request;
240 };
241 
242 #endif
teca_metadata
Definition: teca_metadata.h:17
teca_algorithm_internals
Definition: teca_algorithm.cxx:27
teca_threaded_algorithm
Definition: teca_threaded_algorithm.h:34
teca_algorithm
Definition: teca_algorithm.h:25
teca_data_request
Definition: teca_threaded_algorithm.cxx:31