Blame src/mpi/datatype/type_create_subarray.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
#include "mpiimpl.h"
Packit 0848f5
Packit 0848f5
/* -- Begin Profiling Symbol Block for routine MPI_Type_create_subarray */
Packit 0848f5
#if defined(HAVE_PRAGMA_WEAK)
Packit 0848f5
#pragma weak MPI_Type_create_subarray = PMPI_Type_create_subarray
Packit 0848f5
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
Packit 0848f5
#pragma _HP_SECONDARY_DEF PMPI_Type_create_subarray  MPI_Type_create_subarray
Packit 0848f5
#elif defined(HAVE_PRAGMA_CRI_DUP)
Packit 0848f5
#pragma _CRI duplicate MPI_Type_create_subarray as PMPI_Type_create_subarray
Packit 0848f5
#elif defined(HAVE_WEAK_ATTRIBUTE)
Packit 0848f5
int MPI_Type_create_subarray(int ndims, const int array_of_sizes[],
Packit 0848f5
                             const int array_of_subsizes[], const int array_of_starts[],
Packit 0848f5
                             int order, MPI_Datatype oldtype, MPI_Datatype *newtype) __attribute__((weak,alias("PMPI_Type_create_subarray")));
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_Type_create_subarray
Packit 0848f5
#define MPI_Type_create_subarray PMPI_Type_create_subarray
Packit 0848f5
Packit 0848f5
#endif
Packit 0848f5
Packit 0848f5
#undef FUNCNAME
Packit 0848f5
#define FUNCNAME MPI_Type_create_subarray
Packit 0848f5
Packit 0848f5
/*@
Packit 0848f5
   MPI_Type_create_subarray - Create a datatype for a subarray of a regular,
Packit 0848f5
    multidimensional array
Packit 0848f5
Packit 0848f5
Input Parameters:
Packit 0848f5
+ ndims - number of array dimensions (positive integer)
Packit 0848f5
. array_of_sizes - number of elements of type oldtype in each dimension of the
Packit 0848f5
  full array (array of positive integers)
Packit 0848f5
. array_of_subsizes - number of elements of type oldtype in each dimension of
Packit 0848f5
  the subarray (array of positive integers)
Packit 0848f5
. array_of_starts - starting coordinates of the subarray in each dimension
Packit 0848f5
  (array of nonnegative integers)
Packit 0848f5
. order - array storage order flag (state)
Packit 0848f5
- oldtype - array element 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_ARG
Packit 0848f5
@*/
Packit 0848f5
int MPI_Type_create_subarray(int ndims,
Packit 0848f5
			     const int array_of_sizes[],
Packit 0848f5
			     const int array_of_subsizes[],
Packit 0848f5
			     const int array_of_starts[],
Packit 0848f5
			     int order,
Packit 0848f5
			     MPI_Datatype oldtype,
Packit 0848f5
			     MPI_Datatype *newtype)
Packit 0848f5
{
Packit 0848f5
    static const char FCNAME[] = "MPI_Type_create_subarray";
Packit 0848f5
    int mpi_errno = MPI_SUCCESS, i;
Packit 0848f5
    MPI_Datatype new_handle;
Packit 0848f5
Packit 0848f5
    /* these variables are from the original version in ROMIO */
Packit 0848f5
    MPI_Aint size, extent, disps[3];
Packit 0848f5
    MPI_Datatype tmp1, tmp2;
Packit 0848f5
Packit 0848f5
#   ifdef HAVE_ERROR_CHECKING
Packit 0848f5
    MPI_Aint   size_with_aint;
Packit 0848f5
    MPI_Offset size_with_offset;
Packit 0848f5
#   endif
Packit 0848f5
Packit 0848f5
    /* for saving contents */
Packit 0848f5
    int *ints;
Packit 0848f5
    MPID_Datatype *new_dtp;
Packit 0848f5
Packit 0848f5
    MPIU_CHKLMEM_DECL(1);
Packit 0848f5
    MPID_MPI_STATE_DECL(MPID_STATE_MPI_TYPE_CREATE_SUBARRAY);
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_CREATE_SUBARRAY);
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
	    /* Check parameters */
Packit 0848f5
	    MPIR_ERRTEST_ARGNONPOS(ndims, "ndims", mpi_errno, MPI_ERR_DIMS);
Packit 0848f5
	    MPIR_ERRTEST_ARGNULL(array_of_sizes, "array_of_sizes", mpi_errno);
Packit 0848f5
	    MPIR_ERRTEST_ARGNULL(array_of_subsizes, "array_of_subsizes", mpi_errno);
Packit 0848f5
	    MPIR_ERRTEST_ARGNULL(array_of_starts, "array_of_starts", mpi_errno);
Packit 0848f5
	    for (i=0; mpi_errno == MPI_SUCCESS && i < ndims; i++) {
Packit 0848f5
		MPIR_ERRTEST_ARGNONPOS(array_of_sizes[i], "size", mpi_errno, MPI_ERR_ARG);
Packit 0848f5
		MPIR_ERRTEST_ARGNONPOS(array_of_subsizes[i], "subsize", mpi_errno, MPI_ERR_ARG);
Packit 0848f5
		MPIR_ERRTEST_ARGNEG(array_of_starts[i], "start", mpi_errno);
Packit 0848f5
		if (array_of_subsizes[i] > array_of_sizes[i]) {
Packit 0848f5
		    mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
Packit 0848f5
						     MPIR_ERR_RECOVERABLE,
Packit 0848f5
						     FCNAME,
Packit 0848f5
						     __LINE__,
Packit 0848f5
						     MPI_ERR_ARG,
Packit 0848f5
						     "**argrange",
Packit 0848f5
						     "**argrange %s %d %d",
Packit 0848f5
						     "array_of_subsizes",
Packit 0848f5
						     array_of_subsizes[i],
Packit 0848f5
						     array_of_sizes[i]);
Packit 0848f5
                    goto fn_fail;
Packit 0848f5
		}
Packit 0848f5
		if (array_of_starts[i] > (array_of_sizes[i] - array_of_subsizes[i]))
Packit 0848f5
		{
Packit 0848f5
		    mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
Packit 0848f5
						     MPIR_ERR_RECOVERABLE,
Packit 0848f5
						     FCNAME,
Packit 0848f5
						     __LINE__,
Packit 0848f5
						     MPI_ERR_ARG,
Packit 0848f5
						     "**argrange",
Packit 0848f5
						     "**argrange %s %d %d",
Packit 0848f5
						     "array_of_starts",
Packit 0848f5
						     array_of_starts[i],
Packit 0848f5
						     array_of_sizes[i] -
Packit 0848f5
						     array_of_subsizes[i]);
Packit 0848f5
                    goto fn_fail;
Packit 0848f5
		}
Packit 0848f5
	    }
Packit 0848f5
	    if (order != MPI_ORDER_FORTRAN && order != MPI_ORDER_C) {
Packit 0848f5
		mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
Packit 0848f5
						 MPIR_ERR_RECOVERABLE,
Packit 0848f5
						 FCNAME,
Packit 0848f5
						 __LINE__,
Packit 0848f5
						 MPI_ERR_ARG,
Packit 0848f5
						 "**arg",
Packit 0848f5
						 "**arg %s",
Packit 0848f5
						 "order");
Packit 0848f5
                goto fn_fail;
Packit 0848f5
	    }
Packit 0848f5
Packit 0848f5
	    MPIR_Type_extent_impl(oldtype, &extent);
Packit 0848f5
Packit 0848f5
	    /* check if MPI_Aint is large enough for size of global array.
Packit 0848f5
	       if not, complain. */
Packit 0848f5
Packit 0848f5
	    size_with_aint = extent;
Packit 0848f5
	    for (i=0; i
Packit 0848f5
	    size_with_offset = extent;
Packit 0848f5
	    for (i=0; i
Packit 0848f5
	    if (size_with_aint != size_with_offset) {
Packit 0848f5
		mpi_errno = MPIR_Err_create_code(MPI_SUCCESS,
Packit 0848f5
						 MPIR_ERR_FATAL,
Packit 0848f5
						 FCNAME,
Packit 0848f5
						 __LINE__,
Packit 0848f5
						 MPI_ERR_ARG,
Packit 0848f5
						 "**subarrayoflow",
Packit 0848f5
						 "**subarrayoflow %L",
Packit 0848f5
						 size_with_offset);
Packit 0848f5
                goto fn_fail;
Packit 0848f5
            }
Packit 0848f5
Packit 0848f5
            /* Get handles to MPI objects. */
Packit 0848f5
            MPID_Datatype_get_ptr(oldtype, datatype_ptr);
Packit 0848f5
Packit 0848f5
            /* Validate datatype_ptr */
Packit 0848f5
            MPID_Datatype_valid_ptr(datatype_ptr, mpi_errno);
Packit 0848f5
	    /* If datatype_ptr is not valid, it will be reset to null */
Packit 0848f5
            if (mpi_errno != MPI_SUCCESS) goto fn_fail;
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
    /* TODO: CHECK THE ERROR RETURNS FROM ALL THESE!!! */
Packit 0848f5
Packit 0848f5
    /* TODO: GRAB EXTENT WITH A MACRO OR SOMETHING FASTER */
Packit 0848f5
    MPIR_Type_extent_impl(oldtype, &extent);
Packit 0848f5
Packit 0848f5
    if (order == MPI_ORDER_FORTRAN) {
Packit 0848f5
	if (ndims == 1)
Packit 0848f5
	    mpi_errno = MPID_Type_contiguous(array_of_subsizes[0],
Packit 0848f5
					     oldtype,
Packit 0848f5
					     &tmp1);
Packit 0848f5
	else {
Packit 0848f5
	    mpi_errno = MPID_Type_vector(array_of_subsizes[1],
Packit 0848f5
					 array_of_subsizes[0],
Packit 0848f5
					 (MPI_Aint)(array_of_sizes[0]),
Packit 0848f5
					 0, /* stride in types */
Packit 0848f5
					 oldtype,
Packit 0848f5
					 &tmp1);
Packit 0848f5
            if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
Packit 0848f5
	    size = ((MPI_Aint)(array_of_sizes[0])) * extent;
Packit 0848f5
	    for (i=2; i
Packit 0848f5
		size *= (MPI_Aint)(array_of_sizes[i-1]);
Packit 0848f5
		mpi_errno = MPID_Type_vector(array_of_subsizes[i],
Packit 0848f5
					     1,
Packit 0848f5
					     size,
Packit 0848f5
					     1, /* stride in bytes */
Packit 0848f5
					     tmp1,
Packit 0848f5
					     &tmp2);
Packit 0848f5
                if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
		MPIR_Type_free_impl(&tmp1);
Packit 0848f5
		tmp1 = tmp2;
Packit 0848f5
	    }
Packit 0848f5
	}
Packit 0848f5
        if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
	
Packit 0848f5
	/* add displacement and UB */
Packit 0848f5
	
Packit 0848f5
	disps[1] = (MPI_Aint)(array_of_starts[0]);
Packit 0848f5
	size = 1;
Packit 0848f5
	for (i=1; i
Packit 0848f5
	    size *= (MPI_Aint)(array_of_sizes[i-1]);
Packit 0848f5
	    disps[1] += size * (MPI_Aint)(array_of_starts[i]);
Packit 0848f5
	}
Packit 0848f5
        /* rest done below for both Fortran and C order */
Packit 0848f5
    }
Packit 0848f5
    else /* MPI_ORDER_C */ {
Packit 0848f5
	/* dimension ndims-1 changes fastest */
Packit 0848f5
	if (ndims == 1) {
Packit 0848f5
	    mpi_errno = MPID_Type_contiguous(array_of_subsizes[0],
Packit 0848f5
					     oldtype,
Packit 0848f5
					     &tmp1);
Packit 0848f5
            if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
Packit 0848f5
	}
Packit 0848f5
	else {
Packit 0848f5
	    mpi_errno = MPID_Type_vector(array_of_subsizes[ndims-2],
Packit 0848f5
					 array_of_subsizes[ndims-1],
Packit 0848f5
					 (MPI_Aint)(array_of_sizes[ndims-1]),
Packit 0848f5
					 0, /* stride in types */
Packit 0848f5
					 oldtype,
Packit 0848f5
					 &tmp1);
Packit 0848f5
            if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
Packit 0848f5
	    size = (MPI_Aint)(array_of_sizes[ndims-1]) * extent;
Packit 0848f5
	    for (i=ndims-3; i>=0; i--) {
Packit 0848f5
		size *= (MPI_Aint)(array_of_sizes[i+1]);
Packit 0848f5
		mpi_errno = MPID_Type_vector(array_of_subsizes[i],
Packit 0848f5
					     1,    /* blocklen */
Packit 0848f5
					     size, /* stride */
Packit 0848f5
					     1,    /* stride in bytes */
Packit 0848f5
					     tmp1, /* old type */
Packit 0848f5
					     &tmp2);
Packit 0848f5
                if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
Packit 0848f5
		MPIR_Type_free_impl(&tmp1);
Packit 0848f5
		tmp1 = tmp2;
Packit 0848f5
	    }
Packit 0848f5
	}
Packit 0848f5
	
Packit 0848f5
	/* add displacement and UB */
Packit 0848f5
	
Packit 0848f5
	disps[1] = (MPI_Aint)(array_of_starts[ndims-1]);
Packit 0848f5
	size = 1;
Packit 0848f5
	for (i=ndims-2; i>=0; i--) {
Packit 0848f5
	    size *= (MPI_Aint)(array_of_sizes[i+1]);
Packit 0848f5
	    disps[1] += size * (MPI_Aint)(array_of_starts[i]);
Packit 0848f5
	}
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    disps[1] *= extent;
Packit 0848f5
Packit 0848f5
    disps[2] = extent;
Packit 0848f5
    for (i=0; i
Packit 0848f5
Packit 0848f5
    disps[0] = 0;
Packit 0848f5
Packit 0848f5
/* Instead of using MPI_LB/MPI_UB, which have been removed from MPI in MPI-3,
Packit 0848f5
   use MPI_Type_create_resized. Use hindexed_block to set the starting displacement
Packit 0848f5
   of the datatype (disps[1]) and type_create_resized to set lb to 0 (disps[0])
Packit 0848f5
   and extent to disps[2], which makes ub = disps[2].
Packit 0848f5
 */
Packit 0848f5
Packit 0848f5
    mpi_errno = MPID_Type_blockindexed(1, 1, &disps[1],
Packit 0848f5
                                       1, /* 1 means disp is in bytes */
Packit 0848f5
                                       tmp1, &tmp2);
Packit 0848f5
    if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
Packit 0848f5
    mpi_errno = MPID_Type_create_resized(tmp2, 0, disps[2], &new_handle);
Packit 0848f5
    if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
Packit 0848f5
    MPIR_Type_free_impl(&tmp1);
Packit 0848f5
    MPIR_Type_free_impl(&tmp2);
Packit 0848f5
Packit 0848f5
    /* at this point we have the new type, and we've cleaned up any
Packit 0848f5
     * intermediate types created in the process.  we just need to save
Packit 0848f5
     * all our contents/envelope information.
Packit 0848f5
     */
Packit 0848f5
Packit 0848f5
    /* Save contents */
Packit 0848f5
    MPIU_CHKLMEM_MALLOC_ORJUMP(ints, int *, (3 * ndims + 2) * sizeof(int), mpi_errno, "content description");
Packit 0848f5
Packit 0848f5
    ints[0] = ndims;
Packit 0848f5
    for (i=0; i < ndims; i++) {
Packit 0848f5
	ints[i + 1] = array_of_sizes[i];
Packit 0848f5
    }
Packit 0848f5
    for(i=0; i < ndims; i++) {
Packit 0848f5
	ints[i + ndims + 1] = array_of_subsizes[i];
Packit 0848f5
    }
Packit 0848f5
    for(i=0; i < ndims; i++) {
Packit 0848f5
	ints[i + 2*ndims + 1] = array_of_starts[i];
Packit 0848f5
    }
Packit 0848f5
    ints[3*ndims + 1] = order;
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_SUBARRAY,
Packit 0848f5
					   3 * ndims + 2, /* ints */
Packit 0848f5
					   0, /* aints */
Packit 0848f5
					   1, /* types */
Packit 0848f5
					   ints,
Packit 0848f5
					   NULL,
Packit 0848f5
					   &oldtype);
Packit 0848f5
    if (mpi_errno) MPIR_ERR_POP(mpi_errno);
Packit 0848f5
Packit 0848f5
Packit 0848f5
    MPID_OBJ_PUBLISH_HANDLE(*newtype, new_handle);
Packit 0848f5
    /* ... end of body of routine ... */
Packit 0848f5
Packit 0848f5
  fn_exit:
Packit 0848f5
    MPIU_CHKLMEM_FREEALL();
Packit 0848f5
    MPID_MPI_FUNC_EXIT(MPID_STATE_MPI_TYPE_CREATE_SUBARRAY);
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_create_subarray",
Packit 0848f5
	    "**mpi_type_create_subarray %d %p %p %p %d %D %p", ndims, array_of_sizes, array_of_subsizes,
Packit 0848f5
	    array_of_starts, order, 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
}