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 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 THCE_H
29 #define THCE_H
30 
31 #include "psi4/libmints/typedefs.h"
33 
34 #include <map>
35 
36 namespace psi {
37 
38 class Tensor;
39 class CoreTensor;
40 class DiskTensor;
41 
46 class THCE {
47 
48 
49 protected:
50 
51  // => Object Data <= //
52 
54  std::map<std::string, int> dimensions_;
56  std::map<std::string, std::shared_ptr<Tensor> > tensors_;
57 
58  // => Safety/Debugging <= //
59 
61  void dimension_check(const std::string& name);
63  void tensor_check(const std::string& name);
64 
66  static int unique_id();
67 
68 public:
69 
70  // => Constructors <= //
71 
73  THCE();
75  virtual ~THCE();
76 
77  // => Accessors <= //
78 
80  std::map<std::string, int>& dimensions() { return dimensions_; }
82  std::map<std::string, std::shared_ptr<Tensor> >& tensors() { return tensors_; }
84  std::shared_ptr<Tensor>& operator[] (const std::string& key) { return tensors_[key]; }
85 
87  void print_header(std::string fh="outfile") const { print(fh,0); }
89  void print(std::string fh="outfile", int print = 1) const;
90 
92  size_t core_doubles() const;
94  size_t disk_doubles() const;
95 
96  // => Front-end interface <= //
97 
99  void new_dimension(const std::string& name, int size);
101  void delete_dimension(const std::string& name);
103  void new_core_tensor(const std::string& name, const std::string& dimensions, double* data = NULL, bool trust = false);
105  void new_disk_tensor(const std::string& name, const std::string& dimensions, bool save = false, bool load = false);
107  void add_tensor(const std::string& name, std::shared_ptr<Tensor> tensor);
109  void delete_tensor(const std::string& name);
110 
111 };
112 
117 class Tensor {
118 
119 private:
120 
122  static long int unique_id;
123 
124 protected:
125 
126  // => Object Data <= //
127 
129  std::string name_;
131  std::string filename_;
133  size_t numel_;
135  int order_;
137  std::vector<std::string> dimensions_;
139  std::vector<int> sizes_;
141  std::vector<int> active_sizes_;
142 
143  // => Helper Methods <= //
144 
146  void set_filename();
147 
148 public:
149 
150  // => Constructors <= //
151 
153  Tensor(const std::string& name,
154  std::vector<std::string>& dimensions,
155  std::vector<int>& sizes);
156 
158  virtual ~Tensor();
159 
160  // => Universal Accessors <= //
161 
163  std::string name() const { return name_; }
165  std::string filename() const { return filename_; }
167  size_t numel() const { return numel_; }
169  int order() const { return order_; }
171  std::vector<std::string>& dimensions() { return dimensions_; }
173  std::vector<int>& sizes() { return sizes_; }
175  std::vector<int>& active_sizes() { return active_sizes_; }
176 
178  virtual bool core() const = 0;
180  virtual bool disk() const = 0;
182  virtual bool trust() const = 0;
184  virtual bool swapped() const = 0;
186  virtual void swap_check() const = 0;
187 
189  virtual size_t core_doubles() const = 0;
191  virtual size_t disk_doubles() const = 0;
192 
194  virtual void print(std::string fh="outfile", int level = 2) const = 0;
196  void print_header(std::string fh="outfile") const { print(fh, 0); }
197 
198 
199  // => Common Interface <= //
200 
202  virtual void zero() = 0;
214  virtual void slice(std::shared_ptr<Tensor> A, std::vector<std::tuple<bool,int,int,bool,int,int> >& topology);
216  virtual double* pointer() const { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
218  virtual FILE* file_pointer() { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
219 
220  // => CoreTensor Interface <= //
221 
222  // > Unary Operations < //
223 
225  virtual void swap_out(bool changed = true) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
227  virtual void swap_in(bool read = true) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
229  virtual void scale(double val) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
231  virtual void set_data(double* data) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
233  virtual void set_pointer(double* data) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
234 
235  // > Binary Operations < //
236 
238  virtual void add(std::shared_ptr<Tensor> A, double alpha = 1.0, double beta = 0.0) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
244  virtual void permute(std::shared_ptr<Tensor> A, std::vector<int>& topology) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
245 
246  // > Ternary Operations < //
247 
260  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."); }
261 
262  // => DiskTensor Interface <= //
263 
265  virtual void set_save(bool save) { throw PSIEXCEPTION("Not implemented in this Tensor subclass."); }
266 };
267 
273 class CoreTensor : public Tensor {
274 
275 protected:
276 
277  // => Object Data <= //
278 
280  bool trust_;
282  double* data_;
284  bool swapped_;
286  FILE* fh_;
287 
288 public:
289 
290  // => Constructors <= //
291 
293  CoreTensor(const std::string& name,
294  std::vector<std::string>& dimensions, std::vector<int>& sizes,
295  double* data = NULL, bool trust = false);
296 
298  virtual ~CoreTensor();
299 
301  static std::shared_ptr<Tensor> build(const std::string& name,
302  double* data = NULL, bool trust = false);
304  static std::shared_ptr<Tensor> build(const std::string& name,
305  const std::string& dimension1, int size1,
306  double* data = NULL, bool trust = false);
308  static std::shared_ptr<Tensor> build(const std::string& name,
309  const std::string& dimension1, int size1,
310  const std::string& dimension2, int size2,
311  double* data = NULL, bool trust = false);
313  static std::shared_ptr<Tensor> build(const std::string& name,
314  const std::string& dimension1, int size1,
315  const std::string& dimension2, int size2,
316  const std::string& dimension3, int size3,
317  double* data = NULL, bool trust = false);
319  static std::shared_ptr<Tensor> build(const std::string& name,
320  const std::string& dimension1, int size1,
321  const std::string& dimension2, int size2,
322  const std::string& dimension3, int size3,
323  const std::string& dimension4, int size4,
324  double* data = NULL, bool trust = false);
325 
326  // => Universal Accessors <= //
327 
329  virtual bool core() const { return true; }
331  virtual bool disk() const { return false; }
333  virtual bool trust() const { return trust_; }
335  virtual bool swapped() const { return swapped_; }
337  virtual void swap_check() const;
338 
340  virtual size_t core_doubles() const { return (swapped() | trust() ? 0L : numel_); }
342  virtual size_t disk_doubles() const { return 0L; }
343 
345  virtual void print(std::string fh="outfile", int level = 2) const;
346 
347  // => Conditional Accessors <= //
348 
350  virtual double* pointer() const { return data_; }
352  virtual FILE* file_pointer() { return fh_; }
353 
354  // > Unary Operations < //
355 
357  virtual void swap_out(bool changed = true);
359  virtual void swap_in(bool read = true);
361  virtual void zero();
363  virtual void scale(double val);
365  virtual void set_data(double* data);
367  virtual void set_pointer(double* data);
368 
369  // > Binary Operations < //
370 
372  virtual void add(std::shared_ptr<Tensor> A, double alpha = 1.0, double beta = 0.0);
378  virtual void permute(std::shared_ptr<Tensor> A, std::vector<int>& topology);
379 
380  // > Ternary Operations < //
381 
393  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);
394 
395 };
396 
402 class DiskTensor : public Tensor {
403 
404 protected:
405 
406  // => Object Data <= //
407 
409  bool save_;
411  FILE* fh_;
412 
413 public:
414 
415  // => Constructors <= //
416 
423  DiskTensor(const std::string& name,
424  std::vector<std::string>& dimensions, std::vector<int>& sizes,
425  bool save = false, bool load = false);
426 
428  virtual ~DiskTensor();
429 
431  static std::shared_ptr<Tensor> build(const std::string& name,
432  bool save = false, bool load = false);
434  static std::shared_ptr<Tensor> build(const std::string& name,
435  const std::string& dimension1, int size1,
436  bool save = false, bool load = false);
438  static std::shared_ptr<Tensor> build(const std::string& name,
439  const std::string& dimension1, int size1,
440  const std::string& dimension2, int size2,
441  bool save = false, bool load = false);
443  static std::shared_ptr<Tensor> build(const std::string& name,
444  const std::string& dimension1, int size1,
445  const std::string& dimension2, int size2,
446  const std::string& dimension3, int size3,
447  bool save = false, bool load = false);
449  static std::shared_ptr<Tensor> build(const std::string& name,
450  const std::string& dimension1, int size1,
451  const std::string& dimension2, int size2,
452  const std::string& dimension3, int size3,
453  const std::string& dimension4, int size4,
454  bool save = false, bool load = false);
455 
456  // => Universal Accessors <= //
457 
459  virtual bool core() const { return false; }
461  virtual bool disk() const { return true; }
463  virtual bool trust() const { return false; }
465  virtual bool swapped() const { return false; }
467  virtual void swap_check() const;
468 
470  virtual size_t core_doubles() const { return 0L; }
472  virtual size_t disk_doubles() const { return numel_; }
473 
475  virtual void print(std::string fh="outfile", int level = 2) const;
476 
477  // => Conditional Accessors <= //
478 
480  virtual FILE* file_pointer() { return fh_; }
481 
483  virtual void set_save(bool save) { save_ = save; }
484 
486  virtual void zero();
487 
488 };
489 
490 } // End namespace
491 
492 #endif
virtual size_t core_doubles() const
How many core doubles are currently allocated by this tensor?
Definition: thce.h:340
virtual void zero()
Zero the tensor out.
Definition: thce.cc:848
bool save_
Save file upon destruct?
Definition: thce.h:409
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:225
size_t disk_doubles() const
Return the current disk memory utilization of this instance, in doubles.
Definition: thce.cc:114
virtual void print(std::string fh="outfile", int level=2) const
Print the available tensor data.
Definition: thce.cc:1464
virtual void permute(std::shared_ptr< Tensor > A, std::vector< int > &topology)
Definition: thce.cc:874
virtual bool core() const
Is this a core tensor?
Definition: thce.h:329
int order_
Order (number of dimensions) in this Tensor.
Definition: thce.h:135
virtual bool swapped() const =0
Is this a swapped-out core tensor?
std::string name() const
Name of this Tensor.
Definition: thce.h:163
virtual bool trust() const
Is this a trust core tensor?
Definition: thce.h:333
virtual size_t disk_doubles() const
How many disk doubles are currently allocated by this tensor?
Definition: thce.h:472
size_t numel() const
Total number of elements in this Tensor.
Definition: thce.h:167
virtual void swap_in(bool read=true)
Swap this tensor in to core (if read is false, allocate with zeros)
Definition: thce.cc:830
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:238
std::string filename() const
Unique filename of this Tensor.
Definition: thce.h:165
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:1485
virtual void scale(double val)
Scale the tensor by val.
Definition: thce.cc:854
virtual ~DiskTensor()
Master destructor.
Definition: thce.cc:1379
virtual void set_pointer(double *data)
Update the trust pointer to data (throws if not trust)
Definition: thce.h:233
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:860
DiskTensor(const std::string &name, std::vector< std::string > &dimensions, std::vector< int > &sizes, bool save=false, bool load=false)
Definition: thce.cc:1369
virtual ~Tensor()
Master destructor.
Definition: thce.cc:212
std::vector< int > & sizes()
Allocated sizes of dimensions.
Definition: thce.h:173
void delete_tensor(const std::string &name)
Decrement a Tensor of any type (removes from tensors_)
Definition: thce.cc:188
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:402
virtual FILE * file_pointer()
File pointer underlying this Tensor (careful, returns NULL if not swapped, mirror not guaranteed) ...
Definition: thce.h:352
virtual void slice(std::shared_ptr< Tensor > A, std::vector< std::tuple< bool, int, int, bool, int, int > > &topology)
Definition: thce.cc:234
std::vector< int > sizes_
Sizes of dimensions (lda-style)
Definition: thce.h:139
virtual ~CoreTensor()
Master destructor.
Definition: thce.cc:597
size_t numel_
Total number of elements in this Tensor.
Definition: thce.h:133
void tensor_check(const std::string &name)
Throws if a tensor name is not in tensors_.
Definition: thce.cc:136
virtual bool trust() const
Is this a trust core tensor?
Definition: thce.h:463
static long int unique_id
Unique ID to prevent disk clash.
Definition: thce.h:122
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:150
void dimension_check(const std::string &name)
Throws if a dimension name is not in dimensions_.
Definition: thce.cc:130
virtual void swap_check() const
Throw an exception if the tensor is swapped out or disk based.
Definition: thce.cc:1386
FILE * fh_
File handle (valid if this tensor has ever been swapped out)
Definition: thce.h:286
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:141
std::vector< std::string > dimensions_
Classes of dimensions.
Definition: thce.h:137
static std::shared_ptr< Tensor > build(const std::string &name, bool save=false, bool load=false)
Order-0 Constructor.
Definition: thce.cc:1456
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:54
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:576
std::map< std::string, int > & dimensions()
Accessor to the list of currently declared dimensions (dimension classes)
Definition: thce.h:80
Definition: thce.h:117
std::string name_
Name of this Tensor.
Definition: thce.h:129
virtual bool disk() const
Is this a disk tensor?
Definition: thce.h:461
Definition: thce.h:273
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:167
virtual void permute(std::shared_ptr< Tensor > A, std::vector< int > &topology)
Definition: thce.h:244
void delete_dimension(const std::string &name)
Remove a dimension from this instance.
Definition: thce.cc:146
virtual void set_save(bool save)
Set save flag in DiskTensor.
Definition: thce.h:265
std::map< std::string, std::shared_ptr< Tensor > > tensors_
List of currently declated tensors.
Definition: thce.h:56
virtual void set_data(double *data)
Copy contents of data (ignores active dims)
Definition: thce.cc:792
static std::shared_ptr< Tensor > build(const std::string &name, double *data=NULL, bool trust=false)
Order-0 Constructor.
Definition: thce.cc:683
virtual bool swapped() const
Is this a swapped-out core tensor?
Definition: thce.h:465
virtual void swap_in(bool read=true)
Swap this tensor in to core (explicitly reads if true)
Definition: thce.h:227
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:350
THCE()
Instance Constructor.
Definition: thce.cc:47
virtual double * pointer() const
Access the data pointer.
Definition: thce.h:216
virtual void scale(double val)
Scale the tensor by val (ignores active dims)
Definition: thce.h:229
virtual void swap_out(bool changed=true)
Swap this tensor out to disk (if existing file, only write if changed)
Definition: thce.cc:798
Tensor(const std::string &name, std::vector< std::string > &dimensions, std::vector< int > &sizes)
Master constructor.
Definition: thce.cc:195
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:785
void print_header(std::string fh="outfile") const
Print a brief summary.
Definition: thce.h:87
bool trust_
Is this a trust pointer?
Definition: thce.h:280
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:691
std::vector< std::string > & dimensions()
Classes of dimensions.
Definition: thce.h:171
void set_filename()
Set the filename to scratch, PID, namespace, unique ID, name.
Definition: thce.cc:215
virtual size_t disk_doubles() const
How many disk doubles are currently allocated by this tensor?
Definition: thce.h:342
void print(std::string fh="outfile", int print=1) const
Print a more detailed trace of the object.
Definition: thce.cc:53
bool swapped_
Is this tensor swapped?
Definition: thce.h:284
virtual size_t core_doubles() const
How many core doubles are currently allocated by this tensor?
Definition: thce.h:470
virtual bool swapped() const
Is this a swapped-out core tensor?
Definition: thce.h:335
std::string filename_
Unique filename to prevent disk clash.
Definition: thce.h:131
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:1004
virtual FILE * file_pointer()
Access the file pointer.
Definition: thce.h:218
virtual bool core() const
Is this a core tensor?
Definition: thce.h:459
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:260
void add_tensor(const std::string &name, std::shared_ptr< Tensor > tensor)
Add or alias an existing Tensor of any type.
Definition: thce.cc:184
Definition: thce.h:46
int order() const
Order (number of dimensions) in this Tensor.
Definition: thce.h:169
virtual void set_data(double *data)
Copy contents of data (ignores active dims)
Definition: thce.h:231
Definition: PsiFileImpl.h:38
virtual void swap_check() const
Throw an exception if the tensor is swapped out or disk based.
Definition: thce.cc:612
void new_dimension(const std::string &name, int size)
Add or overwrite a dimension to this instance.
Definition: thce.cc:142
void print_header(std::string fh="outfile") const
Print only name and sizing data.
Definition: thce.h:196
virtual ~THCE()
Instance Destructor.
Definition: thce.cc:50
std::vector< int > & active_sizes()
Active sizes of dimensions.
Definition: thce.h:175
std::map< std::string, std::shared_ptr< Tensor > > & tensors()
Accessor to the list of currently declared tensors.
Definition: thce.h:82
#define PSIEXCEPTION(message)
Definition: exception.h:47
virtual FILE * file_pointer()
File underlying this Tensor.
Definition: thce.h:480
double * data_
Data underlying this Tensor.
Definition: thce.h:282
FILE * fh_
File pointer.
Definition: thce.h:411
std::shared_ptr< Tensor > & operator[](const std::string &key)
Direct tensor element accessor.
Definition: thce.h:84
virtual bool disk() const
Is this a disk tensor?
Definition: thce.h:331
size_t core_doubles() const
Return the current core memory utilization of this instance, in doubles.
Definition: thce.cc:98
virtual void set_save(bool save)
Set save flag for disk resuse.
Definition: thce.h:483