Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'pikachuyann/simgrid-stoprofiles'
[simgrid.git] / teshsuite / smpi / mpich3-test / io / i_rdwrord.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2014 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include "mpitest.h"
10
11 /*
12 static char MTEST_Descrip[] = "Test reading and writing ordered output";
13 */
14
15 int main(int argc, char *argv[])
16 {
17     int errs = 0;
18     int size, rank, i, *buf, rc;
19     MPI_File fh;
20     MPI_Comm comm;
21     MPI_Status status;
22     MPI_Request request;
23
24     MTest_Init(&argc, &argv);
25
26     comm = MPI_COMM_WORLD;
27     MPI_File_open(comm, (char *) "test.ord",
28                   MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE, MPI_INFO_NULL, &fh);
29
30     MPI_Comm_size(comm, &size);
31     MPI_Comm_rank(comm, &rank);
32     buf = (int *) malloc(size * sizeof(int));
33     buf[0] = rank;
34     rc = MPI_File_write_ordered(fh, buf, 1, MPI_INT, &status);
35     if (rc != MPI_SUCCESS) {
36         MTestPrintErrorMsg("File_write_ordered", rc);
37         errs++;
38     }
39     /* make sure all writes finish before we seek/read */
40     MPI_Barrier(comm);
41
42     /* Set the individual pointer to 0, since we want to use a iread_all */
43     MPI_File_seek(fh, 0, MPI_SEEK_SET);
44     rc = MPI_File_iread_all(fh, buf, size, MPI_INT, &request);
45     if (rc != MPI_SUCCESS) {
46         MTestPrintErrorMsg("File_iread_all", rc);
47         errs++;
48     }
49     MPI_Wait(&request, &status);
50
51     for (i = 0; i < size; i++) {
52         if (buf[i] != i) {
53             errs++;
54             fprintf(stderr, "%d: buf[%d] = %d\n", rank, i, buf[i]);
55         }
56     }
57
58     MPI_File_seek_shared(fh, 0, MPI_SEEK_SET);
59     for (i = 0; i < size; i++)
60         buf[i] = -1;
61     MPI_File_read_ordered(fh, buf, 1, MPI_INT, &status);
62     if (buf[0] != rank) {
63         errs++;
64         fprintf(stderr, "%d: buf[0] = %d\n", rank, buf[0]);
65     }
66
67     free(buf);
68     MPI_File_close(&fh);
69
70     MTest_Finalize(errs);
71     MPI_Finalize();
72     return 0;
73 }