-
Notifications
You must be signed in to change notification settings - Fork 0
/
New_Input.cpp
74 lines (60 loc) · 1.78 KB
/
New_Input.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
#include<bits/stdc++.h>
#include<mpi.h>
using namespace std;
int comm_sz=0;
int my_rank=0;
int arra[]={1,2,3,4,5},arrb[]={10,9,8,7,6},arrc[]={0,0,0,0,0};
bool Build_mpi_type(
double* a_p, /* IN */
double* b_p, /* IN */
int* n_p, /* IN */
MPI_Datatype* input_mpi_t_p /* OUT */
)
{
int array_of_blocklength[3] = {1, 1, 1};
MPI_Datatype array_of_types[3] = {MPI_DOUBLE, MPI_DOUBLE, MPI_IN};
MPI_Aint a_addr, b_addr, n_addr;
MPI_Aint array_of_displacement[3] = {0};
MPI_Get_address(a_p,&a_addr);
MPI_Get_address(b_p,&b_addr);
MPI_Get_address(n_p,&n_addr);
array_of_displacement[0] = 0;
array_of_displacement[1] = b_addr-a_addr;
array_of_displacement[2] = n_addr-b_addr;
MPI_Type_create_struct(3,array_of_blocklength,array_of_displacement,array_of_types,input_mpi_t_p);
MPI_Type_commit(input_mpi_t_p);
}
//获取输入,0进程从键盘读取,非0进程从0进程获取
bool get_input(double &a,double &b,int n)
{
MPI_Datatype input_mpi_t;
Build_mpi_type(&a,&b,&n,&input_mpi_t);
if(my_rank==0){ //用户输入
cout<<" enter a,b,n"<<endl;
cin>>a>>b>>n;
}
MPI_Bcast(&a,1,input_mpi_t,0,MPI_COMM_WORLD);
MPI_Type_free(&input_mpi_t);
return true;
}
//加和数组a和数组b的对应项
int count_sum(int beg,int en)
{
int sum=0;
for(int i=beg;i<en;i++){
sum += arra[i]+arrb[i];
}
return sum;
}
int main(void)
{
int local_sum=0,total_sum=0; //local_sum为本进程使用,total_sum为计算风格进程的合使用
MPI_Init(NULL,NULL);
MPI_Comm_rank(MPI_COMM_WORLD,&my_rank);
MPI_Comm_size(MPI_COMM_WORLD,&comm_sz);
int a = 0, b = 0, c = 0;
get_input(&a, &b, &n);
cout<<"a "<<a<<" b "<<b<<" c "<<c<<endl;
MPI_Finalize();
return 0;
}