ergo
Allocator.h
Go to the documentation of this file.
1 /* Ergo, version 3.3, a program for linear scaling electronic structure
2  * calculations.
3  * Copyright (C) 2013 Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Primary academic reference:
19  * Kohn−Sham Density Functional Theory Electronic Structure Calculations
20  * with Linearly Scaling Computational Time and Memory Usage,
21  * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
22  * J. Chem. Theory Comput. 7, 340 (2011),
23  * <http://dx.doi.org/10.1021/ct100611z>
24  *
25  * For further information about Ergo, see <http://www.ergoscf.org>.
26  */
27 #ifndef MAT_ALLOCATOR_HEADER
28 #define MAT_ALLOCATOR_HEADER
29 
30 #include <stdexcept>
31 
32 namespace mat {
33 
34 template<class Treal>
35 class Allocator
36 {
37  public:
38  Allocator(int noOfRealsPerBuffer_,
39  int noOfBuffers_) :
40  noOfRealsPerBuffer(noOfRealsPerBuffer_),
41  noOfBuffers(noOfBuffers_)
42  {
43  buffer = new Treal[noOfBuffers * noOfRealsPerBuffer];
44  nextFreeIndexList = new int[noOfBuffers];
45  // Initialize nextFreeIndexList to indicate that all slots are free.
46  for(int i = 0; i < noOfBuffers-1; i++)
47  nextFreeIndexList[i] = i + 1;
48  nextFreeIndexList[noOfBuffers-1] = -1; // last one points to -1
49  firstFreeIndex = 0;
51  }
53  {
54  delete [] buffer;
55  delete [] nextFreeIndexList;
56  }
57  Treal* alloc() {
58  if(firstFreeIndex < 0)
59  throw std::runtime_error("Error in Allocator::alloc(): no free slots.");
60  Treal* ptrToReturn = &buffer[firstFreeIndex*noOfRealsPerBuffer];
61  int firstFreeIndex_new = nextFreeIndexList[firstFreeIndex];
63  firstFreeIndex = firstFreeIndex_new;
65  return ptrToReturn;
66  }
67  void free(Treal* ptr) {
68  if(ptr < buffer || ptr >= &buffer[noOfBuffers * noOfRealsPerBuffer])
69  throw std::runtime_error("Error in Allocator::free(): unknown ptr.");
70  int count = ptr - buffer;
71  if((count % noOfRealsPerBuffer) != 0)
72  throw std::runtime_error("Error in Allocator::free(): bad ptr.");
73  int bufferIdx = count / noOfRealsPerBuffer;
74  if(nextFreeIndexList[bufferIdx] != -1)
75  throw std::runtime_error("Error in Allocator::free(): -1 not found.");
76  nextFreeIndexList[bufferIdx] = firstFreeIndex;
77  firstFreeIndex = bufferIdx;
79  }
80  bool isFull() {
82  return true;
83  return false;
84  }
85  bool isEmpty() {
86  if(noOfOccupiedSlots == 0)
87  return true;
88  return false;
89  }
90  bool ownsPtr(Treal* ptr) {
91  if(ptr < buffer || ptr >= &buffer[noOfBuffers * noOfRealsPerBuffer])
92  return false;
93  return true;
94  }
96  return noOfOccupiedSlots;
97  }
98  private:
101  Treal* buffer;
105 }; // end class Allocator
106 
107 } /* end namespace mat */
108 
109 #endif
Treal * alloc()
Definition: Allocator.h:57
int noOfOccupiedSlots
Definition: Allocator.h:104
int noOfRealsPerBuffer
Definition: Allocator.h:99
int * nextFreeIndexList
Definition: Allocator.h:102
Definition: allocate.cc:30
bool isFull()
Definition: Allocator.h:80
void free(Treal *ptr)
Definition: Allocator.h:67
int noOfBuffers
Definition: Allocator.h:100
int firstFreeIndex
Definition: Allocator.h:103
bool ownsPtr(Treal *ptr)
Definition: Allocator.h:90
Treal * buffer
Definition: Allocator.h:101
bool isEmpty()
Definition: Allocator.h:85
~Allocator()
Definition: Allocator.h:52
Definition: Allocator.h:35
Allocator(int noOfRealsPerBuffer_, int noOfBuffers_)
Definition: Allocator.h:38
int getNoOfOccupiedSlots()
Definition: Allocator.h:95