forked from QuEST-Kit/QuEST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_config.cpp
More file actions
291 lines (197 loc) · 7.22 KB
/
Copy pathcpu_config.cpp
File metadata and controls
291 lines (197 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/** @file
* Utility functions for querying the CPU multithreadng
* configuration, and allocating and copying RAM data.
*
* @author Tyson Jones
*/
#include "quest/include/modes.h"
#include "quest/include/types.h"
#include "quest/include/paulis.h"
#include "quest/src/core/errors.hpp"
#include <vector>
#include <cstring>
#include <cstdlib>
using std::vector;
// when COMPILE_OPENMP=1, the compiler expects arguments like -fopenmp
// which cause _OPENMP to be defined, which we check to ensure that
// COMPILE_OPENMP has been set correctly. Note that HIP compilers do
// not define _OPENMP even when parsing OpenMP, and it's possible that
// the user is compiling all the source code (including this file) with
// HIP; we tolerate _OPENMP being undefined in that instance
#if COMPILE_OPENMP && !defined(_OPENMP) && !defined(__HIP__)
#error "Attempted to compile in multithreaded mode without enabling OpenMP in the compiler flags."
#endif
#if COMPILE_OPENMP
#include <omp.h>
#endif
/*
* OPENMP CONFIG
*/
bool cpu_isOpenmpCompiled() {
return (bool) COMPILE_OPENMP;
}
int cpu_getCurrentNumThreads() {
#if COMPILE_OPENMP
int n = -1;
#pragma omp parallel shared(n)
n = omp_get_num_threads();
return n;
#else
error_cpuThreadsQueriedButEnvNotMultithreaded();
return -1;
#endif
}
int cpu_getNumOpenmpProcessors() {
#if COMPILE_OPENMP
return omp_get_num_procs();
#else
error_cpuThreadsQueriedButEnvNotMultithreaded();
return -1;
#endif
}
/*
* OPENMP SUBROUTINES
*
* which must be queried within OpenMP parallel
* regions to get reliable results, but which are
* safely invoked when OpenMP is not compiled
*/
int cpu_getOpenmpThreadInd() {
#if COMPILE_OPENMP
return omp_get_thread_num();
#else
return 0;
#endif
}
/*
* MEMORY ALLOCATION
*/
qcomp* cpu_allocArray(qindex length) {
/// @todo
/// here, we calloc the entire array in a serial setting, rather than one malloc
/// followed by threads subsequently memset'ing their own partitions. The latter
/// approach would distribute the array pages across NUMA nodes, accelerating
/// their subsequent access by the same threads (via NUMA's first-touch policy).
/// We have so far foregone this optimisation since a thread's memory-access pattern
/// in many of the QuEST functions is non-trivial, and likely to be inconsistent
/// with the memset pattern. As such, I expect the benefit is totally occluded
/// and only introduces potential new bugs - but this should be tested and confirmed!
// we call calloc over malloc in order to fail immediately if mem isn't available;
// caller must handle nullptr result
return (qcomp*) calloc(length, sizeof(qcomp));
}
void cpu_deallocArray(qcomp* arr) {
// arr can safely be nullptr
free(arr);
}
qcomp** cpu_allocAndInitMatrixWrapper(qcomp* arr, qindex dim) {
// do not allocate if arr alloc failed (caller will handle)
if (arr == nullptr)
return nullptr;
// allocate only the outer memory (i.e. one row's worth)
qcomp** out = (qcomp**) malloc(dim * sizeof *out);
// caller will handle malloc failure
if (out == nullptr)
return out;
// populate out with offsets of arr
for (qindex i=0; i<dim; i++)
out[i] = &arr[i*dim];
return out; // may be nullptr
}
void cpu_deallocMatrixWrapper(qcomp** wrapper) {
// only the outer pointer is freed; the
// inner pointers are offsets to another
// malloc which is separately freed.
// Safe to call even when nullptr
free(wrapper);
}
qcomp** cpu_allocMatrix(qindex dim) {
// NOTE:
// this function creates a matrix where rows are not necessarily
// contiguous in memory, which can incur gratuitous caching penalties
// when accessed in hot loops. As such, we do not use this function
// to allocate memory for CompMatr (instead, cpu_allocAndInitMatrixWrapper()),
// but instead use it for the individual Kraus matrices of a KrausMap,
// which are each quadratically smaller than the important superoperator.
// allocate outer array
qcomp** rows = (qcomp**) malloc(dim * sizeof *rows); // nullptr if failed
// if that did not fail, allocate each inner array
if (rows != nullptr)
for (qindex r=0; r<dim; r++)
rows[r] = cpu_allocArray(dim); // nullptr if failed
// caller will validate whether mallocs were successful
return rows;
}
void cpu_deallocMatrix(qcomp** matrix, qindex dim) {
// we attempt to deallocate every row (assuming the outer array was
// successfully allocated), regardless of whether they are actually
// allocated; it is legal to call free() on nullptr
if (matrix != nullptr)
for (qindex r=0; r<dim; r++)
cpu_deallocArray(matrix[r]);
free(matrix);
}
qcomp*** cpu_allocMatrixList(qindex numRows, int numMatrices) {
// attempt to allocate the outer list
qcomp*** matrices = (qcomp***) malloc(numMatrices * sizeof *matrices); // nullptr if failed
// attempt to allocate each matrix
if (matrices != nullptr)
for (int n=0; n<numMatrices; n++)
matrices[n] = cpu_allocMatrix(numRows); // nullptr if failed
return matrices; // may be or contain nullptrs, user will handle
}
void cpu_deallocMatrixList(qcomp*** matrices, qindex numRows, int numMatrices) {
// free everything that allocated (but permit anything to have failed)
if (matrices != nullptr)
for (int n=0; n<numMatrices; n++)
cpu_deallocMatrix(matrices[n], numRows);
// legal to free nullptr
free(matrices);
}
int* cpu_allocHeapFlag() {
// we use int over bool for the flag, because often we use
// value -1 as a third value
return (int*) malloc(sizeof(int)); // may be nullptr, caller will handle
}
void cpu_deallocHeapFlag(int* ptr) {
// safe to free if nullptr
free(ptr);
}
PauliStr* cpu_allocPauliStrings(qindex numStrings) {
return (PauliStr*) malloc(numStrings * sizeof(PauliStr)); // may be nullptr, caller will handle
}
void cpu_deallocPauliStrings(PauliStr* strings) {
// safe to free if nullptr
free(strings);
}
/*
* MEMORY COPYING
*/
void cpu_copyArray(qcomp* dest, qcomp* src, qindex dim) {
memcpy(dest, src, dim * sizeof(qcomp));
}
void cpu_copyMatrix(qcomp** dest, qcomp** src, qindex dim) {
/// @todo
/// there may be a faster, asynchronous way to perform
/// these memcpys then do a final wait
// note that we cannot call a single memcpy to copy all rows at once,
// because dest/src may not be contiguous stack arrays; instead, each
// row is likely a unique, discontiguous span of heap memory. So we
// memcpy each row in-turn
for (qindex r=0; r<dim; r++)
cpu_copyArray(dest[r], src[r], dim);
}
void cpu_copyMatrix(qcomp** dest, vector<vector<qcomp>> src, qindex dim) {
/// @todo
/// there may be a faster, asynchronous way to perform
/// these memcpys then do a final wait
for (qindex r=0; r<dim; r++)
cpu_copyArray(dest[r], src[r].data(), dim);
}
void cpu_copyPauliStrSum(PauliStrSum out, PauliStr* strings, qcomp* coeffs) {
// serially copy data over to new heap memory
for (int i=0; i<out.numTerms; i++) {
out.strings[i] = strings[i];
out.coeffs[i] = coeffs[i];
}
}