MPQC  3.0.0-alpha
tensor.h
1 //
2 // tensor.h --- tensor class that contains an interface to MemoryGrp etc
3 //
4 // Copyright (C) 2008 Toru Shiozaki
5 //
6 // Author: Toru Shiozaki <shiozaki.toru@gmail.com>
7 // Maintainer: TS
8 //
9 // This file is part of the SC Toolkit.
10 //
11 // The SC Toolkit is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU Library General Public License as published by
13 // the Free Software Foundation; either version 2, or (at your option)
14 // any later version.
15 //
16 // The SC Toolkit 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 Library General Public License for more details.
20 //
21 // You should have received a copy of the GNU Library General Public License
22 // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
23 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24 //
25 // The U.S. Government is granted a limited license as per AL 91-7.
26 //
27 
28 #ifndef _chemistry_qc_ccr12_tensor_h
29 #define _chemistry_qc_ccr12_tensor_h
30 
31 #include <string>
32 #include <vector>
33 #include <util/misc/compute.h>
34 #include <util/group/memory.h>
35 #include <util/group/memregion.h>
36 #include <util/group/message.h>
37 #include <util/group/thread.h>
38 #include <math/distarray4/distarray4.h>
39 
40 // note disk-based algorithm does not support parallel execution so far.
41 #define DISK_BASED_SMITH
42 
43 #ifdef DISK_BASED_SMITH
44 #include <fstream>
45 #endif
46 
47 namespace sc {
48 
49 class Tensor : virtual public RefCount {
50  protected:
51  const Ref<MemoryGrp>& mem_;
52 
54  std::string filename_;
55 
57  std::map<long,long> hash_table_;
58 
60  long filesize_;
61 
63 #ifndef DISK_BASED_SMITH
65 #else
66  std::fstream* file_;
67 #endif
68  bool file_allocated_;
69 
71  std::vector<long> determine_filesizes();
72 
73  public:
74  Tensor(std::string filename,const Ref<MemoryGrp>& mem);
75  ~Tensor();
76 
78  std::string filename() const {return filename_;};
79 
81 #ifndef DISK_BASED_SMITH
82  MemoryGrpRegion* file() const {return file_;};
83 #else
84  std::fstream* file() { return file_; };
85 #endif
86 
88  void set_filesize(long i);
89  long get_filesize() const {return filesize_;};
90 
92  void createfile();
93  void deletefile();
94 
96  void input_offset(long tag, long offset);
97 
99  void get_block(long tag, double* data);
101  void add_block(long tag, double* data);
103  void put_block(long tag, double* data);
105  bool exists(long tag) const;
107  bool is_this_local(long tag);
108 
110  void zero();
111 
113  void assign(double a);
114 
116  double norm() const;
117 
119  double ddot(Ref<Tensor>&) const;
120 
122  Ref<Tensor> copy() const;
123 
125  Ref<Tensor> clone() const;
126 
128  void daxpy(const Ref<Tensor>&, double);
129 
131  void scale(double a);
132 
134 #ifndef DISK_BASED_SMITH
135  void sync() const { const_cast<MemoryGrpRegion*>(file_)->sync();};
136 #else
137  void sync() const {}; // disk-based algorithm does not support parallel runs so far
138 #endif
139 
141  void print(const std::string& label, std::ostream& os = ExEnv::out0()) const;
142 };
143 
147 double RMS(const Tensor& t);
148 
149 }
150 
151 #endif
152 
sc::Tensor::assign
void assign(double a)
assigns all values to a
sc::Tensor::add_block
void add_block(long tag, double *data)
add a block to the distributed file (non-blocking); double* data will be destroyed
sc::Tensor::exists
bool exists(long tag) const
does this block exist?
sc::Tensor::get_block
void get_block(long tag, double *data)
get a block from the distributed file (non-blocking)
sc::Tensor::sync
void sync() const
sync
Definition: tensor.h:137
sc::Tensor
Definition: tensor.h:49
sc::Tensor::ddot
double ddot(Ref< Tensor > &) const
obtain the ddot of two tensors
sc::Tensor::filename_
std::string filename_
for use in disk-based algorithm
Definition: tensor.h:54
sc::Tensor::determine_filesizes
std::vector< long > determine_filesizes()
determines the distribution of blocks to nodes
sc::Tensor::zero
void zero()
initialize/clear tensors to zero
sc::Ref
A template class that maintains references counts.
Definition: ref.h:361
sc::Tensor::filesize_
long filesize_
tensor size in double
Definition: tensor.h:60
sc::Tensor::filename
std::string filename() const
returns filename_
Definition: tensor.h:78
sc::Tensor::scale
void scale(double a)
scale self by a
sc::Tensor::input_offset
void input_offset(long tag, long offset)
input for the hash table
sc::Tensor::clone
Ref< Tensor > clone() const
return a tensor that have the same hashtable, and is intilaized to zero
sc::Tensor::set_filesize
void set_filesize(long i)
set/get the filesize of the tensor
sc::Tensor::hash_table_
std::map< long, long > hash_table_
hash table: hash_table_.insert(key,offset)
Definition: tensor.h:57
sc::Tensor::file_
std::fstream * file_
data area
Definition: tensor.h:66
sc::RMS
double RMS(const Tensor &t)
Computes the `‘RMS norm’' of the tensor, defined as tensor->norm() divided by the size of the tensor.
sc::Tensor::norm
double norm() const
obtain the norm of a tensor
sc::Tensor::print
void print(const std::string &label, std::ostream &os=ExEnv::out0()) const
print
sc::Tensor::put_block
void put_block(long tag, double *data)
put a block to the distributed file (non-blocking)
sc::Tensor::is_this_local
bool is_this_local(long tag)
returns if a block associated with long tag resides in a local memory or not
sc::Tensor::file
std::fstream * file()
returns MemoryGrpRegion for this tensor
Definition: tensor.h:84
sc::ExEnv::out0
static std::ostream & out0()
Return an ostream that writes from node 0.
sc::Tensor::createfile
void createfile()
create/delete the distributed file for the tensor
sc::Tensor::daxpy
void daxpy(const Ref< Tensor > &, double)
perform daxpy of tensors (self+=a*other)
sc::RefCount
The base class for all reference counted objects.
Definition: ref.h:192
sc::Tensor::copy
Ref< Tensor > copy() const
return a copy of self
sc::MemoryGrpRegion
The MemoryGrpRegion is a MemoryGrp proxy to a region of a MemoryGrp.
Definition: memregion.h:46
sc
Contains all MPQC code up to version 3.
Definition: mpqcin.h:14

Generated at Sun Jan 26 2020 23:23:57 for MPQC 3.0.0-alpha using the documentation package Doxygen 1.8.16.