BioDynaMo  v1.05.119-a4ff3934
mpi_helper.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_MULTI_SIMULATION_MPI_HELPER_H_
16 #define CORE_MULTI_SIMULATION_MPI_HELPER_H_
17 
18 #include <sstream>
19 #include <string>
20 #include <vector>
21 
22 #include <TMessage.h>
23 
24 // Hide MPI headers from Cling
25 #if (!defined(__CLING__) && !defined(__ROOTCLING__))
26 #include "mpi.h"
27 #endif // __ROOTCLING__
28 
29 #include "core/util/log.h"
30 #include "core/util/root.h"
31 
32 namespace bdm {
33 namespace experimental {
34 
35 #ifdef USE_MPI
36 
39 class MPIObject : public TMessage {
40  public:
41  MPIObject() = default;
42  ~MPIObject() override = default;
43  MPIObject(void* buf, Int_t len) : TMessage(buf, len) {}
44 
45  private:
46  BDM_CLASS_DEF_OVERRIDE(MPIObject, 1);
47 };
48 
49 // Hide MPI functions from Cling
50 #if (!defined(__CLING__) && !defined(__ROOTCLING__))
51 template <typename T>
53 int MPI_Send_Obj_ROOT(T* obj, int dest, int tag,
54  MPI_Status* status = MPI_STATUS_IGNORE) {
55  MPIObject mpio;
56  mpio.WriteObject(obj);
57  int size = mpio.Length();
58  // First send the size of the buffer
59  MPI_Send(&size, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
60  // Then send the buffer
61  return MPI_Send(mpio.Buffer(), size, MPI_BYTE, dest, tag, MPI_COMM_WORLD);
62 }
63 
65 template <typename T>
66 T* MPI_Recv_Obj_ROOT(int size, int source, int tag,
67  MPI_Status* status = MPI_STATUS_IGNORE) {
68  char* buf = (char*)malloc(size);
69  // Then receive the buffer
70  MPI_Recv(buf, size, MPI_BYTE, source, tag, MPI_COMM_WORLD, status);
71  MPIObject* mpio = new MPIObject(buf, size);
72  T* obj = (T*)(mpio->ReadObject(mpio->GetClass()));
73  free(buf);
74  return obj;
75 }
76 
77 #endif // __ROOTCLING__
78 
79 } // namespace experimental
80 } // namespace bdm
81 
82 #endif // USE_MPI
83 
84 #endif // CORE_MULTI_SIMULATION_MPI_HELPER_H_
bdm
Definition: agent.cc:39
BDM_CLASS_DEF_OVERRIDE
#define BDM_CLASS_DEF_OVERRIDE(class_name, class_version_id)
Definition: root.h:202
root.h
log.h