This source file includes following definitions.
- handle_error
- is_little_or_big_endian
- main
1
2
3
4
5
6
7
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include "mpi.h"
12
13 #define TEST_LE 0x1
14 #define TEST_BE 0x2
15 #define TEST_FILENAME "test.datarep"
16
17 #define CHECK(fn) {int errcode; errcode = (fn); if (errcode != MPI_SUCCESS) handle_error(errcode, NULL); }
18
19 static void handle_error(int errcode, char *str)
20 {
21 char msg[MPI_MAX_ERROR_STRING];
22 int resultlen;
23 MPI_Error_string(errcode, msg, &resultlen);
24 fprintf(stderr, "%s: (%d) %s\n", str, errcode, msg);
25 MPI_Abort(MPI_COMM_WORLD, 1);
26 }
27
28
29
30 static void is_little_or_big_endian( const char* datarep, char* c, char* c_le, int len ) {
31 int i, is_le = 1, is_be = 1;
32 for( i = 0; i < len; i++ ) {
33 is_le = is_le && ( c[i] == c_le[i] );
34 is_be = is_be && ( c[i] == c_le[len-1-i] );
35 }
36 printf( "%s datarep is ", datarep );
37 switch ((is_le ? TEST_LE : 0x0) | (is_be ? TEST_BE : 0x0) ) {
38 case TEST_LE: printf( "LITTLE ENDIAN\n" ); break;
39 case TEST_BE: printf( "BIG ENDIAN\n" ); break;
40 case TEST_LE | TEST_BE: printf( "LITTLE or BIG ENDIAN\n" ); break;
41 default: printf( "unknown\n" ); break;
42 }
43 }
44
45
46 int main( int argc, char* argv[] ) {
47 int sample_i = 123456789, i, j;
48 char sample_i_le[4] = {0x15,0xcd,0x5b,0x07}, c[4];
49 const char* datarep[3] = { "native", "external32", "internal" };
50 MPI_File fileh;
51 int rank;
52 FILE* fileh_std;
53
54 if( sizeof(int) != 4 ) { printf( "non-supported sizeof(int)=%ld\n", sizeof(int) ); return (-1); }
55
56 MPI_Init( &argc, &argv );
57 MPI_Comm_rank( MPI_COMM_WORLD, &rank );
58
59
60 for( i = 0; i < 3; i++ ) {
61
62
63 CHECK(MPI_File_open( MPI_COMM_WORLD, TEST_FILENAME,
64 MPI_MODE_RDWR | MPI_MODE_CREATE, MPI_INFO_NULL, &fileh ) );
65
66
67 CHECK(MPI_File_set_view( fileh, 0, MPI_INT, MPI_INT, datarep[i], MPI_INFO_NULL ));
68
69
70 CHECK(MPI_File_write_at( fileh, (MPI_Offset)rank, (void*)&sample_i, 1,
71 MPI_INT, MPI_STATUS_IGNORE ));
72
73
74 CHECK(MPI_File_close( &fileh ));
75
76
77 MPI_Barrier( MPI_COMM_WORLD );
78 if( rank == 0 ) {
79 fileh_std = fopen( TEST_FILENAME, "r" );
80 for( j = 0; j < 4; j++ ) {
81 if( feof( fileh_std ) ) { printf( "unexpected eof, aborted\n" ); return (-1); }
82 fscanf( fileh_std, "%c", &c[j] );
83 }
84 is_little_or_big_endian( datarep[i], c, sample_i_le, 4 );
85 fclose( fileh_std );
86 }
87
88
89 if( rank == 0 ) {
90 CHECK(MPI_File_delete( TEST_FILENAME, MPI_INFO_NULL ));
91 }
92 }
93
94 MPI_Finalize();
95
96 return 0;
97 }