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 <vector>
36 #include <string>
37 #include <map>
38 
39 namespace psi {
40 
41 class Tensor;
42 class CoreTensor;
43 class DiskTensor;
44 
49 class THCE {
50 
51 
52 protected:
53 
54  // => Object Data <= //
55 
57  std::map<std::string, int> dimensions_;
59  std::map<std::string, std::shared_ptr<Tensor> > tensors_;
60 
61  // => Safety/Debugging <= //
62 
64  void dimension_check(const std::string& name);
66  void tensor_check(const std::string& name);
67 
69  static int unique_id();
70 
71 public:
72 
73  // => Constructors <= //
74 
76  THCE();
78  virtual ~THCE();
79 
80  // => Accessors <= //
81 
83  std::map<std::string, int>& dimensions() { return dimensions_; }
85  std::map<std::string, std::shared_ptr<Tensor> >& tensors() { return tensors_; }
87  std::shared_ptr<Tensor>& operator[] (const std::string& key) { return tensors_[key]; }
88 
90  void print_header(std::string fh="outfile") const { print(fh,0); }
92  void print(std::string fh="outfile", int print = 1) const;
93 
95  size_t core_doubles() const;
97  size_t disk_doubles() const;
98 
99  // => Front-end interface <= //
100 
102  void new_dimension(const std::string& name, int size);
104  void delete_dimension(const std::string& name);
106  void new_core_tensor(const std::string& name, const std::string& dimensions, double* data = NULL, bool trust = false);
108  void new_disk_tensor(const std::string& name, const std::string& dimensions, bool save = false, bool load = false);
110  void add_tensor(const std::string& name, std::shared_ptr<Tensor> tensor);
112  void delete_tensor(const std::string& name);
113 
114 };
115 
120 class Tensor {
121 
122 private:
123 
125  static long int unique_id;
126 
127 protected:
128 
129  // => Object Data <= //
130 
132  std::string name_;
134  std::string filename_;
136  size_t numel_;
138  int order_;
140  std::vector<std::string> dimensions_;
142  std::vector<int> sizes_;
144  std::vector<int> active_sizes_;
145 
146  // => Helper Methods <= //
147 
149  void set_filename();
150 
151 public:
152 
153  // => Constructors <= //
154 
156  Tensor(const std::string& name,
157  std::vector<std::string>& dimensions,
158  std::vector<int>& sizes);
159 
161  virtual ~Tensor();
162 
163  // => Universal Accessors <= //
164 
166  std::string name() const { return name_; }
168  std::string filename() const { return filename_; }
170  size_t numel() const { return numel_; }
172  int order() const { return order_; }
174  std::vector<std::string>& dimensions() { return dimensions_; }
176  std::vector<int>& sizes() { return sizes_; }
178  std::vector<int>& active_sizes() { return active_sizes_; }
179 
181  virtual bool core() const = 0;
183  virtual bool disk() const = 0;
185  virtual bool trust() const = 0;
187  virtual bool swapped() const = 0;
189  virtual void swap_check() const = 0;
190 
192  virtual size_t core_doubles() const = 0;
194  virtual size_t disk_doubles() const = 0;
195 
197  virtual void print(std::string fh="outfile", int level = 2) const = 0;
199  void print_header(std::string fh="outfile") const { print(fh, 0); }
200 
201 
202  // => Common Interface <= //
203 
205  virtual void zero() = 0;
217  virtual void slice(std::shared_ptr<Tensor> A, std::vector<std::tuple<bool,int,int,bool,int,int> >& topology);
219  virtual double* pointer() const { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
221  virtual FILE* file_pointer() { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
222 
223  // => CoreTensor Interface <= //
224 
225  // > Unary Operations < //
226 
228  virtual void swap_out(bool changed = true) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
230  virtual void swap_in(bool read = true) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
232  virtual void scale(double val) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
234  virtual void set_data(double* data) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
236  virtual void set_pointer(double* data) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
237 
238  // > Binary Operations < //
239 
241  virtual void add(std::shared_ptr<Tensor> A, double alpha = 1.0, double beta = 0.0) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
247  virtual void permute(std::shared_ptr<Tensor> A, std::vector<int>& topology) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
248 
249  // > Ternary Operations < //
250 
263  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."); }
264 
265  // => DiskTensor Interface <= //
266 
268  virtual void set_save(bool save) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
269 };
270 
276 class CoreTensor : public Tensor {
277 
278 protected:
279 
280  // => Object Data <= //
281 
283  bool trust_;
285  double* data_;
287  bool swapped_;
289  FILE* fh_;
290 
291 public:
292 
293  // => Constructors <= //
294 
296  CoreTensor(const std::string& name,
297  std::vector<std::string>& dimensions, std::vector<int>& sizes,
298  double* data = NULL, bool trust = false);
299 
301  virtual ~CoreTensor();
302 
304  static std::shared_ptr<Tensor> build(const std::string& name,
305  double* data = NULL, bool trust = false);
307  static std::shared_ptr<Tensor> build(const std::string& name,
308  const std::string& dimension1, int size1,
309  double* data = NULL, bool trust = false);
311  static std::shared_ptr<Tensor> build(const std::string& name,
312  const std::string& dimension1, int size1,
313  const std::string& dimension2, int size2,
314  double* data = NULL, bool trust = false);
316  static std::shared_ptr<Tensor> build(const std::string& name,
317  const std::string& dimension1, int size1,
318  const std::string& dimension2, int size2,
319  const std::string& dimension3, int size3,
320  double* data = NULL, bool trust = false);
322  static std::shared_ptr<Tensor> build(const std::string& name,
323  const std::string& dimension1, int size1,
324  const std::string& dimension2, int size2,
325  const std::string& dimension3, int size3,
326  const std::string& dimension4, int size4,
327  double* data = NULL, bool trust = false);
328 
329  // => Universal Accessors <= //
330 
332  virtual bool core() const { return true; }
334  virtual bool disk() const { return false; }
336  virtual bool trust() const { return trust_; }
338  virtual bool swapped() const { return swapped_; }
340  virtual void swap_check() const;
341 
343  virtual size_t core_doubles() const { return (swapped() | trust() ? 0L : numel_); }
345  virtual size_t disk_doubles() const { return 0L; }
346 
348  virtual void print(std::string fh="outfile", int level = 2) const;
349 
350  // => Conditional Accessors <= //
351 
353  virtual double* pointer() const { return data_; }
355  virtual FILE* file_pointer() { return fh_; }
356 
357  // > Unary Operations < //
358 
360  virtual void swap_out(bool changed = true);
362  virtual void swap_in(bool read = true);
364  virtual void zero();
366  virtual void scale(double val);
368  virtual void set_data(double* data);
370  virtual void set_pointer(double* data);
371 
372  // > Binary Operations < //
373 
375  virtual void add(std::shared_ptr<Tensor> A, double alpha = 1.0, double beta = 0.0);
381  virtual void permute(std::shared_ptr<Tensor> A, std::vector<int>& topology);
382 
383  // > Ternary Operations < //
384 
396  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);
397 
398 };
399 
405 class DiskTensor : public Tensor {
406 
407 protected:
408 
409  // => Object Data <= //
410 
412  bool save_;
414  FILE* fh_;
415 
416 public:
417 
418  // => Constructors <= //
419 
426  DiskTensor(const std::string& name,
427  std::vector<std::string>& dimensions, std::vector<int>& sizes,
428  bool save = false, bool load = false);
429 
431  virtual ~DiskTensor();
432 
434  static std::shared_ptr<Tensor> build(const std::string& name,
435  bool save = false, bool load = false);
437  static std::shared_ptr<Tensor> build(const std::string& name,
438  const std::string& dimension1, int size1,
439  bool save = false, bool load = false);
441  static std::shared_ptr<Tensor> build(const std::string& name,
442  const std::string& dimension1, int size1,
443  const std::string& dimension2, int size2,
444  bool save = false, bool load = false);
446  static std::shared_ptr<Tensor> build(const std::string& name,
447  const std::string& dimension1, int size1,
448  const std::string& dimension2, int size2,
449  const std::string& dimension3, int size3,
450  bool save = false, bool load = false);
452  static std::shared_ptr<Tensor> build(const std::string& name,
453  const std::string& dimension1, int size1,
454  const std::string& dimension2, int size2,
455  const std::string& dimension3, int size3,
456  const std::string& dimension4, int size4,
457  bool save = false, bool load = false);
458 
459  // => Universal Accessors <= //
460 
462  virtual bool core() const { return false; }
464  virtual bool disk() const { return true; }
466  virtual bool trust() const { return false; }
468  virtual bool swapped() const { return false; }
470  virtual void swap_check() const;
471 
473  virtual size_t core_doubles() const { return 0L; }
475  virtual size_t disk_doubles() const { return numel_; }
476 
478  virtual void print(std::string fh="outfile", int level = 2) const;
479 
480  // => Conditional Accessors <= //
481 
483  virtual FILE* file_pointer() { return fh_; }
484 
486  virtual void set_save(bool save) { save_ = save; }
487 
489  virtual void zero();
490 
491 };
492 
493 } // End namespace
494 
495 #endif
virtual size_t core_doubles() const
How many core doubles are currently allocated by this tensor?
Definition: thce.h:343
virtual void zero()
Zero the tensor out.
Definition: thce.cc:849
bool save_
Save file upon destruct?
Definition: thce.h:412
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:228
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:332
int order_
Order (number of dimensions) in this Tensor.
Definition: thce.h:138
virtual bool swapped() const =0
Is this a swapped-out core tensor?
std::string name() const
Name of this Tensor.
Definition: thce.h:166
virtual bool trust() const
Is this a trust core tensor?
Definition: thce.h:336
virtual size_t disk_doubles() const
How many disk doubles are currently allocated by this tensor?
Definition: thce.h:475
size_t numel() const
Total number of elements in this Tensor.
Definition: thce.h:170
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:241
std::string filename() const
Unique filename of this Tensor.
Definition: thce.h:168
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:236
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:176
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:405
virtual FILE * file_pointer()
File pointer underlying this Tensor (careful, returns NULL if not swapped, mirror not guaranteed) ...
Definition: thce.h:355
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:142
virtual ~CoreTensor()
Master destructor.
Definition: thce.cc:598
size_t numel_
Total number of elements in this Tensor.
Definition: thce.h:136
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:466
static long int unique_id
Unique ID to prevent disk clash.
Definition: thce.h:125
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:289
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:144
std::vector< std::string > dimensions_
Classes of dimensions.
Definition: thce.h:140
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:57
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:83
Definition: thce.h:120
std::string name_
Name of this Tensor.
Definition: thce.h:132
virtual bool disk() const
Is this a disk tensor?
Definition: thce.h:464
Definition: thce.h:276
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:247
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:268
std::map< std::string, std::shared_ptr< Tensor > > tensors_
List of currently declated tensors.
Definition: thce.h:59
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:468
virtual void swap_in(bool read=true)
Swap this tensor in to core (explicitly reads if true)
Definition: thce.h:230
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:353
THCE()
Instance Constructor.
Definition: thce.cc:48
virtual double * pointer() const
Access the data pointer.
Definition: thce.h:219
virtual void scale(double val)
Scale the tensor by val (ignores active dims)
Definition: thce.h:232
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:90
bool trust_
Is this a trust pointer?
Definition: thce.h:283
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:174
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:345
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:287
virtual size_t core_doubles() const
How many core doubles are currently allocated by this tensor?
Definition: thce.h:473
virtual bool swapped() const
Is this a swapped-out core tensor?
Definition: thce.h:338
std::string filename_
Unique filename to prevent disk clash.
Definition: thce.h:134
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:221
virtual bool core() const
Is this a core tensor?
Definition: thce.h:462
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:263
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:49
int order() const
Order (number of dimensions) in this Tensor.
Definition: thce.h:172
virtual void set_data(double *data)
Copy contents of data (ignores active dims)
Definition: thce.h:234
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:199
virtual ~THCE()
Instance Destructor.
Definition: thce.cc:51
std::vector< int > & active_sizes()
Active sizes of dimensions.
Definition: thce.h:178
std::map< std::string, std::shared_ptr< Tensor > > & tensors()
Accessor to the list of currently declared tensors.
Definition: thce.h:85
#define PSIEXCEPTION(message)
Definition: exception.h:48
virtual FILE * file_pointer()
File underlying this Tensor.
Definition: thce.h:483
double * data_
Data underlying this Tensor.
Definition: thce.h:285
FILE * fh_
File pointer.
Definition: thce.h:414
std::shared_ptr< Tensor > & operator[](const std::string &key)
Direct tensor element accessor.
Definition: thce.h:87
virtual bool disk() const
Is this a disk tensor?
Definition: thce.h:334
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:486