Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI/LB] Intercept realloc and calloc calls for memory consumption
[simgrid.git] / src / smpi / plugins / ampi / ampi.cpp
1 /* Copyright (c) 2018.      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/plugins/load_balancer.h>
7 #include <simgrid/s4u/Actor.hpp>
8 #include <src/smpi/include/smpi_comm.hpp>
9 #include <src/smpi/include/smpi_actor.hpp>
10 #include <src/smpi/plugins/ampi/instr_ampi.hpp>
11 #include <src/instr/instr_smpi.hpp>
12 #include <xbt/replay.hpp>
13
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(plugin_pampi, smpi, "Logging specific to the AMPI functions");
15
16 static std::vector<size_t> memory_size(500, 0); // FIXME cheinrich This needs to be dynamic
17 static std::map</*address*/ void*, size_t> alloc_table; // Keep track of all allocations
18 extern "C" XBT_PUBLIC void* _sampi_malloc(size_t); // FIXME Use declarations from sampi.h instead
19 extern "C" XBT_PUBLIC void _sampi_free(void* ptr);
20 extern "C" XBT_PUBLIC void* _sampi_calloc(size_t num_elm, size_t elem_size);
21 extern "C" XBT_PUBLIC void* _sampi_realloc(void* ptr, size_t size);
22 extern "C" void* _sampi_malloc(size_t size)
23 {
24   void* result = malloc (size); // We need the space here to prevent recursive substitution
25   alloc_table.insert({result, size});
26   if (not simgrid::s4u::this_actor::is_maestro()) {
27     memory_size[simgrid::s4u::this_actor::get_pid()] += size;
28   }
29   return result;
30 }
31
32 extern "C" void _sampi_free(void* ptr)
33 {
34   size_t alloc_size = alloc_table.at(ptr);
35   int my_proc_id    = simgrid::s4u::this_actor::get_pid();
36   memory_size[my_proc_id] -= alloc_size;
37   free(ptr);
38 }
39
40 extern "C" void* _sampi_calloc(size_t num_elm, size_t elem_size)
41 {
42   void* result = calloc (num_elm, elem_size); // We need the space here to prevent recursive substitution
43   alloc_table.insert({result, num_elm * elem_size});
44   if (not simgrid::s4u::this_actor::is_maestro()) {
45     memory_size[simgrid::s4u::this_actor::get_pid()] += num_elm * elem_size;
46   }
47   return result;
48 }
49 extern "C" void* _sampi_realloc(void* ptr, size_t size)
50 {
51   void* result = realloc (ptr, size); // We need the space here to prevent recursive substitution
52   int old_size = alloc_table.at(ptr);
53   alloc_table.erase(ptr);
54   alloc_table.insert({result, size});
55   if (not simgrid::s4u::this_actor::is_maestro()) {
56     memory_size[simgrid::s4u::this_actor::get_pid()] += size - old_size;
57   }
58   return result;
59 }
60
61 #include "ampi.hpp"
62 #include <smpi/sampi.h>
63 namespace simgrid {
64 namespace smpi {
65 namespace plugin {
66 namespace ampi {
67   simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> on_iteration_in;
68   simgrid::xbt::signal<void(simgrid::s4u::ActorPtr)> on_iteration_out;
69 }
70 }
71 }
72 }
73
74 /* FIXME The following contains several times "rank() + 1". This works for one
75  * instance, but we need to find a way to deal with this for several instances and
76  * for daemons: If we just replace this with the process id, we will get id's that
77  * don't start at 0 if we start daemons as well.
78  */
79 int APMPI_Iteration_in(MPI_Comm comm)
80 {
81   smpi_bench_end();
82   TRACE_Iteration_in(comm->rank() + 1, new simgrid::instr::NoOpTIData("iteration_in")); // implemented on instr_smpi.c
83   smpi_bench_begin();
84   return 1;
85 }
86
87 int APMPI_Iteration_out(MPI_Comm comm)
88 {
89   smpi_bench_end();
90   TRACE_Iteration_out(comm->rank() + 1, new simgrid::instr::NoOpTIData("iteration_out"));
91   smpi_bench_begin();
92   return 1;
93 }
94
95 void APMPI_Migrate(MPI_Comm comm)
96 {
97   smpi_bench_end();
98   int my_proc_id = simgrid::s4u::this_actor::get_pid();
99   TRACE_migration_call(comm->rank() + 1, new simgrid::instr::AmpiMigrateTIData(memory_size[my_proc_id]));
100   smpi_bench_begin();
101 }
102
103 int AMPI_Iteration_in(MPI_Comm comm)
104 {
105   return APMPI_Iteration_in(comm);
106 }
107
108 int AMPI_Iteration_out(MPI_Comm comm)
109 {
110   return APMPI_Iteration_out(comm);
111 }
112
113 void AMPI_Migrate(MPI_Comm comm)
114 {
115   APMPI_Migrate(comm);
116 }