Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Also add the mpich3 binaries in the dependencies of make tests
[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
39 #define COUNT 2
40
41 int main(int argc, char **argv)
42 {
43     int myid, numprocs, i;
44     char *mem_buf = NULL, *unpack_buf = NULL;
45     MPI_Datatype tmp_dtype, mem_dtype;
46     MPI_Aint mem_dtype_ext = -1;
47     int mem_dtype_sz = -1;
48     int mem_buf_sz = -1, unpack_buf_sz = -1, buf_pos = 0;
49
50     int blk_arr[COUNT] = { 1, 2 };
51     int dsp_arr[COUNT] = { 0, 2 };
52     int errs = 0;
53
54     MTest_Init(&argc, &argv);
55     MPI_Comm_rank(MPI_COMM_WORLD, &myid);
56     MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
57
58     /* Creating the datatype to use for unpacking */
59     MPI_Type_indexed(COUNT, blk_arr, dsp_arr, MPI_CHAR, &tmp_dtype);
60     MPI_Type_commit(&tmp_dtype);
61     MPI_Type_indexed(COUNT, blk_arr, dsp_arr, tmp_dtype, &mem_dtype);
62     MPI_Type_free(&tmp_dtype);
63     MPI_Type_commit(&mem_dtype);
64
65     MPI_Type_size(mem_dtype, &mem_dtype_sz);
66     MPI_Type_extent(mem_dtype, &mem_dtype_ext);
67
68     mem_buf_sz = 2 * mem_dtype_ext;
69     unpack_buf_sz = 2 * mem_dtype_sz;
70
71     if ((mem_buf = (char *) malloc(mem_buf_sz)) == NULL) {
72         fprintf(stderr, "malloc mem_buf of size %d failed\n", mem_buf_sz);
73         return -1;
74     }
75     memset(mem_buf, '_', mem_buf_sz);
76
77     if ((unpack_buf = (char *) malloc(unpack_buf_sz)) == NULL) {
78         fprintf(stderr, "malloc unpack_buf of size %d failed\n", unpack_buf_sz);
79         return -1;
80     }
81
82     for (i = 0; i < unpack_buf_sz; i++)
83         unpack_buf[i] = 'a' + i;
84
85     /* print_char_buf("mem_buf before unpack", mem_buf, 2 * mem_dtype_ext); */
86
87     MPI_Unpack(unpack_buf, unpack_buf_sz, &buf_pos, mem_buf, 2, mem_dtype, MPI_COMM_SELF);
88     /* Note: Unpack without a Pack is not technically correct, but should work
89      * with MPICH. */
90
91     /* print_char_buf("mem_buf after unpack", mem_buf, 2 * mem_dtype_ext);
92      * print_char_buf("correct buffer should be",
93      * correct_buf, 2 * mem_dtype_ext); */
94
95     if (memcmp(mem_buf, correct_buf, 2 * mem_dtype_ext)) {
96         printf("Unpacked buffer does not match expected buffer\n");
97         errs++;
98     }
99
100     MPI_Type_free(&mem_dtype);
101
102     free(mem_buf);
103     free(unpack_buf);
104     MTest_Finalize(errs);
105     MPI_Finalize();
106
107     return 0;
108 }