Logo AND Algorithmique Numérique Distribuée

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