-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
86 lines (80 loc) · 2.44 KB
/
Source.cpp
File metadata and controls
86 lines (80 loc) · 2.44 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
84
85
86
#include"stdio.h"
#include"stdlib.h"
#include"mpi.h"
void main(int argc, char* argv[]) {
double* v1 = NULL, * v2 = NULL,/* * _v1, * _v2,*/* buf1 = NULL, * buf2 = NULL;
int i, vector_size = 0, _vector_size, ProcNum, ProcRank;
double sum = 0, sum_all = 0, st = 0, fn = 0, _st = 0, _fn = 0;
/*scanf_s("%d", &vector_size);
_v1 = new double[vector_size];
_v2 = new double[vector_size];
for (int j = 0; j < vector_size; j++) {
_v1[j] = rand() % 10;
_v2[j] = rand() % 10;
printf("%10.2f%10.2f\n", _v1[j], _v2[j]);
}
st = MPI_Wtime();
for (int j = 0; j < vector_size; j++) {
sum += _v1[j] * _v2[j];
}
printf("Result = %10.2f\n", sum);
printf("Linear time = %10.2f\n", (fn - st));
*/
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &ProcNum);
MPI_Comm_rank(MPI_COMM_WORLD, &ProcRank);
if (ProcRank == 0) {
scanf_s("%d", &vector_size);
v1 = new double[vector_size];
v2 = new double[vector_size];
buf1 = new double[vector_size];
buf2 = new double[vector_size];
for (int j = 0; j < vector_size; j++) {
v1[j] = rand() % 10;
v2[j] = rand() % 10;
printf("%10.2f %10.2f\n", v1[j], v2[j]);
}
/*for (int j = 0; j < vector_size; j++) {
v1[j] = _v1[j];
v2[j] = _v2[j];
}*/
//ëèíåéíûé àëãîðèòì
st = MPI_Wtime();
for (int j = 0; j < vector_size; j++) {
sum += v1[j] * v2[j];
}
fn = MPI_Wtime();
printf("Linear result = %10.2f\n", sum);
printf("Linear time = %10.2f\n", (fn - st)*1000);
}
//ïàðàëëåëüíûé àëãîðèòì
_st = MPI_Wtime();
MPI_Bcast(&vector_size, 1, MPI_INT, 0, MPI_COMM_WORLD);
_vector_size = vector_size / ProcNum;
if (ProcRank != 0) {
//v1 = new double[_vector_size];
//v2 = new double[_vector_size];
buf1 = new double[_vector_size];
buf2 = new double[_vector_size];
}
MPI_Scatter(v1, _vector_size, MPI_DOUBLE, buf1, _vector_size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
MPI_Scatter(v2, _vector_size, MPI_DOUBLE, buf2, _vector_size, MPI_DOUBLE, 0, MPI_COMM_WORLD);
sum = sum_all = 0;
for (i = 0; i < _vector_size; i++) {
sum += buf1[i] * buf2[i];
}
MPI_Reduce(&sum, &sum_all, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
//_fn = MPI_Wtime();
if (ProcRank == 0) {
for (int i = vector_size - 1; i >= vector_size - vector_size % ProcNum; --i) {
sum_all += v1[i] * v2[i];
}
//sum_all += v1[vector_size-1] * v2[vector_size-1];
_fn = MPI_Wtime();
printf("Parallel result = %10.2f\n", sum_all);
printf("Parallel time = %10.2f\n", (_fn - _st)*1000);
}
MPI_Finalize();
delete[] v1;
delete[] v2;
}