Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill trailing whitespaces in source code files.
[simgrid.git] / teshsuite / smpi / io-simple / io-simple.c
1 /* Copyright (c) 2019-2021. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <memory.h>
11
12 /* Test reading and writing zero bytes (set status correctly) */
13
14 int main( int argc, char *argv[] )
15 {
16     int errs = 0;
17     int size;
18     int rank;
19     int* buf;
20     int count;
21     MPI_File fh;
22     MPI_Comm comm;
23     MPI_Status status;
24
25     MPI_Init( &argc, &argv );
26
27     comm = MPI_COMM_WORLD;
28     MPI_File_open( comm, (char*)"/scratch/testfile", MPI_MODE_RDWR | MPI_MODE_CREATE | MPI_MODE_DELETE_ON_CLOSE, MPI_INFO_NULL, &fh );
29     MPI_Comm_size( comm, &size );
30     MPI_Comm_rank( comm, &rank );
31     buf = (int *)malloc( size * sizeof(int) );
32     buf[0] = rank;
33
34     /* Write to file */
35     MPI_File_seek(fh, sizeof(int) * rank, MPI_SEEK_SET);
36     MPI_File_write( fh, buf, 1, MPI_INT, &status );
37     MPI_Get_count( &status, MPI_INT, &count );
38     if (count != 1) {
39         errs++;
40         fprintf( stderr, "Wrong count (%d) on write\n", count );fflush(stderr);
41     }
42
43     /* Read nothing (check status) */
44     memset( &status, 0xff, sizeof(MPI_Status) );
45     MPI_File_read( fh, buf, 0, MPI_INT, &status );
46     MPI_Get_count( &status, MPI_INT, &count );
47     if (count != 0) {
48         errs++;
49         fprintf( stderr, "Count not zero (%d) on read\n", count );fflush(stderr);
50     }
51
52     /* Write nothing (check status) */
53     memset( &status, 0xff, sizeof(MPI_Status) );
54     MPI_File_write( fh, buf, 0, MPI_INT, &status );
55     if (count != 0) {
56         errs++;
57         fprintf( stderr, "Count not zero (%d) on write\n", count );fflush(stderr);
58     }
59
60     MPI_Barrier( comm );
61
62     MPI_File_seek( fh, sizeof(int)*rank, MPI_SEEK_SET );
63     for (int i = 0; i < size; i++)
64       buf[i] = -1;
65     MPI_File_read( fh, buf, 1, MPI_INT, &status );
66     // if (buf[0] != rank) {
67         // errs++;
68         // fprintf( stderr, "%d: buf = %d\n", rank, buf[0] );fflush(stderr);
69     // }
70
71     free( buf );
72     MPI_File_close( &fh );
73
74     MPI_Finalize();
75     return errs;
76 }