Blame test/mpi/rma/fetchandadd_tree_am.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2001 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include "stdio.h"
Packit 0848f5
#include "stdlib.h"
Packit 0848f5
#include "mpitest.h"
Packit 0848f5
Packit 0848f5
/* This is the tree-based scalable version of the fetch-and-add
Packit 0848f5
   example from Using MPI-2, pg 206-207. The code in the book (Fig
Packit 0848f5
   6.16) has bugs that are fixed below. */
Packit 0848f5
Packit 0848f5
/* same as fetchandadd_tree.c but uses alloc_mem */
Packit 0848f5
Packit 0848f5
#define NTIMES 20       /* no of times each process calls the counter
Packit 0848f5
                         * routine */
Packit 0848f5
Packit 0848f5
int localvalue = 0;             /* contribution of this process to the counter. We
Packit 0848f5
                                 * define it as a global variable because attribute
Packit 0848f5
                                 * caching on the window is not enabled yet. */
Packit 0848f5
Packit 0848f5
void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
Packit 0848f5
                      MPI_Datatype acc_type, int nlevels, int *value);
Packit 0848f5
Packit 0848f5
int compar(const void *a, const void *b);
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    int rank, nprocs, i, *counter_mem, *get_array, *get_idx, *acc_idx,
Packit 0848f5
        mask, nlevels, level, idx, tmp_rank, pof2;
Packit 0848f5
    MPI_Datatype get_type, acc_type;
Packit 0848f5
    MPI_Win win;
Packit 0848f5
    int errs = 0, *results, *counter_vals;
Packit 0848f5
Packit 0848f5
    MTest_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
Packit 0848f5
    if (rank == 0) {
Packit 0848f5
        /* allocate counter memory and initialize to 0 */
Packit 0848f5
Packit 0848f5
        /* find the next power-of-two >= nprocs */
Packit 0848f5
        pof2 = 1;
Packit 0848f5
        while (pof2 < nprocs)
Packit 0848f5
            pof2 *= 2;
Packit 0848f5
Packit 0848f5
        /* counter_mem = (int *) calloc(pof2*2, sizeof(int)); */
Packit 0848f5
Packit 0848f5
        i = MPI_Alloc_mem(pof2 * 2 * sizeof(int), MPI_INFO_NULL, &counter_mem);
Packit 0848f5
        if (i) {
Packit 0848f5
            printf("Can't allocate memory in test program\n");
Packit 0848f5
            MPI_Abort(MPI_COMM_WORLD, 1);
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        for (i = 0; i < (pof2 * 2); i++)
Packit 0848f5
            counter_mem[i] = 0;
Packit 0848f5
Packit 0848f5
        MPI_Win_create(counter_mem, pof2 * 2 * sizeof(int), sizeof(int),
Packit 0848f5
                       MPI_INFO_NULL, MPI_COMM_WORLD, &win);
Packit 0848f5
Packit 0848f5
        MPI_Win_free(&win);
Packit 0848f5
Packit 0848f5
        /* free(counter_mem) */
Packit 0848f5
        MPI_Free_mem(counter_mem);
Packit 0848f5
Packit 0848f5
        /* gather the results from other processes, sort them, and check
Packit 0848f5
         * whether they represent a counter being incremented by 1 */
Packit 0848f5
Packit 0848f5
        results = (int *) malloc(NTIMES * nprocs * sizeof(int));
Packit 0848f5
        for (i = 0; i < NTIMES * nprocs; i++)
Packit 0848f5
            results[i] = -1;
Packit 0848f5
Packit 0848f5
        MPI_Gather(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, results, NTIMES, MPI_INT, 0, MPI_COMM_WORLD);
Packit 0848f5
Packit 0848f5
        qsort(results + NTIMES, NTIMES * (nprocs - 1), sizeof(int), compar);
Packit 0848f5
Packit 0848f5
        for (i = NTIMES + 1; i < (NTIMES * nprocs); i++)
Packit 0848f5
            if (results[i] != results[i - 1] + 1)
Packit 0848f5
                errs++;
Packit 0848f5
Packit 0848f5
        free(results);
Packit 0848f5
    }
Packit 0848f5
    else {
Packit 0848f5
        /* Get the largest power of two smaller than nprocs */
Packit 0848f5
        mask = 1;
Packit 0848f5
        nlevels = 0;
Packit 0848f5
        while (mask < nprocs) {
Packit 0848f5
            mask <<= 1;
Packit 0848f5
            nlevels++;
Packit 0848f5
        }
Packit 0848f5
        mask >>= 1;
Packit 0848f5
Packit 0848f5
        get_array = (int *) malloc(nlevels * sizeof(int));
Packit 0848f5
        get_idx = (int *) malloc(nlevels * sizeof(int));
Packit 0848f5
        acc_idx = (int *) malloc(nlevels * sizeof(int));
Packit 0848f5
Packit 0848f5
        level = 0;
Packit 0848f5
        idx = 0;
Packit 0848f5
        tmp_rank = rank;
Packit 0848f5
        while (mask >= 1) {
Packit 0848f5
            if (tmp_rank < mask) {
Packit 0848f5
                /* go to left for acc_idx, go to right for
Packit 0848f5
                 * get_idx. set idx=acc_idx for next iteration */
Packit 0848f5
                acc_idx[level] = idx + 1;
Packit 0848f5
                get_idx[level] = idx + mask * 2;
Packit 0848f5
                idx = idx + 1;
Packit 0848f5
            }
Packit 0848f5
            else {
Packit 0848f5
                /* go to right for acc_idx, go to left for
Packit 0848f5
                 * get_idx. set idx=acc_idx for next iteration */
Packit 0848f5
                acc_idx[level] = idx + mask * 2;
Packit 0848f5
                get_idx[level] = idx + 1;
Packit 0848f5
                idx = idx + mask * 2;
Packit 0848f5
            }
Packit 0848f5
            level++;
Packit 0848f5
            tmp_rank = tmp_rank % mask;
Packit 0848f5
            mask >>= 1;
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
/*        for (i=0; i
Packit 0848f5
            printf("Rank %d, acc_idx[%d]=%d, get_idx[%d]=%d\n", rank,
Packit 0848f5
                   i, acc_idx[i], i, get_idx[i]);
Packit 0848f5
*/
Packit 0848f5
Packit 0848f5
        MPI_Type_create_indexed_block(nlevels, 1, get_idx, MPI_INT, &get_type);
Packit 0848f5
        MPI_Type_create_indexed_block(nlevels, 1, acc_idx, MPI_INT, &acc_type);
Packit 0848f5
        MPI_Type_commit(&get_type);
Packit 0848f5
        MPI_Type_commit(&acc_type);
Packit 0848f5
Packit 0848f5
        /* allocate array to store the values obtained from the
Packit 0848f5
         * fetch-and-add counter */
Packit 0848f5
        counter_vals = (int *) malloc(NTIMES * sizeof(int));
Packit 0848f5
Packit 0848f5
        MPI_Win_create(NULL, 0, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &win);
Packit 0848f5
Packit 0848f5
        for (i = 0; i < NTIMES; i++) {
Packit 0848f5
            Get_nextval_tree(win, get_array, get_type, acc_type, nlevels, counter_vals + i);
Packit 0848f5
            /* printf("Rank %d, counter %d\n", rank, value); */
Packit 0848f5
        }
Packit 0848f5
Packit 0848f5
        MPI_Win_free(&win);
Packit 0848f5
        free(get_array);
Packit 0848f5
        free(get_idx);
Packit 0848f5
        free(acc_idx);
Packit 0848f5
        MPI_Type_free(&get_type);
Packit 0848f5
        MPI_Type_free(&acc_type);
Packit 0848f5
Packit 0848f5
        /* gather the results to the root */
Packit 0848f5
        MPI_Gather(counter_vals, NTIMES, MPI_INT, NULL, 0, MPI_DATATYPE_NULL, 0, MPI_COMM_WORLD);
Packit 0848f5
        free(counter_vals);
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MTest_Finalize(errs);
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
    return MTestReturnValue(errs);
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
Packit 0848f5
void Get_nextval_tree(MPI_Win win, int *get_array, MPI_Datatype get_type,
Packit 0848f5
                      MPI_Datatype acc_type, int nlevels, int *value)
Packit 0848f5
{
Packit 0848f5
    int *one, i;
Packit 0848f5
Packit 0848f5
    one = (int *) malloc(nlevels * sizeof(int));
Packit 0848f5
    for (i = 0; i < nlevels; i++)
Packit 0848f5
        one[i] = 1;
Packit 0848f5
Packit 0848f5
    MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
Packit 0848f5
    MPI_Accumulate(one, nlevels, MPI_INT, 0, 0, 1, acc_type, MPI_SUM, win);
Packit 0848f5
    MPI_Get(get_array, nlevels, MPI_INT, 0, 0, 1, get_type, win);
Packit 0848f5
    MPI_Win_unlock(0, win);
Packit 0848f5
Packit 0848f5
    *value = localvalue;
Packit 0848f5
    for (i = 0; i < nlevels; i++)
Packit 0848f5
        *value = *value + get_array[i];
Packit 0848f5
Packit 0848f5
    localvalue++;
Packit 0848f5
Packit 0848f5
    free(one);
Packit 0848f5
}
Packit 0848f5
Packit 0848f5
int compar(const void *a, const void *b)
Packit 0848f5
{
Packit 0848f5
    return (*((int *) a) - *((int *) b));
Packit 0848f5
}