JDFTx  1.2.0
ScalarField.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx. If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #ifndef JDFTX_CORE_SCALARFIELD_H
21 #define JDFTX_CORE_SCALARFIELD_H
22 
23 
26 
31 #include <memory>
32 #include <core/scalar.h>
33 #include <core/Util.h>
34 #include <core/ManagedMemory.h>
35 #include <electronic/matrix.h>
36 
37 class GridInfo; //Grid description and memory manager
38 struct ScalarFieldData; //Real space data storage container for real scalar fields
39 struct ScalarFieldTildeData; //Reciprocal space data storage container for real scalar fields
40 struct complexScalarFieldData; //Real space data storage container for complex scalar fields
41 struct complexScalarFieldTildeData; //Reciprocal space data storage container for complex scalar fields
42 
43 //DO NOT work with above data types directly, instead use the following smart pointer types:
44 typedef std::shared_ptr<ScalarFieldData> ScalarField;
45 typedef std::shared_ptr<ScalarFieldTildeData> ScalarFieldTilde;
46 typedef std::shared_ptr<complexScalarFieldData> complexScalarField;
47 typedef std::shared_ptr<complexScalarFieldTildeData> complexScalarFieldTilde;
48 
49 //Define shorthands for fetching gpu data in gpu mode and cpu data in cpu mode:
50 #ifdef GPU_ENABLED
51  #define DECLARE_DATA_PREF_ACCESS \
52  DataType* dataPref(bool shouldAbsorbScale=true) { return dataGpu(shouldAbsorbScale); } \
53  const DataType* dataPref(bool shouldAbsorbScale=true) const { return dataGpu(shouldAbsorbScale); }
54 #else
55  #define DECLARE_DATA_PREF_ACCESS \
56  DataType* dataPref(bool shouldAbsorbScale=true) { return data(shouldAbsorbScale); } \
57  const DataType* dataPref(bool shouldAbsorbScale=true) const { return data(shouldAbsorbScale); }
58 #endif
59 
61 struct FieldData : private ManagedMemory
62 {
63  int nElem;
64  double scale;
65  const GridInfo& gInfo;
66 
67  void absorbScale() const;
68  inline void zero() { ManagedMemory::zero(); }
69 
70  typedef void DataType;
71 
72  void* data(bool shouldAbsorbScale=true);
73  const void* data(bool shouldAbsorbScale=true) const;
74  #ifdef GPU_ENABLED
75  void* dataGpu(bool shouldAbsorbScale=true);
76  const void* dataGpu(bool shouldAbsorbScale=true) const;
77  #endif
78 
79  DECLARE_DATA_PREF_ACCESS
80 
81  inline bool isOnGpu() const { return ManagedMemory::isOnGpu(); }
82 
83  FieldData(const GridInfo& gInfo, string category, int nElem, int nDoublesPerElem=1, bool onGpu=false);
84  void copyData(const FieldData& other);
85 
86  //Inter-process communication (expose corresponding ManagedMemory functions):
87  inline void send(int dest, int tag=0) const { absorbScale(); ManagedMemory::send(dest,tag); }
88  inline void recv(int src, int tag=0) { absorbScale(); ManagedMemory::recv(src,tag); }
89  inline void bcast(int root=0) { absorbScale(); ManagedMemory::bcast(root); }
90  inline void allReduce(MPIUtil::ReduceOp op, bool safeMode=false) { absorbScale(); ManagedMemory::allReduce(op, safeMode, isReal); }
91 
92 protected:
93  struct PrivateTag {};
94 private:
95  bool isReal;
96 };
97 
98 //Shorthand for defining the data() and dataGpu() functions in derived classes of FieldData
99 #ifdef GPU_ENABLED
100  #define DECLARE_DATA_ACCESS \
101  DataType* data(bool shouldAbsorbScale=true) { return (DataType*)FieldData::data(shouldAbsorbScale); } \
102  const DataType* data(bool shouldAbsorbScale=true) const { return (const DataType*)FieldData::data(shouldAbsorbScale); } \
103  DataType* dataGpu(bool shouldAbsorbScale=true) { return (DataType*)FieldData::dataGpu(shouldAbsorbScale); } \
104  const DataType* dataGpu(bool shouldAbsorbScale=true) const { return (const DataType*)FieldData::dataGpu(shouldAbsorbScale); }
105 #else
106  #define DECLARE_DATA_ACCESS \
107  DataType* data(bool shouldAbsorbScale=true) { return (DataType*)FieldData::data(shouldAbsorbScale); } \
108  const DataType* data(bool shouldAbsorbScale=true) const { return (const DataType*)FieldData::data(shouldAbsorbScale); }
109 #endif
110 
117 struct ScalarFieldData : public FieldData
118 { typedef double DataType;
119  DECLARE_DATA_ACCESS
120  DECLARE_DATA_PREF_ACCESS
121  ScalarField clone() const;
122  static ScalarField alloc(const GridInfo& gInfo, bool onGpu=false);
123  ScalarFieldData(const GridInfo& gInfo, bool onGpu, PrivateTag);
124  matrix toMatrix() const;
125 };
126 
127 
135 { typedef complex DataType;
136  DECLARE_DATA_ACCESS
137  DECLARE_DATA_PREF_ACCESS
138  ScalarFieldTilde clone() const;
139  static ScalarFieldTilde alloc(const GridInfo& gInfo, bool onGpu=false);
140  double getGzero() const;
141  void setGzero(double Gzero);
142  ScalarFieldTildeData(const GridInfo& gInfo, bool onGpu, PrivateTag);
143 };
144 
145 
153 { typedef complex DataType;
154  DECLARE_DATA_ACCESS
155  DECLARE_DATA_PREF_ACCESS
156  complexScalarField clone() const;
157  static complexScalarField alloc(const GridInfo& gInfo, bool onGpu=false);
158  complexScalarFieldData(const GridInfo& gInfo, bool onGpu, PrivateTag);
159  matrix toMatrix() const;
160 };
161 
169 { typedef complex DataType;
170  DECLARE_DATA_ACCESS
171  DECLARE_DATA_PREF_ACCESS
173  static complexScalarFieldTilde alloc(const GridInfo& gInfo, bool onGpu=false);
174  complexScalarFieldTildeData(const GridInfo& gInfo, bool onGpu, PrivateTag);
175 };
176 
177 
178 
181 { const GridInfo& gInfo;
182  int nElem;
183  double* data;
184  #ifdef GPU_ENABLED
185  double* dataGpu;
186  #endif
187  double* dataPref;
188 
189  RealKernel(const GridInfo&);
190  ~RealKernel();
191  void set();
192 };
193 
194 #undef DECLARE_DATA_PREF_ACCESS
195 #undef DECLARE_DATA_ACCESS
196 
198 #endif //JDFTX_CORE_SCALARFIELD_H
199 
Reciprocal space real scalar field data Do not use this data structure directly or from a simple poin...
Definition: ScalarField.h:134
Reciprocal space complex scalar field data Do not use this data structure directly or from a simple p...
Definition: ScalarField.h:168
void allReduce(MPIUtil::ReduceOp op, bool safeMode=false, bool ignoreComplexCheck=false)
apply all-to-all reduction (see MPIUtil::allReduce). Optionally ignore unsupported operations for com...
Simulation grid descriptor.
Definition: GridInfo.h:45
void send(int dest, int tag=0) const
send to another process
void zero()
initialize to zero
Definition: ScalarField.h:68
General complex matrix.
Definition: matrix.h:58
std::shared_ptr< ScalarFieldTildeData > ScalarFieldTilde
A smart reference-counting pointer to ScalarFieldTildeData.
Definition: ScalarField.h:45
Base class for managed-memory objects (that could potentially live on GPUs as well) ...
Definition: ManagedMemory.h:26
void setGzero(const VectorFieldTilde &X, vector3<> v)
set G=0 components
Definition: VectorField.h:158
void absorbScale() const
absorb scale factor into data
Used to prevent direct use of ScalarField constructors, and force the shared_ptr usage.
Definition: ScalarField.h:93
complex * dataGpu()
Get a gpu data pointer (must be called from GPU owner thread)
vector3 getGzero(const VectorFieldTilde &X)
return G=0 components
Definition: VectorField.h:157
complex DataType
Type of data in container (useful for templating)
Definition: ScalarField.h:153
bool isOnGpu() const
Check where the data is (for #ifdef simplicity exposed even when no GPU_ENABLED)
Definition: ManagedMemory.h:46
std::shared_ptr< complexScalarFieldTildeData > complexScalarFieldTilde
A smart reference-counting pointer to complexScalarFieldTildeData.
Definition: ScalarField.h:47
Tptr clone(const Tptr &X)
Clone (NOTE: operator= is by reference for the ScalarField classes)
Definition: Operators.h:111
double DataType
Type of data in container (useful for templating)
Definition: ScalarField.h:118
void allReduce(MPIUtil::ReduceOp op, bool safeMode=false)
apply all-to-all reduction
Definition: ScalarField.h:90
complex DataType
Type of data in container (useful for templating)
Definition: ScalarField.h:135
void copyData(const FieldData &other)
copy data and scale (used by clone())
int nElem
number of elements = gInfo.nG
Definition: ScalarField.h:182
void send(int dest, int tag=0) const
send to another process
Definition: ScalarField.h:87
DECLARE_DATA_PREF_ACCESS bool isOnGpu() const
Check where the data is (for #ifdef simplicity exposed even when no GPU_ENABLED)
Definition: ScalarField.h:81
double * data
cpu data pointer
Definition: ScalarField.h:183
double scale
overall scale factor of the data array
Definition: ScalarField.h:64
std::shared_ptr< complexScalarFieldData > complexScalarField
A smart reference-counting pointer to complexScalarFieldData.
Definition: ScalarField.h:46
Miscellaneous utilities.
void zero()
set all elements to zero
Base class for ScalarFieldData and ScalarFieldTildeData.
Definition: ScalarField.h:61
void recv(int src, int tag=0)
receive from another process
Definition: ScalarField.h:88
void DataType
this base class has no specific data type
Definition: ScalarField.h:70
Real space complex scalar field data Do not use this data structure directly or from a simple pointer...
Definition: ScalarField.h:152
complex * data()
Return a pointer to the actual data Return a CPU pointer to the actual data, will move data from GPU ...
Special class for storing real reciprocal-space kernels encountered ever so often for convolutions...
Definition: ScalarField.h:180
int nElem
number of elements = #gInfo.nr
Definition: ScalarField.h:63
std::shared_ptr< ScalarFieldData > ScalarField
A smart reference-counting pointer to ScalarFieldData.
Definition: ScalarField.h:41
void bcast(int root=0)
synchronize across processes (using value on specified root process)
Complex number (need to define our own because we need operators for gpu code as well) ...
Definition: scalar.h:49
complex DataType
Type of data in container (useful for templating)
Definition: ScalarField.h:169
void bcast(int root=0)
synchronize across processes (using value on specified root process)
Definition: ScalarField.h:89
double * dataGpu
gpu data pointer (unlike above classes, both cpu and gpu stored simultaneously)
Definition: ScalarField.h:185
Real space real scalar field data Do not use this data structure directly or from a simple pointer Sc...
Definition: ScalarField.h:117
void recv(int src, int tag=0)
receive from another process
double * dataPref
points to data or dataGpu depending on GPU_ENABLED
Definition: ScalarField.h:187
const GridInfo & gInfo
simulation grid info
Definition: ScalarField.h:65