-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmpi.c
More file actions
83 lines (83 loc) · 1.55 KB
/
Copy pathmpi.c
File metadata and controls
83 lines (83 loc) · 1.55 KB
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
//
// Torbert, 27 October 2014
//
// MPI Demo
// mpicc mpiDemo.c
// mpirun -np 4 a.out
//
// Manager-Worker model for parallel processing.
//
// time ... real ... user
//
// htop
//
#include <stdio.h>
//
#include "mpi.h"
//
int main( int argc , char* argv[] )
{
//
// MPI variables
//
int rank ;
int size ;
MPI_Status status ;
int tag = 0;
//
// other variables
//
int k , j ;
double result ;
//
// boilerplate
//
MPI_Init( &argc , &argv ) ;
MPI_Comm_size( MPI_COMM_WORLD , &size ) ; // same
MPI_Comm_rank( MPI_COMM_WORLD , &rank ) ; // different
//
// manager has rank = 0
//
if( rank == 0 )
{
printf( "\n" ) ;
//
result = 3.141 ; // bogus
//
for( j = 1 ; j < size ; j++ )
{
MPI_Send( &result , 1 , MPI_DOUBLE , j , tag , MPI_COMM_WORLD ) ;
}
//
for( k = 1 ; k < size ; k++ )
{
MPI_Recv( &result , 1 , MPI_DOUBLE , MPI_ANY_SOURCE , tag , MPI_COMM_WORLD , &status ) ;
//
j = status.MPI_SOURCE ;
//
printf( "%d %d %20.16f\n" , j , size , result ) ;
}
//
printf( "\n" );
}
//
// workers have rank > 0
//
else
{
MPI_Recv( &result , 1 , MPI_DOUBLE , 0 , tag , MPI_COMM_WORLD , &status ) ;
//
result *= 2.0 ;
//
MPI_Send( &result , 1 , MPI_DOUBLE , 0 , tag , MPI_COMM_WORLD ) ;
}
//
// boilerplate
//
MPI_Finalize() ;
//
return 0;
}
//
// end of file
//