19#define INCLUDED_BY_FACTORY
32std::atomic_int SimulatorsFactory::requestedGpuDeviceId{0};
33thread_local int SimulatorsFactory::scopedGpuDeviceId = -1;
37GpuLibraryRegistry &GpuLibraries()
39 static GpuLibraryRegistry registry;
44std::shared_ptr<DistributedGpuLibrary> SimulatorsFactory::GetDistributedGpuLibrary()
46 return DistributedGpuLibrary::GetInstance();
49std::shared_ptr<DistributedGpuLibrary> DistributedGpuLibrary::GetInstance()
51 static auto lib = std::shared_ptr<DistributedGpuLibrary>(
new DistributedGpuLibrary());
55std::shared_ptr<DistributedMpiGpuLibrary> DistributedMpiGpuLibrary::GetInstance()
57 static auto lib = std::shared_ptr<DistributedMpiGpuLibrary>(
new DistributedMpiGpuLibrary());
61bool SimulatorsFactory::IsDistributedGpuAvailable() noexcept
65 auto lib = GetDistributedGpuLibrary();
66 return lib->Load() && lib->GetGpuDeviceCount() > 0;
75std::shared_ptr<DistributedMpiGpuLibrary> SimulatorsFactory::GetDistributedMpiGpuLibrary()
77 return DistributedMpiGpuLibrary::GetInstance();
80void SimulatorsFactory::FinalizeDistributedMpiGpuBackend()
82 DistributedMpiGpuLibrary::GetInstance()->FinalizeBackend();
88 throw std::invalid_argument(
"gpu_device must be nonnegative");
89 requestedGpuDeviceId = deviceId;
92int SimulatorsFactory::ResolveGpuDevice(
int deviceId)
96 if (scopedGpuDeviceId >= 0)
97 return scopedGpuDeviceId;
98 return requestedGpuDeviceId.load();
101SimulatorsFactory::ScopedGpuDevice::ScopedGpuDevice(
int deviceId) : previous(scopedGpuDeviceId)
103 scopedGpuDeviceId = ResolveGpuDevice(deviceId);
106SimulatorsFactory::ScopedGpuDevice::~ScopedGpuDevice()
108 scopedGpuDeviceId = previous;
111std::shared_ptr<GpuLibrary> SimulatorsFactory::GetGpuLibrary(
int deviceId)
113 return GpuLibraries().Acquire(ResolveGpuDevice(deviceId));
116bool SimulatorsFactory::InitGpuLibrary()
118 return bool(GetGpuLibrary());
121bool SimulatorsFactory::InitGpuLibraryWithMute()
123 return bool(GpuLibraries().Acquire(ResolveGpuDevice(),
true));
126int SimulatorsFactory::GetGpuDeviceCount()
128 return GpuLibraries().DeviceCount();
131bool SimulatorsFactory::IsGpuLibraryAvailable(
int deviceId)
133 return bool(GpuLibraries().Acquire(ResolveGpuDevice(deviceId),
true));
136namespace FactoryBackends
139std::unique_ptr<ISimulator> CreateGpuImmediate(SimulationType method)
141 std::unique_ptr<ISimulator> sim;
144 case SimulationType::kStatevector:
145 sim = std::make_unique<Private::GpuStatevectorSimulator>();
147 case SimulationType::kMatrixProductState:
148 sim = std::make_unique<Private::GpuMPSSimulator>();
150 case SimulationType::kMatrixProductOperator:
151 sim = std::make_unique<Private::GpuMPOSimulator>();
153 case SimulationType::kTensorNetwork:
154 sim = std::make_unique<Private::GpuTensorNetworkSimulator>();
156 case SimulationType::kPauliPropagator:
157 sim = std::make_unique<Private::GpuPauliPropagatorSimulator>();
159 case SimulationType::kDensityMatrix:
160 sim = std::make_unique<Private::GpuDensityMatrixSimulator>();
165 sim->Configure(
"gpu_device", std::to_string(SimulatorsFactory::ResolveGpuDevice()).c_str());
169std::unique_ptr<ISimulator> CreateGpuSimulator(std::unique_ptr<ISimulator> immediate)
171 return std::make_unique<Private::GpuSimulator>(std::move(immediate));
174std::unique_ptr<ISimulator> CreateDistributedGpuSimulator()
176 return std::make_unique<Private::DistributedGpuSimulator>();
179std::unique_ptr<ISimulator> CreateDistributedMpiGpuSimulator()
181 return std::make_unique<Private::DistributedMpiGpuSimulator>();
static void SelectGpuDevice(int)
Bounded online routing based on current physical bond dimensions.