Logo AND Algorithmique Numérique Distribuée

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