/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include <mpi.h>
#include <stdio.h>

	

int
main(
    int					argc,
    char *				argv[])
{
    int					np;
    int					my_rank;
    int					neighbor_rank;
    MPI_Win				win;
    double				my_data;
    double				neighbor_data;
    
    MPI_Init(NULL, NULL);

    MPI_Comm_size(MPI_COMM_WORLD, &np);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    neighbor_rank = (my_rank + 1) % np;
    
    if (np < 2)
    {
	if ( my_rank == 0)
	{
	    printf("\nERROR: fence_get_simple must be at least (2) "
		   "processes\n\n");
	}
	
	MPI_Finalize();
	return 1;
    }

    MPI_Win_create(&my_data, sizeof(double), sizeof(double), MPI_INFO_NULL,
		   MPI_COMM_WORLD, &win);
    
    my_data = 42.0 * my_rank;
	
    MPI_Win_fence(MPI_MODE_NOPUT | MPI_MODE_NOPRECEDE, win);
    {
	MPI_Get(&neighbor_data, 1, MPI_DOUBLE,
		neighbor_rank, 0, 1, MPI_DOUBLE, win);
    }
    MPI_Win_fence(MPI_MODE_NOSTORE | MPI_MODE_NOSUCCEED, win);
	
    if (neighbor_data == 42.0 * neighbor_rank)
    {
	printf("%d: data[%d]=%f\n", my_rank, neighbor_rank, neighbor_data);
    }
    else
    {
	printf("%d: ERROR - data[%d]=%f NOT %f\n",
	       my_rank, neighbor_rank, neighbor_data, neighbor_rank * 42.0);
    }
	
    MPI_Win_free(&win);

    MPI_Finalize();
    
    return 0;
}
