The Quantum Exact Simulation Toolkit v4.3.0
Loading...
Searching...
No Matches
experimental.h
1/** @file
2 * Experimental functions which are liable to
3 * API breaks within QuEST minor version releases.
4 * Some optional functions require compiling this
5 * file against MPI, despite being outside of /comm/,
6 * and so require opt-in macros (QUEST_COMPILE_SUBCOMM)
7 *
8 * @author Oliver Brown
9 * @author Tyson Jones (formatting)
10 * @author Ashmit JaiSarita Gupta (checkpointing)
11 *
12 * @defgroup experimental Experimental
13 * @ingroup api
14 * @brief Experimental functions with tentative APIs
15 * @{
16 */
17
18#ifndef EXPERIMENTAL_H
19#define EXPERIMENTAL_H
20
21#include "quest/include/config.h"
22
23#if QUEST_COMPILE_SUBCOMM && ! QUEST_COMPILE_MPI
24 #error "Macro QUEST_COMPILE_SUBCOMM was true, but QUEST_COMPILE_MPI was illegally false."
25#endif
26
27#if QUEST_COMPILE_SUBCOMM
28 #include <mpi.h>
29#endif
30
31#include "quest/include/qureg.h"
32
33// C++ gets string overloads
34#ifdef __cplusplus
35 #include <string>
36#endif
37
38
39// enable invocation by both C and C++ binaries
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44
45/** @notyetdoced
46 *
47 * Advanced initialiser which lets the user positively declare that they take responsibility for MPI.
48 * This means we assume they have called MPI_Init, and that they will call MPI_Finalize.
49 *
50 * @author Oliver Brown
51 */
52void initCustomMpiQuESTEnv(int useDistrib, bool userOwnsMpi, int useGpuAccel, int useMultithread);
53
54
55#if QUEST_COMPILE_SUBCOMM
56/** @notyetdoced
57 *
58 * Advanced initialiser which allows the user to provide an MPI communicator for QuEST to use.
59 * Use of this initialiser implies userOwnsMpi = true, (exposed by initCustomMpiQuESTEnv) and
60 * therefore that they have already initialised MPI, and they will call MPI_Finalize at the
61 * appropriate time.
62 *
63 * The user-provided MPI communicator undergoes the same validation procedure as any that QuEST
64 * would use, and so must contain a power-of-2 number of processes.
65 *
66 * > [!IMPORTANT]
67 * > This function is only compiled and exposed when macro QUEST_COMPILE_SUBCOMM is 1, as is
68 * > defined when providing CMake option QUEST_ENABLE_SUBCOMM during building.
69 *
70 * @author Oliver Brown
71 */
72void initCustomMpiCommQuESTEnv(MPI_Comm questComm, int useGpuAccel, int useMultithread);
73#endif // QUEST_COMPILE_SUBCOMM
74
75
76/** @notyetdoced
77 *
78 * @author Oliver Brown
79 */
81
82
83/** Overrides the number of CUDA threads per block (or @p blockDim) used by QuEST's GPU-accelerated backend.
84 *
85 * This changes the GPU parallelisation granularity and can affect performance, and is useful
86 * for performance tuning or diagnostics. Before this function is called, QuEST will use the
87 * number as specified by the environment variable @p QUEST_DEFAULT_NUM_GPU_THREADS_PER_BLOCK,
88 * if defined. Otherwise, it will use the value specified by the CMake/compile option of the
89 * same name, which itself presently defaults to @p 128. After this function is called, QuEST
90 * will adopt @p numThreadsPerBlock for the remainder of execution, or until this function is
91 * called again.
92 *
93 * Practical values of @p numThreadsPerBlock can vary with the simulation size, the user's GPU hardware,
94 * and whether it is NVIDIA or AMD, which have respective warp sizes of @p 32 and @p 64.
95 *
96 * @note
97 * This function has no effect when QuEST is not deployed with GPU-acceleration enabled.
98 *
99 * @param[in] numThreadsPerBlock the new block size.
100 * @throws @validationerror
101 * - if the @p QuESTEnv is not initialised.
102 * - if @p numThreadsPerBlock is negative.
103 * - if @p numThreadsPerBlock is not a multiple of the GPU warp size.
104 * - if @p numThreadsPerBlock exceeds the maximum @p blockDim imposed by the GPU hardware.
105 * @see
106 * - QUEST_DEFAULT_NUM_GPU_THREADS_PER_BLOCK
107 * @author Oliver Brown
108 * @author Tyson Jones
109 */
110void setQuESTNumGpuThreadsPerBlock(int numThreadsPerBlock);
111
112
113/** Writes the contents of @p qureg to the file (or folder) @p fn, so that it may later be
114 * restored with createQuregFromFile(), potentially in another process.
115 *
116 * The output records the @p qureg dimension (number of qubits and whether it is a density matrix),
117 * the amplitude precision, the Qureg's distribution, and the Qureg's full set of amplitudes. Other
118 * deployment information, such as whether the Qureg is multithreaded or GPU-accelerated, is not
119 * recorded.
120 *
121 * There is no particular file extension or folder name suffix required, though since saving is
122 * performed with ADIOS2, a suffix of `.bp` is conventional.
123 *
124 * > [!CAUTION]
125 * > Specifying @p fn equal to an existing directory or file will cause erasure and overwriting of
126 * > its contents. It is especially dangerous to pass @p fn equal to a system directory, such as
127 * > @c / on Unix, and may cause system corruption.
128 *
129 * > [!IMPORTANT]
130 * > This function is only callable when QuEST is compiled with CMake option @c QUEST_ENABLE_ADIOS2=1.
131 *
132 * @param[in] qureg the Qureg to write to disk.
133 * @param[in] fn the output file (or folder) path.
134 * @throws @validationerror
135 * - if @p qureg is uninitialised.
136 * - if QuEST was not compiled with CMake option @c QUEST_ENABLE_ADIOS2=1.
137 * - if opening or writing to @p fn fails.
138 * @see
139 * - createQuregFromFile() to restore a Qureg saved by this function.
140 * @author Ashmit JaiSarita Gupta
141 */
142void saveQuregToFile(Qureg qureg, const char* fn);
143
144
145/** Creates a new Qureg from a file (or folder) previously created by saveQuregToFile(),
146 * with automatically chosen deployments (independent of those used when the
147 * file was saved), and populates the Qureg with the saved amplitudes.
148 *
149 * The chosen deployments are identical to those chosen by createQureg() and createDensityQureg().
150 *
151 * > [!NOTE]
152 * > The number of distributed nodes chosen by the autodeployer must agree with the
153 * > number of nodes of the originally saved Qureg, else a @validationerror is thrown. Therefore,
154 * > the number of MPI processes calling these functions cannot be changed between saveQuregToFile()
155 * > and createQuregFromFile(), unless the Qureg was non-distributed in both settings.
156 *
157 * > [!IMPORTANT]
158 * > This function is only callable when QuEST is compiled with CMake option @c QUEST_ENABLE_ADIOS2=1.
159 *
160 * @param[in] fn the file (or folder) path previously created by saveQuregToFile().
161 * @returns A new Qureg instance matching the saved dimension and amplitudes.
162 * @throws @validationerror
163 * - if QuEST was not compiled with CMake option @c QUEST_ENABLE_ADIOS2=1.
164 * - if @p fn cannot be read (since, for example, it does not exist).
165 * - if the precision of the saved Qureg differs from the current QuEST precision.
166 * - if the number of distributed nodes of the saved Qureg differs from the autodeployer's chosen number.
167 * - if the recorded Qureg dimensions would overflow the @c qindex type.
168 * - if the recorded total Qureg memory would overflow the @c size_t type.
169 * - if the system contains insufficient RAM (or VRAM) to store the Qureg in any deployment.
170 * - if any Qureg memory allocation unexpectedly fails.
171 * @see
172 * - saveQuregToFile() to create a file readable by this function.
173 * @author Ashmit JaiSarita Gupta
174 * @author Tyson Jones (input validation)
175 */
176Qureg createQuregFromFile(const char* fn);
177
178
179// end de-mangler
180#ifdef __cplusplus
181}
182#endif
183
184
185
186#if defined(__cplusplus)
187
188
189 /**
190 * @notyetdoced
191 * @cpponly
192 *
193 * @see
194 * - saveQuregToFile()
195 */
196 void saveQuregToFile(Qureg qureg, std::string);
197
198
199 /**
200 * @notyetdoced
201 * @cpponly
202 *
203 * @see
204 * - createQuregFromFile()
205 */
206 Qureg createQuregFromFile(std::string fn);
207
208
209#endif // __cplusplus
210
211
212#endif // EXPERIMENTAL_H
213
214/** @} */ // (end file-wide doxygen defgroup)
void initCustomMpiQuESTEnv(int useDistrib, bool userOwnsMpi, int useGpuAccel, int useMultithread)
void initCustomMpiCommQuESTEnv(MPI_Comm questComm, int useGpuAccel, int useMultithread)
Qureg createQuregFromFile(const char *fn)
int getQuESTNumGpuThreadsPerBlock()
void setQuESTNumGpuThreadsPerBlock(int numThreadsPerBlock)
void saveQuregToFile(Qureg qureg, const char *fn)
Definition qureg.h:49