JDFTx  1.1.1
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 
36 class GridInfo; //Grid description and memory manager
37 struct ScalarFieldData; //Real space data storage container for real scalar fields
38 struct ScalarFieldTildeData; //Reciprocal space data storage container for real scalar fields
39 struct complexScalarFieldData; //Real space data storage container for complex scalar fields
40 struct complexScalarFieldTildeData; //Reciprocal space data storage container for complex scalar fields
41 
42 //DO NOT work with above data types directly, instead use the following smart pointer types:
43 typedef std::shared_ptr<ScalarFieldData> ScalarField;
44 typedef std::shared_ptr<ScalarFieldTildeData> ScalarFieldTilde;
45 typedef std::shared_ptr<complexScalarFieldData> complexScalarField;
46 typedef std::shared_ptr<complexScalarFieldTildeData> complexScalarFieldTilde;
47 
48 //Define shorthands for fetching gpu data in gpu mode and cpu data in cpu mode:
49 #ifdef GPU_ENABLED
50  #define DECLARE_DATA_PREF_ACCESS \
51  DataType* dataPref(bool shouldAbsorbScale=true) { return dataGpu(shouldAbsorbScale); } \
52  const DataType* dataPref(bool shouldAbsorbScale=true) const { return dataGpu(shouldAbsorbScale); }
53 #else
54  #define DECLARE_DATA_PREF_ACCESS \
55  DataType* dataPref(bool shouldAbsorbScale=true) { return data(shouldAbsorbScale); } \
56  const DataType* dataPref(bool shouldAbsorbScale=true) const { return data(shouldAbsorbScale); }
57 #endif
58 
60 struct FieldData : private ManagedMemory
61 {
62  int nElem;
63  double scale;
64  const GridInfo& gInfo;
65 
66  void absorbScale() const;
67  inline void zero() { ManagedMemory::zero(); }
68 
69  typedef void DataType;
70 
71  void* data(bool shouldAbsorbScale=true);
72  const void* data(bool shouldAbsorbScale=true) const;
73  #ifdef GPU_ENABLED
74  void* dataGpu(bool shouldAbsorbScale=true);
75  const void* dataGpu(bool shouldAbsorbScale=true) const;
76  #endif
77 
78  DECLARE_DATA_PREF_ACCESS
79 
80  inline bool isOnGpu() const { return ManagedMemory::isOnGpu(); }
81 
82  FieldData(const GridInfo& gInfo, string category, int nElem, int nDoublesPerElem=1, bool onGpu=false);
83  void copyData(const FieldData& other);
84 
85  //Inter-process communication (expose corresponding ManagedMemory functions):
86  inline void send(int dest, int tag=0) const { absorbScale(); ManagedMemory::send(dest,tag); }
87  inline void recv(int src, int tag=0) { absorbScale(); ManagedMemory::recv(src,tag); }
88  inline void bcast(int root=0) { absorbScale(); ManagedMemory::bcast(root); }
89  inline void allReduce(MPIUtil::ReduceOp op, bool safeMode=false) { absorbScale(); ManagedMemory::allReduce(op, safeMode, isReal); }
90 
91 protected:
92  struct PrivateTag {};
93 private:
94  bool isReal;
95 };
96 
97 //Shorthand for defining the data() and dataGpu() functions in derived classes of FieldData
98 #ifdef GPU_ENABLED
99  #define DECLARE_DATA_ACCESS \
100  DataType* data(bool shouldAbsorbScale=true) { return (DataType*)FieldData::data(shouldAbsorbScale); } \
101  const DataType* data(bool shouldAbsorbScale=true) const { return (const DataType*)FieldData::data(shouldAbsorbScale); } \
102  DataType* dataGpu(bool shouldAbsorbScale=true) { return (DataType*)FieldData::dataGpu(shouldAbsorbScale); } \
103  const DataType* dataGpu(bool shouldAbsorbScale=true) const { return (const DataType*)FieldData::dataGpu(shouldAbsorbScale); }
104 #else
105  #define DECLARE_DATA_ACCESS \
106  DataType* data(bool shouldAbsorbScale=true) { return (DataType*)FieldData::data(shouldAbsorbScale); } \
107  const DataType* data(bool shouldAbsorbScale=true) const { return (const DataType*)FieldData::data(shouldAbsorbScale); }
108 #endif
109 
116 struct ScalarFieldData : public FieldData
117 { typedef double DataType;
118  DECLARE_DATA_ACCESS
119  DECLARE_DATA_PREF_ACCESS
120  ScalarField clone() const;
121  static ScalarField alloc(const GridInfo& gInfo, bool onGpu=false);
122  ScalarFieldData(const GridInfo& gInfo, bool onGpu, PrivateTag);
123 };
124 
125 
133 { typedef complex DataType;
134  DECLARE_DATA_ACCESS
135  DECLARE_DATA_PREF_ACCESS
136  ScalarFieldTilde clone() const;
137  static ScalarFieldTilde alloc(const GridInfo& gInfo, bool onGpu=false);
138  double getGzero() const;
139  void setGzero(double Gzero);
140  ScalarFieldTildeData(const GridInfo& gInfo, bool onGpu, PrivateTag);
141 };
142 
143 
151 { typedef complex DataType;
152  DECLARE_DATA_ACCESS
153  DECLARE_DATA_PREF_ACCESS
154  complexScalarField clone() const;
155  static complexScalarField alloc(const GridInfo& gInfo, bool onGpu=false);
156  complexScalarFieldData(const GridInfo& gInfo, bool onGpu, PrivateTag);
157 };
158 
166 { typedef complex DataType;
167  DECLARE_DATA_ACCESS
168  DECLARE_DATA_PREF_ACCESS
170  static complexScalarFieldTilde alloc(const GridInfo& gInfo, bool onGpu=false);
171  complexScalarFieldTildeData(const GridInfo& gInfo, bool onGpu, PrivateTag);
172 };
173 
174 
175 
178 { const GridInfo& gInfo;
179  int nElem;
180  double* data;
181  #ifdef GPU_ENABLED
182  double* dataGpu;
183  #endif
184  double* dataPref;
185 
186  RealKernel(const GridInfo&);
187  ~RealKernel();
188  void set();
189 };
190 
191 #undef DECLARE_DATA_PREF_ACCESS
192 #undef DECLARE_DATA_ACCESS
193 
195 #endif //JDFTX_CORE_SCALARFIELD_H
196 
Reciprocal space real scalar field data Do not use this data structure directly or from a simple poin...
Definition: ScalarField.h:132
Reciprocal space complex scalar field data Do not use this data structure directly or from a simple p...
Definition: ScalarField.h:165
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:67
std::shared_ptr< ScalarFieldTildeData > ScalarFieldTilde
A smart reference-counting pointer to ScalarFieldTildeData.
Definition: ScalarField.h:44
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:92
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:151
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:46
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:117
void allReduce(MPIUtil::ReduceOp op, bool safeMode=false)
apply all-to-all reduction
Definition: ScalarField.h:89
complex DataType
Type of data in container (useful for templating)
Definition: ScalarField.h:133
void copyData(const FieldData &other)
copy data and scale (used by clone())
int nElem
number of elements = gInfo.nG
Definition: ScalarField.h:179
void send(int dest, int tag=0) const
send to another process
Definition: ScalarField.h:86
DECLARE_DATA_PREF_ACCESS bool isOnGpu() const
Check where the data is (for #ifdef simplicity exposed even when no GPU_ENABLED)
Definition: ScalarField.h:80
double * data
cpu data pointer
Definition: ScalarField.h:180
double scale
overall scale factor of the data array
Definition: ScalarField.h:63
std::shared_ptr< complexScalarFieldData > complexScalarField
A smart reference-counting pointer to complexScalarFieldData.
Definition: ScalarField.h:45
Miscellaneous utilities.
void zero()
set all elements to zero
Base class for ScalarFieldData and ScalarFieldTildeData.
Definition: ScalarField.h:60
void recv(int src, int tag=0)
receive from another process
Definition: ScalarField.h:87
void DataType
this base class has no specific data type
Definition: ScalarField.h:69
Real space complex scalar field data Do not use this data structure directly or from a simple pointer...
Definition: ScalarField.h:150
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:177
int nElem
number of elements = #gInfo.nr
Definition: ScalarField.h:62
std::shared_ptr< ScalarFieldData > ScalarField
A smart reference-counting pointer to ScalarFieldData.
Definition: ScalarField.h:40
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:166
void bcast(int root=0)
synchronize across processes (using value on specified root process)
Definition: ScalarField.h:88
double * dataGpu
gpu data pointer (unlike above classes, both cpu and gpu stored simultaneously)
Definition: ScalarField.h:182
Real space real scalar field data Do not use this data structure directly or from a simple pointer Sc...
Definition: ScalarField.h:116
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:184
const GridInfo & gInfo
simulation grid info
Definition: ScalarField.h:64