File: alltoall.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 (97 lines) | stat: -rw-r--r-- 2,361 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) by Argonne National Laboratory
 *     See COPYRIGHT in top-level directory
 */

#include <mpi.h>
#include "mpitest.h"
#include "mpithreadtest.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdio.h>

#define NUM_ITER 10000

const int REQ_TAG = 111;
const int ANS_TAG = 222;

/* MPI environment description */
int rank, size, provided;

MTEST_THREAD_RETURN_TYPE listener(void *);

/*
   LISTENER THREAD
   it waits for communication from any source (including calling thread)
   messages which it receives have tag REQ_TAG
   thread runs infinite loop which will stop only if every node in the
   MPI_COMM_WORLD send request containing -1
*/
MTEST_THREAD_RETURN_TYPE listener(void *extra)
{
    int req;
    int source;
    MPI_Status stat;

    int no_fins = 0;            /* this must be equal to size to break loop below */

    while (1) {
        /* wait for request */
        MPI_Recv(&req, 1, MPI_INT, MPI_ANY_SOURCE, REQ_TAG, MPI_COMM_WORLD, &stat);

        /* get request source */
        source = stat.MPI_SOURCE;

        MTestPrintfMsg(1, "node %d got request %d from %d\n", rank, req, source);

        if (req == -1)
            ++no_fins;  /* one more node finished requesting */

        /* no more requests can arrive */
        if (no_fins == size)
            break;
    }

    MTestPrintfMsg(1, "node %d has stopped listener\n", rank);
    return MTEST_THREAD_RETVAL_IGN;
}


int main(int argc, char *argv[])
{
    int buf = 0;
    long int i, j;

    MTest_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);

    if (provided != MPI_THREAD_MULTIPLE) {
        printf("This test requires MPI_THREAD_MULTIPLE\n");
        MPI_Abort(MPI_COMM_WORLD, 1);
    }

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

    for (i = 0; i < NUM_ITER; i++) {
        /* create listener thread */
        MTest_Start_thread(listener, NULL);

        /* no more requests to send
         * inform other in the group that we have finished */
        buf = -1;
        for (j = 0; j < size; ++j) {
            MPI_Send(&buf, 1, MPI_INT, j, REQ_TAG, MPI_COMM_WORLD);
        }

        /* and wait for others to do the same */
        MTest_Join_threads();

        /* barrier to avoid deadlock */
        MPI_Barrier(MPI_COMM_WORLD);
    }

    MTest_Finalize(0);

    return 0;
}