Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
remove unwanted files
[simgrid.git] / teshsuite / smpi / mpich-test / pt2pt / sixth.c
1 #include <stdio.h>
2 #include "mpi.h"
3 #ifdef HAVE_STDLIB_H
4 #include <stdlib.h>
5 #else
6 extern char *malloc();
7 #endif
8 #include "test.h"
9
10 typedef struct _table {
11   int references;
12   int length;
13   int *value;
14 } Table;
15
16 /* Prototypes for picky compilers */
17 int copy_table ( MPI_Comm, int, void *, void *, void *, int * );
18 void create_table ( int, int *, Table ** );
19 int delete_table ( MPI_Comm, int, void *, void * );
20
21 /* These are incorrect...*/
22 int copy_table ( MPI_Comm oldcomm, int keyval, void *extra_state, 
23                  void *attr_in, void *attr_out, int *flag)
24 {
25   Table *table = (Table *)attr_in;;
26
27   table->references++;
28   (*(void **)attr_out) = attr_in;
29   (*flag) = 1;
30   (*(int *)extra_state)++;
31   return (MPI_SUCCESS);
32 }
33
34 void create_table ( int num, int *values, Table **table_out )
35 {
36   int i;
37   (*table_out) = (Table *)malloc(sizeof(Table));
38   (*table_out)->references = 1;
39   (*table_out)->length = num;
40   (*table_out)->value = (int *)malloc(sizeof(int)*num);
41   for (i=0;i<num;i++) 
42     (*table_out)->value[i] = values[i];
43 }
44
45 int delete_table ( MPI_Comm comm, int keyval, 
46                    void *attr_val, void *extra_state)
47 {
48   Table *table = (Table *)attr_val;
49
50   if ( table->references == 1 )
51         free(table);
52   else
53         table->references--;
54   (*(int *)extra_state)--;
55   return MPI_SUCCESS;
56 }
57
58 int main ( int argc, char **argv )
59 {
60   int rank, size;
61   Table *table;
62   MPI_Comm new_comm;
63   int table_key;
64   int values[3]; 
65   int table_copies = 1;
66   int found;
67   int errors = 0;
68
69   MPI_Init ( &argc, &argv );
70   MPI_Comm_rank ( MPI_COMM_WORLD, &rank );
71   MPI_Comm_size ( MPI_COMM_WORLD, &size );
72
73   values[0] = 1; values[1] = 2; values[2] = 3;
74   create_table(3,values,&table);
75   
76   MPI_Keyval_create ( copy_table, delete_table, &table_key, 
77                       (void *)&table_copies );
78   MPI_Attr_put ( MPI_COMM_WORLD, table_key, (void *)table );
79   MPI_Comm_dup ( MPI_COMM_WORLD, &new_comm );
80   MPI_Attr_get ( new_comm, table_key, (void **)&table, &found );
81
82   if (!found) {
83       printf( "did not find attribute on new comm\n" );
84       errors++;
85   }
86
87   if ((table_copies != 2) && (table->references != 2)) {
88       printf( "table_copies != 2 (=%d) and table->references != 2 (=%d)\n",
89               table_copies, table->references );
90       errors++;
91   }
92
93   MPI_Comm_free ( &new_comm );
94
95   if ((table_copies != 1) && (table->references != 1)) {
96       printf( "table_copies != 1 (=%d) and table->references != 1 (=%d)\n",
97               table_copies, table->references );
98       errors++;
99   }
100
101   MPI_Attr_delete ( MPI_COMM_WORLD, table_key );
102
103   if ( table_copies != 0 ) {
104       printf( "table_copies != 0 (=%d)\n", table_copies );
105       errors++;
106   }
107   if (errors)
108     printf("[%d] OOPS.  %d errors!\n",rank,errors);
109
110   MPI_Keyval_free ( &table_key );
111   Test_Waitforall( );
112   MPI_Finalize();
113   return 0;
114 }