JDFTx  1.2.0
ManagedMemory.h
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_ELECTRONIC_MANAGEDMEMORY_H
21 #define JDFTX_ELECTRONIC_MANAGEDMEMORY_H
22 
23 #include <electronic/common.h>
24 
27 {
28 public:
29 
35  complex* data();
36 
42  const complex* data() const;
43 
44  size_t nData() const { return nElements; }
45 
46  bool isOnGpu() const { return onGpu; }
47 
48  #ifdef GPU_ENABLED
49  complex* dataGpu();
50  const complex* dataGpu() const;
51  #endif
52 
53  //Utilities to automatically select "preferred" data i.e. GPU when it is enabled, CPU otherwise
54  //This eases the overload of CPU/GPU functions based on complex vs complex
55  #ifdef GPU_ENABLED
56  inline complex* dataPref() { return dataGpu(); }
57  inline const complex* dataPref() const { return dataGpu(); }
58  #else
59  inline complex* dataPref() { return data(); }
60  inline const complex* dataPref() const { return data(); }
61  #endif
62 
63  //Inter-process communication:
64  void send(int dest, int tag=0) const;
65  void recv(int src, int tag=0);
66  void bcast(int root=0);
67  void allReduce(MPIUtil::ReduceOp op, bool safeMode=false, bool ignoreComplexCheck=false);
68 
69  void write(const char *fname) const;
70  void writea(const char *fname) const;
71  void write(FILE *filep) const;
72  void read(const char *fname);
73  void read(FILE *filep);
74  void read_real(const char *fname);
75  void read_real(FILE *filep);
76  void write_real(const char *fname) const;
77  void write_real(FILE *filep) const;
78  void dump(const char* fname, bool realPartOnly) const;
79  void zero();
80 
81  static void reportUsage();
82 protected:
83  void memFree();
84  void memInit(string category, size_t nElem, bool onGpu=false);
85 
86  void memMove(ManagedMemory&&);
87  ManagedMemory();
88  ~ManagedMemory();
89 
90 private:
91  string category;
92  size_t nElements;
93  complex* c;
94  bool onGpu;
95  #ifdef GPU_ENABLED
96  void toCpu();
97  void toGpu();
98  #endif
99 };
100 
101 //Some common elementwise / vector-like operations
102 void memcpy(ManagedMemory&, const ManagedMemory&);
103 void scale(double alpha, ManagedMemory& y);
104 void scale(complex alpha, ManagedMemory& y);
105 void axpy(complex alpha, const ManagedMemory& x, ManagedMemory& y);
106 double nrm2(const ManagedMemory&);
107 complex dotc(const ManagedMemory& a, const ManagedMemory& b);
108 
109 #endif // JDFTX_ELECTRONIC_MANAGEDMEMORY_H
void memFree()
Free memory.
size_t nData() const
number of data points
Definition: ManagedMemory.h:44
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...
void memInit(string category, size_t nElem, bool onGpu=false)
Allocate memory.
void write_real(const char *fname) const
binary write real-parts to file
void send(int dest, int tag=0) const
send to another process
void read(const char *fname)
binary read from a file
Base class for managed-memory objects (that could potentially live on GPUs as well) ...
Definition: ManagedMemory.h:26
void dump(const char *fname, bool realPartOnly) const
write as complex or real-part alone and report discarded imaginary part, if any
complex * dataGpu()
Get a gpu data pointer (must be called from GPU owner thread)
bool isOnGpu() const
Check where the data is (for #ifdef simplicity exposed even when no GPU_ENABLED)
Definition: ManagedMemory.h:46
static void reportUsage()
print memory usage report
ManagedMemory()
Initialize a valid state, but don&#39;t allocate anything.
void memMove(ManagedMemory &&)
Steal the other object&#39;s data (used for move constructors/assignment)
void zero()
set all elements to zero
void read_real(const char *fname)
binary read real-part from file, setting imaginary parts to 0
void writea(const char *fname) const
binary-append to a file
complex * data()
Return a pointer to the actual data Return a CPU pointer to the actual data, will move data from GPU ...
void write(const char *fname) const
binary-write to a file
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
void axpy(double alpha, const Tptr &X, Tptr &Y)
Generic axpy for complex data types (Note: null pointers are treated as zero)
Definition: Operators.h:158
void recv(int src, int tag=0)
receive from another process
double nrm2(const Tptr &X)
Definition: Operators.h:199