File: sendrecv2.c

package info (click to toggle)
mpich 4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,184 kB
  • sloc: ansic: 1,040,629; cpp: 82,270; javascript: 40,763; perl: 27,933; python: 16,041; sh: 14,676; xml: 14,418; f90: 12,916; makefile: 9,270; fortran: 8,046; java: 4,635; asm: 324; ruby: 103; awk: 27; lisp: 19; php: 8; sed: 4
file content (129 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download | duplicates (7)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include "mpi.h"
#include "mpitestconf.h"
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include "mpitest.h"

static int verbose = 0;

static int parse_args(int argc, char **argv);

int main(int argc, char *argv[])
{
    int i, j, errs = 0;
    int rank, size;
    MPI_Datatype newtype;
    char *buf = NULL;

    MTest_Init(&argc, &argv);
    parse_args(argc, argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    if (size < 2) {
        if (verbose)
            fprintf(stderr, "comm size must be > 1\n");
        errs++;
        goto fn_exit;
    }

    buf = malloc(64 * 129);
    if (buf == NULL) {
        if (verbose)
            fprintf(stderr, "error allocating buffer\n");
        errs++;
        goto fn_exit;
    }

    for (i = 8; i < 64; i += 4) {
        MPI_Type_vector(i, 128, 129, MPI_CHAR, &newtype);

        MPI_Type_commit(&newtype);
        memset(buf, 0, 64 * 129);

        if (rank == 0) {
            /* init buffer */
            for (j = 0; j < i; j++) {
                int k;
                for (k = 0; k < 129; k++) {
                    buf[129 * j + k] = (char) j;
                }
            }

            /* send */
            MPI_Send(buf, 1, newtype, 1, i, MPI_COMM_WORLD);
        } else if (rank == 1) {
            /* recv */
            MPI_Recv(buf, 1, newtype, 0, i, MPI_COMM_WORLD, MPI_STATUS_IGNORE);

            /* check buffer */
            for (j = 0; j < i; j++) {
                int k;
                for (k = 0; k < 129; k++) {
                    if (k < 128 && buf[129 * j + k] != (char) j) {
                        if (verbose)
                            fprintf(stderr,
                                    "(i=%d, pos=%d) should be %d but is %d\n",
                                    i, 129 * j + k, j, (int) buf[129 * j + k]);
                        errs++;
                    } else if (k == 128 && buf[129 * j + k] != (char) 0) {
                        if (verbose)
                            fprintf(stderr,
                                    "(i=%d, pos=%d) should be %d but is %d\n",
                                    i, 129 * j + k, 0, (int) buf[129 * j + k]);
                        errs++;
                    }
                }
            }
        }

        MPI_Type_free(&newtype);
    }

    if (rank == 0) {
        int recv_errs = 0;

        MPI_Recv(&recv_errs, 1, MPI_INT, 1, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
        if (recv_errs) {
            if (verbose)
                fprintf(stderr, "%d errors reported from receiver\n", recv_errs);
            errs += recv_errs;
        }
    } else if (rank == 1) {
        MPI_Send(&errs, 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
    }

  fn_exit:

    free(buf);
    MTest_Finalize(errs);
    return MTestReturnValue(errs);
}

static int parse_args(int argc, char **argv)
{
    /*
     * int ret;
     *
     * while ((ret = getopt(argc, argv, "v")) >= 0)
     * {
     * switch (ret) {
     * case 'v':
     * verbose = 1;
     * break;
     * }
     * }
     */
    if (argc > 1 && strcmp(argv[1], "-v") == 0)
        verbose = 1;
    return 0;
}