-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab_8.cpp
82 lines (65 loc) · 2.1 KB
/
Lab_8.cpp
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
// HANDS-ON: Dot Product using scatter and reduce
// NOTE: to run this code, you need to type in cmd the following command:
// mpiexec -n 3 "Dot_Product.exe"
// This command will run the code with 3 processes and you can pass any number.
#include <iostream>
#include <mpi.h>
// a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + ... + a[n] * b[n]
using namespace std;
const int array_size = 100;
int main()
{
// initialize MPI
MPI_Init(NULL, NULL);
// get rank, size
int rank, processes;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &processes);
// create arrays
int a[array_size], b[array_size];
// int* results = new int[processes];
// rank 0 initializes the arrays
if (rank == 0)
{
for (int i = 0; i < array_size; i++)
{
a[i] = 2;
b[i] = 3;
}
}
// scatter the arrays
int chunk_size = array_size / processes;
int *a_chunk = new int[chunk_size];
int *b_chunk = new int[chunk_size];
MPI_Scatter(&a, chunk_size, MPI_INT, a_chunk, chunk_size, MPI_INT, 0, MPI_COMM_WORLD);
MPI_Scatter(&b, chunk_size, MPI_INT, b_chunk, chunk_size, MPI_INT, 0, MPI_COMM_WORLD);
// calculate the dot product
int dot_product = 0;
for (int i = 0; i < chunk_size; i++)
{
dot_product += a_chunk[i] * b_chunk[i];
}
// reduce the dot products to the final result
int final_result = 0;
MPI_Reduce(&dot_product, &final_result, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
// gather the results
// MPI_Gather(&dot_product, 1, MPI_INT, results, 1, MPI_INT, 0, MPI_COMM_WORLD);
// rank 0 calculates the final result
if (rank == 0)
{
/*int final_result = 0;
for (int i = 0; i < processes; i++)
{
final_result += results[i];
}
*/
int remaining = array_size - (array_size % processes);
for (int i = remaining; i < array_size; i++)
{
final_result += a[i] * b[i];
}
cout << "The dot product is: " << final_result << endl;
}
// finalizing MPI
MPI_Finalize();
}