Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / mpich3-test / rma / get-struct.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2014 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include "mpitest.h"
8 #include <stdio.h>
9 #include <string.h>
10
11 /* Communicating a datatype built out of structs
12  * This test was motivated by the failure of an example program for
13  * RMA involving simple operations on a struct that included a struct
14  *
15  * The observed failure was a SEGV in the MPI_Get
16  *
17  *
18  */
19 #define MAX_KEY_SIZE 64
20 #define MAX_VALUE_SIZE 256
21 typedef struct {
22     MPI_Aint disp;
23     int rank;
24     void *lptr;
25 } Rptr;
26 typedef struct {
27     Rptr next;
28     char key[MAX_KEY_SIZE], value[MAX_VALUE_SIZE];
29 } ListElm;
30 Rptr nullDptr = { 0, -1, 0 };
31
32 int testCases = -1;
33 #define BYTE_ONLY 0x1
34 #define TWO_STRUCT 0x2
35 int isOneLevel = 0;
36
37 int main(int argc, char **argv)
38 {
39     int errors = 0;
40     Rptr headDptr;
41     ListElm *headLptr = 0;
42     int i, wrank;
43     MPI_Datatype dptrType, listelmType;
44     MPI_Win listwin;
45
46     MTest_Init(&argc, &argv);
47     MPI_Comm_rank(MPI_COMM_WORLD, &wrank);
48
49     for (i = 1; i < argc; i++) {
50         if (strcmp(argv[i], "-byteonly") == 0) {
51             testCases = BYTE_ONLY;
52         }
53         else if (strcmp(argv[i], "-twostruct") == 0) {
54             testCases = TWO_STRUCT;
55         }
56         else if (strcmp(argv[i], "-onelevel") == 0) {
57             isOneLevel = 1;
58         }
59         else {
60             printf("Unrecognized argument %s\n", argv[i]);
61         }
62     }
63
64     /* Create the datatypes that we will use to move the data */
65     {
66         int blens[3];
67         MPI_Aint displ[3];
68         MPI_Datatype dtypes[3];
69         ListElm sampleElm;
70
71         blens[0] = 1;
72         blens[1] = 1;
73         MPI_Get_address(&nullDptr.disp, &displ[0]);
74         MPI_Get_address(&nullDptr.rank, &displ[1]);
75         displ[1] = displ[1] - displ[0];
76         displ[0] = 0;
77         dtypes[0] = MPI_AINT;
78         dtypes[1] = MPI_INT;
79         MPI_Type_create_struct(2, blens, displ, dtypes, &dptrType);
80         MPI_Type_commit(&dptrType);
81
82         if (isOneLevel) {
83             blens[0] = sizeof(nullDptr);
84             dtypes[0] = MPI_BYTE;
85         }
86         else {
87             blens[0] = 1;
88             dtypes[0] = dptrType;
89         }
90         blens[1] = MAX_KEY_SIZE;
91         dtypes[1] = MPI_CHAR;
92         blens[2] = MAX_VALUE_SIZE;
93         dtypes[2] = MPI_CHAR;
94         MPI_Get_address(&sampleElm.next, &displ[0]);
95         MPI_Get_address(&sampleElm.key[0], &displ[1]);
96         MPI_Get_address(&sampleElm.value[0], &displ[2]);
97         displ[2] -= displ[0];
98         displ[1] -= displ[0];
99         displ[0] = 0;
100         for (i = 0; i < 3; i++) {
101             MTestPrintfMsg(0, "%d:count=%d,disp=%ld\n", i, blens[i], displ[i]);
102         }
103         MPI_Type_create_struct(3, blens, displ, dtypes, &listelmType);
104         MPI_Type_commit(&listelmType);
105     }
106
107     MPI_Win_create_dynamic(MPI_INFO_NULL, MPI_COMM_WORLD, &listwin);
108
109     headDptr.rank = 0;
110     if (wrank == 0) {
111         /* Create 1 list element (the head) and initialize it */
112         MPI_Alloc_mem(sizeof(ListElm), MPI_INFO_NULL, &headLptr);
113         MPI_Get_address(headLptr, &headDptr.disp);
114         headLptr->next.rank = -1;
115         headLptr->next.disp = (MPI_Aint) MPI_BOTTOM;
116         headLptr->next.lptr = 0;
117         strncpy(headLptr->key, "key1", MAX_KEY_SIZE);
118         strncpy(headLptr->value, "value1", MAX_VALUE_SIZE);
119         MPI_Win_attach(listwin, headLptr, sizeof(ListElm));
120     }
121     MPI_Bcast(&headDptr.disp, 1, MPI_AINT, 0, MPI_COMM_WORLD);
122
123     MPI_Barrier(MPI_COMM_WORLD);
124
125     if (wrank == 1) {
126         ListElm headcopy;
127
128         MPI_Win_lock_all(0, listwin);
129         /* Get head element with simple get of BYTES */
130         if (testCases & BYTE_ONLY) {
131             headcopy.next.rank = 100;
132             headcopy.next.disp = 0xefefefef;
133             MPI_Get(&headcopy, sizeof(ListElm), MPI_BYTE,
134                     headDptr.rank, headDptr.disp, sizeof(ListElm), MPI_BYTE, listwin);
135             MPI_Win_flush(headDptr.rank, listwin);
136             if (headcopy.next.rank != -1 && headcopy.next.disp != (MPI_Aint) MPI_BOTTOM) {
137                 errors++;
138                 printf("MPI_BYTE: headcopy contains incorrect next:<%d,%ld>\n",
139                        headcopy.next.rank, (long) headcopy.next.disp);
140             }
141         }
142
143         if (testCases & TWO_STRUCT) {
144             headcopy.next.rank = 100;
145             headcopy.next.disp = 0xefefefef;
146             /* Get head element using struct of struct type.  This is
147              * not an identical get to the simple BYTE one above but should
148              * work */
149             MPI_Get(&headcopy, 1, listelmType, headDptr.rank, headDptr.disp,
150                     1, listelmType, listwin);
151             MPI_Win_flush(headDptr.rank, listwin);
152             if (headcopy.next.rank != -1 && headcopy.next.disp != (MPI_Aint) MPI_BOTTOM) {
153                 errors++;
154                 printf("ListelmType: headcopy contains incorrect next:<%d,%ld>\n",
155                        headcopy.next.rank, (long) headcopy.next.disp);
156             }
157         }
158
159         MPI_Win_unlock_all(listwin);
160     }
161     MPI_Barrier(MPI_COMM_WORLD);
162     if (wrank == 0) {
163         MPI_Win_detach(listwin, headLptr);
164         MPI_Free_mem(headLptr);
165     }
166     MPI_Win_free(&listwin);
167     MPI_Type_free(&dptrType);
168     MPI_Type_free(&listelmType);
169
170     MTest_Finalize(errors);
171     MPI_Finalize();
172     return MTestReturnValue(errors);
173 }