File: unpack.c

package info (click to toggle)
mpich 4.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 423,384 kB
  • sloc: ansic: 1,088,434; cpp: 71,364; javascript: 40,763; f90: 22,829; sh: 17,463; perl: 14,773; xml: 14,418; python: 10,265; makefile: 9,246; fortran: 8,008; java: 4,355; asm: 324; ruby: 176; lisp: 19; php: 8; sed: 4
file content (107 lines) | stat: -rw-r--r-- 2,988 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include <stdio.h>
#include "mpitest.h"
#include <stdlib.h>
#include <string.h>

/* Test sent in by Avery Ching to report a bug in MPICH.
   Adding it as a regression test. */

/*
static void print_char_buf(char *buf_name, char *buf, int buf_len)
{
    int i;

    printf("print_char_buf: %s\n", buf_name);
    for (i = 0; i < buf_len; i++)
    {
        printf("%c ", buf[i]);
        if (((i + 1) % 10) == 0)
            printf("\n");
        else if (((i + 1) % 5) == 0)
            printf("  ");
    }
    printf("\n");
}
*/

char correct_buf[] = { 'a', '_', 'b', 'c', '_', '_', '_', '_', 'd', '_',
    'e', 'f', 'g', '_', 'h', 'i', 'j', '_', 'k', 'l',
    '_', '_', '_', '_', 'm', '_', 'n', 'o', 'p', '_',
    'q', 'r'
};

#define COUNT 2

int main(int argc, char **argv)
{
    int myid, numprocs, i;
    char *mem_buf = NULL, *unpack_buf = NULL;
    MPI_Datatype tmp_dtype, mem_dtype;
    MPI_Aint mem_dtype_ext = -1, tmp_lb;
    int mem_dtype_sz = -1;
    int mem_buf_sz = -1, unpack_buf_sz = -1, buf_pos = 0;

    int blk_arr[COUNT] = { 1, 2 };
    int dsp_arr[COUNT] = { 0, 2 };
    int errs = 0;

    MTest_Init(&argc, &argv);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);
    MPI_Comm_size(MPI_COMM_WORLD, &numprocs);

    /* Creating the datatype to use for unpacking */
    MPI_Type_indexed(COUNT, blk_arr, dsp_arr, MPI_CHAR, &tmp_dtype);
    MPI_Type_commit(&tmp_dtype);
    MPI_Type_indexed(COUNT, blk_arr, dsp_arr, tmp_dtype, &mem_dtype);
    MPI_Type_free(&tmp_dtype);
    MPI_Type_commit(&mem_dtype);

    MPI_Type_size(mem_dtype, &mem_dtype_sz);
    MPI_Type_get_extent(mem_dtype, &tmp_lb, &mem_dtype_ext);

    mem_buf_sz = 2 * mem_dtype_ext;
    unpack_buf_sz = 2 * mem_dtype_sz;

    if ((mem_buf = (char *) malloc(mem_buf_sz)) == NULL) {
        fprintf(stderr, "malloc mem_buf of size %d failed\n", mem_buf_sz);
        return -1;
    }
    memset(mem_buf, '_', mem_buf_sz);

    if ((unpack_buf = (char *) malloc(unpack_buf_sz)) == NULL) {
        fprintf(stderr, "malloc unpack_buf of size %d failed\n", unpack_buf_sz);
        return -1;
    }

    for (i = 0; i < unpack_buf_sz; i++)
        unpack_buf[i] = 'a' + i;

    /* print_char_buf("mem_buf before unpack", mem_buf, 2 * mem_dtype_ext); */

    MPI_Unpack(unpack_buf, unpack_buf_sz, &buf_pos, mem_buf, 2, mem_dtype, MPI_COMM_SELF);
    /* Note: Unpack without a Pack is not technically correct, but should work
     * with MPICH. */

    /* print_char_buf("mem_buf after unpack", mem_buf, 2 * mem_dtype_ext);
     * print_char_buf("correct buffer should be",
     * correct_buf, 2 * mem_dtype_ext); */

    if (memcmp(mem_buf, correct_buf, 2 * mem_dtype_ext)) {
        printf("Unpacked buffer does not match expected buffer\n");
        errs++;
    }

    MPI_Type_free(&mem_dtype);

    free(mem_buf);
    free(unpack_buf);
    MTest_Finalize(errs);

    return MTestReturnValue(errs);
}