TECA
The Toolkit for Extreme Climate Analysis
teca_cf_time_step_mapper.h
1 #ifndef teca_cf_time_step_mapper_h
2 #define teca_cf_time_step_mapper_h
3 
4 #include "teca_config.h"
5 #include "teca_metadata.h"
6 #include "teca_cf_layout_manager.h"
7 #include "teca_mpi.h"
8 
9 #include <iostream>
10 #include <sstream>
11 #include <cstring>
12 #include <cerrno>
13 #include <string>
14 #include <unordered_map>
15 #include <vector>
16 #include <memory>
17 
19 using p_teca_cf_time_step_mapper = std::shared_ptr<teca_cf_time_step_mapper>;
20 
21 /// Defines the interface for mapping time steps to files
23 {
24 public:
25  virtual ~teca_cf_time_step_mapper() {}
26 
27  // returns true if the mapper has been successfully initialized
28  virtual bool initialized() { return this->file_comms.size(); }
29 
30  // close all files, destroy file managers, and release communicators
31  // this should be done once all I/O is complete.
32  virtual int finalize();
33 
34  // construct requests for this rank
35  virtual int get_upstream_requests(teca_metadata base_req,
36  std::vector<teca_metadata> &up_reqs);
37 
38  // given a time step, get the corresponding layout manager that
39  // can be used to create, define and write data to disk.
40  virtual p_teca_cf_layout_manager get_layout_manager(long time_step) = 0;
41 
42  // print a summary to the stream
43  virtual int to_stream(std::ostream &os) = 0;
44 
45  // call the passed in functor once per file table entry, safe
46  // for MPI collective operations. The required functor signature
47  // is:
48  // int f(long file_id, teca_cf_layout_manager &manager)
49  //
50  // a return of non-zero from the functor will immediately stop the
51  // apply and the value will be returned, but no error will be
52  // reported.
53  template<typename op_t>
54  int file_table_apply(const op_t &op);
55 
56 protected:
57  teca_cf_time_step_mapper() : index_initializer_key(""),
58  index_request_key(""), start_time_step(0), end_time_step(-1),
59  n_files(0)
60  {}
61 
62  // remove these for convenience
65  void operator=(const teca_cf_time_step_mapper&) = delete;
66  void operator=(const teca_cf_time_step_mapper&&) = delete;
67 
68  // create/free the per-file communicators
69  int alloc_file_comms();
70  int free_file_comms();
71 
72 protected:
73  // communicator to partition into per-file communicators
74  MPI_Comm comm;
75 
76  // pipeline control key names
77  std::string index_initializer_key;
78  std::string index_request_key;
79 
80  // user provided overrides
81  long start_time_step;
82  long end_time_step;
83 
84  // time_steps to request by rank
85  long n_time_steps;
86  std::vector<long> block_size;
87  std::vector<long> block_start;
88 
89  // output files
90  long n_files;
91  std::vector<std::set<int>> file_ranks;
92 
93  // per file communicators
94  std::vector<MPI_Comm> file_comms;
95 
96  // the file table maps from a time step to a specific layout manager
97  using file_table_t = std::unordered_map<long, p_teca_cf_layout_manager>;
98  file_table_t file_table;
99 };
100 
101 
102 // --------------------------------------------------------------------------
103 template<typename op_t>
104 int teca_cf_time_step_mapper::file_table_apply(const op_t &op)
105 {
106  for (long i = 0; i < this->n_files; ++i)
107  {
108  MPI_Comm comm_i = this->file_comms[i];
109  if (comm_i != MPI_COMM_NULL)
110  {
111  if (int ierr = op(comm, i, this->file_table[i]))
112  return ierr;
113  }
114  }
115  return 0;
116 }
117 
118 #endif
teca_metadata
A generic container for meta data in the form of name=value pairs.
Definition: teca_metadata.h:21
teca_cf_time_step_mapper
Defines the interface for mapping time steps to files.
Definition: teca_cf_time_step_mapper.h:22
teca_error::TECA_EXPORT
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.