-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathcache.cpp
More file actions
217 lines (156 loc) · 5.43 KB
/
Copy pathcache.cpp
File metadata and controls
217 lines (156 loc) · 5.43 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
/** @file
* Testing utilities which create Quregs across all
* available hardware deployments
*
* @author Tyson Jones
*/
#include "quest.h"
#include "macros.hpp"
#include "qvector.hpp"
#include "qmatrix.hpp"
#include "macros.hpp"
#include "linalg.hpp"
#include "config.hpp"
#include "cache.hpp"
#include <unordered_map>
#include <tuple>
#include <vector>
#include <string>
using std::tuple;
using std::vector;
using std::string;
/*
* caches of Quregs and FullStateDiagMatr
* which persist between calls of get-cache()
*/
quregCache statevecs1;
quregCache statevecs2;
quregCache densmatrs1;
quregCache densmatrs2;
matrixCache matrices;
int getNumCachedQubits() {
// we are merely aliasing the below env-var fetching function
// to minimise a diff since pre-runtime controlling the tested
// Qureg sizes is experimental and not fully designed (we may
// eventually wish to specify different sizes for statevectors
// vs density matrices, or control integration test Qureg sizes
// also through environment variables, etc)
return getNumQubitsInUnitTestedQuregs();
}
/*
* deployments in cache
*/
deployInfo getSupportedDeployments() {
deployInfo out;
// determine which Qureg deployments are supported
QuESTEnv env = getQuESTEnv();
bool omp = env.isMultithreaded;
bool mpi = env.isDistributed;
bool gpu = env.isGpuAccelerated;
// return only the "most-accelerated" deployment, unless all are desired
bool one = ! getWhetherToTestAllDeployments();
// add only those supported to the output list, in order of preference.
// flag order is (MPI, GPU, OMP), matching createCustomQureg
if (gpu && omp && mpi) { out.push_back({"GPU + OMP + MPI", 1, 1, 1}); if (one) return out; }
if (gpu && mpi) { out.push_back({"GPU + MPI", 1, 1, 0}); if (one) return out; }
if (gpu && omp) { out.push_back({"GPU + OMP", 0, 1, 1}); if (one) return out; }
if (gpu) { out.push_back({"GPU", 0, 1, 0}); if (one) return out; }
if (mpi && omp) { out.push_back({"CPU + OMP + MPI", 1, 0, 1}); if (one) return out; }
if (mpi) { out.push_back({"CPU + MPI", 1, 0, 0}); if (one) return out; }
if (omp) { out.push_back({"CPU + OMP", 0, 0, 1}); if (one) return out; }
if (true) { out.push_back({"CPU", 0, 0, 0}); if (one) return out; }
// always contains CPU obviously, but this makes it explicit
DEMAND( !out.empty() );
// return all supported deployments
return out;
}
/*
* manage cached quregs
*/
quregCache createCachedStatevecsOrDensmatrs(bool isDensMatr) {
quregCache out;
// only add supported-deployment quregs to the cache
for (auto [label, mpi, gpu, omp] : getSupportedDeployments())
out[label] = createCustomQureg(getNumCachedQubits(), isDensMatr, mpi, gpu, omp);
return out;
}
void createCachedQuregs() {
// must not be called twice nor pre-creation
DEMAND( statevecs1.empty() );
DEMAND( statevecs2.empty() );
DEMAND( densmatrs1.empty() );
DEMAND( densmatrs2.empty() );
statevecs1 = createCachedStatevecsOrDensmatrs(false);
statevecs2 = createCachedStatevecsOrDensmatrs(false);
densmatrs1 = createCachedStatevecsOrDensmatrs(true);
densmatrs2 = createCachedStatevecsOrDensmatrs(true);
}
void destroyCachedQuregs() {
// must not be called twice nor pre-creation
DEMAND( ! statevecs1.empty() );
DEMAND( ! statevecs2.empty() );
DEMAND( ! densmatrs1.empty() );
DEMAND( ! densmatrs2.empty() );
auto caches = {
statevecs1, statevecs2,
densmatrs1, densmatrs2};
for (auto& cache : caches)
for (auto& [label, qureg]: cache)
destroyQureg(qureg);
statevecs1.clear();
statevecs2.clear();
densmatrs1.clear();
densmatrs2.clear();
}
quregCache getCachedStatevecs() {
// must not be called pre-creation nor post-destruction
DEMAND( !statevecs1.empty() );
return statevecs1;
}
quregCache getCachedDensmatrs() {
// must not be called pre-creation nor post-destruction
DEMAND( !densmatrs1.empty() );
return densmatrs1;
}
quregCache getAltCachedStatevecs() {
// must not be called pre-creation nor post-destruction
DEMAND( !statevecs2.empty() );
return statevecs2;
}
quregCache getAltCachedDensmatrs() {
// must not be called pre-creation nor post-destruction
DEMAND( !densmatrs2.empty() );
return densmatrs2;
}
/*
* manage cached FullStateDiagMatr
*/
void createCachedFullStateDiagMatrs() {
// must not be called twice
DEMAND( matrices.empty() );
// only add supported-deployment matrices to the cache
for (auto [label, mpi, gpu, omp] : getSupportedDeployments())
matrices[label] = createCustomFullStateDiagMatr(getNumCachedQubits(), mpi, gpu, omp);
}
void destroyCachedFullStateDiagMatrs() {
// must not be called twice
DEMAND( !matrices.empty() );
for (auto& [label, matrix]: matrices)
destroyFullStateDiagMatr(matrix);
matrices.clear();
}
matrixCache getCachedFullStateDiagMatrs() {
// must not be called pre-creation nor post-destruction
DEMAND( !matrices.empty() );
return matrices;
}
/*
* reference states of equivalent
* dimension to the cached quregs
*/
qvector getRefStatevec() {
return getZeroVector(getPow2(getNumCachedQubits()));
}
qmatrix getRefDensmatr() {
return getZeroMatrix(getPow2(getNumCachedQubits()));
}