-
Notifications
You must be signed in to change notification settings - Fork 1
/
openmp_mpi.c
90 lines (75 loc) · 2.04 KB
/
openmp_mpi.c
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <mpi.h>
#define pi2o6 1.6449340668482264364724151666460251892189499012067984
typedef double Real;
void iter(int length);
Real sum_array( double* vec , long limit );
Real * genarray ( long length );
int numprocs,rank,namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];
MPI_Status status;
int main(int argc, char ** argv)
{
MPI_Init( &argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Get_processor_name(processor_name, &namelen);
for(int i = 3; i<15; i++)
iter(i);
MPI_Finalize();
return 0;
}
void iter(int length)
{
int partition_size = ( 1 << length ) / numprocs,
offset = ( 1 << length) % partition_size;
Real * array ;
if ( rank == 0 ) {
array = genarray( 1 << length);
Real * arr_os = array+offset, sum = 0 ;
for ( int i = 1; i < numprocs ; i++ ) {
MPI_Send ( arr_os + i * partition_size, partition_size, MPI_DOUBLE, i , 100 , MPI_COMM_WORLD);
}
}
if (rank == 0 ){
Real sum = 0 , retsum = 0;
#pragma omp paralell for reduction( +: sum) schedule(static)
for ( int i = 0 ; i < partition_size+offset ; i++){
sum +=array[i];
}
#pragma omp paralell for reduction( +: sum)
for ( int i = 1; i < numprocs ; i++ ) {
MPI_Recv ( &retsum , 1 , MPI_DOUBLE , MPI_ANY_SOURCE , 101, MPI_COMM_WORLD, &status );
sum+= retsum;
}
printf("Summen: %.16lf; feilen: %.16lf\n", sum, pi2o6 -sum);
}
else {
Real sum = 0 ;
array = malloc ( partition_size * sizeof( Real ) );
MPI_Recv( array , partition_size , MPI_DOUBLE, 0 , 100 , MPI_COMM_WORLD , &status);
/*
* vector received, do math
*/
#pragma omp parlell for reduction( +: sum) schedule(static)
for ( int i = 0 ; i < partition_size ; ++i )
{
sum += array[i];
}
/*
*I is sending the data back
*/
MPI_Send(&sum ,1 ,MPI_DOUBLE , 0 , 101, MPI_COMM_WORLD );
}
}
Real * genarray ( long length )
{
Real *array = malloc ( length*sizeof(double) );
for (int i = 1 ;i < length ; ++i)
{
array[i] = pow ( i , -2 );
}
return array;
}