Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce the size of partial shared malloc tests.
[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     MPI_Datatype type, duptype;
29     int rank;
30
31     MPI_Init(&argc, &argv);
32
33     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
34
35     foo_initialize();
36
37     MPI_Type_contiguous(2, MPI_INT, &type);
38
39     MPI_Type_set_attr(type, foo_keyval, NULL);
40
41     MPI_Type_dup(type, &duptype);
42
43     my_func = "Free of type";
44     MPI_Type_free(&type);
45
46     my_func = "free of duptype";
47     MPI_Type_free(&duptype);
48
49     foo_finalize();
50
51     if (rank == 0) {
52         int errs = 0;
53         if (copy_called != 1) {
54             printf("Copy called %d times; expected once\n", copy_called);
55             errs++;
56         }
57         if (delete_called != 2) {
58             printf("Delete called %d times; expected twice\n", delete_called);
59             errs++;
60         }
61         if (errs == 0) {
62             printf(" No Errors\n");
63         }
64         else {
65             printf(" Found %d errors\n", errs);
66         }
67         fflush(stdout);
68     }
69
70     MPI_Finalize();
71     return 0;
72 }
73
74 int foo_copy_attr_function(MPI_Datatype type,
75                            int type_keyval,
76                            void *extra_state,
77                            void *attribute_val_in, void *attribute_val_out, int *flag)
78 {
79     if (verbose)
80         printf("copy fn. called\n");
81     copy_called++;
82     *(char **) attribute_val_out = NULL;
83     *flag = 1;
84
85     return MPI_SUCCESS;
86 }
87
88 int foo_delete_attr_function(MPI_Datatype type,
89                              int type_keyval, void *attribute_val, void *extra_state)
90 {
91     if (verbose)
92         printf("delete fn. called in %s\n", my_func);
93     delete_called++;
94
95     return MPI_SUCCESS;
96 }
97
98 int foo_initialize(void)
99 {
100     /* create keyval for use later */
101     MPI_Type_create_keyval(foo_copy_attr_function,
102                                        foo_delete_attr_function, &foo_keyval, NULL);
103     if (verbose)
104         printf("created keyval\n");
105
106     return 0;
107 }
108
109 void foo_finalize(void)
110 {
111     /* remove keyval */
112     MPI_Type_free_keyval(&foo_keyval);
113
114     if (verbose)
115         printf("freed keyval\n");
116
117     return;
118 }