Qrack  9.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 namespace Qrack {
18 
19 class ParallelFor {
20 private:
23  unsigned numCores;
24 
25 public:
26  ParallelFor();
27 
28  void SetConcurrencyLevel(unsigned num)
29  {
30  if (numCores == num) {
31  return;
32  }
33  numCores = num;
34  const bitLenInt pStridePow = log2(pStride);
35  const bitLenInt minStridePow = (numCores > 1U) ? (bitLenInt)pow2Ocl(log2(numCores - 1U)) : 0U;
36  dispatchThreshold = (pStridePow > minStridePow) ? (pStridePow - minStridePow) : 0U;
37  }
38  unsigned GetConcurrencyLevel() { return numCores; }
41  /*
42  * Parallelization routines for spreading work across multiple cores.
43  */
44 
49  void par_for_inc(const bitCapIntOcl begin, const bitCapIntOcl itemCount, IncrementFunc, ParallelFunc fn);
50 
52  void par_for(const bitCapIntOcl begin, const bitCapIntOcl end, ParallelFunc fn);
53 
62  void par_for_skip(const bitCapIntOcl begin, const bitCapIntOcl end, const bitCapIntOcl skipPower,
63  const bitLenInt skipBitCount, ParallelFunc fn);
64 
66  void par_for_mask(
67  const bitCapIntOcl, const bitCapIntOcl, const std::vector<bitCapIntOcl>& maskArray, ParallelFunc fn);
68 
70  void par_for_set(const std::set<bitCapIntOcl>& sparseSet, ParallelFunc fn);
71 
73  void par_for_set(const std::vector<bitCapIntOcl>& sparseSet, ParallelFunc fn);
74 
76  void par_for_sparse_compose(const std::vector<bitCapIntOcl>& lowSet, const std::vector<bitCapIntOcl>& highSet,
77  const bitLenInt& highStart, ParallelFunc fn);
78 
80  real1_f par_norm(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray, real1_f norm_thresh = ZERO_R1_F);
81 
83  real1_f par_norm_exact(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray);
84 };
85 
86 } // namespace Qrack
Definition: parallel_for.hpp:19
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:134
real1_f par_norm_exact(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray)
Calculate the normal for the array, (without flooring.)
Definition: parallel_for.cpp:355
ParallelFor()
Definition: parallel_for.cpp:32
unsigned GetConcurrencyLevel()
Definition: parallel_for.hpp:38
const bitCapIntOcl pStride
Definition: parallel_for.hpp:21
bitLenInt GetPreferredConcurrencyPower()
Definition: parallel_for.hpp:40
bitLenInt dispatchThreshold
Definition: parallel_for.hpp:22
bitCapIntOcl GetStride()
Definition: parallel_for.hpp:39
void SetConcurrencyLevel(unsigned num)
Definition: parallel_for.hpp:28
void par_for_set(const std::set< bitCapIntOcl > &sparseSet, ParallelFunc fn)
Iterate over a sparse state vector.
Definition: parallel_for.cpp:57
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:101
void par_for_sparse_compose(const std::vector< bitCapIntOcl > &lowSet, const std::vector< bitCapIntOcl > &highSet, const bitLenInt &highStart, ParallelFunc fn)
Iterate over the power set of 2 sparse state vectors.
Definition: parallel_for.cpp:81
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:51
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:338
unsigned numCores
Definition: parallel_for.hpp:23
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:329
Definition: complex16x2simd.hpp:25
constexpr real1_f ZERO_R1_F
Definition: qrack_types.hpp:152
std::function< bitCapIntOcl(const bitCapIntOcl &)> IncrementFunc
Definition: qrack_types.hpp:127
float real1_f
Definition: qrack_types.hpp:64
std::function< void(const bitCapIntOcl &, const unsigned &cpu)> ParallelFunc
Definition: qrack_types.hpp:126
std::shared_ptr< StateVector > StateVectorPtr
Definition: qrack_types.hpp:133
bitCapIntOcl pow2Ocl(const bitLenInt &p)
Definition: qrack_functions.hpp:23
bitLenInt log2(bitCapInt n)
Definition: qrack_functions.hpp:26
MICROSOFT_QUANTUM_DECL void U(_In_ uintq sid, _In_ uintq q, _In_ double theta, _In_ double phi, _In_ double lambda)
(External API) 3-parameter unitary gate
Definition: pinvoke_api.cpp:1362
#define bitLenInt
Definition: qrack_types.hpp:44
#define bitCapIntOcl
Definition: qrack_types.hpp:91