-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_sum.cpp
111 lines (93 loc) · 2.49 KB
/
vector_sum.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<bits/stdc++.h>
#include<mpi.h>
using namespace std;
const int maxn=10;
//计算x,y两个数组对应位的和,存入第三个数组z
bool Parallel_vector_sum(
double local_x[], //加数组
double local_y[], //被加数组
double local_z[], //存储和的数组
int local_n //加的个数
)
{
for(int local_i=0;local_i<local_n;local_i++){
local_z[local_i] = local_x[local_i]+local_y[local_i];
}
return true;
}
//从键盘读取数组
bool Read_vector(
double local_a[], //存储数据的数组,OUT
int local_n, //每个进程需要分配的数量
int n, //读取的总个数
string vec_name, //数组的名字
int my_rank, //进程号
MPI_Comm comm //通信子
)
{
double* a = NULL;
if(my_rank==0){
//输入数组
a = new double[n];
cout<<"Enter the vector "<<vec_name<<endl;
for(int i=0;i<n;i++){
cin>>a[i];
}
//数据分发
MPI_Scatter(a,local_n,MPI_DOUBLE,local_a,local_n,MPI_DOUBLE,0,comm);
delete [] a;
}
else{
//接收数据
MPI_Scatter(a,local_n,MPI_DOUBLE,local_a,local_n,MPI_DOUBLE,0,comm);
}
return true;
}
bool Print_vector(
double local_b[], //需要输出的数组
int local_n, //每个进程需要输出的数量
int n, //总数量
string title, //数组的名字
int my_rank, //进程号
MPI_Comm comm //通信子
)
{
double* b=NULL;
if(my_rank==0){
//接收各个结果数组
b = new double[n];
MPI_Gather(local_b,local_n,MPI_DOUBLE,b,local_n,MPI_DOUBLE,0,comm);
//打印数组
cout<<title<<endl;
for(int i=0;i<n;i++){
cout<<b[i]<<" ";
}
cout<<endl;
delete [] b;
}
else{
//发送自己的结果数组
MPI_Gather(local_b,local_n,MPI_DOUBLE,b,local_n,MPI_DOUBLE,0,comm);
}
return true;
}
int main(void)
{
int my_rank=0;
int comm_sz=0;
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);
streambuf * inbuf = cin.rdbuf((new ifstream("input"))->rdbuf());//重定向,OJ时将它注释掉
int local_n=maxn/comm_sz;
double local_a[maxn],local_b[maxn],local_c[maxn];
//读取待相加的两个数组
Read_vector(local_a,local_n,maxn,"arryA",my_rank,MPI_COMM_WORLD);
Read_vector(local_b,local_n,maxn,"arryB",my_rank,MPI_COMM_WORLD);
//计算对应的和
Parallel_vector_sum(local_a,local_b,local_c,local_n);
//打印结果数组
Print_vector(local_c,local_n,maxn,"arry",my_rank,MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}