7class DistributedMpiGpuLibrary :
public DistributedGpuLibrary {
9 using Communicator = DistributedGpuApi::MgdMpiCommunicator;
10 using RuntimeInfo = DistributedGpuApi::MgdMpiRuntimeInfo;
11 static std::shared_ptr<DistributedMpiGpuLibrary> GetInstance();
12 void* CreateNative(
int,
int)
override {
13 throw std::logic_error(
"MPI GPU states require a communicator");
15 RuntimeInfo GetRuntimeInfo(
const Communicator* comm) {
16 std::lock_guard<std::recursive_mutex> lock(mutex);
18 RuntimeInfo info{
sizeof(RuntimeInfo), 0, 0, 0};
19 Check(getInfo(comm, &info),
"GetMpiRuntimeInfo");
22 void GatherDevices(
const Communicator* comm, int32_t device,
23 std::vector<int32_t>& devices) {
24 std::lock_guard<std::recursive_mutex> lock(mutex);
26 Check(gather(comm, device, devices.data(), devices.size()),
29 void* CreateMpiNative(
const Communicator* comm,
int device,
31 std::lock_guard<std::recursive_mutex> lock(mutex);
33 Check(validate(comm, std::getenv(
"MAESTRO_LICENSE_KEY")),
34 "ValidateMpiLicenseRuntime");
35 if (!context) context = InitLib();
37 auto obj = create(context, comm, device, p2pBits);
38 if (!obj) Fail(
"CreateMpiStateVectorRuntime");
43 void FinalizeBackend() {
44 std::lock_guard<std::recursive_mutex> lock(mutex);
46 throw std::logic_error(
47 "Destroy all MPI GPU states before finalizing the backend");
48 if (finalized)
return;
51 Check(finalize(),
"FinalizeMpiBackend");
57 DistributedMpiGpuLibrary() : DistributedGpuLibrary(true) {}
59 T Resolve(
const char* name) {
60 auto fn =
reinterpret_cast<T
>(GetFunction(name));
62 throw std::runtime_error(std::string(
"MPI GPU plugin missing ") + name +
63 "; update maestro-gpu-distributed");
66 void RequireRuntime() {
67 if (finalized)
throw std::runtime_error(
"MPI GPU backend is finalized");
69 if (runtimeLoaded)
return;
70 getInfo = Resolve<decltype(getInfo)>(
"GetMpiRuntimeInfo");
71 gather = Resolve<decltype(gather)>(
"GatherMpiDevices");
72 validate = Resolve<decltype(validate)>(
"ValidateMpiLicenseRuntime");
73 create = Resolve<decltype(create)>(
"CreateMpiStateVectorRuntime");
74 finalize = Resolve<decltype(finalize)>(
"FinalizeMpiBackend");
77 int (*getInfo)(
const Communicator*, RuntimeInfo*) =
nullptr;
78 int (*gather)(
const Communicator*, int32_t, int32_t*, uint32_t) =
nullptr;
79 int (*validate)(
const Communicator*,
const char*) =
nullptr;
80 void* (*create)(
void*,
const Communicator*, int32_t, uint32_t) =
nullptr;
81 int (*finalize)() =
nullptr;
82 bool finalized =
false, runtimeLoaded =
false;