File: alltoall.c

package info (click to toggle)
mpich 3.2-7
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 81,040 kB
  • ctags: 68,664
  • sloc: ansic: 358,905; f90: 54,597; perl: 18,527; cpp: 10,203; sh: 9,839; xml: 8,195; fortran: 7,799; makefile: 4,868; ruby: 53; sed: 9; php: 8
file content (109 lines) | stat: -rw-r--r-- 2,605 bytes parent folder | download
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
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/*
 *  (C) 2006 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>

#ifdef DO_DEBUG
#define DEBUG(_a){ _a ;fflush(stdout);}
#else
#define DEBUG(_a)
#endif

#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;

        DEBUG(printf("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;

    }

    DEBUG(printf("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;

    MPI_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);
    }

    MPI_Finalize();

    /* This program works if it gets here */
    if (rank == 0) {
        printf(" No Errors\n");
    }

    return 0;
}