helics  3.5.2
MpiService.h
1 /*
2 Copyright (c) 2017-2024,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 
9 #include "../../core/ActionMessage.hpp"
10 #include "MpiComms.h"
11 #include "gmlc/containers/BlockingQueue.hpp"
12 #include "helics/helics-config.h"
13 
14 #include <atomic>
15 #include <functional>
16 #include <list>
17 #include <memory>
18 #include <mpi.h>
19 #include <mutex>
20 #include <string>
21 #include <thread>
22 #include <utility>
23 #include <vector>
24 
25 namespace helics {
26 namespace mpi {
28  class MpiService {
29  public:
31  MpiService(const MpiService&) = delete;
33  MpiService& operator=(const MpiService&) = delete;
34 
35  static MpiService& getInstance();
36  static void setMpiCommunicator(MPI_Comm communicator);
37  static void setStartServiceThread(bool start);
38 
39  std::string addMpiComms(MpiComms* comm);
40  void removeMpiComms(MpiComms* comm);
41  std::string getAddress(MpiComms* comm);
42  int getRank();
43  int getTag(MpiComms* comm);
44 
45  void sendMessage(std::pair<int, int> address, std::vector<char> message)
46  {
47  txMessageQueue.emplace(address, std::move(message));
48  }
49 
50  void sendAndReceiveMessages();
51  void drainRemainingMessages();
52 
53  private:
54  MpiService() = default;
55  ~MpiService();
56 
57  int commRank = -1;
58  static MPI_Comm mpiCommunicator;
59  static bool startServiceThread;
60 
61  std::mutex mpiDataLock;
62  std::vector<MpiComms*> comms;
63  gmlc::containers::BlockingQueue<std::pair<std::pair<int, int>, std::vector<char>>>
64  txMessageQueue;
65 
66  bool helics_initialized_mpi{false};
67  std::atomic<int> comms_connected{0};
68  std::atomic<bool> startup_flag{false};
69  std::atomic<bool> stop_service{false};
70  std::unique_ptr<std::thread> service_thread;
71 
72  void startService();
73  void serviceLoop();
74 
75  bool initMPI();
76  };
77 
78 } // namespace mpi
79 } // namespace helics
Definition: MpiComms.h:24
Definition: MpiService.h:28
MpiService & operator=(const MpiService &)=delete
MpiService(const MpiService &)=delete
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14