Qrack  7.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-2021. 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 /* Needed for bitCapInt typedefs. */
16 #include "qrack_types.hpp"
17 
18 #include <algorithm>
19 #include <set>
20 #include <vector>
21 
22 namespace Qrack {
23 
24 class ParallelFor {
25 private:
27  unsigned numCores;
28 
29 public:
30  ParallelFor();
31 
32  virtual ~ParallelFor() {}
33 
34  void SetConcurrencyLevel(unsigned num) { numCores = num; }
35  unsigned GetConcurrencyLevel() { return numCores; }
37  /*
38  * Parallelization routines for spreading work across multiple cores.
39  */
40 
45  void par_for_inc(const bitCapIntOcl begin, const bitCapIntOcl itemCount, IncrementFunc, ParallelFunc fn);
46 
48  void par_for(const bitCapIntOcl begin, const bitCapIntOcl end, ParallelFunc fn);
49 
58  void par_for_skip(const bitCapIntOcl begin, const bitCapIntOcl end, const bitCapIntOcl skipPower,
59  const bitLenInt skipBitCount, ParallelFunc fn);
60 
62  void par_for_mask(const bitCapIntOcl, const bitCapIntOcl, const bitCapIntOcl* maskArray, const bitLenInt maskLen,
63  ParallelFunc fn);
64 
66  void par_for_set(const std::set<bitCapIntOcl>& sparseSet, ParallelFunc fn);
67 
69  void par_for_set(const std::vector<bitCapIntOcl>& sparseSet, ParallelFunc fn);
70 
72  void par_for_sparse_compose(const std::vector<bitCapIntOcl>& lowSet, const std::vector<bitCapIntOcl>& highSet,
73  const bitLenInt& highStart, ParallelFunc fn);
74 
76  void par_for_qbdt(const bitCapInt begin, const bitCapInt end, BdtFunc fn);
77 
79  real1_f par_norm(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray, real1_f norm_thresh = ZERO_R1);
80 
82  real1_f par_norm_exact(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray);
83 };
84 
85 } // namespace Qrack
real1_f par_norm(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray, real1_f norm_thresh=ZERO_R1)
Calculate the normal for the array, (with flooring).
Definition: parallel_for.cpp:396
virtual ~ParallelFor()
Definition: parallel_for.hpp:32
void SetConcurrencyLevel(unsigned num)
Definition: parallel_for.hpp:34
Definition: parallel_for.hpp:24
std::function< bitCapIntOcl(const bitCapIntOcl &, const unsigned &cpu)> IncrementFunc
Definition: qrack_types.hpp:156
#define ZERO_R1
Definition: qrack_types.hpp:99
unsigned numCores
Definition: parallel_for.hpp:27
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
#define bitCapIntOcl
Definition: qrack_types.hpp:27
void par_for_set(const std::set< bitCapIntOcl > &sparseSet, ParallelFunc fn)
Iterate over a sparse state vector.
Definition: parallel_for.cpp:52
ParallelFor()
Definition: parallel_for.cpp:34
float real1_f
Definition: qrack_types.hpp:98
#define bitLenInt
Definition: qrack_types.hpp:40
unsigned GetConcurrencyLevel()
Definition: parallel_for.hpp:35
bitCapIntOcl GetStride()
Definition: parallel_for.hpp:36
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:46
void par_for_qbdt(const bitCapInt begin, const bitCapInt end, BdtFunc fn)
Iterate over a QBDT tree.
Definition: parallel_for.cpp:338
std::function< bitCapInt(const bitCapInt &, const unsigned &cpu)> BdtFunc
Definition: qrack_types.hpp:157
std::function< void(const bitCapIntOcl &, const unsigned &cpu)> ParallelFunc
Called once per value between begin and end.
Definition: qrack_types.hpp:155
void par_for_mask(const bitCapIntOcl, const bitCapIntOcl, const bitCapIntOcl *maskArray, const bitLenInt maskLen, ParallelFunc fn)
Skip over the bits listed in maskArray in the same fashion as par_for_skip.
Definition: parallel_for.cpp:130
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:96
#define bitCapInt
Definition: qrack_types.hpp:51
real1_f par_norm_exact(const bitCapIntOcl maxQPower, const StateVectorPtr stateArray)
Calculate the normal for the array, (without flooring.)
Definition: parallel_for.cpp:413
Definition: complex16x2simd.hpp:25
const bitCapIntOcl pStride
Definition: parallel_for.hpp:26
std::shared_ptr< StateVector > StateVectorPtr
Definition: qrack_types.hpp:161
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:76