- MPI_Request ref = static_cast<MPI_Request>(a);
- MPI_Request req = static_cast<MPI_Request>(b);
- XBT_DEBUG("Trying to match a send of src %d against %d, tag %d against %d, id %d against %d",ref->src_,req->src_, ref->tag_, req->tag_,ref->comm_->id(),req->comm_->id());
- xbt_assert(ref, "Cannot match send against null reference");
- xbt_assert(req, "Cannot match send against null request");
-
- if((ref->comm_->id()==MPI_UNDEFINED || req->comm_->id() == MPI_UNDEFINED || (ref->comm_->id()==req->comm_->id()))
- && ((req->src_ == MPI_ANY_SOURCE && (req->comm_->group()->rank(ref->src_) != MPI_UNDEFINED)) || req->src_ == ref->src_)
- && ((req->tag_ == MPI_ANY_TAG && ref->tag_ >=0)|| req->tag_ == ref->tag_)){
- if(req->src_ == MPI_ANY_SOURCE)
- req->real_src_ = ref->src_;
- if(req->tag_ == MPI_ANY_TAG)
- req->real_tag_ = ref->tag_;
- if(req->real_size_ < ref->real_size_)
- req->truncated_ = true;
- if (ref->detached_)
- req->detached_sender_=ref; //tie the sender to the receiver, as it is detached and has to be freed in the receiver
- if(req->cancelled_==0)
- req->cancelled_=-1;//mark as uncancellable
- XBT_DEBUG("match succeeded");
- return 1;
- } else
- return 0;
+ auto* ref = static_cast<MPI_Request>(a);
+ auto* req = static_cast<MPI_Request>(b);
+ bool match = match_common(req, req, ref);
+ if (not match || ref->comm_ == MPI_COMM_UNINITIALIZED || ref->comm_->is_smp_comm())
+ return match;
+ auto it = std::find(req->message_id_.begin(), req->message_id_.end(), ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
+ ref->comm_->group()->rank(req->dst_), req->tag_));
+ if (it != req->message_id_.end()) {
+ if (((ref->flags_ & MPI_REQ_PROBE) == 0) && ((req->flags_ & MPI_REQ_PROBE) == 0)) {
+ req->message_id_.erase(it);
+ XBT_DEBUG("increasing count in comm %p, which was %u from pid %ld, to pid %ld with tag %d", ref->comm_,
+ ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
+ ref->comm_->group()->rank(req->dst_), req->tag_),
+ req->src_, req->dst_, req->tag_);
+ ref->comm_->increment_received_messages_count(ref->comm_->group()->rank(req->src_),
+ ref->comm_->group()->rank(req->dst_), req->tag_);
+ if (ref->real_size_ > req->real_size_) {
+ ref->real_size_ = req->real_size_;
+ }
+ }
+ } else {
+ match = false;
+ req->flags_ &= ~MPI_REQ_MATCHED;
+ ref->detached_sender_ = nullptr;
+ XBT_DEBUG("Refusing to match message, as its ID is not the one I expect. in comm %p, %u, "
+ "from pid %ld to pid %ld, with tag %d",
+ ref->comm_,
+ ref->comm_->get_received_messages_count(ref->comm_->group()->rank(req->src_),
+ ref->comm_->group()->rank(req->dst_), req->tag_),
+ req->src_, req->dst_, req->tag_);
+ }
+ return match;