Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / src / smpi / plugins / ampi / ampi.cpp
1 /* Copyright (c) 2018-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include <simgrid/s4u/Actor.hpp>
7 #include <src/instr/instr_smpi.hpp>
8 #include <src/smpi/include/smpi_actor.hpp>
9 #include <src/smpi/include/smpi_comm.hpp>
10 #include <src/smpi/plugins/ampi/instr_ampi.hpp>
11 #include <xbt/replay.hpp>
12 #include <xbt/sysdep.h>
13
14 #include "ampi.hpp"
15 #include <smpi/sampi.h>
16
17 class AllocTracker {
18   std::vector<size_t> memory_size_;
19   std::map</*address*/ void*, size_t> alloc_table_; // Keep track of all allocations
20
21 public:
22   void record(void* ptr, size_t size)
23   {
24     alloc_table_.emplace(ptr, size);
25     if (not simgrid::s4u::this_actor::is_maestro()) {
26       auto actor = static_cast<size_t>(simgrid::s4u::this_actor::get_pid());
27       if (actor >= memory_size_.size())
28         memory_size_.resize(actor + 1, 0);
29       memory_size_[actor] += size;
30     }
31   }
32
33   void forget(void* ptr)
34   {
35     auto elm = alloc_table_.find(ptr);
36     xbt_assert(elm != alloc_table_.end());
37     if (not simgrid::s4u::this_actor::is_maestro()) {
38       auto actor = static_cast<size_t>(simgrid::s4u::this_actor::get_pid());
39       memory_size_.at(actor) -= elm->second; // size
40     }
41     alloc_table_.erase(elm);
42   }
43
44   size_t summary() const
45   {
46     if (simgrid::s4u::this_actor::is_maestro())
47       return 0;
48     auto actor = static_cast<size_t>(simgrid::s4u::this_actor::get_pid());
49     return actor < memory_size_.size() ? memory_size_[actor] : 0;
50   }
51 };
52
53 static AllocTracker alloc_tracker;
54
55 extern "C" void* _sampi_malloc(size_t size)
56 {
57   void* result = xbt_malloc(size);
58   alloc_tracker.record(result, size);
59   return result;
60 }
61
62 extern "C" void _sampi_free(void* ptr)
63 {
64   alloc_tracker.forget(ptr);
65   xbt_free(ptr);
66 }
67
68 extern "C" void* _sampi_calloc(size_t num_elm, size_t elem_size)
69 {
70   size_t size  = num_elm * elem_size;
71   void* result = xbt_malloc0(size);
72   alloc_tracker.record(result, size);
73   return result;
74 }
75 extern "C" void* _sampi_realloc(void* ptr, size_t size)
76 {
77   alloc_tracker.forget(ptr);
78   void* result = xbt_realloc(ptr, size);
79   alloc_tracker.record(result, size);
80   return result;
81 }
82
83 namespace simgrid::smpi::plugin::ampi {
84 xbt::signal<void(s4u::Actor const&)> on_iteration_in;
85 xbt::signal<void(s4u::Actor const&)> on_iteration_out;
86 } // namespace simgrid::smpi::plugin::ampi
87
88 /* FIXME The following contains several times "rank() + 1". This works for one
89  * instance, but we need to find a way to deal with this for several instances and
90  * for daemons: If we just replace this with the process id, we will get id's that
91  * don't start at 0 if we start daemons as well.
92  */
93 int APMPI_Iteration_in(MPI_Comm comm)
94 {
95   const SmpiBenchGuard suspend_bench;
96   TRACE_Iteration_in(comm->rank() + 1, new simgrid::instr::NoOpTIData("iteration_in")); // implemented on instr_smpi.c
97   return 1;
98 }
99
100 int APMPI_Iteration_out(MPI_Comm comm)
101 {
102   const SmpiBenchGuard suspend_bench;
103   TRACE_Iteration_out(comm->rank() + 1, new simgrid::instr::NoOpTIData("iteration_out"));
104   return 1;
105 }
106
107 void APMPI_Migrate(MPI_Comm comm)
108 {
109   const SmpiBenchGuard suspend_bench;
110   TRACE_migration_call(comm->rank() + 1, new simgrid::instr::AmpiMigrateTIData(alloc_tracker.summary()));
111 }
112
113 int AMPI_Iteration_in(MPI_Comm comm)
114 {
115   return APMPI_Iteration_in(comm);
116 }
117
118 int AMPI_Iteration_out(MPI_Comm comm)
119 {
120   return APMPI_Iteration_out(comm);
121 }
122
123 void AMPI_Migrate(MPI_Comm comm)
124 {
125   APMPI_Migrate(comm);
126 }