Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / smpi / smpi_datatype.cpp
index 877b40e..d93fc2d 100644 (file)
@@ -1,39 +1,29 @@
-/* smpi_datatype.cpp -- MPI primitives to handle datatypes                      */
-/* Copyright (c) 2009-2015. The SimGrid Team.
- * All rights reserved.                                                     */
+/* smpi_datatype.cpp -- MPI primitives to handle datatypes                  */
+/* Copyright (c) 2009-2017. 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 "mc/mc.h"
-#include "private.h"
 #include "simgrid/modelchecker.h"
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <string>
-#include <unordered_map>
-#include <xbt/ex.hpp>
+#include "src/smpi/private.h"
+#include "src/smpi/smpi_datatype_derived.hpp"
+#include "src/smpi/smpi_op.hpp"
+#include "src/smpi/smpi_process.hpp"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_datatype, smpi, "Logging specific to SMPI (datatype)");
 
-std::unordered_map<int, smpi_type_key_elem> smpi_type_keyvals;
-int type_keyval_id=0;//avoid collisions
-
-
 #define CREATE_MPI_DATATYPE(name, type)               \
-  static Datatype mpi_##name (         \
+  static simgrid::smpi::Datatype mpi_##name (         \
     (char*) # name,                                   \
     sizeof(type),   /* size */                        \
     0,              /* lb */                          \
     sizeof(type),   /* ub = lb + size */              \
-    DT_FLAG_BASIC  /* flags */                       \
+    DT_FLAG_BASIC  /* flags */                        \
   );                                                  \
 const MPI_Datatype name = &mpi_##name;
 
 #define CREATE_MPI_DATATYPE_NULL(name)                \
-  static Datatype mpi_##name (         \
+  static simgrid::smpi::Datatype mpi_##name (         \
     (char*) # name,                                   \
     0,              /* size */                        \
     0,              /* lb */                          \
@@ -107,46 +97,46 @@ CREATE_MPI_DATATYPE(MPI_PTR, void*);
 namespace simgrid{
 namespace smpi{
 
-MPI_Datatype Datatype::null_id_=MPI_DATATYPE_NULL;
+std::unordered_map<int, smpi_key_elem> Datatype::keyvals_;
+int Datatype::keyval_id_=0;
 
-Datatype::Datatype(int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(nullptr), size_(size), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), refcount_(1){
-#if HAVE_MC
+Datatype::Datatype(int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(nullptr), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(1){
+#if SIMGRID_HAVE_MC
   if(MC_is_active())
     MC_ignore(&(refcount_), sizeof(refcount_));
 #endif
 }
 
 //for predefined types, so in_use = 0.
-Datatype::Datatype(char* name, int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(name), size_(size), lb_(lb), ub_(ub), flags_(flags), attributes_(nullptr), refcount_(0){
-#if HAVE_MC
+Datatype::Datatype(char* name, int size,MPI_Aint lb, MPI_Aint ub, int flags) : name_(name), size_(size), lb_(lb), ub_(ub), flags_(flags), refcount_(0){
+#if SIMGRID_HAVE_MC
   if(MC_is_active())
     MC_ignore(&(refcount_), sizeof(refcount_));
 #endif
 }
 
-Datatype::Datatype(Datatype *datatype, int* ret) : name_(nullptr), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), attributes_(nullptr), refcount_(1)
+Datatype::Datatype(Datatype *datatype, int* ret) : name_(nullptr), lb_(datatype->lb_), ub_(datatype->ub_), flags_(datatype->flags_), refcount_(1)
 {
   flags_ &= ~DT_FLAG_PREDEFINED;
   *ret = MPI_SUCCESS;
   if(datatype->name_)
     name_ = xbt_strdup(datatype->name_);
-  if(datatype->attributes_ !=nullptr){
-    attributes_ = xbt_dict_new_homogeneous(nullptr);
-    xbt_dict_cursor_t cursor = nullptr;
-    char* key;
+
+  if (not datatype->attributes()->empty()) {
     int flag;
-    void* value_in;
     void* value_out;
-    xbt_dict_foreach (datatype->attributes_, cursor, key, value_in) {
-      smpi_type_key_elem elem = smpi_type_keyvals.at(atoi(key));
-      if (elem != nullptr && elem->copy_fn != MPI_NULL_COPY_FN) {
-        *ret = elem->copy_fn(datatype, atoi(key), nullptr, value_in, &value_out, &flag);
+    for(auto it = datatype->attributes()->begin(); it != datatype->attributes()->end(); it++){
+      smpi_key_elem elem = keyvals_.at((*it).first);
+
+      if (elem != nullptr && elem->copy_fn.type_copy_fn != MPI_NULL_COPY_FN) {
+        *ret = elem->copy_fn.type_copy_fn(datatype, (*it).first, nullptr, (*it).second, &value_out, &flag);
         if (*ret != MPI_SUCCESS) {
-          xbt_dict_cursor_free(&cursor);
           break;
         }
-        if (flag)
-          xbt_dict_set_ext(attributes_, key, sizeof(int), value_out, nullptr);
+        if (flag){
+          elem->refcount++;
+          attributes()->insert({(*it).first, value_out});
+        }
       }
     }
   }
@@ -164,18 +154,7 @@ Datatype::~Datatype(){
       return;
   }
 
-  if(attributes_ !=nullptr){
-    xbt_dict_cursor_t cursor = nullptr;
-    char* key;
-    void * value;
-    int flag;
-    xbt_dict_foreach(attributes_, cursor, key, value){
-      smpi_type_key_elem elem = smpi_type_keyvals.at(atoi(key));
-      if(elem!=nullptr && elem->delete_fn!=nullptr)
-        elem->delete_fn(this,*key, value, &flag);
-    }
-    xbt_dict_free(&attributes_);
-  }
+  cleanup_attr<Datatype>();
 
   xbt_free(name_);
 }
@@ -185,7 +164,7 @@ void Datatype::ref(){
 
   refcount_++;
 
-#if HAVE_MC
+#if SIMGRID_HAVE_MC
   if(MC_is_active())
     MC_ignore(&(refcount_), sizeof(refcount_));
 #endif
@@ -196,10 +175,10 @@ void Datatype::unref(MPI_Datatype datatype)
   if (datatype->refcount_ > 0)
     datatype->refcount_--;
 
-  if (datatype->refcount_ == 0  && !(datatype->flags_ & DT_FLAG_PREDEFINED))
+  if (datatype->refcount_ == 0 && not(datatype->flags_ & DT_FLAG_PREDEFINED))
     delete datatype;
 
-#if HAVE_MC
+#if SIMGRID_HAVE_MC
   if(MC_is_active())
     MC_ignore(&(datatype->refcount_), sizeof(datatype->refcount_));
 #endif
@@ -223,6 +202,10 @@ int Datatype::flags(){
   return flags_;
 }
 
+int Datatype::refcount(){
+  return refcount_;
+}
+
 void Datatype::addflag(int flag){
   flags_ &= flag;
 }
@@ -261,88 +244,6 @@ void Datatype::set_name(char* name){
   name_ = xbt_strdup(name);
 }
 
-int Datatype::attr_delete(int keyval){
-  smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
-  if(elem==nullptr)
-    return MPI_ERR_ARG;
-  if(elem->delete_fn!=MPI_NULL_DELETE_FN){
-    void * value = nullptr;
-    int flag;
-    if(this->attr_get(keyval, &value, &flag)==MPI_SUCCESS){
-      int ret = elem->delete_fn(this, keyval, value, &flag);
-      if(ret!=MPI_SUCCESS) 
-        return ret;
-    }
-  }  
-  if(attributes_==nullptr)
-    return MPI_ERR_ARG;
-
-  xbt_dict_remove_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
-  return MPI_SUCCESS;
-}
-
-
-int Datatype::attr_get(int keyval, void* attr_value, int* flag){
-  smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
-  if(elem==nullptr)
-    return MPI_ERR_ARG;
-  if(attributes_==nullptr){
-    *flag=0;
-    return MPI_SUCCESS;
-  }
-  try {
-    *static_cast<void**>(attr_value) = xbt_dict_get_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int));
-    *flag=1;
-  }
-  catch (xbt_ex& ex) {
-    *flag=0;
-  }
-  return MPI_SUCCESS;
-}
-
-int Datatype::attr_put(int keyval, void* attr_value){
-  smpi_type_key_elem elem = smpi_type_keyvals.at(keyval);
-  if(elem==nullptr)
-    return MPI_ERR_ARG;
-  int flag;
-  void* value = nullptr;
-  this->attr_get(keyval, &value, &flag);
-  if(flag!=0 && elem->delete_fn!=MPI_NULL_DELETE_FN){
-    int ret = elem->delete_fn(this, keyval, value, &flag);
-    if(ret!=MPI_SUCCESS) 
-      return ret;
-  }
-  if(attributes_==nullptr)
-    attributes_ = xbt_dict_new_homogeneous(nullptr);
-
-  xbt_dict_set_ext(attributes_, reinterpret_cast<const char*>(&keyval), sizeof(int), attr_value, nullptr);
-  return MPI_SUCCESS;
-}
-
-int Datatype::keyval_create(MPI_Type_copy_attr_function* copy_fn, MPI_Type_delete_attr_function* delete_fn, int* keyval, void* extra_state){
-
-  smpi_type_key_elem value = (smpi_type_key_elem) xbt_new0(s_smpi_mpi_type_key_elem_t,1);
-
-  value->copy_fn=copy_fn;
-  value->delete_fn=delete_fn;
-
-  *keyval = type_keyval_id;
-  smpi_type_keyvals.insert({*keyval, value});
-  type_keyval_id++;
-  return MPI_SUCCESS;
-}
-
-int Datatype::keyval_free(int* keyval){
-  smpi_type_key_elem elem = smpi_type_keyvals.at(*keyval);
-  if(elem==0){
-    return MPI_ERR_ARG;
-  }
-  smpi_type_keyvals.erase(*keyval);
-  xbt_free(elem);
-  return MPI_SUCCESS;
-}
-
-
 int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* position,MPI_Comm comm){
   if (outcount - *position < incount*static_cast<int>(size_))
     return MPI_ERR_BUFFER;
@@ -352,7 +253,7 @@ int Datatype::pack(void* inbuf, int incount, void* outbuf, int outcount, int* po
 }
 
 int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int outcount,MPI_Comm comm){
-  if (outcount*(int)size_> insize)
+  if (outcount*static_cast<int>(size_)> insize)
     return MPI_ERR_BUFFER;
   Datatype::copy(static_cast<char*>(inbuf) + *position, insize, MPI_CHAR, outbuf, outcount, this);
   *position += outcount * size_;
@@ -362,26 +263,24 @@ int Datatype::unpack(void* inbuf, int insize, int* position, void* outbuf, int o
 
 int Datatype::copy(void *sendbuf, int sendcount, MPI_Datatype sendtype,
                        void *recvbuf, int recvcount, MPI_Datatype recvtype){
-  int count;
-  if(smpi_privatize_global_variables){
-    smpi_switch_data_segment(smpi_process_index());
+
+// FIXME Handle the case of a partial shared malloc.
+
+  if(smpi_privatize_global_variables == SMPI_PRIVATIZE_MMAP){
+    smpi_switch_data_segment(smpi_process()->index());
   }
   /* First check if we really have something to do */
   if (recvcount > 0 && recvbuf != sendbuf) {
     sendcount *= sendtype->size();
     recvcount *= recvtype->size();
-    count = sendcount < recvcount ? sendcount : recvcount;
+    int count = sendcount < recvcount ? sendcount : recvcount;
 
-    if(!(sendtype->flags() & DT_FLAG_DERIVED) && !(recvtype->flags() & DT_FLAG_DERIVED)) {
-      if(!smpi_process_get_replaying()) 
+    if (not(sendtype->flags() & DT_FLAG_DERIVED) && not(recvtype->flags() & DT_FLAG_DERIVED)) {
+      if (not smpi_process()->replaying())
         memcpy(recvbuf, sendbuf, count);
-    }
-    else if (!(sendtype->flags() & DT_FLAG_DERIVED))
-    {
+    } else if (not(sendtype->flags() & DT_FLAG_DERIVED)) {
       recvtype->unserialize( sendbuf, recvbuf, recvcount/recvtype->size(), MPI_REPLACE);
-    }
-    else if (!(recvtype->flags() & DT_FLAG_DERIVED))
-    {
+    } else if (not(recvtype->flags() & DT_FLAG_DERIVED)) {
       sendtype->serialize(sendbuf, recvbuf, sendcount/sendtype->size());
     }else{
 
@@ -429,7 +328,7 @@ int Datatype::create_contiguous(int count, MPI_Datatype old_type, MPI_Aint lb, M
 int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatype old_type, MPI_Datatype* new_type)
 {
   int retval;
-  if (block_length<0) 
+  if (block_length<0)
     return MPI_ERR_ARG;
   MPI_Aint lb = 0;
   MPI_Aint ub = 0;
@@ -454,7 +353,7 @@ int Datatype::create_vector(int count, int block_length, int stride, MPI_Datatyp
 int Datatype::create_hvector(int count, int block_length, MPI_Aint stride, MPI_Datatype old_type, MPI_Datatype* new_type)
 {
   int retval;
-  if (block_length<0) 
+  if (block_length<0)
     return MPI_ERR_ARG;
   MPI_Aint lb = 0;
   MPI_Aint ub = 0;
@@ -500,7 +399,7 @@ int Datatype::create_indexed(int count, int* block_lengths, int* indices, MPI_Da
   if(old_type->flags_ & DT_FLAG_DERIVED)
     contiguous=false;
 
-  if(!contiguous){
+  if (not contiguous) {
     *new_type = new Type_Indexed(size * old_type->size(),lb,ub,
                                  DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
   }else{
@@ -523,9 +422,9 @@ int Datatype::create_hindexed(int count, int* block_lengths, MPI_Aint* indices,
       return MPI_ERR_ARG;
     size += block_lengths[i];
 
-    if(indices[i]+old_type->lb()<lb) 
+    if(indices[i]+old_type->lb()<lb)
       lb = indices[i]+old_type->lb();
-    if(indices[i]+block_lengths[i]*old_type->ub()>ub) 
+    if(indices[i]+block_lengths[i]*old_type->ub()>ub)
       ub = indices[i]+block_lengths[i]*old_type->ub();
 
     if ( (i< count -1) && (indices[i]+block_lengths[i]*(static_cast<int>(old_type->size())) != indices[i+1]) )
@@ -534,7 +433,7 @@ int Datatype::create_hindexed(int count, int* block_lengths, MPI_Aint* indices,
   if (old_type->flags_ & DT_FLAG_DERIVED || lb!=0)
     contiguous=false;
 
-  if(!contiguous){
+  if (not contiguous) {
     *new_type = new Type_Hindexed(size * old_type->size(),lb,ub,
                                    DT_FLAG_DERIVED|DT_FLAG_DATA, count, block_lengths, indices, old_type);
   }else{
@@ -571,16 +470,16 @@ int Datatype::create_struct(int count, int* block_lengths, MPI_Aint* indices, MP
       forced_ub=true;
     }
 
-    if(!forced_lb && indices[i]+old_types[i]->lb()<lb) 
+    if (not forced_lb && indices[i] + old_types[i]->lb() < lb)
       lb = indices[i];
-    if(!forced_ub &&  indices[i]+block_lengths[i]*old_types[i]->ub()>ub)
+    if (not forced_ub && indices[i] + block_lengths[i] * old_types[i]->ub() > ub)
       ub = indices[i]+block_lengths[i]*old_types[i]->ub();
 
     if ( (i< count -1) && (indices[i]+block_lengths[i]*static_cast<int>(old_types[i]->size()) != indices[i+1]) )
       contiguous=false;
   }
-  if(!contiguous){
-    *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA, 
+  if (not contiguous) {
+    *new_type = new Type_Struct(size, lb,ub, DT_FLAG_DERIVED|DT_FLAG_DATA,
                                 count, block_lengths, indices, old_types);
   }else{
     Datatype::create_contiguous(size, MPI_CHAR, lb, new_type);