Blame src/mpi/pt2pt/recv.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *
Packit 0848f5
 *  (C) 2001 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
#include "mpiimpl.h"
Packit 0848f5
Packit 0848f5
/* -- Begin Profiling Symbol Block for routine MPI_Recv */
Packit 0848f5
#if defined(HAVE_PRAGMA_WEAK)
Packit 0848f5
#pragma weak MPI_Recv = PMPI_Recv
Packit 0848f5
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
Packit 0848f5
#pragma _HP_SECONDARY_DEF PMPI_Recv  MPI_Recv
Packit 0848f5
#elif defined(HAVE_PRAGMA_CRI_DUP)
Packit 0848f5
#pragma _CRI duplicate MPI_Recv as PMPI_Recv
Packit 0848f5
#elif defined(HAVE_WEAK_ATTRIBUTE)
Packit 0848f5
int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag,
Packit 0848f5
             MPI_Comm comm, MPI_Status *status) __attribute__((weak,alias("PMPI_Recv")));
Packit 0848f5
#endif
Packit 0848f5
/* -- End Profiling Symbol Block */
Packit 0848f5
Packit 0848f5
/* Define MPICH_MPI_FROM_PMPI if weak symbols are not supported to build
Packit 0848f5
   the MPI routines */
Packit 0848f5
#ifndef MPICH_MPI_FROM_PMPI
Packit 0848f5
#undef MPI_Recv
Packit 0848f5
#define MPI_Recv PMPI_Recv
Packit 0848f5
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
#undef FUNCNAME
Packit 0848f5
#define FUNCNAME MPI_Recv
Packit 0848f5
Packit 0848f5
/*@
Packit 0848f5
    MPI_Recv - Blocking receive for a message
Packit 0848f5
Packit 0848f5
Output Parameters:
Packit 0848f5
+ buf - initial address of receive buffer (choice) 
Packit 0848f5
- status - status object (Status) 
Packit 0848f5
Packit 0848f5
Input Parameters:
Packit 0848f5
+ count - maximum number of elements in receive buffer (integer) 
Packit 0848f5
. datatype - datatype of each receive buffer element (handle) 
Packit 0848f5
. source - rank of source (integer) 
Packit 0848f5
. tag - message tag (integer) 
Packit 0848f5
- comm - communicator (handle) 
Packit 0848f5
Packit 0848f5
Notes:
Packit 0848f5
The 'count' argument indicates the maximum length of a message; the actual 
Packit 0848f5
length of the message can be determined with 'MPI_Get_count'.  
Packit 0848f5
Packit 0848f5
.N ThreadSafe
Packit 0848f5
Packit 0848f5
.N Fortran
Packit 0848f5
Packit 0848f5
.N FortranStatus
Packit 0848f5
Packit 0848f5
.N Errors
Packit 0848f5
.N MPI_SUCCESS
Packit 0848f5
.N MPI_ERR_COMM
Packit 0848f5
.N MPI_ERR_TYPE
Packit 0848f5
.N MPI_ERR_COUNT
Packit 0848f5
.N MPI_ERR_TAG
Packit 0848f5
.N MPI_ERR_RANK
Packit 0848f5
Packit 0848f5
@*/
Packit 0848f5
int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag,
Packit 0848f5
	     MPI_Comm comm, MPI_Status *status)
Packit 0848f5
{
Packit 0848f5
    static const char FCNAME[] = "MPI_Recv";
Packit 0848f5
    int mpi_errno = MPI_SUCCESS;
Packit 0848f5
    MPID_Comm *comm_ptr = NULL;
Packit 0848f5
    MPID_Request * request_ptr = NULL;
Packit 0848f5
    MPID_MPI_STATE_DECL(MPID_STATE_MPI_RECV);
Packit 0848f5
Packit 0848f5
    MPIR_ERRTEST_INITIALIZED_ORDIE();
Packit 0848f5
    
Packit 0848f5
    MPID_THREAD_CS_ENTER(GLOBAL, MPIR_THREAD_GLOBAL_ALLFUNC_MUTEX);
Packit 0848f5
    MPID_MPI_PT2PT_FUNC_ENTER_BACK(MPID_STATE_MPI_RECV);
Packit 0848f5
    
Packit 0848f5
    /* Validate handle parameters needing to be converted */
Packit 0848f5
#   ifdef HAVE_ERROR_CHECKING
Packit 0848f5
    {
Packit 0848f5
        MPID_BEGIN_ERROR_CHECKS;
Packit 0848f5
        {
Packit 0848f5
	    MPIR_ERRTEST_COMM(comm, mpi_errno);
Packit 0848f5
	    /* NOTE: MPI_STATUS_IGNORE != NULL */
Packit 0848f5
	    MPIR_ERRTEST_ARGNULL(status, "status", mpi_errno);
Packit 0848f5
	}
Packit 0848f5
        MPID_END_ERROR_CHECKS;
Packit 0848f5
    }
Packit 0848f5
    
Packit 0848f5
#   endif /* HAVE_ERROR_CHECKING */
Packit 0848f5
    
Packit 0848f5
    /* Convert MPI object handles to object pointers */
Packit 0848f5
    MPID_Comm_get_ptr( comm, comm_ptr );
Packit 0848f5
Packit 0848f5
    /* Validate parameters if error checking is enabled */
Packit 0848f5
#   ifdef HAVE_ERROR_CHECKING
Packit 0848f5
    {
Packit 0848f5
        MPID_BEGIN_ERROR_CHECKS;
Packit 0848f5
        {
Packit 0848f5
            MPID_Comm_valid_ptr( comm_ptr, mpi_errno, FALSE );
Packit 0848f5
            if (mpi_errno) goto fn_fail;
Packit 0848f5
	    
Packit 0848f5
	    MPIR_ERRTEST_COUNT(count, mpi_errno);
Packit 0848f5
	    MPIR_ERRTEST_RECV_RANK(comm_ptr, source, mpi_errno);
Packit 0848f5
	    MPIR_ERRTEST_RECV_TAG(tag, mpi_errno);
Packit 0848f5
	    
Packit 0848f5
	    /* Validate datatype handle */
Packit 0848f5
	    MPIR_ERRTEST_DATATYPE(datatype, "datatype", mpi_errno);
Packit 0848f5
	    
Packit 0848f5
	    /* Validate datatype object */
Packit 0848f5
	    if (HANDLE_GET_KIND(datatype) != HANDLE_KIND_BUILTIN)
Packit 0848f5
	    {
Packit 0848f5
		MPID_Datatype *datatype_ptr = NULL;
Packit 0848f5
Packit 0848f5
		MPID_Datatype_get_ptr(datatype, datatype_ptr);
Packit 0848f5
		MPID_Datatype_valid_ptr(datatype_ptr, mpi_errno);
Packit 0848f5
		if (mpi_errno) goto fn_fail;
Packit 0848f5
		MPID_Datatype_committed_ptr(datatype_ptr, mpi_errno);
Packit 0848f5
		if (mpi_errno) goto fn_fail;
Packit 0848f5
	    }
Packit 0848f5
	    
Packit 0848f5
	    /* Validate buffer */
Packit 0848f5
	    MPIR_ERRTEST_USERBUFFER(buf,count,datatype,mpi_errno);
Packit 0848f5
        }
Packit 0848f5
        MPID_END_ERROR_CHECKS;
Packit 0848f5
    }
Packit 0848f5
#   endif /* HAVE_ERROR_CHECKING */
Packit 0848f5
Packit 0848f5
    /* ... body of routine ...  */
Packit 0848f5
Packit 0848f5
    /* MT: Note that MPID_Recv may release the SINGLE_CS if it
Packit 0848f5
       decides to block internally.  MPID_Recv in that case will
Packit 0848f5
       re-aquire the SINGLE_CS before returnning */
Packit 0848f5
    mpi_errno = MPID_Recv(buf, count, datatype, source, tag, comm_ptr, 
Packit 0848f5
			  MPID_CONTEXT_INTRA_PT2PT, status, &request_ptr);
Packit 0848f5
    if (mpi_errno != MPI_SUCCESS) goto fn_fail;
Packit 0848f5
Packit 0848f5
    if (request_ptr == NULL)
Packit 0848f5
    {
Packit 0848f5
	goto fn_exit;
Packit 0848f5
    }
Packit 0848f5
    
Packit 0848f5
    /* If a request was returned, then we need to block until the request is 
Packit 0848f5
       complete */
Packit 0848f5
    if (!MPID_Request_is_complete(request_ptr))
Packit 0848f5
    {
Packit 0848f5
	MPID_Progress_state progress_state;
Packit 0848f5
	    
Packit 0848f5
	MPID_Progress_start(&progress_state);
Packit 0848f5
        while (!MPID_Request_is_complete(request_ptr))
Packit 0848f5
	{
Packit 0848f5
	    /* MT: Progress_wait may release the SINGLE_CS while it
Packit 0848f5
	       waits */
Packit 0848f5
	    mpi_errno = MPID_Progress_wait(&progress_state);
Packit 0848f5
	    if (mpi_errno != MPI_SUCCESS)
Packit 0848f5
	    { 
Packit 0848f5
		/* --BEGIN ERROR HANDLING-- */
Packit 0848f5
		MPID_Progress_end(&progress_state);
Packit 0848f5
		goto fn_fail;
Packit 0848f5
		/* --END ERROR HANDLING-- */
Packit 0848f5
	    }
Packit 0848f5
Packit 0848f5
            if (unlikely(MPIR_CVAR_ENABLE_FT &&
Packit 0848f5
                        !MPID_Request_is_complete(request_ptr) &&
Packit 0848f5
                        MPID_Request_is_anysource(request_ptr) &&
Packit 0848f5
                        !MPID_Comm_AS_enabled(request_ptr->comm))) {
Packit 0848f5
                /* --BEGIN ERROR HANDLING-- */
Packit 0848f5
                MPID_Cancel_recv(request_ptr);
Packit 0848f5
                MPIR_STATUS_SET_CANCEL_BIT(request_ptr->status, FALSE);
Packit 0848f5
                MPIR_ERR_SET(request_ptr->status.MPI_ERROR, MPIX_ERR_PROC_FAILED, "**proc_failed");
Packit 0848f5
                mpi_errno = request_ptr->status.MPI_ERROR;
Packit 0848f5
                goto fn_fail;
Packit 0848f5
                /* --END ERROR HANDLING-- */
Packit 0848f5
            }
Packit 0848f5
	}
Packit 0848f5
	MPID_Progress_end(&progress_state);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    mpi_errno = request_ptr->status.MPI_ERROR;
Packit 0848f5
    MPIR_Request_extract_status(request_ptr, status);
Packit 0848f5
    MPID_Request_release(request_ptr);
Packit 0848f5
Packit 0848f5
    if (mpi_errno != MPI_SUCCESS) goto fn_fail;
Packit 0848f5
Packit 0848f5
    /* ... end of body of routine ... */
Packit 0848f5
    
Packit 0848f5
  fn_exit:
Packit 0848f5
    MPID_MPI_PT2PT_FUNC_EXIT_BACK(MPID_STATE_MPI_RECV);
Packit 0848f5
    MPID_THREAD_CS_EXIT(GLOBAL, MPIR_THREAD_GLOBAL_ALLFUNC_MUTEX);
Packit 0848f5
    return mpi_errno;
Packit 0848f5
Packit 0848f5
  fn_fail:
Packit 0848f5
    /* --BEGIN ERROR HANDLING-- */
Packit 0848f5
#   ifdef HAVE_ERROR_CHECKING
Packit 0848f5
    {
Packit 0848f5
	mpi_errno = MPIR_Err_create_code(
Packit 0848f5
	    mpi_errno, MPIR_ERR_RECOVERABLE, FCNAME, __LINE__, MPI_ERR_OTHER, "**mpi_recv",
Packit 0848f5
	    "**mpi_recv %p %d %D %i %t %C %p", buf, count, datatype, source, tag, comm, status);
Packit 0848f5
    }
Packit 0848f5
#   endif
Packit 0848f5
    mpi_errno = MPIR_Err_return_comm( comm_ptr, FCNAME, mpi_errno );
Packit 0848f5
    goto fn_exit;
Packit 0848f5
    /* --END ERROR HANDLING-- */
Packit 0848f5
}