Maestro 0.3.11
Unified interface for quantum circuit simulation
Loading...
Searching...
No Matches
FactoryGpu.cpp
Go to the documentation of this file.
1
13
14#ifdef __linux__
15
17#include "FactoryBackends.h"
18
19#define INCLUDED_BY_FACTORY
25#include "../Gpu/GpuSimulator.h"
28
29namespace Simulators
30{
31
32std::atomic_int SimulatorsFactory::requestedGpuDeviceId{0};
33thread_local int SimulatorsFactory::scopedGpuDeviceId = -1;
34
35namespace
36{
37GpuLibraryRegistry &GpuLibraries()
38{
39 static GpuLibraryRegistry registry;
40 return registry;
41}
42} // namespace
43
44std::shared_ptr<DistributedGpuLibrary> SimulatorsFactory::GetDistributedGpuLibrary()
45{
46 return DistributedGpuLibrary::GetInstance();
47}
48
49std::shared_ptr<DistributedGpuLibrary> DistributedGpuLibrary::GetInstance()
50{
51 static auto lib = std::shared_ptr<DistributedGpuLibrary>(new DistributedGpuLibrary());
52 return lib;
53}
54
55std::shared_ptr<DistributedMpiGpuLibrary> DistributedMpiGpuLibrary::GetInstance()
56{
57 static auto lib = std::shared_ptr<DistributedMpiGpuLibrary>(new DistributedMpiGpuLibrary());
58 return lib;
59}
60
61bool SimulatorsFactory::IsDistributedGpuAvailable() noexcept
62{
63 try
64 {
65 auto lib = GetDistributedGpuLibrary();
66 return lib->Load() && lib->GetGpuDeviceCount() > 0;
67 }
68 catch (...)
69 {
70 // Initialization still exposes the full diagnostic; discovery is a probe.
71 return false;
72 }
73}
74
75std::shared_ptr<DistributedMpiGpuLibrary> SimulatorsFactory::GetDistributedMpiGpuLibrary()
76{
77 return DistributedMpiGpuLibrary::GetInstance();
78}
79
80void SimulatorsFactory::FinalizeDistributedMpiGpuBackend()
81{
82 DistributedMpiGpuLibrary::GetInstance()->FinalizeBackend();
83}
84
86{
87 if (deviceId < 0)
88 throw std::invalid_argument("gpu_device must be nonnegative");
89 requestedGpuDeviceId = deviceId;
90}
91
92int SimulatorsFactory::ResolveGpuDevice(int deviceId)
93{
94 if (deviceId >= 0)
95 return deviceId;
96 if (scopedGpuDeviceId >= 0)
97 return scopedGpuDeviceId;
98 return requestedGpuDeviceId.load();
99}
100
101SimulatorsFactory::ScopedGpuDevice::ScopedGpuDevice(int deviceId) : previous(scopedGpuDeviceId)
102{
103 scopedGpuDeviceId = ResolveGpuDevice(deviceId);
104}
105
106SimulatorsFactory::ScopedGpuDevice::~ScopedGpuDevice()
107{
108 scopedGpuDeviceId = previous;
109}
110
111std::shared_ptr<GpuLibrary> SimulatorsFactory::GetGpuLibrary(int deviceId)
112{
113 return GpuLibraries().Acquire(ResolveGpuDevice(deviceId));
114}
115
116bool SimulatorsFactory::InitGpuLibrary()
117{
118 return bool(GetGpuLibrary());
119}
120
121bool SimulatorsFactory::InitGpuLibraryWithMute()
122{
123 return bool(GpuLibraries().Acquire(ResolveGpuDevice(), true));
124}
125
126int SimulatorsFactory::GetGpuDeviceCount()
127{
128 return GpuLibraries().DeviceCount();
129}
130
131bool SimulatorsFactory::IsGpuLibraryAvailable(int deviceId)
132{
133 return bool(GpuLibraries().Acquire(ResolveGpuDevice(deviceId), true));
134}
135
136namespace FactoryBackends
137{
138
139std::unique_ptr<ISimulator> CreateGpuImmediate(SimulationType method)
140{
141 std::unique_ptr<ISimulator> sim;
142 switch (method)
143 {
144 case SimulationType::kStatevector:
145 sim = std::make_unique<Private::GpuStatevectorSimulator>();
146 break;
147 case SimulationType::kMatrixProductState:
148 sim = std::make_unique<Private::GpuMPSSimulator>();
149 break;
150 case SimulationType::kMatrixProductOperator:
151 sim = std::make_unique<Private::GpuMPOSimulator>();
152 break;
153 case SimulationType::kTensorNetwork:
154 sim = std::make_unique<Private::GpuTensorNetworkSimulator>();
155 break;
156 case SimulationType::kPauliPropagator:
157 sim = std::make_unique<Private::GpuPauliPropagatorSimulator>();
158 break;
159 case SimulationType::kDensityMatrix:
160 sim = std::make_unique<Private::GpuDensityMatrixSimulator>();
161 break;
162 default:
163 return nullptr;
164 }
165 sim->Configure("gpu_device", std::to_string(SimulatorsFactory::ResolveGpuDevice()).c_str());
166 return sim;
167}
168
169std::unique_ptr<ISimulator> CreateGpuSimulator(std::unique_ptr<ISimulator> immediate)
170{
171 return std::make_unique<Private::GpuSimulator>(std::move(immediate));
172}
173
174std::unique_ptr<ISimulator> CreateDistributedGpuSimulator()
175{
176 return std::make_unique<Private::DistributedGpuSimulator>();
177}
178
179std::unique_ptr<ISimulator> CreateDistributedMpiGpuSimulator()
180{
181 return std::make_unique<Private::DistributedMpiGpuSimulator>();
182}
183
184} // namespace FactoryBackends
185} // namespace Simulators
186
187#endif // __linux__
Bounded online routing based on current physical bond dimensions.