1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
3 * (C) 2001 by Argonne National Laboratory.
4 * See COPYRIGHT in top-level directory.
12 /* Test sent in by Avery Ching to report a bug in MPICH.
13 Adding it as a regression test. */
16 static void print_char_buf(char *buf_name, char *buf, int buf_len)
20 printf("print_char_buf: %s\n", buf_name);
21 for (i = 0; i < buf_len; i++)
23 printf("%c ", buf[i]);
24 if (((i + 1) % 10) == 0)
26 else if (((i + 1) % 5) == 0)
33 char correct_buf[] = {'a', '_', 'b', 'c', '_', '_', '_', '_', 'd', '_',
34 'e', 'f', 'g', '_', 'h', 'i', 'j', '_', 'k', 'l',
35 '_', '_', '_', '_', 'm', '_', 'n', 'o', 'p', '_',
40 int main(int argc, char **argv)
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;
49 int blk_arr[COUNT] = {1, 2};
50 int dsp_arr[COUNT] = {0, 2};
53 MTest_Init(&argc, &argv);
54 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
55 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
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);
66 MPI_Type_size(mem_dtype, &mem_dtype_sz);
67 MPI_Type_extent(mem_dtype, &mem_dtype_ext);
69 mem_buf_sz = 2 * mem_dtype_ext;
70 unpack_buf_sz = 2 * mem_dtype_sz;
72 if ((mem_buf = (char *) malloc(mem_buf_sz)) == NULL)
74 fprintf(stderr, "malloc mem_buf of size %d failed\n", mem_buf_sz);
77 memset(mem_buf, '_', mem_buf_sz);
79 if ((unpack_buf = (char *) malloc(unpack_buf_sz)) == NULL)
81 fprintf(stderr, "malloc unpack_buf of size %d failed\n",
86 for (i = 0; i < unpack_buf_sz; i++)
87 unpack_buf[i] = 'a' + i;
89 /* print_char_buf("mem_buf before unpack", mem_buf, 2 * mem_dtype_ext); */
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
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); */
100 if (memcmp(mem_buf, correct_buf, 2 * mem_dtype_ext)) {
101 printf("Unpacked buffer does not match expected buffer\n");
105 MPI_Type_free(&mem_dtype);
107 MTest_Finalize(errs);