TECA
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_metadata.h"
5 #include "teca_cf_layout_manager.h"
6 #include "teca_mpi.h"
7 
8 #include <iostream>
9 #include <sstream>
10 #include <cstring>
11 #include <cerrno>
12 #include <string>
13 #include <unordered_map>
14 #include <vector>
15 #include <memory>
16 
18 using p_teca_cf_time_step_mapper = std::shared_ptr<teca_cf_time_step_mapper>;
19 
21 {
22 public:
23 
24  // allocate and return a new object
25  static p_teca_cf_time_step_mapper New()
26  { return p_teca_cf_time_step_mapper(new teca_cf_time_step_mapper); }
27 
29 
30  // intialize based on input metadata. this is a collective call
31  // creates communicator groups for each file and creates the
32  // file layout managers for the local rank. After this call
33  // on can access file managers to create, define and write
34  // local datasets to the netcdf files in cf format.
35  int initialize(MPI_Comm comm, long first_step, long last_step,
36  long steps_per_file, const teca_metadata &md);
37 
38  // returns true if the mapper has been successfully initallized
39  bool initialized() { return this->file_comms.size(); }
40 
41  // close all files, destroy file managers, and release communicators
42  // this should be done once all I/O is complete.
43  int finalize();
44 
45  // construct requests for this rank
46  int get_upstream_requests(teca_metadata base_req,
47  std::vector<teca_metadata> &up_reqs);
48 
49  // given a time step, get the corresponding layout manager that
50  // can be used to create, define and write data to disk.
51  p_teca_cf_layout_manager get_layout_manager(long time_step);
52 
53  // print a summary to the stream
54  int to_stream(std::ostream &os);
55 
56  // call the passed in functor once per file table entry, safe
57  // for MPI collective operations. The required functor signature
58  // is:
59  // int f(long file_id, teca_cf_layout_manager &manager)
60  //
61  // a return of non-zero from the fucntor will immediately stop the
62  // apply and the value will be returned, but no error will be
63  // reported.
64  template<typename op_t>
65  int file_table_apply(const op_t &op);
66 
67 protected:
68  teca_cf_time_step_mapper() : index_initializer_key(""),
69  index_request_key(""), start_time_step(0), end_time_step(-1),
70  n_files(0), n_time_steps_per_file(1)
71  {}
72 
73  // remove these for convenience
76  void operator=(const teca_cf_time_step_mapper&) = delete;
77  void operator=(const teca_cf_time_step_mapper&&) = delete;
78 
79  // create/free the per-file communicators
80  int alloc_file_comms();
81  int free_file_comms();
82 
83  // given a time step, get the corresponding file id
84  int get_file_id(long time_step, long &file_id);
85 
86 private:
87  // communicator to partition into per-file communicators
88  MPI_Comm comm;
89 
90  // pipeline control key names
91  std::string index_initializer_key;
92  std::string index_request_key;
93 
94  // user provided overrides
95  long start_time_step;
96  long end_time_step;
97 
98  // time_steps to request by rank
99  long n_time_steps;
100  std::vector<long> block_size;
101  std::vector<long> block_start;
102 
103  // output files
104  long n_files;
105  long n_time_steps_per_file;
106  std::vector<std::set<int>> file_ranks;
107 
108  // per file communcators
109  std::vector<MPI_Comm> file_comms;
110 
111  // the file table maps from a time step to a specific layout manager
112  using file_table_t = std::unordered_map<long, p_teca_cf_layout_manager>;
113  file_table_t file_table;
114 };
115 
116 
117 // --------------------------------------------------------------------------
118 template<typename op_t>
119 int teca_cf_time_step_mapper::file_table_apply(const op_t &op)
120 {
121  for (long i = 0; i < this->n_files; ++i)
122  {
123  MPI_Comm comm_i = this->file_comms[i];
124  if (comm_i != MPI_COMM_NULL)
125  {
126  if (int ierr = op(comm, i, this->file_table[i]))
127  return ierr;
128  }
129  }
130  return 0;
131 }
132 
133 #endif
teca_metadata
Definition: teca_metadata.h:17
teca_cf_time_step_mapper
Definition: teca_cf_time_step_mapper.h:21