ergo
Memory_buffer_thread.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 MEMORY_BUFFER_THREAD_HEADER
28 #define MEMORY_BUFFER_THREAD_HEADER
29 
30 /* We need to include config.h to get the USE_SSE_INTRINSICS flag. */
31 #include "config.h"
32 
33 /* This file is only used if USE_SSE_INTRINSICS is defined. */
34 #ifdef USE_SSE_INTRINSICS
35 
36 #include <iostream> // for bad_alloc
37 #include <stdexcept>
38 #include <vector>
39 #include <emmintrin.h>
40 #ifdef __SSE3__
41 #include <pmmintrin.h>
42 #endif
43 #ifdef _OPENMP
44 #include <omp.h>
45 #endif
46 
47 namespace mat{
48 
49  class Memory_aligned {
50  protected:
51  inline void * operator new (size_t s) {
52  void * p = _mm_malloc (s, 16);
53  if (p == NULL)
54  throw std::bad_alloc();
55  else
56  return p;
57  }
58 
59  inline void * operator new[] (size_t s) {
60  void * p = _mm_malloc (s, 16);
61  if (p == NULL)
62  throw std::bad_alloc();
63  else
64  return p;
65  }
66 
67  inline void operator delete (void * p) {
68  _mm_free(p);
69  }
70 
71  inline void operator delete[] (void * p) {
72  _mm_free(p);
73  }
74  };
75 
76  class Memory_buffer_thread : public Memory_aligned {
77  // Hidden stuff
78  private:
79  static void create();
80  static Memory_buffer_thread* ptr_to_instance;
81  // All values allowed for char -
82  // may be important for double checked locking pattern in instance()
83  static volatile char ptr_to_instance_is_valid;
84  static unsigned int bufSize;
85  Memory_buffer_thread(Memory_buffer_thread const &);
86  protected:
87  Memory_buffer_thread() {} // No instances of Memory_buffer_thread ever!
88 
89  public:
90  static Memory_buffer_thread& instance();
91  template<typename T>
92  void get_buffer(size_t size, T* & buffer) const {
93  if (sizeof(T) * size > bufSize)
94  throw std::runtime_error("In Memory_buffer_thread::get_buffer : "
95  "Allocated buffer smaller than requested "
96  "buffer size");
97  int threadID = 0;
98 #ifdef _OPENMP
99  for (int ind = 0; ind <= omp_get_level(); ind++) {
100  int tmp = omp_get_ancestor_thread_num(ind);
101  threadID = threadID > tmp ? threadID : tmp;
102  }
103 #endif
104  if (buffers.empty())
105  throw std::runtime_error("In Memory_buffer_thread::get_buffer : "
106  "buffers empty!");
107  if ((unsigned)threadID >= buffers.size())
108  throw std::runtime_error("In Memory_buffer_thread::get_buffer : "
109  "thread id larger than number of buffers");
110  buffer = (T*)buffers[threadID];
111  }
112  void init_buffers(unsigned int const nThreads);
113  ~Memory_buffer_thread();
114  private:
115  std::vector<char*> buffers;
116 
117  };
118 
119 
120 } // end namespace mat
121 
122 #endif
123 
124 #endif
Definition: allocate.cc:30