Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
still not our
[simgrid.git] / teshsuite / smpi / mpich3-test / datatype / unpack.c
1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2 /*
3  *  (C) 2001 by Argonne National Laboratory.
4  *      See COPYRIGHT in top-level directory.
5  */
6 #include "mpi.h"
7 #include <stdio.h>
8 #include "mpitest.h"
9 #include <stdlib.h>
10 #include <string.h>
11
12 /* Test sent in by Avery Ching to report a bug in MPICH. 
13    Adding it as a regression test. */
14
15 /*
16 static void print_char_buf(char *buf_name, char *buf, int buf_len)
17 {
18     int i;
19
20     printf("print_char_buf: %s\n", buf_name);
21     for (i = 0; i < buf_len; i++)
22     {
23         printf("%c ", buf[i]);
24         if (((i + 1) % 10) == 0)
25             printf("\n");
26         else if (((i + 1) % 5) == 0)
27             printf("  ");
28     }
29     printf("\n");
30 }
31 */
32
33 char correct_buf[] = {'a', '_', 'b', 'c', '_', '_', '_', '_', 'd', '_', 
34                       'e', 'f', 'g', '_', 'h', 'i', 'j', '_', 'k', 'l',
35                       '_', '_', '_', '_', 'm', '_', 'n', 'o', 'p', '_',
36                       'q', 'r'};
37
38 #define COUNT 2
39
40 int main(int argc, char **argv)
41 {
42     int myid, numprocs, i;
43     char *mem_buf = NULL, *unpack_buf = NULL;
44     MPI_Datatype tmp_dtype, mem_dtype;
45     MPI_Aint mem_dtype_ext = -1;
46     int mem_dtype_sz = -1;
47     int mem_buf_sz = -1, unpack_buf_sz = -1, buf_pos = 0;
48
49     int blk_arr[COUNT] = {1, 2};
50     int dsp_arr[COUNT] = {0, 2};
51     int errs = 0;
52
53     MTest_Init(&argc, &argv);
54     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
55     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
56
57     /* Creating the datatype to use for unpacking */
58     MPI_Type_indexed(COUNT, blk_arr, dsp_arr,
59                      MPI_CHAR, &tmp_dtype);
60     MPI_Type_commit(&tmp_dtype);
61     MPI_Type_indexed(COUNT, blk_arr, dsp_arr,
62                      tmp_dtype, &mem_dtype);
63     MPI_Type_free( &tmp_dtype );
64     MPI_Type_commit(&mem_dtype);
65
66     MPI_Type_size(mem_dtype, &mem_dtype_sz);
67     MPI_Type_extent(mem_dtype, &mem_dtype_ext);
68
69     mem_buf_sz    = 2 * mem_dtype_ext;
70     unpack_buf_sz = 2 * mem_dtype_sz;
71
72     if ((mem_buf = (char *) malloc(mem_buf_sz)) == NULL)
73     {
74         fprintf(stderr, "malloc mem_buf of size %d failed\n", mem_buf_sz);
75         return -1;
76     }
77     memset(mem_buf, '_', mem_buf_sz);
78
79     if ((unpack_buf = (char *) malloc(unpack_buf_sz)) == NULL)
80     {
81         fprintf(stderr, "malloc unpack_buf of size %d failed\n", 
82                 unpack_buf_sz);
83         return -1;
84     }
85     
86     for (i = 0; i < unpack_buf_sz; i++)
87         unpack_buf[i] = 'a' + i;
88     
89     /* print_char_buf("mem_buf before unpack", mem_buf, 2 * mem_dtype_ext); */
90
91     MPI_Unpack(unpack_buf, unpack_buf_sz, &buf_pos,
92                mem_buf, 2, mem_dtype, MPI_COMM_SELF);
93     /* Note: Unpack without a Pack is not technically correct, but should work
94      * with MPICH. */
95
96     /* print_char_buf("mem_buf after unpack", mem_buf, 2 * mem_dtype_ext);
97        print_char_buf("correct buffer should be", 
98                        correct_buf, 2 * mem_dtype_ext); */
99
100     if (memcmp(mem_buf, correct_buf, 2 * mem_dtype_ext)) {
101         printf("Unpacked buffer does not match expected buffer\n");
102         errs++;
103     }
104
105     MPI_Type_free(&mem_dtype);
106
107     MTest_Finalize(errs);
108     MPI_Finalize();
109
110     return 0;
111 }