Logo AND Algorithmique Numérique Distribuée

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