Psi4
thce.h
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 THCE_H
30 #define THCE_H
31 
32 #include "psi4/libmints/typedefs.h"
34 
35 #include <map>
36 
37 namespace psi {
38 
39 class Tensor;
40 class CoreTensor;
41 class DiskTensor;
42 
47 class THCE {
48 
49 
50 protected:
51 
52  // => Object Data <= //
53 
55  std::map<std::string, int> dimensions_;
57  std::map<std::string, std::shared_ptr<Tensor> > tensors_;
58 
59  // => Safety/Debugging <= //
60 
62  void dimension_check(const std::string& name);
64  void tensor_check(const std::string& name);
65 
67  static int unique_id();
68 
69 public:
70 
71  // => Constructors <= //
72 
74  THCE();
76  virtual ~THCE();
77 
78  // => Accessors <= //
79 
81  std::map<std::string, int>& dimensions() { return dimensions_; }
83  std::map<std::string, std::shared_ptr<Tensor> >& tensors() { return tensors_; }
85  std::shared_ptr<Tensor>& operator[] (const std::string& key) { return tensors_[key]; }
86 
88  void print_header(std::string fh="outfile") const { print(fh,0); }
90  void print(std::string fh="outfile", int print = 1) const;
91 
93  size_t core_doubles() const;
95  size_t disk_doubles() const;
96 
97  // => Front-end interface <= //
98 
100  void new_dimension(const std::string& name, int size);
102  void delete_dimension(const std::string& name);
104  void new_core_tensor(const std::string& name, const std::string& dimensions, double* data = NULL, bool trust = false);
106  void new_disk_tensor(const std::string& name, const std::string& dimensions, bool save = false, bool load = false);
108  void add_tensor(const std::string& name, std::shared_ptr<Tensor> tensor);
110  void delete_tensor(const std::string& name);
111 
112 };
113 
118 class Tensor {
119 
120 private:
121 
123  static long int unique_id;
124 
125 protected:
126 
127  // => Object Data <= //
128 
130  std::string name_;
132  std::string filename_;
134  size_t numel_;
136  int order_;
138  std::vector<std::string> dimensions_;
140  std::vector<int> sizes_;
142  std::vector<int> active_sizes_;
143 
144  // => Helper Methods <= //
145 
147  void set_filename();
148 
149 public:
150 
151  // => Constructors <= //
152 
154  Tensor(const std::string& name,
155  std::vector<std::string>& dimensions,
156  std::vector<int>& sizes);
157 
159  virtual ~Tensor();
160 
161  // => Universal Accessors <= //
162 
164  std::string name() const { return name_; }
166  std::string filename() const { return filename_; }
168  size_t numel() const { return numel_; }
170  int order() const { return order_; }
172  std::vector<std::string>& dimensions() { return dimensions_; }
174  std::vector<int>& sizes() { return sizes_; }
176  std::vector<int>& active_sizes() { return active_sizes_; }
177 
179  virtual bool core() const = 0;
181  virtual bool disk() const = 0;
183  virtual bool trust() const = 0;
185  virtual bool swapped() const = 0;
187  virtual void swap_check() const = 0;
188 
190  virtual size_t core_doubles() const = 0;
192  virtual size_t disk_doubles() const = 0;
193 
195  virtual void print(std::string fh="outfile", int level = 2) const = 0;
197  void print_header(std::string fh="outfile") const { print(fh, 0); }
198 
199 
200  // => Common Interface <= //
201 
203  virtual void zero() = 0;
215  virtual void slice(std::shared_ptr<Tensor> A, std::vector<std::tuple<bool,int,int,bool,int,int> >& topology);
217  virtual double* pointer() const { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
219  virtual FILE* file_pointer() { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
220 
221  // => CoreTensor Interface <= //
222 
223  // > Unary Operations < //
224 
226  virtual void swap_out(bool changed = true) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
228  virtual void swap_in(bool read = true) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
230  virtual void scale(double val) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
232  virtual void set_data(double* data) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
234  virtual void set_pointer(double* data) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
235 
236  // > Binary Operations < //
237 
239  virtual void add(std::shared_ptr<Tensor> A, double alpha = 1.0, double beta = 0.0) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
245  virtual void permute(std::shared_ptr<Tensor> A, std::vector<int>& topology) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
246 
247  // > Ternary Operations < //
248 
261  virtual void contract(std::shared_ptr<Tensor> A, std::shared_ptr<Tensor> B, std::vector<std::tuple<std::string,int,int,int> >& topology, double alpha = 1.0, double beta = 0.0) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
262 
263  // => DiskTensor Interface <= //
264 
266  virtual void set_save(bool save) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
267 };
268 
274 class CoreTensor : public Tensor {
275 
276 protected:
277 
278  // => Object Data <= //
279 
281  bool trust_;
283  double* data_;
285  bool swapped_;
287  FILE* fh_;
288 
289 public:
290 
291  // => Constructors <= //
292 
294  CoreTensor(const std::string& name,
295  std::vector<std::string>& dimensions, std::vector<int>& sizes,
296  double* data = NULL, bool trust = false);
297 
299  virtual ~CoreTensor();
300 
302  static std::shared_ptr<Tensor> build(const std::string& name,
303  double* data = NULL, bool trust = false);
305  static std::shared_ptr<Tensor> build(const std::string& name,
306  const std::string& dimension1, int size1,
307  double* data = NULL, bool trust = false);
309  static std::shared_ptr<Tensor> build(const std::string& name,
310  const std::string& dimension1, int size1,
311  const std::string& dimension2, int size2,
312  double* data = NULL, bool trust = false);
314  static std::shared_ptr<Tensor> build(const std::string& name,
315  const std::string& dimension1, int size1,
316  const std::string& dimension2, int size2,
317  const std::string& dimension3, int size3,
318  double* data = NULL, bool trust = false);
320  static std::shared_ptr<Tensor> build(const std::string& name,
321  const std::string& dimension1, int size1,
322  const std::string& dimension2, int size2,
323  const std::string& dimension3, int size3,
324  const std::string& dimension4, int size4,
325  double* data = NULL, bool trust = false);
326 
327  // => Universal Accessors <= //
328 
330  virtual bool core() const { return true; }
332  virtual bool disk() const { return false; }
334  virtual bool trust() const { return trust_; }
336  virtual bool swapped() const { return swapped_; }
338  virtual void swap_check() const;
339 
341  virtual size_t core_doubles() const { return (swapped() | trust() ? 0L : numel_); }
343  virtual size_t disk_doubles() const { return 0L; }
344 
346  virtual void print(std::string fh="outfile", int level = 2) const;
347 
348  // => Conditional Accessors <= //
349 
351  virtual double* pointer() const { return data_; }
353  virtual FILE* file_pointer() { return fh_; }
354 
355  // > Unary Operations < //
356 
358  virtual void swap_out(bool changed = true);
360  virtual void swap_in(bool read = true);
362  virtual void zero();
364  virtual void scale(double val);
366  virtual void set_data(double* data);
368  virtual void set_pointer(double* data);
369 
370  // > Binary Operations < //
371 
373  virtual void add(std::shared_ptr<Tensor> A, double alpha = 1.0, double beta = 0.0);
379  virtual void permute(std::shared_ptr<Tensor> A, std::vector<int>& topology);
380 
381  // > Ternary Operations < //
382 
394  virtual void contract(std::shared_ptr<Tensor> A, std::shared_ptr<Tensor> B, std::vector<std::tuple<std::string,int,int,int> >& topology, double alpha = 1.0, double beta = 0.0);
395 
396 };
397 
403 class DiskTensor : public Tensor {
404 
405 protected:
406 
407  // => Object Data <= //
408 
410  bool save_;
412  FILE* fh_;
413 
414 public:
415 
416  // => Constructors <= //
417 
424  DiskTensor(const std::string& name,
425  std::vector<std::string>& dimensions, std::vector<int>& sizes,
426  bool save = false, bool load = false);
427 
429  virtual ~DiskTensor();
430 
432  static std::shared_ptr<Tensor> build(const std::string& name,
433  bool save = false, bool load = false);
435  static std::shared_ptr<Tensor> build(const std::string& name,
436  const std::string& dimension1, int size1,
437  bool save = false, bool load = false);
439  static std::shared_ptr<Tensor> build(const std::string& name,
440  const std::string& dimension1, int size1,
441  const std::string& dimension2, int size2,
442  bool save = false, bool load = false);
444  static std::shared_ptr<Tensor> build(const std::string& name,
445  const std::string& dimension1, int size1,
446  const std::string& dimension2, int size2,
447  const std::string& dimension3, int size3,
448  bool save = false, bool load = false);
450  static std::shared_ptr<Tensor> build(const std::string& name,
451  const std::string& dimension1, int size1,
452  const std::string& dimension2, int size2,
453  const std::string& dimension3, int size3,
454  const std::string& dimension4, int size4,
455  bool save = false, bool load = false);
456 
457  // => Universal Accessors <= //
458 
460  virtual bool core() const { return false; }
462  virtual bool disk() const { return true; }
464  virtual bool trust() const { return false; }
466  virtual bool swapped() const { return false; }
468  virtual void swap_check() const;
469 
471  virtual size_t core_doubles() const { return 0L; }
473  virtual size_t disk_doubles() const { return numel_; }
474 
476  virtual void print(std::string fh="outfile", int level = 2) const;
477 
478  // => Conditional Accessors <= //
479 
481  virtual FILE* file_pointer() { return fh_; }
482 
484  virtual void set_save(bool save) { save_ = save; }
485 
487  virtual void zero();
488 
489 };
490 
491 } // End namespace
492 
493 #endif
virtual size_t core_doubles() const
How many core doubles are currently allocated by this tensor?
Definition: thce.h:341
virtual void zero()
Zero the tensor out.
Definition: thce.cc:849
bool save_
Save file upon destruct?
Definition: thce.h:410
virtual void swap_out(bool changed=true)
Swap this tensor out to disk (does not write if existing disk mirror and not changed) ...
Definition: thce.h:226
size_t disk_doubles() const
Return the current disk memory utilization of this instance, in doubles.
Definition: thce.cc:115
virtual void print(std::string fh="outfile", int level=2) const
Print the available tensor data.
Definition: thce.cc:1465
virtual void permute(std::shared_ptr< Tensor > A, std::vector< int > &topology)
Definition: thce.cc:875
virtual bool core() const
Is this a core tensor?
Definition: thce.h:330
int order_
Order (number of dimensions) in this Tensor.
Definition: thce.h:136
virtual bool swapped() const =0
Is this a swapped-out core tensor?
std::string name() const
Name of this Tensor.
Definition: thce.h:164
virtual bool trust() const
Is this a trust core tensor?
Definition: thce.h:334
virtual size_t disk_doubles() const
How many disk doubles are currently allocated by this tensor?
Definition: thce.h:473
size_t numel() const
Total number of elements in this Tensor.
Definition: thce.h:168
virtual void swap_in(bool read=true)
Swap this tensor in to core (if read is false, allocate with zeros)
Definition: thce.cc:831
virtual void add(std::shared_ptr< Tensor > A, double alpha=1.0, double beta=0.0)
Direct elementwise generalized DAXPY: C = alpha * A + beta * C (ignores active dims) ...
Definition: thce.h:239
std::string filename() const
Unique filename of this Tensor.
Definition: thce.h:166
virtual void swap_check() const =0
Throw an exception if the tensor is swapped out or disk based.
virtual void zero()
Zero the tensor out and prestripe.
Definition: thce.cc:1486
virtual void scale(double val)
Scale the tensor by val.
Definition: thce.cc:855
virtual ~DiskTensor()
Master destructor.
Definition: thce.cc:1380
virtual void set_pointer(double *data)
Update the trust pointer to data (throws if not trust)
Definition: thce.h:234
virtual void add(std::shared_ptr< Tensor > A, double alpha=1.0, double beta=0.0)
Direct elementwise generalized DAXPY: C = alpha * A + beta * C (ignores active dims) ...
Definition: thce.cc:861
DiskTensor(const std::string &name, std::vector< std::string > &dimensions, std::vector< int > &sizes, bool save=false, bool load=false)
Definition: thce.cc:1370
virtual ~Tensor()
Master destructor.
Definition: thce.cc:213
std::vector< int > & sizes()
Allocated sizes of dimensions.
Definition: thce.h:174
void delete_tensor(const std::string &name)
Decrement a Tensor of any type (removes from tensors_)
Definition: thce.cc:189
virtual void print(std::string fh="outfile", int level=2) const =0
Print the full tensor data in scalar/vector/matrix/pages style if print &gt;= 2.
Definition: thce.h:403
virtual FILE * file_pointer()
File pointer underlying this Tensor (careful, returns NULL if not swapped, mirror not guaranteed) ...
Definition: thce.h:353
virtual void slice(std::shared_ptr< Tensor > A, std::vector< std::tuple< bool, int, int, bool, int, int > > &topology)
Definition: thce.cc:235
std::vector< int > sizes_
Sizes of dimensions (lda-style)
Definition: thce.h:140
virtual ~CoreTensor()
Master destructor.
Definition: thce.cc:598
size_t numel_
Total number of elements in this Tensor.
Definition: thce.h:134
void tensor_check(const std::string &name)
Throws if a tensor name is not in tensors_.
Definition: thce.cc:137
virtual bool trust() const
Is this a trust core tensor?
Definition: thce.h:464
static long int unique_id
Unique ID to prevent disk clash.
Definition: thce.h:123
virtual void zero()=0
Zero the tensor out (ignores active dims). Prestripes if DiskTensor.
void new_core_tensor(const std::string &name, const std::string &dimensions, double *data=NULL, bool trust=false)
Add or overwrite a new core tensor, given a name and comma-separated dimension list.
Definition: thce.cc:151
void dimension_check(const std::string &name)
Throws if a dimension name is not in dimensions_.
Definition: thce.cc:131
virtual void swap_check() const
Throw an exception if the tensor is swapped out or disk based.
Definition: thce.cc:1387
FILE * fh_
File handle (valid if this tensor has ever been swapped out)
Definition: thce.h:287
virtual bool trust() const =0
Is this a trust core tensor?
std::vector< int > active_sizes_
Active sizes of dimensions (gimp-style)
Definition: thce.h:142
std::vector< std::string > dimensions_
Classes of dimensions.
Definition: thce.h:138
static std::shared_ptr< Tensor > build(const std::string &name, bool save=false, bool load=false)
Order-0 Constructor.
Definition: thce.cc:1457
virtual size_t core_doubles() const =0
How many core doubles are currently allocated by this tensor?
std::map< std::string, int > dimensions_
List of currently declared dimensions (dimension classes)
Definition: thce.h:55
CoreTensor(const std::string &name, std::vector< std::string > &dimensions, std::vector< int > &sizes, double *data=NULL, bool trust=false)
Master constructor.
Definition: thce.cc:577
std::map< std::string, int > & dimensions()
Accessor to the list of currently declared dimensions (dimension classes)
Definition: thce.h:81
Definition: thce.h:118
std::string name_
Name of this Tensor.
Definition: thce.h:130
virtual bool disk() const
Is this a disk tensor?
Definition: thce.h:462
Definition: thce.h:274
static int unique_id()
Get a unique ID number for a tensor (prevents disk clash)
void new_disk_tensor(const std::string &name, const std::string &dimensions, bool save=false, bool load=false)
Add or overwrite a new disk tensor, given a name and comma-separated dimension list.
Definition: thce.cc:168
virtual void permute(std::shared_ptr< Tensor > A, std::vector< int > &topology)
Definition: thce.h:245
void delete_dimension(const std::string &name)
Remove a dimension from this instance.
Definition: thce.cc:147
virtual void set_save(bool save)
Set save flag in DiskTensor.
Definition: thce.h:266
std::map< std::string, std::shared_ptr< Tensor > > tensors_
List of currently declated tensors.
Definition: thce.h:57
virtual void set_data(double *data)
Copy contents of data (ignores active dims)
Definition: thce.cc:793
static std::shared_ptr< Tensor > build(const std::string &name, double *data=NULL, bool trust=false)
Order-0 Constructor.
Definition: thce.cc:684
virtual bool swapped() const
Is this a swapped-out core tensor?
Definition: thce.h:466
virtual void swap_in(bool read=true)
Swap this tensor in to core (explicitly reads if true)
Definition: thce.h:228
virtual bool core() const =0
Is this a core tensor?
virtual bool disk() const =0
Is this a disk tensor?
virtual double * pointer() const
Data underlying this Tensor (careful, returns NULL if swapped)
Definition: thce.h:351
THCE()
Instance Constructor.
Definition: thce.cc:48
virtual double * pointer() const
Access the data pointer.
Definition: thce.h:217
virtual void scale(double val)
Scale the tensor by val (ignores active dims)
Definition: thce.h:230
virtual void swap_out(bool changed=true)
Swap this tensor out to disk (if existing file, only write if changed)
Definition: thce.cc:799
Tensor(const std::string &name, std::vector< std::string > &dimensions, std::vector< int > &sizes)
Master constructor.
Definition: thce.cc:196
virtual size_t disk_doubles() const =0
How many disk doubles are currently allocated by this tensor?
virtual void set_pointer(double *data)
Update the trust pointer to data (throws if not trust)
Definition: thce.cc:786
void print_header(std::string fh="outfile") const
Print a brief summary.
Definition: thce.h:88
bool trust_
Is this a trust pointer?
Definition: thce.h:281
virtual void print(std::string fh="outfile", int level=2) const
Print the full tensor data in scalar/vector/matrix/pages style if print &gt;= 2.
Definition: thce.cc:692
std::vector< std::string > & dimensions()
Classes of dimensions.
Definition: thce.h:172
void set_filename()
Set the filename to scratch, PID, namespace, unique ID, name.
Definition: thce.cc:216
virtual size_t disk_doubles() const
How many disk doubles are currently allocated by this tensor?
Definition: thce.h:343
void print(std::string fh="outfile", int print=1) const
Print a more detailed trace of the object.
Definition: thce.cc:54
bool swapped_
Is this tensor swapped?
Definition: thce.h:285
virtual size_t core_doubles() const
How many core doubles are currently allocated by this tensor?
Definition: thce.h:471
virtual bool swapped() const
Is this a swapped-out core tensor?
Definition: thce.h:336
std::string filename_
Unique filename to prevent disk clash.
Definition: thce.h:132
virtual void contract(std::shared_ptr< Tensor > A, std::shared_ptr< Tensor > B, std::vector< std::tuple< std::string, int, int, int > > &topology, double alpha=1.0, double beta=0.0)
Definition: thce.cc:1005
virtual FILE * file_pointer()
Access the file pointer.
Definition: thce.h:219
virtual bool core() const
Is this a core tensor?
Definition: thce.h:460
virtual void contract(std::shared_ptr< Tensor > A, std::shared_ptr< Tensor > B, std::vector< std::tuple< std::string, int, int, int > > &topology, double alpha=1.0, double beta=0.0)
Definition: thce.h:261
void add_tensor(const std::string &name, std::shared_ptr< Tensor > tensor)
Add or alias an existing Tensor of any type.
Definition: thce.cc:185
Definition: thce.h:47
int order() const
Order (number of dimensions) in this Tensor.
Definition: thce.h:170
virtual void set_data(double *data)
Copy contents of data (ignores active dims)
Definition: thce.h:232
Definition: PsiFileImpl.h:39
virtual void swap_check() const
Throw an exception if the tensor is swapped out or disk based.
Definition: thce.cc:613
void new_dimension(const std::string &name, int size)
Add or overwrite a dimension to this instance.
Definition: thce.cc:143
void print_header(std::string fh="outfile") const
Print only name and sizing data.
Definition: thce.h:197
virtual ~THCE()
Instance Destructor.
Definition: thce.cc:51
std::vector< int > & active_sizes()
Active sizes of dimensions.
Definition: thce.h:176
std::map< std::string, std::shared_ptr< Tensor > > & tensors()
Accessor to the list of currently declared tensors.
Definition: thce.h:83
#define PSIEXCEPTION(message)
Definition: exception.h:48
virtual FILE * file_pointer()
File underlying this Tensor.
Definition: thce.h:481
double * data_
Data underlying this Tensor.
Definition: thce.h:283
FILE * fh_
File pointer.
Definition: thce.h:412
std::shared_ptr< Tensor > & operator[](const std::string &key)
Direct tensor element accessor.
Definition: thce.h:85
virtual bool disk() const
Is this a disk tensor?
Definition: thce.h:332
size_t core_doubles() const
Return the current core memory utilization of this instance, in doubles.
Definition: thce.cc:99
virtual void set_save(bool save)
Set save flag for disk resuse.
Definition: thce.h:484