Psi4
psio.hpp
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 #ifndef _psi_src_lib_libpsio_psio_hpp_
30 #define _psi_src_lib_libpsio_psio_hpp_
31 
32 #include <string>
33 #include <map>
34 #include <set>
35 #include <queue>
36 #include <memory>
37 
38 #include "psi4/libpsio/config.h"
39 
40 namespace psi {
41 
42 class PSIO;
43 class PSIOManager;
44 extern std::shared_ptr<PSIO> _default_psio_lib_;
45 extern std::shared_ptr<PSIOManager> _default_psio_manager_;
46 
54 class PSIOManager {
55 private:
57  // (defaults to either $TMP, $TEMPDIR, $TMP or /tmp/ in
58  // that order)
59  std::string default_path_;
61  std::map<int, std::string> specific_paths_;
63  std::set<int> specific_retains_;
64 
66  std::map<std::string, bool> files_;
68  std::set<std::string> retained_files_;
69 
70  std::string pid_;
71 public:
73  PSIOManager();
75  ~PSIOManager();
76 
80  void mirror_to_disk();
84  void build_from_disk();
85 
90  void set_default_path(const std::string& path);
96  void set_specific_path(int fileno, const std::string& path);
102  void set_specific_retention(int fileno, bool retain);
108  bool get_specific_retention(int fileno);
109 
115  std::string get_file_path(int fileno);
116 
121  std::string get_default_path() { return default_path_; }
122 
128  void write_scratch_file(const std::string &full_path, const std::string &text);
129 
134  void open_file(const std::string & full_path, int fileno);
142  void close_file(const std::string & full_path, int fileno, bool keep);
149  void move_file(const std::string & old_full_path, const std::string & new_full_path);
156  void mark_file_for_retention(const std::string & full_path, bool retain);
161  void print(std::string out = "outfile");
165  void print_out() { print("outfile"); }
176  void psiclean();
181  void crashclean();
183  static std::shared_ptr<PSIOManager> shared_object();
184 };
185 
197 class PSIO {
198 public:
199  PSIO();
200  ~PSIO();
201 
203  int state() {
204  return state_;
205  }
218  void filecfg_kwd(const char* kwdgrp, const char* kwd, int unit,
219  const char* kwdval);
221  const std::string& filecfg_kwd(const char* kwdgrp, const char* kwd,
222  int unit);
223 
225  void rename_file(size_t old_unit,size_t new_unit);
226 
228  bool exists(size_t unit);
230  void open(size_t unit, int status);
232  void close(size_t unit, int keep);
234  std::string getpid(void);
236  void rehash(size_t unit);
238  int open_check(size_t unit);
250  void read(size_t unit, const char *key, char *buffer, size_t size,
251  psio_address start, psio_address *end);
263  void write(size_t unit, const char *key, char *buffer, size_t size,
264  psio_address start, psio_address *end);
265 
266  void read_entry(size_t unit, const char *key, char *buffer, size_t size);
267  void write_entry(size_t unit, const char *key, char *buffer, size_t size);
268 
280  void zero_disk(size_t unit, const char *key, size_t rows, size_t cols);
281 
291  void rw(size_t unit, char *buffer, psio_address address, size_t size,
292  int wrt);
293 
295  void tocclean(size_t unit, const char *key);
297  void tocprint(size_t unit);
299  psio_tocentry* tocscan(size_t unit, const char *key);
301  bool tocentry_exists(size_t unit, const char *key);
303  void tocwrite(size_t unit);
304 
306  static int _error_exit_code_;
307 
309  static void set_default_namespace(const std::string &_ns) { default_namespace_ = _ns; }
310 
312  static std::string get_default_namespace() { return default_namespace_; }
313 
315  static void change_file_namespace(size_t fileno, const std::string & ns1, const std::string & ns2);
316 
318  static std::shared_ptr<PSIO> shared_object();
319 
330  size_t rd_toclen(size_t unit);
331 
333  void get_filename(size_t unit, char **name, bool remove_namespace = false);
334 
336  bool tocdel(size_t unit, const char *key);
337 
338 private:
341 
343  std::string pid_;
344 
345 
347  static std::string default_namespace_;
348 
349  typedef std::map<std::string,std::string> KWDMap;
352 
353 #ifdef PSIO_STATS
354  size_t *psio_readlen;
355  size_t *psio_writlen;
356 #endif
357 
359  int state_;
361  size_t get_numvols(size_t unit);
363  void get_volpath(size_t unit, size_t volume, char **path);
365  psio_tocentry* toclast(size_t unit);
367  size_t toclen(size_t unit);
374  void wt_toclen(size_t unit, size_t toclen);
376  void tocread(size_t unit);
377 
378  friend class AIO_Handler;
379 
380 public:
381  void set_pid(const std::string &pid) { pid_ = pid; }
382 };
383 
384 }
385 
386 #endif /* header guard */
void status(const char *, std::string)
Definition: cchbar/status.cc:38
size_t toclen(size_t unit)
Compute the length of the TOC for a given unit using the in-core TOC list.
Definition: toclen.cc:43
psio_tocentry * toclast(size_t unit)
return the last TOC entry
Definition: toclast.cc:39
void tocclean(size_t unit, const char *key)
Delete all TOC entries after the given key. If a blank key is given, the entire TOC will be wiped...
Definition: tocclean.cc:45
std::string pid_
Definition: psio.hpp:70
void tocprint(size_t unit)
Print the table of contents for the given unit.
Definition: tocprint.cc:50
std::string get_default_path()
Definition: psio.hpp:121
std::map< std::string, std::string > KWDMap
Definition: psio.hpp:349
std::shared_ptr< PSIO > _default_psio_lib_
Definition: libpsio/init.cc:52
std::map< std::string, bool > files_
Map of files, bool denotes open or closed.
Definition: psio.hpp:66
void write_entry(size_t unit, const char *key, char *buffer, size_t size)
Definition: write_entry.cc:44
void filecfg_kwd(const char *kwdgrp, const char *kwd, int unit, const char *kwdval)
Definition: filescfg.cc:65
static int _error_exit_code_
Upon catastrophic failure, the library will exit() with this code. The default is 1...
Definition: psio.hpp:306
static std::string get_default_namespace()
Get the default namespace (for PREFIX.NAMESPACE.UNIT file numbering)
Definition: psio.hpp:312
int state()
return 1 if activated
Definition: psio.hpp:203
void rw(size_t unit, char *buffer, psio_address address, size_t size, int wrt)
Definition: rw.cc:42
~PSIO()
Definition: done.cc:49
static std::shared_ptr< PSIOManager > shared_object()
The one and (should be) only instance of PSIOManager for a PSI4 instance.
Definition: filemanager.cc:62
void build_from_disk()
Definition: filemanager.cc:201
void zero_disk(size_t unit, const char *key, size_t rows, size_t cols)
Definition: zero_disk.cc:45
Definition: libpsio/config.h:63
psio_tocentry * tocscan(size_t unit, const char *key)
Scans the TOC for a particular keyword and returns either a pointer to the entry or NULL to the calle...
Definition: tocscan.cc:45
std::string getpid(void)
lookup process id
Definition: getpid.cc:47
void crashclean()
Definition: filemanager.cc:224
static std::shared_ptr< PSIO > shared_object()
Return the global shared object.
Definition: libpsio/init.cc:118
void print_out()
Definition: psio.hpp:165
friend class AIO_Handler
Definition: psio.hpp:378
void set_specific_retention(int fileno, bool retain)
Definition: filemanager.cc:82
int state_
Library state variable.
Definition: psio.hpp:359
std::string get_file_path(int fileno)
Definition: filemanager.cc:75
void tocwrite(size_t unit)
Write the table of contents for file number &#39;unit&#39;. NB: This function should NOT call psio_error beca...
Definition: tocwrite.cc:46
KWDMap files_keywords_
library configuration is described by a set of keywords
Definition: psio.hpp:351
void set_specific_path(int fileno, const std::string &path)
Definition: filemanager.cc:71
void get_volpath(size_t unit, size_t volume, char **path)
grab the path to volume of unit and strdup into path.
Definition: get_volpath.cc:43
void psiclean()
Definition: filemanager.cc:237
~PSIOManager()
Default destructor (does nothing)
Definition: filemanager.cc:58
void rename_file(size_t old_unit, size_t new_unit)
moves a file from old_unit to new_unit
Definition: rename_file.cc:49
static void set_default_namespace(const std::string &_ns)
Set the current namespace (for PREFIX.NAMESPACE.UNIT file numbering)
Definition: psio.hpp:309
PSIOManager()
Default constructor (does nothing)
Definition: filemanager.cc:42
void mark_file_for_retention(const std::string &full_path, bool retain)
Definition: filemanager.cc:229
PSIO()
Definition: libpsio/init.cc:59
void open(size_t unit, int status)
open unit. status can be PSIO_OPEN_OLD (if existing file is to be opened) or PSIO_OPEN_NEW if new fil...
Definition: open.cc:49
void read(size_t unit, const char *key, char *buffer, size_t size, psio_address start, psio_address *end)
Definition: read.cc:47
void set_default_path(const std::string &path)
Definition: filemanager.cc:67
Definition: libpsio/config.h:73
void write(size_t unit, const char *key, char *buffer, size_t size, psio_address start, psio_address *end)
Definition: write.cc:46
size_t get_numvols(size_t unit)
return the number of volumes over which unit will be striped
Definition: get_numvols.cc:46
std::map< int, std::string > specific_paths_
Specific paths for arbitrary file numbers.
Definition: psio.hpp:61
void tocread(size_t unit)
Read the table of contents for file number &#39;unit&#39;.
Definition: tocread.cc:41
Definition: psio.hpp:54
void open_file(const std::string &full_path, int fileno)
Definition: filemanager.cc:117
std::set< std::string > retained_files_
Set of files to retain after psiclean.
Definition: psio.hpp:68
std::string pid_
Process ID.
Definition: psio.hpp:343
static void change_file_namespace(size_t fileno, const std::string &ns1, const std::string &ns2)
Change file FILENO from NS1 to NS2.
Definition: change_namespace.cc:47
void read_entry(size_t unit, const char *key, char *buffer, size_t size)
Definition: read_entry.cc:39
void close(size_t unit, int keep)
close unit. if keep == 0, will remove the file, else keep it
Definition: libpsio/close.cc:44
void write_scratch_file(const std::string &full_path, const std::string &text)
Definition: filemanager.cc:106
void print(std::string out="outfile")
Definition: filemanager.cc:138
bool tocdel(size_t unit, const char *key)
delete a specific TOC entry (only deletes entry, not data)
Definition: tocdel.cc:48
void move_file(const std::string &old_full_path, const std::string &new_full_path)
Definition: filemanager.cc:132
psio_ud * psio_unit
vector of units
Definition: psio.hpp:340
void close_file(const std::string &full_path, int fileno, bool keep)
Definition: filemanager.cc:124
std::string default_path_
Default path for unspec&#39;d file numbers.
Definition: psio.hpp:59
std::set< int > specific_retains_
Default retained files.
Definition: psio.hpp:63
std::shared_ptr< PSIOManager > _default_psio_manager_
Definition: libpsio/init.cc:53
int open_check(size_t unit)
return 1 if unit is open
Definition: open_check.cc:44
size_t rd_toclen(size_t unit)
Definition: toclen.cc:57
static std::string default_namespace_
Current default namespace (for PREFIX.NAMESPACE.UNIT numbering)
Definition: psio.hpp:347
void set_pid(const std::string &pid)
Definition: psio.hpp:381
bool tocentry_exists(size_t unit, const char *key)
Checks the TOC to see if a particular keyword exists there or not.
Definition: tocscan.cc:82
void mirror_to_disk()
Definition: filemanager.cc:184
Definition: libpsio/config.h:81
void wt_toclen(size_t unit, size_t toclen)
Definition: toclen.cc:82
void get_filename(size_t unit, char **name, bool remove_namespace=false)
grab the filename of unit and strdup into name.
Definition: get_filename.cc:43
void rehash(size_t unit)
sync up the object to the file on disk by closing and opening the file, if necessary ...
Definition: open.cc:226
bool get_specific_retention(int fileno)
Definition: filemanager.cc:95
Definition: psio.hpp:197
bool exists(size_t unit)
check if a psi unit already exists or not
Definition: open.cc:141