Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill all trailling whitespaces
[simgrid.git] / src / smpi / smpi_win.cpp
index fb16d16..5d83d8c 100644 (file)
@@ -1,11 +1,17 @@
-/* Copyright (c) 2007-2017. The SimGrid Team.
- * All rights reserved.                                                     */
+/* Copyright (c) 2007-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 "private.h"
-#include <vector>
+#include "src/smpi/private.h"
+#include "src/smpi/smpi_coll.hpp"
+#include "src/smpi/smpi_comm.hpp"
+#include "src/smpi/smpi_datatype.hpp"
+#include "src/smpi/smpi_info.hpp"
+#include "src/smpi/smpi_keyvals.hpp"
+#include "src/smpi/smpi_process.hpp"
+#include "src/smpi/smpi_request.hpp"
+#include "src/smpi/smpi_win.hpp"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(smpi_rma, smpi, "Logging specific to SMPI (RMA operations)");
 
@@ -26,6 +32,7 @@ Win::Win(void *base, MPI_Aint size, int disp_unit, MPI_Info info, MPI_Comm comm,
   requests_ = new std::vector<MPI_Request>();
   mut_=xbt_mutex_init();
   lock_mut_=xbt_mutex_init();
+  atomic_mut_=xbt_mutex_init();
   connected_wins_ = new MPI_Win[comm_size];
   connected_wins_[rank_] = this;
   count_ = 0;
@@ -68,6 +75,7 @@ Win::~Win(){
     MSG_barrier_destroy(bar_);
   xbt_mutex_destroy(mut_);
   xbt_mutex_destroy(lock_mut_);
+  xbt_mutex_destroy(atomic_mut_);
 
   if(allocated_ !=0)
     xbt_free(base_);
@@ -76,7 +84,7 @@ Win::~Win(){
 }
 
 int Win::attach (void *base, MPI_Aint size){
-  if (!(base_ == MPI_BOTTOM || base_ == 0))
+  if (not(base_ == MPI_BOTTOM || base_ == 0))
     return MPI_ERR_ARG;
   base_=0;//actually the address will be given in the RMA calls, as being the disp.
   size_+=size;
@@ -179,7 +187,7 @@ int Win::fence(int assert)
 }
 
 int Win::put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
-              MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype)
+              MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Request* request)
 {
   //get receiver pointer
   MPI_Win recv_win = connected_wins_[target_rank];
@@ -211,24 +219,32 @@ int Win::put( void *origin_addr, int origin_count, MPI_Datatype origin_datatype,
 
     //start send
     sreq->start();
+
+    if(request!=nullptr){
+      *request=sreq;
+    }else{
+      xbt_mutex_acquire(mut_);
+      requests_->push_back(sreq);
+      xbt_mutex_release(mut_);
+    }
+
     //push request to receiver's win
     xbt_mutex_acquire(recv_win->mut_);
     recv_win->requests_->push_back(rreq);
     rreq->start();
     xbt_mutex_release(recv_win->mut_);
-    //push request to sender's win
-    xbt_mutex_acquire(mut_);
-    requests_->push_back(sreq);
-    xbt_mutex_release(mut_);
+
   }else{
     Datatype::copy(origin_addr, origin_count, origin_datatype, recv_addr, target_count, target_datatype);
+    if(request!=nullptr)
+      *request = MPI_REQUEST_NULL;
   }
 
   return MPI_SUCCESS;
 }
 
 int Win::get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
-              MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype)
+              MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Request* request)
 {
   //get sender pointer
   MPI_Win send_win = connected_wins_[target_rank];
@@ -260,7 +276,7 @@ int Win::get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype,
         comm_->group()->index(target_rank), smpi_process()->index(), SMPI_RMA_TAG+2, comm_,
         MPI_OP_NULL);
 
-    //start the send, with another process than us as sender. 
+    //start the send, with another process than us as sender.
     sreq->start();
     //push request to receiver's win
     xbt_mutex_acquire(send_win->mut_);
@@ -269,12 +285,19 @@ int Win::get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype,
 
     //start recv
     rreq->start();
-    //push request to sender's win
-    xbt_mutex_acquire(mut_);
-    requests_->push_back(rreq);
-    xbt_mutex_release(mut_);
+
+    if(request!=nullptr){
+      *request=rreq;
+    }else{
+      xbt_mutex_acquire(mut_);
+      requests_->push_back(rreq);
+      xbt_mutex_release(mut_);
+    }
+
   }else{
     Datatype::copy(send_addr, target_count, target_datatype, origin_addr, origin_count, origin_datatype);
+    if(request!=nullptr)
+      *request=MPI_REQUEST_NULL;
   }
 
   return MPI_SUCCESS;
@@ -282,7 +305,7 @@ int Win::get( void *origin_addr, int origin_count, MPI_Datatype origin_datatype,
 
 
 int Win::accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, int target_rank,
-              MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op)
+              MPI_Aint target_disp, int target_count, MPI_Datatype target_datatype, MPI_Op op, MPI_Request* request)
 {
 
   //get receiver pointer
@@ -297,7 +320,7 @@ int Win::accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_da
     if(locked != 1)
       return MPI_ERR_WIN;
   }
-  //FIXME: local version 
+  //FIXME: local version
 
   if(target_count*target_datatype->get_extent()>recv_win->size_)
     return MPI_ERR_ARG;
@@ -323,17 +346,21 @@ int Win::accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_da
     recv_win->requests_->push_back(rreq);
     rreq->start();
     xbt_mutex_release(recv_win->mut_);
-    //push request to sender's win
-    xbt_mutex_acquire(mut_);
-    requests_->push_back(sreq);
-    xbt_mutex_release(mut_);
+
+    if(request!=nullptr){
+      *request=sreq;
+    }else{
+      xbt_mutex_acquire(mut_);
+      requests_->push_back(sreq);
+      xbt_mutex_release(mut_);
+    }
 
   return MPI_SUCCESS;
 }
 
-int Win::get_accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr, 
-              int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count, 
-              MPI_Datatype target_datatype, MPI_Op op){
+int Win::get_accumulate( void *origin_addr, int origin_count, MPI_Datatype origin_datatype, void *result_addr,
+              int result_count, MPI_Datatype result_datatype, int target_rank, MPI_Aint target_disp, int target_count,
+              MPI_Datatype target_datatype, MPI_Op op, MPI_Request* request){
 
   //get sender pointer
   MPI_Win send_win = connected_wins_[target_rank];
@@ -352,17 +379,54 @@ int Win::get_accumulate( void *origin_addr, int origin_count, MPI_Datatype origi
     return MPI_ERR_ARG;
 
   XBT_DEBUG("Entering MPI_Get_accumulate from %d", target_rank);
-
+  //need to be sure ops are correctly ordered, so finish request here ? slow.
+  MPI_Request req;
+  xbt_mutex_acquire(send_win->atomic_mut_);
   get(result_addr, result_count, result_datatype, target_rank,
-              target_disp, target_count, target_datatype);
+              target_disp, target_count, target_datatype, &req);
+  if (req != MPI_REQUEST_NULL)
+    Request::wait(&req, MPI_STATUS_IGNORE);
   if(op!=MPI_NO_OP)
     accumulate(origin_addr, origin_count, origin_datatype, target_rank,
-              target_disp, target_count, target_datatype, op);
-
+              target_disp, target_count, target_datatype, op, &req);
+  if (req != MPI_REQUEST_NULL)
+    Request::wait(&req, MPI_STATUS_IGNORE);
+  xbt_mutex_release(send_win->atomic_mut_);
   return MPI_SUCCESS;
 
 }
 
+int Win::compare_and_swap(void *origin_addr, void *compare_addr,
+        void *result_addr, MPI_Datatype datatype, int target_rank,
+        MPI_Aint target_disp){
+  //get sender pointer
+  MPI_Win send_win = connected_wins_[target_rank];
+
+  if(opened_==0){//check that post/start has been done
+    // no fence or start .. lock ok ?
+    int locked=0;
+    for(auto it : send_win->lockers_)
+      if (it == comm_->rank())
+        locked = 1;
+    if(locked != 1)
+      return MPI_ERR_WIN;
+  }
+
+  XBT_DEBUG("Entering MPI_Compare_and_swap with %d", target_rank);
+  MPI_Request req;
+  xbt_mutex_acquire(send_win->atomic_mut_);
+  get(result_addr, 1, datatype, target_rank,
+              target_disp, 1, datatype, &req);
+  if (req != MPI_REQUEST_NULL)
+    Request::wait(&req, MPI_STATUS_IGNORE);
+  if (not memcmp(result_addr, compare_addr, datatype->get_extent())) {
+    put(origin_addr, 1, datatype, target_rank,
+              target_disp, 1, datatype);
+  }
+  xbt_mutex_release(send_win->atomic_mut_);
+  return MPI_SUCCESS;
+}
+
 int Win::start(MPI_Group group, int assert){
     /* From MPI forum advices
     The call to MPI_WIN_COMPLETE does not return until the put call has completed at the origin; and the target window
@@ -501,9 +565,6 @@ int Win::wait(){
 }
 
 int Win::lock(int lock_type, int rank, int assert){
-  if(opened_!=0)
-    return MPI_ERR_WIN;
-
   MPI_Win target_win = connected_wins_[rank];
 
   if ((lock_type == MPI_LOCK_EXCLUSIVE && target_win->mode_ != MPI_LOCK_SHARED)|| target_win->mode_ == MPI_LOCK_EXCLUSIVE){
@@ -512,14 +573,15 @@ int Win::lock(int lock_type, int rank, int assert){
     if(lock_type == MPI_LOCK_SHARED){//the window used to be exclusive, it's now shared.
       xbt_mutex_release(target_win->lock_mut_);
    }
-  } else if(!(target_win->mode_==MPI_LOCK_SHARED && lock_type == MPI_LOCK_EXCLUSIVE))
-        target_win->mode_+= lock_type; // don't set to exclusive if it's already shared
+  } else if (not(target_win->mode_ == MPI_LOCK_SHARED && lock_type == MPI_LOCK_EXCLUSIVE))
+    target_win->mode_ += lock_type; // don't set to exclusive if it's already shared
 
   target_win->lockers_.push_back(comm_->rank());
 
-  int finished = finish_comms();
+  int finished = finish_comms(rank);
   XBT_DEBUG("Win_lock %d - Finished %d RMA calls", rank, finished);
-
+  finished = target_win->finish_comms(rank_);
+  XBT_DEBUG("Win_lock target %d - Finished %d RMA calls", rank, finished);
   return MPI_SUCCESS;
 }
 
@@ -535,9 +597,6 @@ int Win::lock_all(int assert){
 }
 
 int Win::unlock(int rank){
-  if(opened_!=0)
-    return MPI_ERR_WIN;
-
   MPI_Win target_win = connected_wins_[rank];
   int target_mode = target_win->mode_;
   target_win->mode_= 0;
@@ -546,9 +605,10 @@ int Win::unlock(int rank){
     xbt_mutex_release(target_win->lock_mut_);
   }
 
-  int finished = finish_comms();
+  int finished = finish_comms(rank);
   XBT_DEBUG("Win_unlock %d - Finished %d RMA calls", rank, finished);
-
+  finished = target_win->finish_comms(rank_);
+  XBT_DEBUG("Win_unlock target %d - Finished %d RMA calls", rank, finished);
   return MPI_SUCCESS;
 }
 
@@ -622,22 +682,21 @@ int Win::finish_comms(int rank){
   int size = static_cast<int>(reqqs->size());
   if (size > 0) {
     size = 0;
-    std::vector<MPI_Request>* myreqqs = new std::vector<MPI_Request>();
+    std::vector<MPI_Request> myreqqs;
     std::vector<MPI_Request>::iterator iter = reqqs->begin();
     while (iter != reqqs->end()){
-      if(((*iter)->src() == rank) || ((*iter)->dst() == rank)){
-          myreqqs->push_back(*iter);
-          iter = reqqs->erase(iter);
-          size++;
+      if(((*iter)!=MPI_REQUEST_NULL) && (((*iter)->src() == rank) || ((*iter)->dst() == rank))){
+        myreqqs.push_back(*iter);
+        iter = reqqs->erase(iter);
+        size++;
       } else {
         ++iter;
       }
     }
     if(size >0){
-      MPI_Request* treqs = &(*myreqqs)[0];
+      MPI_Request* treqs = &myreqqs[0];
       Request::waitall(size, treqs, MPI_STATUSES_IGNORE);
-      myreqqs->clear();
-      delete myreqqs;
+      myreqqs.clear();
     }
   }
   xbt_mutex_release(mut_);