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-2018 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 namespace psi {
37 
38 /*
39  * Computes the size in mebibytes (MiB) of a given amount of type T
40  */
41 template <typename T>
42 double type_to_MiB(size_t n) {
43  // 1 MiB = 1048576 bytes
44  size_t bites = n * static_cast<size_t>(sizeof(T));
45  return (static_cast<double>(bites) / 1048576.0);
46 }
47 
48 /*
49  * Convert bytes to mebibytes (MiB)
50  */
51 double bytes_to_MiB(size_t n);
52 
53 typedef struct {
54  void *variable;
55  std::string type;
56  std::string variableName;
57  std::string fileName;
58  size_t lineNumber;
59  std::vector<size_t> argumentList;
61 
63  public:
64  MemoryManager(size_t maxcor = 256000000);
66 
67  void MemCheck(std::string output);
68 
69  size_t get_FreeMemory() const { return (MaximumAllowed - CurrentAllocated); }
70  size_t get_CurrentAllocated() const { return (CurrentAllocated); }
71  size_t get_MaximumAllowedMemory() const { return (MaximumAllowed); }
72 
73  template <typename T>
74  void allocate(const char *type, T *&matrix, size_t size, const char *variableName, const char *fileName,
75  size_t lineNumber);
76  template <typename T>
77  void release_one(T *&matrix, const char *fileName, size_t lineNumber);
78 
79  template <typename T>
80  void allocate(const char *type, T **&matrix, size_t size1, size_t size2, const char *variableName,
81  const char *fileName, size_t lineNumber);
82  template <typename T>
83  void release_two(T **&matrix, const char *fileName, size_t lineNumber);
84 
85  template <typename T>
86  void allocate(const char *type, T ***&matrix, size_t size1, size_t size2, size_t size3, const char *variableName,
87  const char *fileName, size_t lineNumber);
88  template <typename T>
89  void release_three(T ***&matrix, const char *fileName, size_t lineNumber);
90 
91  private:
92  void RegisterMemory(void *mem, AllocationEntry &entry, size_t size);
93  void UnregisterMemory(void *mem, size_t size, const char *fileName, size_t lineNumber);
94 
98  std::map<void *, AllocationEntry> AllocationTable;
99 };
100 
101 template <typename T>
102 void MemoryManager::allocate(const char *type, T *&matrix, size_t size, const char *variableName, const char *fileName,
103  size_t lineNumber) {
104  AllocationEntry newEntry;
105 
106  if (size <= 0) {
107  matrix = nullptr;
108  } else {
109  matrix = new T[size];
110  for (size_t i = 0; i < size; i++) 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  if (matrix == nullptr) return;
125 
126  size_t size = AllocationTable[static_cast<void *>(matrix)].argumentList[0];
127 
128  UnregisterMemory(static_cast<void *>(matrix), size * sizeof(T), fileName, lineNumber);
129 
130  delete[] matrix;
131  matrix = nullptr;
132 }
133 
134 template <typename T>
135 void MemoryManager::allocate(const char *type, T **&matrix, size_t size1, size_t size2, const char *variableName,
136  const char *fileName, size_t lineNumber) {
137  AllocationEntry newEntry;
138  size_t size = size1 * size2;
139 
140  if (size <= 0) {
141  matrix = nullptr;
142  return;
143  } else {
144  matrix = new T *[size1];
145  auto *vector = new T[size];
146  for (size_t i = 0; i < size; i++) vector[i] = static_cast<T>(0); // Zero all the elements
147  for (size_t i = 0; i < size1; i++) matrix[i] = &(vector[i * size2]); // Assign the rows pointers
148 
149  newEntry.variable = matrix;
150  newEntry.type = type;
151  newEntry.variableName = variableName;
152  newEntry.fileName = fileName;
153  newEntry.lineNumber = lineNumber;
154  newEntry.argumentList.push_back(size1);
155  newEntry.argumentList.push_back(size2);
156  RegisterMemory(static_cast<void *>(matrix), newEntry, size * sizeof(T));
157  }
158 }
159 
160 template <typename T>
161 void MemoryManager::release_two(T **&matrix, const char *fileName, size_t lineNumber) {
162  if (matrix == nullptr) return;
163 
164  size_t size = AllocationTable[static_cast<void *>(matrix)].argumentList[0] *
165  AllocationTable[static_cast<void *>(matrix)].argumentList[1];
166 
167  UnregisterMemory(static_cast<void *>(matrix), size * sizeof(T), fileName, lineNumber);
168 
169  delete[] matrix[0];
170  delete[] matrix;
171  matrix = nullptr;
172 }
173 
174 template <typename T>
175 void MemoryManager::allocate(const char *type, T ***&matrix, size_t size1, size_t size2, size_t size3,
176  const char *variableName, const char *fileName, size_t lineNumber) {
177  AllocationEntry newEntry;
178  size_t size = size1 * size2 * size3;
179  if (size <= 0) {
180  matrix = nullptr;
181  return;
182  } else {
183  matrix = new T **[size1];
184  for (size_t i = 0; i < size1; i++) matrix[i] = new T *[size2];
185  auto *vector = new T[size];
186  for (size_t i = 0; i < size; i++) vector[i] = static_cast<T>(0); // Zero all the elements
187  for (size_t i = 0; i < size1; i++)
188  for (size_t j = 0; j < size2; j++)
189  matrix[i][j] = &(vector[i * size2 * size3 + j * size3]); // Assign the rows pointers
190  newEntry.variable = matrix;
191  newEntry.type = type;
192  newEntry.variableName = variableName;
193  newEntry.fileName = fileName;
194  newEntry.lineNumber = lineNumber;
195  newEntry.argumentList.push_back(size1);
196  newEntry.argumentList.push_back(size2);
197  newEntry.argumentList.push_back(size3);
198  RegisterMemory(static_cast<void *>(matrix), newEntry, size * sizeof(T));
199  }
200 }
201 
202 template <typename T>
203 void MemoryManager::release_three(T ***&matrix, const char *fileName, size_t lineNumber) {
204  if (matrix == nullptr) return;
205 
206  size_t size1 = AllocationTable[static_cast<void *>(matrix)].argumentList[0];
207  size_t size = AllocationTable[static_cast<void *>(matrix)].argumentList[0] *
208  AllocationTable[static_cast<void *>(matrix)].argumentList[1] *
209  AllocationTable[static_cast<void *>(matrix)].argumentList[2];
210 
211  UnregisterMemory(static_cast<void *>(matrix), size * sizeof(T), fileName, lineNumber);
212 
213  delete[] matrix[0][0];
214  for (size_t i = 0; i < size1; i++) delete[] matrix[i];
215  delete[] matrix;
216  matrix = nullptr;
217 }
218 
219 #define allocate1(type, variable, size) memory_manager->allocate(#type, variable, size, #variable, __FILE__, __LINE__);
220 #define release1(variable) memory_manager->release_one(variable, __FILE__, __LINE__);
221 
222 #define allocate2(type, variable, size1, size2) \
223  memory_manager->allocate(#type, variable, size1, size2, #variable, __FILE__, __LINE__);
224 #define release2(variable) memory_manager->release_two(variable, __FILE__, __LINE__);
225 
226 #define allocate3(type, variable, size1, size2, size3) \
227  memory_manager->allocate(#type, variable, size1, size2, size3, #variable, __FILE__, __LINE__);
228 #define release3(variable) memory_manager->release_three(variable, __FILE__, __LINE__);
229 
230 } /* End Namespaces */
231 
232 #endif // _psi_src_bin_psimrccmemory_managerh_
void RegisterMemory(void *mem, AllocationEntry &entry, size_t size)
Definition: memory_manager.cc:55
size_t get_FreeMemory() const
Definition: memory_manager.h:69
size_t CurrentAllocated
Definition: memory_manager.h:95
void release_three(T ***&matrix, const char *fileName, size_t lineNumber)
Definition: memory_manager.h:203
std::map< void *, AllocationEntry > AllocationTable
Definition: memory_manager.h:98
size_t MaximumAllocated
Definition: memory_manager.h:96
Definition: pointgrp.h:104
std::string variableName
Definition: memory_manager.h:56
size_t lineNumber
Definition: memory_manager.h:58
MemoryManager(size_t maxcor=256000000)
Definition: memory_manager.cc:47
size_t get_CurrentAllocated() const
Definition: memory_manager.h:70
int * T
Definition: stringlist.cc:66
std::string type
Definition: memory_manager.h:55
void release_two(T **&matrix, const char *fileName, size_t lineNumber)
Definition: memory_manager.h:161
void * variable
Definition: memory_manager.h:54
Definition: memory_manager.h:62
size_t get_MaximumAllowedMemory() const
Definition: memory_manager.h:71
size_t MaximumAllowed
Definition: memory_manager.h:97
std::string fileName
Definition: memory_manager.h:57
double bytes_to_MiB(size_t n)
Definition: memory_manager.cc:42
double type_to_MiB(size_t n)
Definition: memory_manager.h:42
std::vector< size_t > argumentList
Definition: memory_manager.h:59
void allocate(const char *type, T *&matrix, size_t size, const char *variableName, const char *fileName, size_t lineNumber)
Definition: memory_manager.h:102
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:88
void UnregisterMemory(void *mem, size_t size, const char *fileName, size_t lineNumber)
Definition: memory_manager.cc:71
Definition: memory_manager.h:53
~MemoryManager()
Definition: memory_manager.cc:53