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(unsigned int old_unit,unsigned int new_unit);
226 
228  bool exists(unsigned int unit);
230  void open(unsigned int unit, int status);
232  void close(unsigned int unit, int keep);
234  std::string getpid(void);
236  void rehash(unsigned int unit);
238  int open_check(unsigned int unit);
250  void read(unsigned int unit, const char *key, char *buffer, ULI size,
251  psio_address start, psio_address *end);
263  void write(unsigned int unit, const char *key, char *buffer, ULI size,
264  psio_address start, psio_address *end);
265 
266  void read_entry(unsigned int unit, const char *key, char *buffer, ULI size);
267  void write_entry(unsigned int unit, const char *key, char *buffer, ULI size);
268 
280  void zero_disk(unsigned int unit, const char *key, ULI rows, ULI cols);
281 
291  void rw(unsigned int unit, char *buffer, psio_address address, ULI size,
292  int wrt);
293 
295  void tocclean(unsigned int unit, const char *key);
297  void tocprint(unsigned int unit);
299  psio_tocentry* tocscan(unsigned int unit, const char *key);
301  bool tocentry_exists(unsigned int unit, const char *key);
303  void tocwrite(unsigned int 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(unsigned int fileno, const std::string & ns1, const std::string & ns2);
316 
318  static std::shared_ptr<PSIO> shared_object();
319 
330  ULI rd_toclen(unsigned int unit);
331 
333  void get_filename(unsigned int unit, char **name, bool remove_namespace = false);
334 
336  bool tocdel(unsigned int 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  ULI *psio_readlen;
355  ULI *psio_writlen;
356 #endif
357 
359  int state_;
361  unsigned int get_numvols(unsigned int unit);
363  void get_volpath(unsigned int unit, unsigned int volume, char **path);
365  psio_tocentry* toclast(unsigned int unit);
367  unsigned int toclen(unsigned int unit);
374  void wt_toclen(unsigned int unit, ULI toclen);
376  void tocread(unsigned int 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 tocread(unsigned int unit)
Read the table of contents for file number &#39;unit&#39;.
Definition: tocread.cc:41
void status(const char *, std::string)
Definition: cchbar/status.cc:38
std::string pid_
Definition: psio.hpp:70
void zero_disk(unsigned int unit, const char *key, ULI rows, ULI cols)
Definition: zero_disk.cc:45
psio_tocentry * tocscan(unsigned int 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 get_default_path()
Definition: psio.hpp:121
std::map< std::string, std::string > KWDMap
Definition: psio.hpp:349
void tocprint(unsigned int unit)
Print the table of contents for the given unit.
Definition: tocprint.cc:47
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 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
void tocwrite(unsigned int 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
static std::string get_default_namespace()
Get the default namespace (for PREFIX.NAMESPACE.UNIT file numbering)
Definition: psio.hpp:312
void get_filename(unsigned int unit, char **name, bool remove_namespace=false)
grab the filename of unit and strdup into name.
Definition: get_filename.cc:43
int state()
return 1 if activated
Definition: psio.hpp:203
~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
Definition: libpsio/config.h:65
unsigned long int ULI
Definition: tensors.h:46
std::string getpid(void)
lookup process id
Definition: getpid.cc:46
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
void rehash(unsigned int unit)
sync up the object to the file on disk by closing and opening the file, if necessary ...
Definition: open.cc:226
psio_tocentry * toclast(unsigned int unit)
return the last TOC entry
Definition: toclast.cc:39
unsigned int get_numvols(unsigned int unit)
return the number of volumes over which unit will be striped
Definition: get_numvols.cc:46
void close(unsigned int unit, int keep)
close unit. if keep == 0, will remove the file, else keep it
Definition: libpsio/close.cc:44
int state_
Library state variable.
Definition: psio.hpp:359
std::string get_file_path(int fileno)
Definition: filemanager.cc:75
void wt_toclen(unsigned int unit, ULI toclen)
Definition: toclen.cc:82
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 psiclean()
Definition: filemanager.cc:237
~PSIOManager()
Default destructor (does nothing)
Definition: filemanager.cc:58
void write_entry(unsigned int unit, const char *key, char *buffer, ULI size)
Definition: write_entry.cc:44
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 tocclean(unsigned int 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
void set_default_path(const std::string &path)
Definition: filemanager.cc:67
Definition: libpsio/config.h:75
bool exists(unsigned int unit)
check if a psi unit already exists or not
Definition: open.cc:141
void read(unsigned int unit, const char *key, char *buffer, ULI size, psio_address start, psio_address *end)
Definition: read.cc:47
std::map< int, std::string > specific_paths_
Specific paths for arbitrary file numbers.
Definition: psio.hpp:61
void write(unsigned int unit, const char *key, char *buffer, ULI size, psio_address start, psio_address *end)
Definition: write.cc:46
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
bool tocentry_exists(unsigned int unit, const char *key)
Checks the TOC to see if a particular keyword exists there or not.
Definition: tocscan.cc:82
std::string pid_
Process ID.
Definition: psio.hpp:343
void read_entry(unsigned int unit, const char *key, char *buffer, ULI size)
Definition: read_entry.cc:39
void write_scratch_file(const std::string &full_path, const std::string &text)
Definition: filemanager.cc:106
bool tocdel(unsigned int unit, const char *key)
delete a specific TOC entry (only deletes entry, not data)
Definition: tocdel.cc:48
void print(std::string out="outfile")
Definition: filemanager.cc:138
void rw(unsigned int unit, char *buffer, psio_address address, ULI size, int wrt)
Definition: rw.cc:42
void move_file(const std::string &old_full_path, const std::string &new_full_path)
Definition: filemanager.cc:132
static void change_file_namespace(unsigned int fileno, const std::string &ns1, const std::string &ns2)
Change file FILENO from NS1 to NS2.
Definition: change_namespace.cc:47
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
ULI rd_toclen(unsigned int unit)
Definition: toclen.cc:57
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
void rename_file(unsigned int old_unit, unsigned int new_unit)
moves a file from old_unit to new_unit
Definition: rename_file.cc:50
void get_volpath(unsigned int unit, unsigned int volume, char **path)
grab the path to volume of unit and strdup into path.
Definition: get_volpath.cc:43
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
int open_check(unsigned int unit)
return 1 if unit is open
Definition: open_check.cc:44
void mirror_to_disk()
Definition: filemanager.cc:184
Definition: libpsio/config.h:83
unsigned int toclen(unsigned int unit)
Compute the length of the TOC for a given unit using the in-core TOC list.
Definition: toclen.cc:43
void open(unsigned int 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
bool get_specific_retention(int fileno)
Definition: filemanager.cc:95
Definition: psio.hpp:197