1 /* Copyright (c) 2018-2019. The SimGrid Team. All rights reserved. */
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. */
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>
15 #include <smpi/sampi.h>
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(plugin_pampi, smpi, "Logging specific to the AMPI functions");
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)
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;
35 extern "C" void _sampi_free(void* ptr)
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;
43 extern "C" void* _sampi_calloc(size_t num_elm, size_t elem_size)
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;
52 extern "C" void* _sampi_realloc(void* ptr, size_t size)
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;
68 simgrid::xbt::signal<void(simgrid::s4u::Actor const&)> on_iteration_in;
69 simgrid::xbt::signal<void(simgrid::s4u::Actor const&)> on_iteration_out;
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.
80 int APMPI_Iteration_in(MPI_Comm comm)
83 TRACE_Iteration_in(comm->rank() + 1, new simgrid::instr::NoOpTIData("iteration_in")); // implemented on instr_smpi.c
88 int APMPI_Iteration_out(MPI_Comm comm)
91 TRACE_Iteration_out(comm->rank() + 1, new simgrid::instr::NoOpTIData("iteration_out"));
96 void APMPI_Migrate(MPI_Comm comm)
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]));
104 int AMPI_Iteration_in(MPI_Comm comm)
106 return APMPI_Iteration_in(comm);
109 int AMPI_Iteration_out(MPI_Comm comm)
111 return APMPI_Iteration_out(comm);
114 void AMPI_Migrate(MPI_Comm comm)