ergo
Memory_buffer_thread.h
Go to the documentation of this file.
1
/* Ergo, version 3.8.2, a program for linear scaling electronic structure
2
* calculations.
3
* Copyright (C) 2023 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
4
* and Anastasia Kruchinina.
5
*
6
* This program 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
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
18
*
19
* Primary academic reference:
20
* Ergo: An open-source program for linear-scaling electronic structure
21
* calculations,
22
* Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
23
* Kruchinina,
24
* SoftwareX 7, 107 (2018),
25
* <http://dx.doi.org/10.1016/j.softx.2018.03.005>
26
*
27
* For further information about Ergo, see <http://www.ergoscf.org>.
28
*/
29
36
#ifndef MEMORY_BUFFER_THREAD_HEADER
37
#define MEMORY_BUFFER_THREAD_HEADER
38
39
/* We need to include config.h to get the USE_SSE_INTRINSICS flag. */
40
#include "
config.h
"
41
42
/* This file is only used if USE_SSE_INTRINSICS is defined. */
43
#ifdef USE_SSE_INTRINSICS
44
45
#include <iostream>
// for bad_alloc
46
#include <stdexcept>
47
#include <vector>
48
#include <emmintrin.h>
49
#ifdef __SSE3__
50
#include <pmmintrin.h>
51
#endif
52
#ifdef _OPENMP
53
#include <omp.h>
54
#endif
55
56
namespace
mat
{
57
58
class
Memory_aligned
{
59
protected
:
60
inline
void
*
operator
new
(
size_t
s) {
61
void
* p =
_mm_malloc
(s, 16);
62
if
(p ==
NULL
)
63
throw
std::bad_alloc();
64
else
65
return
p;
66
}
67
68
inline
void
*
operator
new
[] (
size_t
s) {
69
void
* p =
_mm_malloc
(s, 16);
70
if
(p ==
NULL
)
71
throw
std::bad_alloc();
72
else
73
return
p;
74
}
75
76
inline
void
operator
delete
(
void
* p) {
77
_mm_free
(p);
78
}
79
80
inline
void
operator
delete
[] (
void
* p) {
81
_mm_free
(p);
82
}
83
};
84
85
class
Memory_buffer_thread
:
public
Memory_aligned
{
86
// Hidden stuff
87
private
:
88
static
void
create
();
89
static
Memory_buffer_thread
*
ptr_to_instance
;
90
// All values allowed for char -
91
// may be important for double checked locking pattern in instance()
92
static
volatile
char
ptr_to_instance_is_valid
;
93
static
unsigned
int
bufSize
;
94
Memory_buffer_thread
(
Memory_buffer_thread
const
&);
95
protected
:
96
Memory_buffer_thread
() {}
// No instances of Memory_buffer_thread ever!
97
98
public
:
99
static
Memory_buffer_thread
& instance();
100
template
<
typename
T>
101
void
get_buffer
(
size_t
size,
T
* & buffer)
const
{
102
if
(
sizeof
(
T
) * size >
bufSize
)
103
throw
std::runtime_error(
"In Memory_buffer_thread::get_buffer : "
104
"Allocated buffer smaller than requested "
105
"buffer size"
);
106
int
threadID = 0;
107
#ifdef _OPENMP
108
for
(
int
ind
= 0;
ind
<=
omp_get_level
();
ind
++) {
109
int
tmp
=
omp_get_ancestor_thread_num
(
ind
);
110
threadID = threadID >
tmp
? threadID :
tmp
;
111
}
112
#endif
113
if
(
buffers
.empty())
114
throw
std::runtime_error(
"In Memory_buffer_thread::get_buffer : "
115
"buffers empty!"
);
116
if
((
unsigned
)threadID >=
buffers
.size())
117
throw
std::runtime_error(
"In Memory_buffer_thread::get_buffer : "
118
"thread id larger than number of buffers"
);
119
buffer = (
T
*)
buffers
[threadID];
120
}
121
void
init_buffers
(
unsigned
int
const
nThreads);
122
~Memory_buffer_thread
();
123
private
:
124
std::vector<char*>
buffers
;
125
126
};
127
128
129
}
// end namespace mat
130
131
#endif
132
133
#endif
config.h
mat
Definition
allocate.cc:39
mat::getMachineEpsilon
static Treal getMachineEpsilon()
Definition
matInclude.h:147
source
matrix
Memory_buffer_thread.h
Generated on Fri Jan 19 2024 00:00:00 for ergo by
1.10.0