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', '_',
41 int main(int argc, char **argv)
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;
50 int blk_arr[COUNT] = { 1, 2 };
51 int dsp_arr[COUNT] = { 0, 2 };
54 MTest_Init(&argc, &argv);
55 MPI_Comm_rank(MPI_COMM_WORLD, &myid);
56 MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
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);
65 MPI_Type_size(mem_dtype, &mem_dtype_sz);
66 MPI_Type_extent(mem_dtype, &mem_dtype_ext);
68 mem_buf_sz = 2 * mem_dtype_ext;
69 unpack_buf_sz = 2 * mem_dtype_sz;
71 if ((mem_buf = (char *) malloc(mem_buf_sz)) == NULL) {
72 fprintf(stderr, "malloc mem_buf of size %d failed\n", mem_buf_sz);
75 memset(mem_buf, '_', mem_buf_sz);
77 if ((unpack_buf = (char *) malloc(unpack_buf_sz)) == NULL) {
78 fprintf(stderr, "malloc unpack_buf of size %d failed\n", unpack_buf_sz);
82 for (i = 0; i < unpack_buf_sz; i++)
83 unpack_buf[i] = 'a' + i;
85 /* print_char_buf("mem_buf before unpack", mem_buf, 2 * mem_dtype_ext); */
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
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); */
95 if (memcmp(mem_buf, correct_buf, 2 * mem_dtype_ext)) {
96 printf("Unpacked buffer does not match expected buffer\n");
100 MPI_Type_free(&mem_dtype);
104 MTest_Finalize(errs);