Qrack  10.0
General classical-emulating-quantum development framework
parallel_for.hpp
Go to the documentation of this file.
1 //
3 // (C) Daniel Strano and the Qrack contributors 2017-2023. All rights reserved.
4 //
5 // This is a multithreaded, universal quantum register simulation, allowing
6 // (nonphysical) register cloning and direct measurement of probability and
7 // phase, to leverage what advantages classical emulation of qubits can have.
8 //
9 // Licensed under the GNU Lesser General Public License V3.
10 // See LICENSE.md in the project root or https://www.gnu.org/licenses/lgpl-3.0.en.html
11 // for details.
12 
13 #pragma once
14 
15 #include "qrack_functions.hpp"
16 
17 #include <functional>
18 
19 namespace Qrack {
20 
21 // Called once per value between begin and end.
22 typedef std::function<void(const bitCapIntOcl&, const unsigned& cpu)> ParallelFunc;
23 typedef std::function<void(const bitCapInt&, const unsigned& cpu)> ParallelFuncSparse;
24 typedef std::function<bitCapIntOcl(const bitCapIntOcl&)> IncrementFunc;
25 typedef std::function<bitCapInt(const bitCapIntOcl&)> IncrementFuncSparse;
26 
27 class ParallelFor {
28 private:
31  unsigned numCores;
32 
33 public:
34  ParallelFor();
35 
36  void SetConcurrencyLevel(unsigned num)
37  {
38  if (!num) {
39  num = 1U;
40  }
41  if (numCores == num) {
42  return;
43  }
44  numCores = num;
45  const bitLenInt pStridePow = log2Ocl(pStride);
46  const bitLenInt minStridePow = (bitLenInt)pow2Ocl(log2Ocl(numCores - 1U));
47  dispatchThreshold = (pStridePow > minStridePow) ? (pStridePow - minStridePow) : 0U;
48  }
49  unsigned GetConcurrencyLevel() { return numCores; }
52  /*
53  * Parallelization routines for spreading work across multiple cores.
54  */
55 
60  void par_for_inc(const bitCapIntOcl begin, const bitCapIntOcl itemCount, IncrementFunc, ParallelFunc fn);
61  void par_for_inc_sparse(
62  const bitCapIntOcl begin, const bitCapIntOcl itemCount, IncrementFuncSparse, ParallelFuncSparse fn);
63 
65  void par_for(const bitCapIntOcl begin, const bitCapIntOcl end, ParallelFunc fn);
66 
75  void par_for_skip(const bitCapIntOcl begin, const bitCapIntOcl end, const bitCapIntOcl skipPower,
76  const bitLenInt skipBitCount, ParallelFunc fn);
77 
79  void par_for_mask(
80  const bitCapIntOcl, const bitCapIntOcl, const std::vector<bitCapIntOcl>& maskArray, ParallelFunc fn);
81 
83  void par_for_set(const std::set<bitCapInt>& sparseSet, ParallelFuncSparse fn);
84 
86  void par_for_set(const std::vector<bitCapInt>& sparseSet, ParallelFuncSparse fn);
87 
89  void par_for_sparse_compose(const std::vector<bitCapInt>& lowSet, const std::vector<bitCapInt>& highSet,
90  const bitLenInt& highStart, ParallelFuncSparse fn);
91 
93  real1_f par_norm(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray, real1_f norm_thresh = ZERO_R1_F);
94 
96  real1_f par_norm_exact(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray);
97 };
98 
99 } // namespace Qrack
Definition: parallel_for.hpp:27
void par_for_mask(const bitCapIntOcl, const bitCapIntOcl, const std::vector< bitCapIntOcl > &maskArray, ParallelFunc fn)
Skip over the bits listed in maskArray in the same fashion as par_for_skip.
Definition: parallel_for.cpp:118
real1_f par_norm_exact(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray)
Calculate the normal for the array, (without flooring.)
Definition: parallel_for.cpp:390
ParallelFor()
Definition: parallel_for.cpp:32
void par_for_inc_sparse(const bitCapIntOcl begin, const bitCapIntOcl itemCount, IncrementFuncSparse, ParallelFuncSparse fn)
Definition: parallel_for.cpp:364
unsigned GetConcurrencyLevel()
Definition: parallel_for.hpp:49
const bitCapIntOcl pStride
Definition: parallel_for.hpp:29
bitLenInt GetPreferredConcurrencyPower()
Definition: parallel_for.hpp:51
bitLenInt dispatchThreshold
Definition: parallel_for.hpp:30
bitCapIntOcl GetStride()
Definition: parallel_for.hpp:50
void SetConcurrencyLevel(unsigned num)
Definition: parallel_for.hpp:36
void par_for_skip(const bitCapIntOcl begin, const bitCapIntOcl end, const bitCapIntOcl skipPower, const bitLenInt skipBitCount, ParallelFunc fn)
Skip over the skipPower bits.
Definition: parallel_for.cpp:86
void par_for_sparse_compose(const std::vector< bitCapInt > &lowSet, const std::vector< bitCapInt > &highSet, const bitLenInt &highStart, ParallelFuncSparse fn)
Iterate over the power set of 2 sparse state vectors.
Definition: parallel_for.cpp:66
void par_for(const bitCapIntOcl begin, const bitCapIntOcl end, ParallelFunc fn)
Call fn once for every numerical value between begin and end.
Definition: parallel_for.cpp:50
real1_f par_norm(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray, real1_f norm_thresh=ZERO_R1_F)
Calculate the normal for the array, (with flooring).
Definition: parallel_for.cpp:373
unsigned numCores
Definition: parallel_for.hpp:31
void par_for_set(const std::set< bitCapInt > &sparseSet, ParallelFuncSparse fn)
Iterate over a sparse state vector.
Definition: parallel_for.cpp:55
void par_for_inc(const bitCapIntOcl begin, const bitCapIntOcl itemCount, IncrementFunc, ParallelFunc fn)
Iterate through the permutations a maximum of end-begin times, allowing the caller to control the inc...
Definition: parallel_for.cpp:355
GLOSSARY: bitLenInt - "bit-length integer" - unsigned integer ID of qubit position in register bitCap...
Definition: complex16x2simd.hpp:25
bitLenInt log2Ocl(bitCapIntOcl n)
Definition: qrack_functions.hpp:97
std::function< bitCapIntOcl(const bitCapIntOcl &)> IncrementFunc
Definition: parallel_for.hpp:24
void U(quid sid, bitLenInt q, real1_f theta, real1_f phi, real1_f lambda)
(External API) 3-parameter unitary gate
Definition: wasm_api.cpp:1199
std::function< bitCapInt(const bitCapIntOcl &)> IncrementFuncSparse
Definition: parallel_for.hpp:25
float real1_f
Definition: qrack_types.hpp:107
std::function< void(const bitCapInt &, const unsigned &cpu)> ParallelFuncSparse
Definition: parallel_for.hpp:23
std::function< void(const bitCapIntOcl &, const unsigned &cpu)> ParallelFunc
Definition: parallel_for.hpp:22
std::shared_ptr< StateVector > StateVectorPtr
Definition: qrack_types.hpp:147
bitCapIntOcl pow2Ocl(const bitLenInt &p)
Definition: qrack_functions.hpp:157
#define bitLenInt
Definition: qrack_types.hpp:41
#define ZERO_R1_F
Definition: qrack_types.hpp:162
#define bitCapInt
Definition: qrack_types.hpp:65
#define bitCapIntOcl
Definition: qrack_types.hpp:53