| 1 | /*****************************************************************************
|
|---|
| 2 | * FILE: mpithreads_both.c
|
|---|
| 3 | * DESCRIPTION:
|
|---|
| 4 | * This program illustrates the simultaneous use of MPI and Pthreads.
|
|---|
| 5 | * It is essentially a simple combination of a code that implements a dot
|
|---|
| 6 | * product using threads, and a code that uses MPI for the same purpose.
|
|---|
| 7 | * It is the last of four codes used to show the progression from a serial
|
|---|
| 8 | * program to a hybrid MPI/Pthreads program. The other relevant codes are:
|
|---|
| 9 | * - mpithreads_serial.c - The serial version
|
|---|
| 10 | * - mpithreads_threads.c - A shared memory programming model using
|
|---|
| 11 | * Pthreads
|
|---|
| 12 | * - mpithreads_mpi.c - A distributed memory programming model with MPI
|
|---|
| 13 | * All the internode MPI communication is done by the main thread on each
|
|---|
| 14 | * node - the other threads within that node need not even be aware that
|
|---|
| 15 | * internode communication is being performed. Use of the SPMD model for
|
|---|
| 16 | * MPI was chosen for convenience, with replication of the main data on
|
|---|
| 17 | * all nodes. A more memory efficient implementation would be advisable
|
|---|
| 18 | * for larger data sets. This is the simplest model for mixed MPI/Pthreads
|
|---|
| 19 | * programming.
|
|---|
| 20 | * SOURCE: Vijay Sonnad, IBM
|
|---|
| 21 | * LAST REVISED: 01/29/09 Blaise Barney
|
|---|
| 22 | ******************************************************************************/
|
|---|
| 23 | #include "mpi.h"
|
|---|
| 24 | #include <pthread.h>
|
|---|
| 25 | #include <stdio.h>
|
|---|
| 26 | #include <stdlib.h>
|
|---|
| 27 |
|
|---|
| 28 | /* Define globally accessible variables*/
|
|---|
| 29 | pthread_t callThd[2];
|
|---|
| 30 |
|
|---|
| 31 | /*
|
|---|
| 32 | The function dotprod has only minor changes from the code
|
|---|
| 33 | that used threads or MPI.
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | void *dotprod(void *arg)
|
|---|
| 37 | {
|
|---|
| 38 |
|
|---|
| 39 | /* Define and use local variables for convenience */
|
|---|
| 40 |
|
|---|
| 41 | int myid;
|
|---|
| 42 | long mythrd;
|
|---|
| 43 |
|
|---|
| 44 | /*
|
|---|
| 45 | The number of threads and nodes defines the beginning
|
|---|
| 46 | and ending for the dot product; each thread does work
|
|---|
| 47 | on a vector of length VECLENGTH.
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | mythrd = (long)arg;
|
|---|
| 51 | MPI_Comm_rank (MPI_COMM_WORLD, &myid);
|
|---|
| 52 | if (myid == 0) {
|
|---|
| 53 | MPI_Send(NULL, 0, MPI_INT, 1, mythrd, MPI_COMM_WORLD);
|
|---|
| 54 | MPI_Send(NULL, 0, MPI_INT, 1, mythrd, MPI_COMM_WORLD);
|
|---|
| 55 | } else if (myid == 1) {
|
|---|
| 56 | MPI_Recv(NULL, 0, MPI_INT, 0, mythrd, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 57 | MPI_Recv(NULL, 0, MPI_INT, 0, mythrd, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
|
|---|
| 58 | }
|
|---|
| 59 | pthread_exit((void*)0);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /*
|
|---|
| 63 | As before,the main program does very little computation. It creates
|
|---|
| 64 | threads on each node and the main thread does all the MPI calls.
|
|---|
| 65 | */
|
|---|
| 66 |
|
|---|
| 67 | int main(int argc, char* argv[])
|
|---|
| 68 | {
|
|---|
| 69 | int myid, numprocs;
|
|---|
| 70 | long i;
|
|---|
| 71 | int nump1, numthrds;
|
|---|
| 72 | void *status;
|
|---|
| 73 | pthread_attr_t attr;
|
|---|
| 74 |
|
|---|
| 75 | /* MPI Initialization */
|
|---|
| 76 | MPI_Init (&argc, &argv);
|
|---|
| 77 | MPI_Comm_size (MPI_COMM_WORLD, &numprocs);
|
|---|
| 78 | MPI_Comm_rank (MPI_COMM_WORLD, &myid);
|
|---|
| 79 |
|
|---|
| 80 | /* Assign storage and initialize values */
|
|---|
| 81 | numthrds=2;
|
|---|
| 82 |
|
|---|
| 83 | /*
|
|---|
| 84 | Create thread attribute to specify that the main thread needs
|
|---|
| 85 | to join with the threads it creates.
|
|---|
| 86 | */
|
|---|
| 87 | pthread_attr_init(&attr );
|
|---|
| 88 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
|
|---|
| 89 |
|
|---|
| 90 | /* Create threads within this node to perform the dotproduct */
|
|---|
| 91 | for(i=0;i<numthrds;i++) {
|
|---|
| 92 | pthread_create( &callThd[i], &attr, dotprod, (void *)i);
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | /* Release the thread attribute handle as it is no longer needed */
|
|---|
| 96 | pthread_attr_destroy(&attr );
|
|---|
| 97 |
|
|---|
| 98 | /* Wait on the other threads within this node */
|
|---|
| 99 | for(i=0;i<numthrds;i++) {
|
|---|
| 100 | pthread_join( callThd[i], &status);
|
|---|
| 101 | }
|
|---|
| 102 | MPI_Finalize();
|
|---|
| 103 | //exit (0);
|
|---|
| 104 | }
|
|---|