Logo AND Algorithmique Numérique Distribuée

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