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 program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  * @END LICENSE
26  */
27 
28 #ifndef _psi_src_lib_libpsio_psio_hpp_
29 #define _psi_src_lib_libpsio_psio_hpp_
30 
31 #include <string>
32 #include <map>
33 #include <set>
34 #include <queue>
35 #include <memory>
36 
37 #include "psi4/libpsio/config.h"
38 
39 namespace psi {
40 
41 class PSIO;
42 class PSIOManager;
43 extern std::shared_ptr<PSIO> _default_psio_lib_;
44 extern std::shared_ptr<PSIOManager> _default_psio_manager_;
45 
53 class PSIOManager {
54 private:
56  // (defaults to either $TMP, $TEMPDIR, $TMP or /tmp/ in
57  // that order)
58  std::string default_path_;
60  std::map<int, std::string> specific_paths_;
62  std::set<int> specific_retains_;
63 
65  std::map<std::string, bool> files_;
67  std::set<std::string> retained_files_;
68 
69  std::string pid_;
70 public:
72  PSIOManager();
74  ~PSIOManager();
75 
79  void mirror_to_disk();
83  void build_from_disk();
84 
89  void set_default_path(const std::string& path);
95  void set_specific_path(int fileno, const std::string& path);
101  void set_specific_retention(int fileno, bool retain);
107  bool get_specific_retention(int fileno);
108 
114  std::string get_file_path(int fileno);
115 
120  std::string get_default_path() { return default_path_; }
121 
127  void write_scratch_file(const std::string &full_path, const std::string &text);
128 
133  void open_file(const std::string & full_path, int fileno);
141  void close_file(const std::string & full_path, int fileno, bool keep);
148  void move_file(const std::string & old_full_path, const std::string & new_full_path);
155  void mark_file_for_retention(const std::string & full_path, bool retain);
160  void print(std::string out = "outfile");
164  void print_out() { print("outfile"); }
175  void psiclean();
180  void crashclean();
182  static std::shared_ptr<PSIOManager> shared_object();
183 };
184 
196 class PSIO {
197 public:
198  PSIO();
199  ~PSIO();
200 
202  int state() {
203  return state_;
204  }
217  void filecfg_kwd(const char* kwdgrp, const char* kwd, int unit,
218  const char* kwdval);
220  const std::string& filecfg_kwd(const char* kwdgrp, const char* kwd,
221  int unit);
222 
224  void rename_file(unsigned int old_unit,unsigned int new_unit);
225 
227  bool exists(unsigned int unit);
229  void open(unsigned int unit, int status);
231  void close(unsigned int unit, int keep);
233  std::string getpid(void);
235  void rehash(unsigned int unit);
237  int open_check(unsigned int unit);
249  void read(unsigned int unit, const char *key, char *buffer, ULI size,
250  psio_address start, psio_address *end);
262  void write(unsigned int unit, const char *key, char *buffer, ULI size,
263  psio_address start, psio_address *end);
264 
265  void read_entry(unsigned int unit, const char *key, char *buffer, ULI size);
266  void write_entry(unsigned int unit, const char *key, char *buffer, ULI size);
267 
279  void zero_disk(unsigned int unit, const char *key, ULI rows, ULI cols);
280 
290  void rw(unsigned int unit, char *buffer, psio_address address, ULI size,
291  int wrt);
292 
294  void tocclean(unsigned int unit, const char *key);
296  void tocprint(unsigned int unit);
298  psio_tocentry* tocscan(unsigned int unit, const char *key);
300  bool tocentry_exists(unsigned int unit, const char *key);
302  void tocwrite(unsigned int unit);
303 
305  static int _error_exit_code_;
306 
308  static void set_default_namespace(const std::string &_ns) { default_namespace_ = _ns; }
309 
311  static std::string get_default_namespace() { return default_namespace_; }
312 
314  static void change_file_namespace(unsigned int fileno, const std::string & ns1, const std::string & ns2);
315 
317  static std::shared_ptr<PSIO> shared_object();
318 
329  ULI rd_toclen(unsigned int unit);
330 
332  void get_filename(unsigned int unit, char **name, bool remove_namespace = false);
333 
335  bool tocdel(unsigned int unit, const char *key);
336 
337 private:
340 
342  std::string pid_;
343 
344 
346  static std::string default_namespace_;
347 
348  typedef std::map<std::string,std::string> KWDMap;
351 
352 #ifdef PSIO_STATS
353  ULI *psio_readlen;
354  ULI *psio_writlen;
355 #endif
356 
358  int state_;
360  unsigned int get_numvols(unsigned int unit);
362  void get_volpath(unsigned int unit, unsigned int volume, char **path);
364  psio_tocentry* toclast(unsigned int unit);
366  unsigned int toclen(unsigned int unit);
373  void wt_toclen(unsigned int unit, ULI toclen);
375  void tocread(unsigned int unit);
376 
377  friend class AIO_Handler;
378 
379 public:
380  void set_pid(const std::string &pid) { pid_ = pid; }
381 };
382 
383 }
384 
385 #endif /* header guard */
void tocread(unsigned int unit)
Read the table of contents for file number &#39;unit&#39;.
Definition: tocread.cc:40
void status(const char *, std::string)
Definition: cchbar/status.cc:37
std::string pid_
Definition: psio.hpp:69
void zero_disk(unsigned int unit, const char *key, ULI rows, ULI cols)
Definition: zero_disk.cc:44
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:44
std::string get_default_path()
Definition: psio.hpp:120
std::map< std::string, std::string > KWDMap
Definition: psio.hpp:348
void tocprint(unsigned int unit)
Print the table of contents for the given unit.
Definition: tocprint.cc:46
std::shared_ptr< PSIO > _default_psio_lib_
Definition: libpsio/init.cc:51
std::map< std::string, bool > files_
Map of files, bool denotes open or closed.
Definition: psio.hpp:65
void filecfg_kwd(const char *kwdgrp, const char *kwd, int unit, const char *kwdval)
Definition: filescfg.cc:64
static int _error_exit_code_
Upon catastrophic failure, the library will exit() with this code. The default is 1...
Definition: psio.hpp:305
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:45
static std::string get_default_namespace()
Get the default namespace (for PREFIX.NAMESPACE.UNIT file numbering)
Definition: psio.hpp:311
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:42
int state()
return 1 if activated
Definition: psio.hpp:202
~PSIO()
Definition: done.cc:48
static std::shared_ptr< PSIOManager > shared_object()
The one and (should be) only instance of PSIOManager for a PSI4 instance.
Definition: filemanager.cc:61
void build_from_disk()
Definition: filemanager.cc:200
Definition: libpsio/config.h:64
unsigned long int ULI
Definition: libpsio/config.h:62
std::string getpid(void)
lookup process id
Definition: getpid.cc:45
void crashclean()
Definition: filemanager.cc:223
static std::shared_ptr< PSIO > shared_object()
Return the global shared object.
Definition: libpsio/init.cc:117
void print_out()
Definition: psio.hpp:164
friend class AIO_Handler
Definition: psio.hpp:377
void set_specific_retention(int fileno, bool retain)
Definition: filemanager.cc:81
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:225
psio_tocentry * toclast(unsigned int unit)
return the last TOC entry
Definition: toclast.cc:38
unsigned int get_numvols(unsigned int unit)
return the number of volumes over which unit will be striped
Definition: get_numvols.cc:45
void close(unsigned int unit, int keep)
close unit. if keep == 0, will remove the file, else keep it
Definition: libpsio/close.cc:43
int state_
Library state variable.
Definition: psio.hpp:358
std::string get_file_path(int fileno)
Definition: filemanager.cc:74
void wt_toclen(unsigned int unit, ULI toclen)
Definition: toclen.cc:81
KWDMap files_keywords_
library configuration is described by a set of keywords
Definition: psio.hpp:350
void set_specific_path(int fileno, const std::string &path)
Definition: filemanager.cc:70
void psiclean()
Definition: filemanager.cc:236
~PSIOManager()
Default destructor (does nothing)
Definition: filemanager.cc:57
void write_entry(unsigned int unit, const char *key, char *buffer, ULI size)
Definition: write_entry.cc:43
static void set_default_namespace(const std::string &_ns)
Set the current namespace (for PREFIX.NAMESPACE.UNIT file numbering)
Definition: psio.hpp:308
PSIOManager()
Default constructor (does nothing)
Definition: filemanager.cc:41
void mark_file_for_retention(const std::string &full_path, bool retain)
Definition: filemanager.cc:228
PSIO()
Definition: libpsio/init.cc:58
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:44
void set_default_path(const std::string &path)
Definition: filemanager.cc:66
Definition: libpsio/config.h:74
bool exists(unsigned int unit)
check if a psi unit already exists or not
Definition: open.cc:140
void read(unsigned int unit, const char *key, char *buffer, ULI size, psio_address start, psio_address *end)
Definition: read.cc:46
std::map< int, std::string > specific_paths_
Specific paths for arbitrary file numbers.
Definition: psio.hpp:60
void write(unsigned int unit, const char *key, char *buffer, ULI size, psio_address start, psio_address *end)
Definition: write.cc:45
Definition: psio.hpp:53
void open_file(const std::string &full_path, int fileno)
Definition: filemanager.cc:116
std::set< std::string > retained_files_
Set of files to retain after psiclean.
Definition: psio.hpp:67
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:81
std::string pid_
Process ID.
Definition: psio.hpp:342
void read_entry(unsigned int unit, const char *key, char *buffer, ULI size)
Definition: read_entry.cc:38
void write_scratch_file(const std::string &full_path, const std::string &text)
Definition: filemanager.cc:105
bool tocdel(unsigned int unit, const char *key)
delete a specific TOC entry (only deletes entry, not data)
Definition: tocdel.cc:47
void print(std::string out="outfile")
Definition: filemanager.cc:137
void rw(unsigned int unit, char *buffer, psio_address address, ULI size, int wrt)
Definition: rw.cc:41
void move_file(const std::string &old_full_path, const std::string &new_full_path)
Definition: filemanager.cc:131
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:46
psio_ud * psio_unit
vector of units
Definition: psio.hpp:339
void close_file(const std::string &full_path, int fileno, bool keep)
Definition: filemanager.cc:123
ULI rd_toclen(unsigned int unit)
Definition: toclen.cc:56
std::string default_path_
Default path for unspec&#39;d file numbers.
Definition: psio.hpp:58
std::set< int > specific_retains_
Default retained files.
Definition: psio.hpp:62
std::shared_ptr< PSIOManager > _default_psio_manager_
Definition: libpsio/init.cc:52
void rename_file(unsigned int old_unit, unsigned int new_unit)
moves a file from old_unit to new_unit
Definition: rename_file.cc:49
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:42
static std::string default_namespace_
Current default namespace (for PREFIX.NAMESPACE.UNIT numbering)
Definition: psio.hpp:346
void set_pid(const std::string &pid)
Definition: psio.hpp:380
int open_check(unsigned int unit)
return 1 if unit is open
Definition: open_check.cc:43
void mirror_to_disk()
Definition: filemanager.cc:183
Definition: libpsio/config.h:82
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:42
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:48
bool get_specific_retention(int fileno)
Definition: filemanager.cc:94
Definition: psio.hpp:196