-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
99 lines (66 loc) · 1.79 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
/*
* Applications of parallel computing - mpi
* Assignment: Implementation of the power method
* File: matrix.c
* Last modification: 27/3/2013
*
* Author: Jeroen Mulkers (student at university of Antwerp)
* Mail: [email protected]
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "mpi.h"
#include "matrix.h"
void generateMatrix(double matrix[], int N);
int main(int argc, char* argv[]) {
/* Set parameters */
int N = atoi(argv[1]);
int nIterations = atoi(argv[2]);
/* Start parallel calculations */
int rank, p;
MPI_Init(&argc, & argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
/* p has to be a divisor of N !! */
if(rank==0) assert(!(N%p));
/* Test the used algorithms */
//matrix_testAll();
/* Generate the matrix */
double matrix[N*N/p];
generateMatrix(matrix,N);
/* Run the powerMethod algorithm */
double start = MPI_Wtime();
double lambda = powerMethod(matrix,nIterations,N);
double stop = MPI_Wtime();
/* Calculating times */
double timediff = stop - start;
double times[p];
double average = 0;
MPI_Gather(&timediff,1,MPI_DOUBLE,times,1,MPI_DOUBLE,0,MPI_COMM_WORLD);
if(rank==0){
for(int i=0; i<p; i++)
average += times[i];
average /= p;
}
/* Print the results */
if(rank==0)
printf("dimension\t%d\tlambda\t%f\ttime\t%f\n",N,lambda,average);
/* End MPI */
MPI_Finalize();
return 0;
}
void generateMatrix(double matrix[], int N){
int rank, p;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &p);
/* Define the matrix on the (different) processors */
for(int j=0; j<N ; j++) {
for(int i=0; i<N/p; i++) {
if(j-rank*N/p>i)
matrix[j+N*i] = 0;
else
matrix[j+N*i] = rank*(N/p)+i+1;
}}
}