|
Packit Service |
c5cf8c |
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
|
|
Packit Service |
c5cf8c |
/*
|
|
Packit Service |
c5cf8c |
* (C) 2006 by Argonne National Laboratory.
|
|
Packit Service |
c5cf8c |
* See COPYRIGHT in top-level directory.
|
|
Packit Service |
c5cf8c |
*/
|
|
Packit Service |
c5cf8c |
#include <mpi.h>
|
|
Packit Service |
c5cf8c |
#include "mpitest.h"
|
|
Packit Service |
c5cf8c |
#include "mpithreadtest.h"
|
|
Packit Service |
c5cf8c |
#ifdef HAVE_UNISTD_H
|
|
Packit Service |
c5cf8c |
#include <unistd.h>
|
|
Packit Service |
c5cf8c |
#endif
|
|
Packit Service |
c5cf8c |
#include <stdio.h>
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
#ifdef DO_DEBUG
|
|
Packit Service |
c5cf8c |
#define DEBUG(_a){ _a ;fflush(stdout);}
|
|
Packit Service |
c5cf8c |
#else
|
|
Packit Service |
c5cf8c |
#define DEBUG(_a)
|
|
Packit Service |
c5cf8c |
#endif
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
#define NUM_ITER 10000
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
const int REQ_TAG = 111;
|
|
Packit Service |
c5cf8c |
const int ANS_TAG = 222;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* MPI environment description */
|
|
Packit Service |
c5cf8c |
int rank, size, provided;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MTEST_THREAD_RETURN_TYPE listener(void *);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/*
|
|
Packit Service |
c5cf8c |
LISTENER THREAD
|
|
Packit Service |
c5cf8c |
it waits for communication from any source (including calling thread)
|
|
Packit Service |
c5cf8c |
messages which it receives have tag REQ_TAG
|
|
Packit Service |
c5cf8c |
thread runs infinite loop which will stop only if every node in the
|
|
Packit Service |
c5cf8c |
MPI_COMM_WORLD send request containing -1
|
|
Packit Service |
c5cf8c |
*/
|
|
Packit Service |
c5cf8c |
MTEST_THREAD_RETURN_TYPE listener(void *extra)
|
|
Packit Service |
c5cf8c |
{
|
|
Packit Service |
c5cf8c |
int req;
|
|
Packit Service |
c5cf8c |
int source;
|
|
Packit Service |
c5cf8c |
MPI_Status stat;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
int no_fins = 0; /* this must be equal to size to break loop below */
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
while (1) {
|
|
Packit Service |
c5cf8c |
/* wait for request */
|
|
Packit Service |
c5cf8c |
MPI_Recv(&req, 1, MPI_INT, MPI_ANY_SOURCE, REQ_TAG, MPI_COMM_WORLD, &stat;;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* get request source */
|
|
Packit Service |
c5cf8c |
source = stat.MPI_SOURCE;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
DEBUG(printf("node %d got request %d from %d\n", rank, req, source));
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
if (req == -1)
|
|
Packit Service |
c5cf8c |
++no_fins; /* one more node finished requesting */
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* no more requests can arrive */
|
|
Packit Service |
c5cf8c |
if (no_fins == size)
|
|
Packit Service |
c5cf8c |
break;
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
DEBUG(printf("node %d has stopped listener\n", rank));
|
|
Packit Service |
c5cf8c |
return MTEST_THREAD_RETVAL_IGN;
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
int main(int argc, char *argv[])
|
|
Packit Service |
c5cf8c |
{
|
|
Packit Service |
c5cf8c |
int buf = 0;
|
|
Packit Service |
c5cf8c |
long int i, j;
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MTest_Init_thread(&argc, &argv, MPI_THREAD_MULTIPLE, &provided);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
if (provided != MPI_THREAD_MULTIPLE) {
|
|
Packit Service |
c5cf8c |
printf("This test requires MPI_THREAD_MULTIPLE\n");
|
|
Packit Service |
c5cf8c |
MPI_Abort(MPI_COMM_WORLD, 1);
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
|
|
Packit Service |
c5cf8c |
MPI_Comm_size(MPI_COMM_WORLD, &size);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
for (i = 0; i < NUM_ITER; i++) {
|
|
Packit Service |
c5cf8c |
/* create listener thread */
|
|
Packit Service |
c5cf8c |
MTest_Start_thread(listener, NULL);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* no more requests to send
|
|
Packit Service |
c5cf8c |
* inform other in the group that we have finished */
|
|
Packit Service |
c5cf8c |
buf = -1;
|
|
Packit Service |
c5cf8c |
for (j = 0; j < size; ++j) {
|
|
Packit Service |
c5cf8c |
MPI_Send(&buf, 1, MPI_INT, j, REQ_TAG, MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* and wait for others to do the same */
|
|
Packit Service |
c5cf8c |
MTest_Join_threads();
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
/* barrier to avoid deadlock */
|
|
Packit Service |
c5cf8c |
MPI_Barrier(MPI_COMM_WORLD);
|
|
Packit Service |
c5cf8c |
}
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
MTest_Finalize(0);
|
|
Packit Service |
c5cf8c |
|
|
Packit Service |
c5cf8c |
return 0;
|
|
Packit Service |
c5cf8c |
}
|