Blame src/mutex/mutex_create.c

Packit Service c5cf8c
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit Service c5cf8c
/*
Packit Service c5cf8c
 *  (C) 2001 by Argonne National Laboratory.
Packit Service c5cf8c
 *      See COPYRIGHT in top-level directory.
Packit Service c5cf8c
 */
Packit Service c5cf8c
Packit Service c5cf8c
#include <stdio.h>
Packit Service c5cf8c
#include <stdlib.h>
Packit Service c5cf8c
#include <assert.h>
Packit Service c5cf8c
#include <string.h>
Packit Service c5cf8c
#include <strings.h>
Packit Service c5cf8c
Packit Service c5cf8c
#include <mpi.h>
Packit Service c5cf8c
#include "muteximpl.h"
Packit Service c5cf8c
Packit Service c5cf8c
Packit Service c5cf8c
/* -- Begin Profiling Symbol Block for routine MPIX_Mutex_create */
Packit Service c5cf8c
#if defined(HAVE_PRAGMA_WEAK)
Packit Service c5cf8c
#pragma weak MPIX_Mutex_create = PMPIX_Mutex_create
Packit Service c5cf8c
#elif defined(HAVE_PRAGMA_HP_SEC_DEF)
Packit Service c5cf8c
#pragma _HP_SECONDARY_DEF PMPIX_Mutex_create  MPIX_Mutex_create
Packit Service c5cf8c
#elif defined(HAVE_PRAGMA_CRI_DUP)
Packit Service c5cf8c
#pragma _CRI duplicate MPIX_Mutex_create as PMPIX_Mutex_create
Packit Service c5cf8c
#elif defined(HAVE_WEAK_ATTRIBUTE)
Packit Service c5cf8c
int MPIX_Mutex_create(int my_count, MPI_Comm comm, MPIX_Mutex * hdl_out)
Packit Service c5cf8c
    __attribute__ ((weak, alias("PMPIX_Mutex_create")));
Packit Service c5cf8c
#endif
Packit Service c5cf8c
/* -- End Profiling Symbol Block */
Packit Service c5cf8c
Packit Service c5cf8c
/* Define MPICH_MPI_FROM_PMPI if weak symbols are not supported to build
Packit Service c5cf8c
   the MPI routines */
Packit Service c5cf8c
#ifndef MPICH_MPI_FROM_PMPI
Packit Service c5cf8c
#undef MPIX_Mutex_create
Packit Service c5cf8c
#define MPIX_Mutex_create PMPIX_Mutex_create
Packit Service c5cf8c
#endif
Packit Service c5cf8c
Packit Service c5cf8c
#undef FUNCNAME
Packit Service c5cf8c
#define FUNCNAME MPIX_Mutex_create
Packit Service c5cf8c
#undef FCNAME
Packit Service c5cf8c
#define FCNAME MPL_QUOTE(FUNCNAME)
Packit Service c5cf8c
Packit Service c5cf8c
/** Create a group of MPI mutexes.  Collective on the given communicator.
Packit Service c5cf8c
  *
Packit Service c5cf8c
  * @param[in]  count   Number of mutexes on the local process.
Packit Service c5cf8c
  * @param[in]  comm    MPI communicator on which to create mutexes
Packit Service c5cf8c
  * @param[out] hdl_out Handle to the mutex group
Packit Service c5cf8c
  * @return             MPI status
Packit Service c5cf8c
  */
Packit Service c5cf8c
int MPIX_Mutex_create(int my_count, MPI_Comm comm, MPIX_Mutex * hdl_out)
Packit Service c5cf8c
{
Packit Service c5cf8c
    int rank, nproc, max_count, i;
Packit Service c5cf8c
    MPIX_Mutex hdl;
Packit Service c5cf8c
Packit Service c5cf8c
    hdl = malloc(sizeof(struct mpixi_mutex_s));
Packit Service c5cf8c
    assert(hdl != NULL);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Comm_dup(comm, &hdl->comm);
Packit Service c5cf8c
Packit Service c5cf8c
    MPI_Comm_rank(hdl->comm, &rank;;
Packit Service c5cf8c
    MPI_Comm_size(hdl->comm, &nproc);
Packit Service c5cf8c
Packit Service c5cf8c
    hdl->my_count = my_count;
Packit Service c5cf8c
Packit Service c5cf8c
    /* Find the max. count to determine how many windows we need. */
Packit Service c5cf8c
    MPI_Allreduce(&my_count, &max_count, 1, MPI_INT, MPI_MAX, hdl->comm);
Packit Service c5cf8c
    assert(max_count > 0);
Packit Service c5cf8c
Packit Service c5cf8c
    hdl->max_count = max_count;
Packit Service c5cf8c
    hdl->windows = malloc(sizeof(MPI_Win) * max_count);
Packit Service c5cf8c
    assert(hdl->windows != NULL);
Packit Service c5cf8c
Packit Service c5cf8c
    if (my_count > 0) {
Packit Service c5cf8c
        hdl->bases = malloc(sizeof(uint8_t *) * my_count);
Packit Service c5cf8c
        assert(hdl->bases != NULL);
Packit Service c5cf8c
    } else {
Packit Service c5cf8c
        hdl->bases = NULL;
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    /* We need multiple windows here: one for each mutex.  Otherwise
Packit Service c5cf8c
     * performance will suffer due to exclusive access epochs. */
Packit Service c5cf8c
    for (i = 0; i < max_count; i++) {
Packit Service c5cf8c
        int size = 0;
Packit Service c5cf8c
        void *base = NULL;
Packit Service c5cf8c
Packit Service c5cf8c
        if (i < my_count) {
Packit Service c5cf8c
            MPI_Alloc_mem(nproc, MPI_INFO_NULL, &hdl->bases[i]);
Packit Service c5cf8c
            assert(hdl->bases[i] != NULL);
Packit Service c5cf8c
            memset(hdl->bases[i], 0, nproc);
Packit Service c5cf8c
Packit Service c5cf8c
            base = hdl->bases[i];
Packit Service c5cf8c
            size = nproc;
Packit Service c5cf8c
        }
Packit Service c5cf8c
Packit Service c5cf8c
        MPI_Win_create(base, size, sizeof(uint8_t), MPI_INFO_NULL, hdl->comm, &hdl->windows[i]);
Packit Service c5cf8c
    }
Packit Service c5cf8c
Packit Service c5cf8c
    *hdl_out = hdl;
Packit Service c5cf8c
    return MPI_SUCCESS;
Packit Service c5cf8c
}