Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of https://github.com/mpoquet/simgrid
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / strided_acc_onelock.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6
7 /* One-Sided MPI 2-D Strided Accumulate Test
8  *
9  * Author: James Dinan <dinan@mcs.anl.gov> 
10  * Date  : December, 2010
11  *
12  * This code performs one-sided accumulate into a 2d patch of a shared array.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <math.h>
18 #include <mpi.h>
19 #include "mpitest.h"
20 #include "squelch.h"
21
22 #define XDIM 1024 
23 #define YDIM 1024
24 #define ITERATIONS 10
25
26 int main(int argc, char **argv) {
27     int i, j, rank, nranks, peer, bufsize, errors;
28     double *buffer, *src_buf;
29     MPI_Win buf_win;
30
31     MTest_Init(&argc, &argv);
32
33     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
34     MPI_Comm_size(MPI_COMM_WORLD, &nranks);
35
36     bufsize = XDIM * YDIM * sizeof(double);
37     MPI_Alloc_mem(bufsize, MPI_INFO_NULL, &buffer);
38     MPI_Alloc_mem(bufsize, MPI_INFO_NULL, &src_buf);
39
40     for (i = 0; i < XDIM*YDIM; i++) {
41         *(buffer  + i) = 1.0 + rank;
42         *(src_buf + i) = 1.0 + rank;
43     }
44
45     MPI_Win_create(buffer, bufsize, 1, MPI_INFO_NULL, MPI_COMM_WORLD, &buf_win);
46
47     peer = (rank+1) % nranks;
48
49     for (i = 0; i < ITERATIONS; i++) {
50
51       MPI_Win_lock(MPI_LOCK_EXCLUSIVE, peer, 0, buf_win);
52
53       for (j = 0; j < YDIM; j++) {
54         MPI_Accumulate(src_buf + j*XDIM, XDIM, MPI_DOUBLE, peer,
55                        j*XDIM*sizeof(double), XDIM, MPI_DOUBLE, MPI_SUM, buf_win);
56       }
57
58       MPI_Win_unlock(peer, buf_win);
59     }
60
61     MPI_Barrier(MPI_COMM_WORLD);
62
63     MPI_Win_lock(MPI_LOCK_EXCLUSIVE, rank, 0, buf_win);
64     for (i = errors = 0; i < XDIM; i++) {
65       for (j = 0; j < YDIM; j++) {
66         const double actual   = *(buffer + i + j*XDIM);
67         const double expected = (1.0 + rank) + (1.0 + ((rank+nranks-1)%nranks)) * (ITERATIONS);
68         if (fabs(actual - expected) > 1.0e-10) {
69           SQUELCH( printf("%d: Data validation failed at [%d, %d] expected=%f actual=%f\n",
70               rank, j, i, expected, actual); );
71           errors++;
72           fflush(stdout);
73         }
74       }
75     }
76     MPI_Win_unlock(rank, buf_win);
77
78     MPI_Win_free(&buf_win);
79     MPI_Free_mem(buffer);
80     MPI_Free_mem(src_buf);
81
82     MTest_Finalize( errors );
83     MPI_Finalize();
84     return MTestReturnValue( errors );
85 }