Logo AND Algorithmique Numérique Distribuée

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