Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[SMPI/PAPI] Make PAPI counters compile again ;)
[simgrid.git] / src / smpi / internals / smpi_process.cpp
index d341b28..661968f 100644 (file)
@@ -1,52 +1,41 @@
-/* Copyright (c) 2009-2017. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2009-2018. The SimGrid Team. All rights reserved.          */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include "smpi_process.hpp"
 #include "mc/mc.h"
-#include "private.hpp"
 #include "smpi_comm.hpp"
-#include "smpi_group.hpp"
 #include "src/mc/mc_replay.hpp"
 #include "src/msg/msg_private.hpp"
 #include "src/simix/smx_private.hpp"
 
-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
-
-extern int* index_to_process_data;
-
-#define MAILBOX_NAME_MAXLEN (5 + sizeof(int) * 2 + 1)
-
-static char *get_mailbox_name(char *str, int index)
-{
-  snprintf(str, MAILBOX_NAME_MAXLEN, "SMPI-%0*x", static_cast<int>(sizeof(int) * 2), static_cast<unsigned>(index));
-  return str;
-}
+#if HAVE_PAPI
+#include "papi.h"
+extern std::string papi_default_config_name;
+#endif
 
-static char *get_mailbox_name_small(char *str, int index)
-{
-  snprintf(str, MAILBOX_NAME_MAXLEN, "small%0*x", static_cast<int>(sizeof(int) * 2), static_cast<unsigned>(index));
-  return str;
-}
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_process, smpi, "Logging specific to SMPI (kernel)");
 
 namespace simgrid{
 namespace smpi{
 
-Process::Process(int index, msg_bar_t finalization_barrier)
-  : finalization_barrier_(finalization_barrier)
+using simgrid::s4u::Actor;
+using simgrid::s4u::ActorPtr;
+
+Process::Process(ActorPtr actor, msg_bar_t finalization_barrier)
+    : finalization_barrier_(finalization_barrier), actor_(actor)
 {
-  char name[MAILBOX_NAME_MAXLEN];
-  mailbox_              = simgrid::s4u::Mailbox::byName(get_mailbox_name(name, index));
-  mailbox_small_        = simgrid::s4u::Mailbox::byName(get_mailbox_name_small(name, index));
-  mailboxes_mutex_      = xbt_mutex_init();
-  timer_                = xbt_os_timer_new();
-  state_                = SMPI_UNINITIALIZED;
+  mailbox_         = simgrid::s4u::Mailbox::by_name("SMPI-" + std::to_string(actor_->get_pid()));
+  mailbox_small_   = simgrid::s4u::Mailbox::by_name("small-" + std::to_string(actor_->get_pid()));
+  mailboxes_mutex_ = xbt_mutex_init();
+  timer_           = xbt_os_timer_new();
+  state_           = SmpiProcessState::UNINITIALIZED;
   if (MC_is_active())
     MC_ignore_heap(timer_, xbt_os_timer_size());
 
 #if HAVE_PAPI
-  if (xbt_cfg_get_string("smpi/papi-events")[0] != '\0') {
+  if (not simgrid::config::get_value<std::string>("smpi/papi-events").empty()) {
     // TODO: Implement host/process/thread based counters. This implementation
     // just always takes the values passed via "default", like this:
     // "default:COUNTER1:COUNTER2:COUNTER3;".
@@ -54,90 +43,90 @@ Process::Process(int index, msg_bar_t finalization_barrier)
     if (it != units2papi_setup.end()) {
       papi_event_set_    = it->second.event_set;
       papi_counter_data_ = it->second.counter_data;
-      XBT_DEBUG("Setting PAPI set for process %i", i);
+      XBT_DEBUG("Setting PAPI set for process %li", actor->get_pid());
     } else {
       papi_event_set_ = PAPI_NULL;
-      XBT_DEBUG("No PAPI set for process %i", i);
+      XBT_DEBUG("No PAPI set for process %li", actor->get_pid());
     }
   }
 #endif
 }
 
-void Process::set_data(int index, int* argc, char*** argv)
+Process::~Process()
 {
-    char* instance_id = (*argv)[1];
-    comm_world_       = smpi_deployment_comm_world(instance_id);
-    msg_bar_t bar     = smpi_deployment_finalization_barrier(instance_id);
-    if (bar != nullptr) // don't overwrite the current one if the instance has none
-      finalization_barrier_ = bar;
-    instance_id_ = instance_id;
-    index_       = index;
-
-    process_                                                       = SIMIX_process_self();
-    static_cast<simgrid::msg::ActorExt*>(process_->userdata)->data = this;
-
-    if (*argc > 3) {
-      memmove(&(*argv)[0], &(*argv)[2], sizeof(char *) * (*argc - 2));
-      (*argv)[(*argc) - 1] = nullptr;
-      (*argv)[(*argc) - 2] = nullptr;
-    }
-    (*argc)-=2;
-    argc_ = argc;
-    argv_ = argv;
-    // set the process attached to the mailbox
-    mailbox_small_->setReceiver(simgrid::s4u::Actor::self());
-    XBT_DEBUG("<%d> New process in the game: %p", index_, process_);
+  if (comm_self_ != MPI_COMM_NULL)
+    simgrid::smpi::Comm::destroy(comm_self_);
+  if (comm_intra_ != MPI_COMM_NULL)
+    simgrid::smpi::Comm::destroy(comm_intra_);
+  xbt_os_timer_free(timer_);
+  xbt_mutex_destroy(mailboxes_mutex_);
+}
+
+void Process::set_data(int* argc, char*** argv)
+{
+  instance_id_      = std::string((*argv)[1]);
+  comm_world_       = smpi_deployment_comm_world(instance_id_);
+  msg_bar_t barrier = smpi_deployment_finalization_barrier(instance_id_);
+  if (barrier != nullptr) // don't overwrite the current one if the instance has none
+    finalization_barrier_ = barrier;
+
+  actor_                                                                        = simgrid::s4u::Actor::self();
+  static_cast<simgrid::msg::ActorExt*>(actor_->get_impl()->get_user_data())->data = this;
+
+  if (*argc > 3) {
+    memmove(&(*argv)[0], &(*argv)[2], sizeof(char*) * (*argc - 2));
+    (*argv)[(*argc) - 1] = nullptr;
+    (*argv)[(*argc) - 2] = nullptr;
+  }
+  (*argc) -= 2;
+  argc_ = argc;
+  argv_ = argv;
+  // set the process attached to the mailbox
+  mailbox_small_->set_receiver(actor_);
+  XBT_DEBUG("<%ld> SMPI process has been initialized: %p", actor_->get_pid(), actor_.get());
 }
 
 /** @brief Prepares the current process for termination. */
 void Process::finalize()
 {
-  state_ = SMPI_FINALIZED;
-  XBT_DEBUG("<%d> Process left the game", index_);
+  state_ = SmpiProcessState::FINALIZED;
+  XBT_DEBUG("<%ld> Process left the game", actor_->get_pid());
 
-    // This leads to an explosion of the search graph which cannot be reduced:
-    if(MC_is_active() || MC_record_replay_is_active())
-      return;
-    // wait for all pending asynchronous comms to finish
-    MSG_barrier_wait(finalization_barrier_);
+  // This leads to an explosion of the search graph which cannot be reduced:
+  if(MC_is_active() || MC_record_replay_is_active())
+    return;
+  // wait for all pending asynchronous comms to finish
+  MSG_barrier_wait(finalization_barrier_);
 }
 
 /** @brief Check if a process is finalized */
 int Process::finalized()
 {
-    if (index_ != MPI_UNDEFINED)
-      return (state_ == SMPI_FINALIZED);
-    else
-      return 0;
+  return (state_ == SmpiProcessState::FINALIZED);
 }
 
 /** @brief Check if a process is initialized */
 int Process::initialized()
 {
-  if (index_to_process_data == nullptr){
-    return false;
-  } else{
-    return ((index_ != MPI_UNDEFINED) && (state_ == SMPI_INITIALIZED));
-  }
+  // TODO cheinrich: Check if we still need this. This should be a global condition, not for a
+  // single process ... ?
+  return (state_ == SmpiProcessState::INITIALIZED);
 }
 
 /** @brief Mark a process as initialized (=MPI_Init called) */
 void Process::mark_as_initialized()
 {
-  if ((index_ != MPI_UNDEFINED) && (state_ != SMPI_FINALIZED))
-    state_ = SMPI_INITIALIZED;
+  if (state_ != SmpiProcessState::FINALIZED)
+    state_ = SmpiProcessState::INITIALIZED;
 }
 
 void Process::set_replaying(bool value){
-  if ((index_ != MPI_UNDEFINED) && (state_ != SMPI_FINALIZED))
+  if (state_ != SmpiProcessState::FINALIZED)
     replaying_ = value;
 }
 
 bool Process::replaying(){
-  if (index_ != MPI_UNDEFINED)
-    return replaying_;
-  else
-    return false;
+  return replaying_;
 }
 
 void Process::set_user_data(void *data)
@@ -150,8 +139,9 @@ void *Process::get_user_data()
   return data_;
 }
 
-smx_actor_t Process::process(){
-  return process_;
+ActorPtr Process::get_actor()
+{
+  return actor_;
 }
 
 /**
@@ -174,11 +164,6 @@ smpi_privatization_region_t Process::privatized_region()
   return privatized_region_;
 }
 
-int Process::index()
-{
-  return index_;
-}
-
 MPI_Comm Process::comm_world()
 {
   return comm_world_==nullptr ? MPI_COMM_NULL : *comm_world_;
@@ -186,12 +171,12 @@ MPI_Comm Process::comm_world()
 
 smx_mailbox_t Process::mailbox()
 {
-  return mailbox_->getImpl();
+  return mailbox_->get_impl();
 }
 
 smx_mailbox_t Process::mailbox_small()
 {
-  return mailbox_small_->getImpl();
+  return mailbox_small_->get_impl();
 }
 
 xbt_mutex_t Process::mailboxes_mutex()
@@ -205,7 +190,7 @@ int Process::papi_event_set()
   return papi_event_set_;
 }
 
-papi_counter_t& smpi_process_papi_counters()
+papi_counter_t& Process::papi_counters()
 {
   return papi_counter_data_;
 }
@@ -231,7 +216,7 @@ MPI_Comm Process::comm_self()
   if(comm_self_==MPI_COMM_NULL){
     MPI_Group group = new  Group(1);
     comm_self_ = new  Comm(group, nullptr);
-    group->set_mapping(index_, 0);
+    group->set_mapping(actor_, 0);
   }
   return comm_self_;
 }
@@ -256,57 +241,45 @@ int Process::sampling()
   return sampling_;
 }
 
-msg_bar_t Process::finalization_barrier(){
-  return finalization_barrier_;
-}
-
-int Process::return_value(){
-  return return_value_;
-}
-
-void Process::set_return_value(int val){
-  return_value_=val;
-}
-
 void Process::init(int *argc, char ***argv){
 
   if (smpi_process_count() == 0) {
     xbt_die("SimGrid was not initialized properly before entering MPI_Init. Aborting, please check compilation process and use smpirun\n");
   }
   if (argc != nullptr && argv != nullptr) {
-    smx_actor_t proc = SIMIX_process_self();
-    proc->context->set_cleanup(&MSG_process_cleanup_from_SIMIX);
-
-    int index = proc->pid - 1; // The maestro process has always ID 0 but we don't need that process here
-
-    if(index_to_process_data == nullptr){
-      index_to_process_data=static_cast<int*>(xbt_malloc(SIMIX_process_count()*sizeof(int)));
-    }
+    simgrid::s4u::ActorPtr proc = simgrid::s4u::Actor::self();
+    proc->get_impl()->context_->set_cleanup(&MSG_process_cleanup_from_SIMIX);
 
     char* instance_id = (*argv)[1];
     try {
       int rank = std::stoi(std::string((*argv)[2]));
-      smpi_deployment_register_process(instance_id, rank, index);
+      smpi_deployment_register_process(instance_id, rank, proc);
     } catch (std::invalid_argument& ia) {
       throw std::invalid_argument(std::string("Invalid rank: ") + (*argv)[2]);
     }
 
     // cheinrich: I'm not sure what the impact of the SMPI_switch_data_segment on this call is. I moved
     // this up here so that I can set the privatized region before the switch.
-    Process* process = smpi_process_remote(index);
-    if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){
+    Process* process = smpi_process_remote(proc);
+    if (smpi_privatize_global_variables == SmpiPrivStrategies::MMAP) {
       /* Now using the segment index of this process  */
-      index = proc->segment_index;
       process->set_privatized_region(smpi_init_global_memory_segment_process());
       /* Done at the process's creation */
-      SMPI_switch_data_segment(index);
+      SMPI_switch_data_segment(proc);
     }
 
-    process->set_data(index, argc, argv);
+    process->set_data(argc, argv);
   }
-  xbt_assert(smpi_process(),
-      "smpi_process() returned nullptr. You probably gave a nullptr parameter to MPI_Init. "
-      "Although it's required by MPI-2, this is currently not supported by SMPI.");
+  xbt_assert(smpi_process(), "smpi_process() returned nullptr. You probably gave a nullptr parameter to MPI_Init. "
+                             "Although it's required by MPI-2, this is currently not supported by SMPI. "
+                             "Please use MPI_Init(&argc, &argv) as usual instead.");
+}
+
+int Process::get_optind(){
+  return optind;
+}
+void Process::set_optind(int new_optind){
+  optind=new_optind;
 }
 
 }