/home/runner/work/HiCR/HiCR/include/hicr/backends/mpi/instanceManager.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/backends/mpi/instanceManager.hpp Source File
HiCR
instanceManager.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
24#pragma once
25
26#include <memory>
27#include <mpi.h>
28#include <hicr/core/definitions.hpp>
31
32namespace HiCR::backend::mpi
33{
34
35#ifndef _HICR_MPI_INSTANCE_BASE_TAG
41 #define _HICR_MPI_INSTANCE_BASE_TAG 4096
42#endif
43
47#define _HICR_MPI_RPC_TAG (_HICR_MPI_INSTANCE_BASE_TAG + 1)
48
52#define _HICR_MPI_INSTANCE_RETURN_SIZE_TAG (_HICR_MPI_INSTANCE_BASE_TAG + 2)
53
57#define _HICR_MPI_INSTANCE_RETURN_DATA_TAG (_HICR_MPI_INSTANCE_BASE_TAG + 3)
58
63{
64 public:
65
71 InstanceManager(MPI_Comm comm)
72 : HiCR::InstanceManager(),
73 _comm(comm)
74 {
75 // Getting current rank within and size of communicator
76 MPI_Comm_size(_comm, &_size);
77 MPI_Comm_rank(_comm, &_rank);
78
79 // In MPI, the initial set of processes represents all the currently available instances of HiCR
80 for (int i = 0; i < _size; i++)
81 {
82 // Creating new MPI-based HiCR instance
83 auto instance = std::make_shared<HiCR::backend::mpi::Instance>(i);
84
85 // If this is the current rank, set it as current instance
86 if (i == _rank) setCurrentInstance(instance);
87
88 // Adding instance to the collection
89 addInstance(instance);
90 }
91 }
92
93 ~InstanceManager() override = default;
94
95 __INLINE__ void finalize() override { MPI_Finalize(); }
96
97 __INLINE__ void abort(int errorCode) override { MPI_Abort(MPI_COMM_WORLD, errorCode); }
98
106 __INLINE__ static std::unique_ptr<HiCR::InstanceManager> createDefault(int *argc, char ***argv)
107 {
108 // Initializing MPI
109 int initialized = 0;
110 MPI_Initialized(&initialized);
111 if (initialized == 0)
112 {
113 int requested = MPI_THREAD_SINGLE;
114 int provided = 0;
115 MPI_Init_thread(argc, argv, requested, &provided);
116 if (provided < requested) fprintf(stderr, "Warning, your application may not work properly if MPI does not support threaded access\n");
117 }
118
119 // Setting MPI_COMM_WORLD error handler
120 MPI_Comm_set_errhandler(MPI_COMM_WORLD, MPI_ERRORS_RETURN);
121
122 // Creating instance manager
123 return std::make_unique<HiCR::backend::mpi::InstanceManager>(MPI_COMM_WORLD);
124 }
125
127
128 private:
129
133 int _RPCRequestRank = 0;
134
138 const MPI_Comm _comm;
139
143 int _size{};
144
148 int _rank{};
149};
150
151} // namespace HiCR::backend::mpi
Provides a definition for the instance class for the MPI backend.
constexpr int _HICR_MPI_INSTANCE_ROOT_ID
Definition instance.hpp:34
Definition instanceManager.hpp:57
__INLINE__ void addInstance(HiCR::Instance::instanceId_t instanceId)
Definition instanceManager.hpp:139
__INLINE__ void setCurrentInstance(const std::shared_ptr< HiCR::Instance > &instance)
Definition instanceManager.hpp:203
uint64_t instanceId_t
Definition instance.hpp:44
Definition instanceManager.hpp:63
__INLINE__ void finalize() override
Definition instanceManager.hpp:95
__INLINE__ HiCR::Instance::instanceId_t getRootInstanceId() const override
Definition instanceManager.hpp:126
static __INLINE__ std::unique_ptr< HiCR::InstanceManager > createDefault(int *argc, char ***argv)
Definition instanceManager.hpp:106
InstanceManager(MPI_Comm comm)
Definition instanceManager.hpp:71
__INLINE__ void abort(int errorCode) override
Definition instanceManager.hpp:97
Provides a definition for the abstract instance manager class.