libSplash
HandleMgr.hpp
1 
23 #ifndef HANDLEMGR_HPP
24 #define HANDLEMGR_HPP
25 
26 #include <map>
27 #include <set>
28 #include <string>
29 
30 #include "splash/DCException.hpp"
31 #include "splash/Dimensions.hpp"
32 
33 namespace splash
34 {
35 
40  typedef hid_t H5Handle;
41 
42  static const H5Handle INVALID_HANDLE = -1;
43 
50  class HandleMgr
51  {
52  private:
53 
54  typedef struct
55  {
56  H5Handle handle;
57  uint32_t ctr;
58  } HandleCtrStr;
59 
60  typedef struct
61  {
62  uint32_t index;
63  uint32_t ctr;
64  } IndexCtrStr;
65 
66  typedef std::map<uint32_t, HandleCtrStr> HandleMap;
67 
68  public:
75  enum FileNameScheme
76  {
77  FNS_MPI = 0, FNS_ITERATIONS, FNS_FULLNAME
78  };
79 
80  // callback function types
81  typedef void (*FileCreateCallback)(H5Handle handle, uint32_t index, void *userData);
82  typedef void (*FileOpenCallback)(H5Handle handle, uint32_t index, void *userData);
83  typedef void (*FileCloseCallback)(H5Handle handle, uint32_t index, void *userData);
84 
90  HandleMgr(uint32_t maxHandles, FileNameScheme fileNameScheme);
91 
95  virtual ~HandleMgr();
96 
102  void setFileNameScheme(FileNameScheme fileNameScheme) throw (DCException);
103 
111  void open(Dimensions mpiSize, const std::string baseFilename,
112  hid_t fileAccProperties, unsigned flags) throw (DCException);
113 
121  void open(const std::string fullFilename,
122  hid_t fileAccProperties, unsigned flags) throw (DCException);
123 
127  void close();
128 
134  H5Handle get(uint32_t index) throw (DCException);
135 
141  H5Handle get(Dimensions mpiPos) throw (DCException);
142 
148  void registerFileCreate(FileCreateCallback callback, void *userData);
149 
155  void registerFileOpen(FileOpenCallback callback, void *userData);
156 
162  void registerFileClose(FileCloseCallback callback, void *userData);
163 
164  private:
165  uint32_t maxHandles;
166 
167  Dimensions mpiSize;
168  std::string filename;
169  FileNameScheme fileNameScheme;
170 
171  hid_t fileAccProperties;
172  unsigned fileFlags;
173 
174  HandleMap handles;
175  IndexCtrStr leastAccIndex;
176  std::set<uint32_t> createdFiles;
177 
178  // callback handles
179  FileCreateCallback fileCreateCallback;
180  void *fileCreateUserData;
181  FileOpenCallback fileOpenCallback;
182  void *fileOpenUserData;
183  FileCloseCallback fileCloseCallback;
184  void *fileCloseUserData;
185 
186  static std::string getExceptionString(std::string func,
187  std::string msg, const char *info);
188 
189  uint32_t indexFromPos(Dimensions& mpiPos);
190  Dimensions posFromIndex(uint32_t index);
191  };
196 }
197 
198 #endif /* HANDLEMGR_HPP */