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