Blame src/mpi/datatype/type_contiguous.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2002 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_Type_contigous */
Packit 0848f5
#if defined(HAVE_PRAGMA_WEAK)
Packit 0848f5
#pragma weak MPI_Type_contiguous = PMPI_Type_contiguous
Packit 0848f5
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
Packit 0848f5
#pragma _HP_SECONDARY_DEF PMPI_Type_contiguous  MPI_Type_contiguous
Packit 0848f5
#elif defined(HAVE_PRAGMA_CRI_DUP)
Packit 0848f5
#pragma _CRI duplicate MPI_Type_contiguous as PMPI_Type_contiguous
Packit 0848f5
#elif defined(HAVE_WEAK_ATTRIBUTE)
Packit 0848f5
int MPI_Type_contiguous(int count, MPI_Datatype oldtype, MPI_Datatype *newtype) __attribute__((weak,alias("PMPI_Type_contiguous")));
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.  You can use USE_WEAK_SYMBOLS to see if MPICH is
Packit 0848f5
   using weak symbols to implement the MPI routines. */
Packit 0848f5
#ifndef MPICH_MPI_FROM_PMPI
Packit 0848f5
#undef MPI_Type_contiguous
Packit 0848f5
#define MPI_Type_contiguous PMPI_Type_contiguous
Packit 0848f5
Packit 0848f5
#undef FUNCNAME
Packit 0848f5
#define FUNCNAME MPIR_Type_contiguous_impl
Packit 0848f5
#undef FCNAME
Packit 0848f5
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit 0848f5
int MPIR_Type_contiguous_impl(int count,
Packit 0848f5
                              MPI_Datatype oldtype,
Packit 0848f5
                              MPI_Datatype *newtype)
Packit 0848f5
{
Packit 0848f5
    int mpi_errno = MPI_SUCCESS;
Packit 0848f5
    MPID_Datatype *new_dtp;
Packit 0848f5
    MPI_Datatype new_handle;
Packit 0848f5
    
Packit 0848f5
    mpi_errno = MPID_Type_contiguous(count,
Packit 0848f5
				     oldtype,
Packit 0848f5
				     &new_handle);
Packit 0848f5
Packit 0848f5
    if (mpi_errno != MPI_SUCCESS) goto fn_fail;
Packit 0848f5
Packit 0848f5
    MPID_Datatype_get_ptr(new_handle, new_dtp);
Packit 0848f5
    mpi_errno = MPID_Datatype_set_contents(new_dtp,
Packit 0848f5
				           MPI_COMBINER_CONTIGUOUS,
Packit 0848f5
				           1, /* ints (count) */
Packit 0848f5
				           0,
Packit 0848f5
				           1,
Packit 0848f5
				           &count,
Packit 0848f5
				           NULL,
Packit 0848f5
				           &oldtype);
Packit 0848f5
Packit 0848f5
    if (mpi_errno != MPI_SUCCESS) goto fn_fail;
Packit 0848f5
Packit 0848f5
    MPID_OBJ_PUBLISH_HANDLE(*newtype, new_handle);
Packit 0848f5
Packit 0848f5
 fn_exit:
Packit 0848f5
    return mpi_errno;
Packit 0848f5
 fn_fail:
Packit 0848f5
Packit 0848f5
    goto fn_exit;
Packit 0848f5
}
Packit 0848f5
#undef FUNCNAME
Packit 0848f5
#define FUNCNAME MPIR_Type_contiguous_x_impl
Packit 0848f5
#undef FCNAME
Packit 0848f5
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit 0848f5
int MPIR_Type_contiguous_x_impl(MPI_Count count,
Packit 0848f5
                              MPI_Datatype oldtype,
Packit 0848f5
			      MPI_Datatype *newtype)
Packit 0848f5
{
Packit 0848f5
    /* to make 'count' fit MPI-3 type processing routines (which take integer
Packit 0848f5
     * counts), we construct a type consisting of N INT_MAX chunks followed by
Packit 0848f5
     * a remainder.  e.g for a count of 4000000000 bytes you would end up with
Packit 0848f5
     * one 2147483647-byte chunk followed immediately by a 1852516353-byte
Packit 0848f5
     * chunk */
Packit 0848f5
    MPI_Datatype chunks, remainder;
Packit 0848f5
    MPI_Aint lb, extent, disps[2];
Packit 0848f5
    int blocklens[2];
Packit 0848f5
    MPI_Datatype types[2];
Packit 0848f5
    int mpi_errno;
Packit 0848f5
Packit 0848f5
    /* truly stupendously large counts will overflow an integer with this math,
Packit 0848f5
     * but that is a problem for a few decades from now.  Sorry, few decades
Packit 0848f5
     * from now! */
Packit 0848f5
    MPIU_Assert(count/INT_MAX == (int)(count/INT_MAX));
Packit 0848f5
    int c = (int)(count/INT_MAX); /* OK to cast until 'count' is 256 bits */
Packit 0848f5
    int r = count%INT_MAX;
Packit 0848f5
Packit 0848f5
    mpi_errno = MPIR_Type_vector_impl(c, INT_MAX, INT_MAX, oldtype, &chunks);
Packit 0848f5
    if (mpi_errno != MPI_SUCCESS) goto fn_fail;
Packit 0848f5
    mpi_errno = MPIR_Type_contiguous_impl(r, oldtype, &remainder);
Packit 0848f5
    if (mpi_errno != MPI_SUCCESS) goto fn_fail;
Packit 0848f5
Packit 0848f5
    MPIR_Type_get_extent_impl(oldtype, &lb, &extent);
Packit 0848f5
Packit 0848f5
    blocklens[0] = 1;      blocklens[1] = 1;
Packit 0848f5
    disps[0]     = 0;      disps[1]     = c*extent*INT_MAX;
Packit 0848f5
    types[0]     = chunks; types[1]     = remainder;
Packit 0848f5
Packit 0848f5
    mpi_errno = MPIR_Type_create_struct_impl(2, blocklens, disps, types, newtype);
Packit 0848f5
Packit 0848f5
    MPIR_Type_free_impl(&chunks);
Packit 0848f5
    MPIR_Type_free_impl(&remainder);
Packit 0848f5
Packit 0848f5
fn_exit:
Packit 0848f5
    return mpi_errno;
Packit 0848f5
fn_fail:
Packit 0848f5
    goto fn_exit;
Packit 0848f5
}
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
Packit 0848f5
#undef FUNCNAME
Packit 0848f5
#define FUNCNAME MPI_Type_contiguous
Packit 0848f5
#undef FCNAME
Packit 0848f5
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit 0848f5
/*@
Packit 0848f5
    MPI_Type_contiguous - Creates a contiguous datatype
Packit 0848f5
Packit 0848f5
Input Parameters:
Packit 0848f5
+ count - replication count (nonnegative integer) 
Packit 0848f5
- oldtype - old datatype (handle) 
Packit 0848f5
Packit 0848f5
Output Parameters:
Packit 0848f5
. newtype - new datatype (handle) 
Packit 0848f5
Packit 0848f5
.N ThreadSafe
Packit 0848f5
Packit 0848f5
.N Fortran
Packit 0848f5
Packit 0848f5
.N Errors
Packit 0848f5
.N MPI_SUCCESS
Packit 0848f5
.N MPI_ERR_TYPE
Packit 0848f5
.N MPI_ERR_COUNT
Packit 0848f5
.N MPI_ERR_EXHAUSTED
Packit 0848f5
@*/
Packit 0848f5
int MPI_Type_contiguous(int count,
Packit 0848f5
			MPI_Datatype oldtype,
Packit 0848f5
			MPI_Datatype *newtype)
Packit 0848f5
{
Packit 0848f5
    int mpi_errno = MPI_SUCCESS;
Packit 0848f5
    MPID_MPI_STATE_DECL(MPID_STATE_MPI_TYPE_CONTIGUOUS);
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_FUNC_ENTER(MPID_STATE_MPI_TYPE_CONTIGUOUS);
Packit 0848f5
Packit 0848f5
#   ifdef HAVE_ERROR_CHECKING
Packit 0848f5
    {
Packit 0848f5
        MPID_BEGIN_ERROR_CHECKS;
Packit 0848f5
        {
Packit 0848f5
            MPID_Datatype *datatype_ptr = NULL;
Packit 0848f5
Packit 0848f5
	    MPIR_ERRTEST_COUNT(count, mpi_errno);
Packit 0848f5
            MPIR_ERRTEST_DATATYPE(oldtype, "datatype", mpi_errno);
Packit 0848f5
	    
Packit 0848f5
            if (HANDLE_GET_KIND(oldtype) != HANDLE_KIND_BUILTIN) {
Packit 0848f5
                MPID_Datatype_get_ptr(oldtype, datatype_ptr);
Packit 0848f5
                MPID_Datatype_valid_ptr(datatype_ptr, mpi_errno);
Packit 0848f5
                if (mpi_errno != MPI_SUCCESS) goto fn_fail;
Packit 0848f5
	    }
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
    mpi_errno = MPIR_Type_contiguous_impl(count, oldtype, newtype);
Packit 0848f5
    if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
Packit 0848f5
    /* ... end of body of routine ... */
Packit 0848f5
    
Packit 0848f5
  fn_exit:
Packit 0848f5
    MPID_MPI_FUNC_EXIT(MPID_STATE_MPI_TYPE_CONTIGUOUS);
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_type_contiguous",
Packit 0848f5
	"**mpi_type_contiguous %d %D %p", count, oldtype, newtype);
Packit 0848f5
    }
Packit 0848f5
#   endif
Packit 0848f5
    mpi_errno = MPIR_Err_return_comm( NULL, FCNAME, mpi_errno );
Packit 0848f5
    goto fn_exit;
Packit 0848f5
    /* --END ERROR HANDLING-- */
Packit 0848f5
}