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