|
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 |
/* Fetch and add example from Using MPI-2 (the non-scalable version,
|
|
Packit |
0848f5 |
Fig. 6.12). */
|
|
Packit |
0848f5 |
|
|
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(MPI_Win win, int *val_array, MPI_Datatype get_type,
|
|
Packit |
0848f5 |
int rank, int nprocs, 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, blens[2], disps[2], *counter_mem, *val_array, *results, *counter_vals;
|
|
Packit |
0848f5 |
MPI_Datatype get_type;
|
|
Packit |
0848f5 |
MPI_Win win;
|
|
Packit |
0848f5 |
int errs = 0;
|
|
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 |
counter_mem = (int *) calloc(nprocs, sizeof(int));
|
|
Packit |
0848f5 |
MPI_Win_create(counter_mem, nprocs * sizeof(int), sizeof(int),
|
|
Packit |
0848f5 |
MPI_INFO_NULL, MPI_COMM_WORLD, &win);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Win_free(&win);
|
|
Packit |
0848f5 |
free(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 |
blens[0] = rank;
|
|
Packit |
0848f5 |
disps[0] = 0;
|
|
Packit |
0848f5 |
blens[1] = nprocs - rank - 1;
|
|
Packit |
0848f5 |
disps[1] = rank + 1;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Type_indexed(2, blens, disps, MPI_INT, &get_type);
|
|
Packit |
0848f5 |
MPI_Type_commit(&get_type);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
val_array = (int *) malloc(nprocs * sizeof(int));
|
|
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(win, val_array, get_type, rank, nprocs, 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 |
|
|
Packit |
0848f5 |
free(val_array);
|
|
Packit |
0848f5 |
MPI_Type_free(&get_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 0;
|
|
Packit |
0848f5 |
}
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
void Get_nextval(MPI_Win win, int *val_array, MPI_Datatype get_type,
|
|
Packit |
0848f5 |
int rank, int nprocs, int *value)
|
|
Packit |
0848f5 |
{
|
|
Packit |
0848f5 |
int one = 1, i;
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, 0, 0, win);
|
|
Packit |
0848f5 |
MPI_Accumulate(&one, 1, MPI_INT, 0, rank, 1, MPI_INT, MPI_SUM, win);
|
|
Packit |
0848f5 |
MPI_Get(val_array, 1, get_type, 0, 0, 1, get_type, win);
|
|
Packit |
0848f5 |
MPI_Win_unlock(0, win);
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
*value = 0;
|
|
Packit |
0848f5 |
val_array[rank] = localvalue;
|
|
Packit |
0848f5 |
for (i = 0; i < nprocs; i++)
|
|
Packit |
0848f5 |
*value = *value + val_array[i];
|
|
Packit |
0848f5 |
|
|
Packit |
0848f5 |
localvalue++;
|
|
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 |
}
|