Psi4
memory_manager.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 file is part of Psi4.
12  *
13  * Psi4 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License as published by
15  * the Free Software Foundation, version 3.
16  *
17  * Psi4 is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License along
23  * with Psi4; if not, write to the Free Software Foundation, Inc.,
24  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25  *
26  * @END LICENSE
27  */
28 
29 #ifndef _psi_src_bin_psimrccmemory_managerh_
30 #define _psi_src_bin_psimrccmemory_managerh_
31 
32 #include <map>
33 #include <vector>
34 #include <string>
35 
36 
37 namespace psi{
38 
39 /*
40  * Computes the size in mebibytes (MiB) of a given amount of type T
41  */
42 template <typename T>
43 double type_to_MiB(size_t n)
44 {
45  // 1 MiB = 1048576 bytes
46  size_t bites = n * static_cast<size_t>(sizeof(T));
47  return(static_cast<double>(bites)/1048576.0);
48 }
49 
50 /*
51  * Convert bytes to mebibytes (MiB)
52  */
53 double bytes_to_MiB(size_t n);
54 
55 typedef struct {
56  void* variable;
57  std::string type;
58  std::string variableName;
59  std::string fileName;
60  size_t lineNumber;
61  std::vector<size_t> argumentList;
63 
65 {
66 public:
67  MemoryManager(size_t maxcor = 256000000);
69 
70  void MemCheck(std::string output);
71 
72  size_t get_FreeMemory() const {return(MaximumAllowed - CurrentAllocated);}
73  size_t get_CurrentAllocated() const {return(CurrentAllocated);}
74  size_t get_MaximumAllowedMemory() const {return(MaximumAllowed);}
75 
76  template <typename T>
77  void allocate(const char *type, T*& matrix, size_t size, const char *variableName, const char *fileName, size_t lineNumber);
78  template <typename T>
79  void release_one(T*& matrix, const char *fileName, size_t lineNumber);
80 
81  template <typename T>
82  void allocate(const char *type, T**& matrix, size_t size1, size_t size2, const char *variableName, const char *fileName, size_t lineNumber);
83  template <typename T>
84  void release_two(T**& matrix, const char *fileName, size_t lineNumber);
85 
86  template <typename T>
87  void allocate(const char *type, T***& matrix,size_t size1,size_t size2,size_t size3, const char *variableName, const char *fileName, size_t lineNumber);
88  template <typename T>
89  void release_three(T***& matrix, const char *fileName, size_t lineNumber);
90 private:
91  void RegisterMemory(void *mem, AllocationEntry& entry, size_t size);
92  void UnregisterMemory(void *mem, size_t size, const char *fileName, size_t lineNumber);
93 
97  std::map<void *, AllocationEntry> AllocationTable;
98 };
99 
100 template <typename T>
101 void MemoryManager::allocate(const char *type, T*& matrix, size_t size, const char *variableName, const char *fileName, size_t lineNumber)
102 {
103  AllocationEntry newEntry;
104 
105  if(size<=0){
106  matrix = NULL;
107  }else{
108  matrix = new T[size];
109  for(size_t i=0;i<size;i++)
110  matrix[i]=static_cast<T>(0); // Zero all the elements
111 
112  newEntry.variable = matrix;
113  newEntry.type = type;
114  newEntry.variableName = variableName;
115  newEntry.fileName = fileName;
116  newEntry.lineNumber = lineNumber;
117  newEntry.argumentList.push_back(size);
118  RegisterMemory(static_cast<void*>(matrix), newEntry, size*sizeof(T));
119  }
120 }
121 
122 template <typename T>
123 void MemoryManager::release_one(T*& matrix, const char *fileName, size_t lineNumber)
124 {
125  if(matrix == NULL)
126  return;
127 
128  size_t size = AllocationTable[static_cast<void*>(matrix)].argumentList[0];
129 
130  UnregisterMemory(static_cast<void*>(matrix), size*sizeof(T),fileName,lineNumber);
131 
132  delete[] matrix;
133  matrix = NULL;
134 }
135 
136 template <typename T>
137 void MemoryManager::allocate(const char *type, T**& matrix, size_t size1, size_t size2, const char *variableName, const char *fileName, size_t lineNumber)
138 {
139  AllocationEntry newEntry;
140  size_t size = size1*size2;
141 
142  if(size<=0){
143  matrix = NULL;
144  return;
145  }else{
146  matrix = new T*[size1];
147  T* vector = new T[size];
148  for(size_t i=0;i<size;i++)
149  vector[i]=static_cast<T>(0); // Zero all the elements
150  for(size_t i=0;i<size1;i++)
151  matrix[i]=&(vector[i*size2]); // Assign the rows pointers
152 
153  newEntry.variable = matrix;
154  newEntry.type = type;
155  newEntry.variableName = variableName;
156  newEntry.fileName = fileName;
157  newEntry.lineNumber = lineNumber;
158  newEntry.argumentList.push_back(size1);
159  newEntry.argumentList.push_back(size2);
160  RegisterMemory(static_cast<void*>(matrix), newEntry, size*sizeof(T));
161  }
162 }
163 
164 template <typename T>
165 void MemoryManager::release_two(T**& matrix, const char *fileName, size_t lineNumber)
166 {
167  if(matrix == NULL)
168  return;
169 
170  size_t size = AllocationTable[static_cast<void*>(matrix)].argumentList[0] * AllocationTable[static_cast<void*>(matrix)].argumentList[1];
171 
172  UnregisterMemory(static_cast<void*>(matrix), size*sizeof(T),fileName,lineNumber);
173 
174  delete[] matrix[0];
175  delete[] matrix;
176  matrix = NULL;
177 }
178 
179 template <typename T>
180 void MemoryManager::allocate(const char *type, T***& matrix,size_t size1,size_t size2,size_t size3, const char *variableName, const char *fileName, size_t lineNumber)
181 {
182  AllocationEntry newEntry;
183  size_t size = size1*size2*size3;
184  if(size<=0){
185  matrix = NULL;
186  return;
187  }else{
188  matrix = new T**[size1];
189  for(size_t i=0;i<size1;i++)
190  matrix[i]= new T*[size2];
191  T* vector = new T[size];
192  for(size_t i=0;i<size;i++)
193  vector[i]=static_cast<T>(0); // Zero all the elements
194  for(size_t i=0;i<size1;i++)
195  for(size_t j=0;j<size2;j++)
196  matrix[i][j]=&(vector[i*size2*size3+j*size3]); // Assign the rows pointers
197  newEntry.variable = matrix;
198  newEntry.type = type;
199  newEntry.variableName = variableName;
200  newEntry.fileName = fileName;
201  newEntry.lineNumber = lineNumber;
202  newEntry.argumentList.push_back(size1);
203  newEntry.argumentList.push_back(size2);
204  newEntry.argumentList.push_back(size3);
205  RegisterMemory(static_cast<void*>(matrix), newEntry, size*sizeof(T));
206  }
207 }
208 
209 template <typename T>
210 void MemoryManager::release_three(T***& matrix, const char *fileName, size_t lineNumber)
211 {
212  if(matrix == NULL)
213  return;
214 
215  size_t size1 = AllocationTable[static_cast<void*>(matrix)].argumentList[0];
216  size_t size = AllocationTable[static_cast<void*>(matrix)].argumentList[0] * AllocationTable[static_cast<void*>(matrix)].argumentList[1]
217  * AllocationTable[static_cast<void*>(matrix)].argumentList[2];
218 
219  UnregisterMemory(static_cast<void*>(matrix), size*sizeof(T),fileName,lineNumber);
220 
221  delete[] matrix[0][0];
222  for(size_t i=0;i<size1;i++)
223  delete[] matrix[i];
224  delete[] matrix;
225  matrix = NULL;
226 }
227 
228 #define allocate1(type, variable, size) \
229  memory_manager->allocate(#type, variable, size, #variable, __FILE__, __LINE__);
230 #define release1(variable) \
231  memory_manager->release_one(variable, __FILE__, __LINE__);
232 
233 #define allocate2(type, variable, size1, size2) \
234  memory_manager->allocate(#type, variable, size1, size2, #variable, __FILE__, __LINE__);
235 #define release2(variable) \
236  memory_manager->release_two(variable, __FILE__, __LINE__);
237 
238 #define allocate3(type, variable, size1, size2, size3) \
239  memory_manager->allocate(#type, variable, size1, size2, size3, #variable, __FILE__, __LINE__);
240 #define release3(variable) \
241  memory_manager->release_three(variable, __FILE__, __LINE__);
242 
243 } /* End Namespaces */
244 
245 #endif // _psi_src_bin_psimrccmemory_managerh_
void RegisterMemory(void *mem, AllocationEntry &entry, size_t size)
Definition: memory_manager.cc:60
size_t get_FreeMemory() const
Definition: memory_manager.h:72
size_t CurrentAllocated
Definition: memory_manager.h:94
void release_three(T ***&matrix, const char *fileName, size_t lineNumber)
Definition: memory_manager.h:210
std::map< void *, AllocationEntry > AllocationTable
Definition: memory_manager.h:97
size_t MaximumAllocated
Definition: memory_manager.h:95
Definition: pointgrp.h:106
std::string variableName
Definition: memory_manager.h:58
size_t lineNumber
Definition: memory_manager.h:60
MemoryManager(size_t maxcor=256000000)
Definition: memory_manager.cc:50
size_t get_CurrentAllocated() const
Definition: memory_manager.h:73
std::string type
Definition: memory_manager.h:57
void release_two(T **&matrix, const char *fileName, size_t lineNumber)
Definition: memory_manager.h:165
void * variable
Definition: memory_manager.h:56
Definition: memory_manager.h:64
size_t get_MaximumAllowedMemory() const
Definition: memory_manager.h:74
size_t MaximumAllowed
Definition: memory_manager.h:96
std::string fileName
Definition: memory_manager.h:59
double bytes_to_MiB(size_t n)
Definition: memory_manager.cc:44
double type_to_MiB(size_t n)
Definition: memory_manager.h:43
std::vector< size_t > argumentList
Definition: memory_manager.h:61
void allocate(const char *type, T *&matrix, size_t size, const char *variableName, const char *fileName, size_t lineNumber)
Definition: memory_manager.h:101
void release_one(T *&matrix, const char *fileName, size_t lineNumber)
Definition: memory_manager.h:123
void MemCheck(std::string output)
Definition: memory_manager.cc:94
Definition: PsiFileImpl.h:40
void UnregisterMemory(void *mem, size_t size, const char *fileName, size_t lineNumber)
Definition: memory_manager.cc:77
Definition: memory_manager.h:55
~MemoryManager()
Definition: memory_manager.cc:56