Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add MPI_Comm_*keyval and MPI_Comm*attr functions
[simgrid.git] / teshsuite / smpi / mpich3-test / attr / attr2type.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *
4  *  (C) 2007 by Argonne National Laboratory.
5  *      See COPYRIGHT in top-level directory.
6  */
7 #include <mpi.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 static int foo_keyval = MPI_KEYVAL_INVALID;
12
13 int foo_initialize(void);
14 void foo_finalize(void);
15
16 int foo_copy_attr_function(MPI_Datatype type, int type_keyval,
17                            void *extra_state, void *attribute_val_in,
18                            void *attribute_val_out, int *flag);
19 int foo_delete_attr_function(MPI_Datatype type, int type_keyval,
20                              void *attribute_val, void *extra_state);
21 static const char *my_func = 0;
22 static int verbose = 0;
23 static int delete_called = 0;
24 static int copy_called = 0;
25
26 int main(int argc, char *argv[])
27 {
28     int mpi_errno;
29     MPI_Datatype type, duptype;
30     int rank;
31
32     MPI_Init(&argc, &argv);
33     
34     MPI_Comm_rank( MPI_COMM_WORLD, &rank );
35
36     foo_initialize();
37
38     mpi_errno = MPI_Type_contiguous(2, MPI_INT, &type);
39
40     mpi_errno = MPI_Type_set_attr(type, foo_keyval, NULL);
41
42     mpi_errno = MPI_Type_dup(type, &duptype);
43
44     my_func = "Free of type";
45     mpi_errno = MPI_Type_free(&type);
46
47     my_func = "free of duptype";
48     mpi_errno = MPI_Type_free(&duptype);
49
50     foo_finalize();
51
52     if (rank == 0) {
53       int errs = 0;
54       if (copy_called != 1) {
55         printf( "Copy called %d times; expected once\n", copy_called );
56         errs++;
57       }
58       if (delete_called != 2) {
59         printf( "Delete called %d times; expected twice\n", delete_called );
60         errs++;
61       }
62       if (errs == 0) {
63         printf( " No Errors\n" );
64       }else if(mpi_errno!=MPI_SUCCESS){
65         printf( " Output fail - Found %d errors\n", errs );
66       }else {
67         printf( " Found %d errors\n", errs );
68       }
69       fflush(stdout);
70     }
71
72     MPI_Finalize();
73     return 0;
74 }
75
76 int foo_copy_attr_function(MPI_Datatype type,
77                            int type_keyval,
78                            void *extra_state,
79                            void *attribute_val_in,
80                            void *attribute_val_out,
81                            int *flag)
82 {
83     if (verbose) printf("copy fn. called\n");
84     copy_called ++;
85     * (char **) attribute_val_out = NULL;
86     *flag = 1;
87
88     return MPI_SUCCESS;
89 }
90
91 int foo_delete_attr_function(MPI_Datatype type,
92                              int type_keyval,
93                              void *attribute_val,
94                              void *extra_state)
95 {
96     if (verbose) printf("delete fn. called in %s\n", my_func );
97     delete_called ++;
98
99     return MPI_SUCCESS;
100 }
101
102 int foo_initialize(void)
103 {
104     int mpi_errno;
105
106     /* create keyval for use later */
107     mpi_errno = MPI_Type_create_keyval(foo_copy_attr_function,
108                                        foo_delete_attr_function,
109                                        &foo_keyval,
110                                        NULL);
111     if (mpi_errno==MPI_SUCCESS && verbose) printf("created keyval\n");
112
113     return 0;
114 }
115
116 void foo_finalize(void)
117 {
118     int mpi_errno;
119
120     /* remove keyval */
121     mpi_errno = MPI_Type_free_keyval(&foo_keyval);
122
123     if (mpi_errno==MPI_SUCCESS && verbose) printf("freed keyval\n");
124
125     return;
126 }