Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge commit '045db1657e870c721be490b411868f4181a12ced' into surf++
[simgrid.git] / teshsuite / smpi / mpich3-test / attr / attric.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2001 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 /*
8
9   Exercise communicator routines for intercommunicators
10
11   This C version derived from attrt, which in turn was
12   derived from a Fortran test program from ...
13
14  */
15 #include <stdio.h>
16 #include "mpi.h"
17 #include "mpitest.h"
18
19 /* #define DEBUG */
20 int test_communicators ( void );
21 int copy_fn ( MPI_Comm, int, void *, void *, void *, int * );
22 int delete_fn ( MPI_Comm, int, void *, void * );
23 #ifdef DEBUG
24 #define FFLUSH fflush(stdout);
25 #else
26 #define FFLUSH
27 #endif
28
29 int main( int argc, char **argv )
30 {
31     int errs = 0;
32     MTest_Init( &argc, &argv );
33     
34     errs = test_communicators();
35
36     MTest_Finalize( errs );
37     MPI_Finalize();
38     return 0;
39 }
40
41 int copy_fn( MPI_Comm oldcomm, int keyval, void *extra_state,
42              void *attribute_val_in, void *attribute_val_out, int *flag)
43 {
44     /* Note that if (sizeof(int) < sizeof(void *), just setting the int
45        part of attribute_val_out may leave some dirty bits
46     */
47     *(MPI_Aint *)attribute_val_out = (MPI_Aint)attribute_val_in;
48     *flag = 1;
49     return MPI_SUCCESS;
50 }
51
52 int delete_fn( MPI_Comm comm, int keyval, void *attribute_val, 
53                void *extra_state)
54 {
55     int world_rank;
56     MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
57     if ((MPI_Aint)attribute_val != (MPI_Aint)world_rank) {
58         printf( "incorrect attribute value %d\n", *(int*)attribute_val );
59         MPI_Abort(MPI_COMM_WORLD, 1005 );
60     }
61     return MPI_SUCCESS;
62 }
63
64 int test_communicators( void )
65 {
66     MPI_Comm dup_comm, comm;
67     void *vvalue;
68     int flag, world_rank, world_size, key_1, key_3;
69     int errs = 0;
70     MPI_Aint value;
71     int      isLeft;
72
73     MPI_Comm_rank( MPI_COMM_WORLD, &world_rank );
74     MPI_Comm_size( MPI_COMM_WORLD, &world_size );
75 #ifdef DEBUG
76     if (world_rank == 0) {
77         printf( "*** Communicators ***\n" ); fflush(stdout);
78     }
79 #endif
80
81     while (MTestGetIntercomm( &comm, &isLeft, 2 )) {
82         MTestPrintfMsg(1, "start while loop, isLeft=%s\n", (isLeft ? "TRUE" : "FALSE"));
83
84         if (comm == MPI_COMM_NULL) {
85             MTestPrintfMsg(1, "got COMM_NULL, skipping\n");
86             continue;
87         }
88
89         /*
90           Check Comm_dup by adding attributes to comm & duplicating
91         */
92     
93         value = 9;
94         MPI_Keyval_create(copy_fn,     delete_fn,   &key_1, &value );
95         MTestPrintfMsg(1, "Keyval_create key=%#x value=%d\n", key_1, value);
96         value = 7;
97         MPI_Keyval_create(MPI_NULL_COPY_FN, MPI_NULL_DELETE_FN,
98                           &key_3, &value ); 
99         MTestPrintfMsg(1, "Keyval_create key=%#x value=%d\n", key_3, value);
100
101         /* This may generate a compilation warning; it is, however, an
102            easy way to cache a value instead of a pointer */
103         /* printf( "key1 = %x key3 = %x\n", key_1, key_3 ); */
104         MPI_Attr_put(comm, key_1, (void *) (MPI_Aint) world_rank );
105         MPI_Attr_put(comm, key_3, (void *)0 );
106         
107         MTestPrintfMsg(1, "Comm_dup\n" );
108         MPI_Comm_dup(comm, &dup_comm );
109
110         /* Note that if sizeof(int) < sizeof(void *), we can't use
111            (void **)&value to get the value we passed into Attr_put.  To avoid 
112            problems (e.g., alignment errors), we recover the value into 
113            a (void *) and cast to int. Note that this may generate warning
114            messages from the compiler.  */
115         MPI_Attr_get(dup_comm, key_1, (void **)&vvalue, &flag );
116         value = (MPI_Aint)vvalue;
117         
118         if (! flag) {
119             errs++;
120             printf( "dup_comm key_1 not found on %d\n", world_rank );
121             fflush( stdout );
122             MPI_Abort(MPI_COMM_WORLD, 3004 );
123         }
124         
125         if (value != world_rank) {
126             errs++;
127             printf( "dup_comm key_1 value incorrect: %ld\n", (long)value );
128             fflush( stdout );
129             MPI_Abort(MPI_COMM_WORLD, 3005 );
130         }
131
132         MPI_Attr_get(dup_comm, key_3, (void **)&vvalue, &flag );
133         value = (MPI_Aint)vvalue;
134         if (flag) {
135             errs++;
136             printf( "dup_comm key_3 found!\n" );
137             fflush( stdout );
138             MPI_Abort(MPI_COMM_WORLD, 3008 );
139         }
140         MTestPrintfMsg(1, "Keyval_free key=%#x\n", key_1);
141         MPI_Keyval_free(&key_1 );
142         MTestPrintfMsg(1, "Keyval_free key=%#x\n", key_3);
143         MPI_Keyval_free(&key_3 );
144         /*
145           Free all communicators created
146         */
147         MTestPrintfMsg(1, "Comm_free comm\n");
148         MPI_Comm_free( &comm );
149         MTestPrintfMsg(1, "Comm_free dup_comm\n");
150         MPI_Comm_free( &dup_comm );
151     }
152
153     return errs;
154 }
155