-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi.c
146 lines (125 loc) · 3.65 KB
/
mpi.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int MinLength,MaxLength;
int Random1 (int Min,int Max){
int x=Max-Min;
return (int) (((double)(x+1)/RAND_MAX)*rand()+Min);
}
int main(int argc,char *argv[]){
MPI_Status stu;
int count;
int numtasks;
int rank;
int ran1;
int ran2;
int j;
int ran;
int i;
int k;
int min,max;
int *outmsg1;
int* outmsg2;
int *inmsg1;
int* inmsg2;
int *inmsg;
int flag=0;
int tag;
int DesRan1,DesRan2,DesRan;
int sum=0;
int se,se2,rc,rc2;
MPI_Request reque1[100];
MPI_Request reque[100];
MPI_Request req[4];
MPI_Request request1[2];
MPI_Request reques2[100];
MPI_Status stats[4],st[20];
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0){
printf("enter the minimum Length of the array \n ");
fflush(stdout);
scanf("%d",&MinLength);
min=MinLength;
printf("enter the maximum Length of the array \n ");
fflush(stdout);
scanf("%d",&MaxLength);
max=MaxLength;
printf("\n oKaY ... \n");
}
MPI_Bcast(&min,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(&max,1,MPI_INT,0,MPI_COMM_WORLD);
srand(rank);
ran1= rand() % (max-min+1) +min;
ran2=rand() % (max-min+1) +min;
outmsg1= (int*) malloc(ran1 * sizeof(int));
printf("\n process ( %d ) created array1[ %d ] = ",rank,ran1);
for (i=0;i<ran1;i++){
outmsg1[i]=Random1(i,i+150);
printf("%d ",outmsg1[i]);
}
outmsg2= (int*) malloc(ran2 * sizeof(int));
printf("\n process ( %d ) created array2[ %d ] = ",rank,ran2);
for (i=0;i<ran2;i++){
outmsg2[i]=Random1(i,i+150);
printf("%d ",outmsg2[i]);
}
srand(time(NULL)+rank);
DesRan1 = rand() % (numtasks) ;
dr1=DesRan1;
srand(rank);
DesRan2 = rand() % (numtasks) ;
MPI_Barrier(MPI_COMM_WORLD);
printf("\n creation finished .... \n");
for (i=0;i<numtasks;i++){
MPI_Isend(&DesRan1, 1, MPI_INT,i, 5000, MPI_COMM_WORLD,&req[i]);
}
for (i=0;i<numtasks;i++){
MPI_Isend(&DesRan2, 1, MPI_INT,i, 5001, MPI_COMM_WORLD,&req[i]);
}
printf("\n process %d send to the destenation process1 = %d",rank,DesRan1);
printf("\n destenation process2 = %d",DesRan2);
printf("\n \n");
MPI_Barrier(MPI_COMM_WORLD);
for (i=0;i<numtasks;i++){
MPI_Irecv(&DesRan, 1, MPI_INT,MPI_ANY_SOURCE, 5000, MPI_COMM_WORLD, &req[i]);
if (rank==DesRan ){
sum =sum+1;
}
}
for (i=0;i<numtasks;i++){
MPI_Irecv(&DesRan, 1, MPI_INT, MPI_ANY_SOURCE, 5001, MPI_COMM_WORLD, &req[i]);
MPI_Barrier(MPI_COMM_WORLD);
if (rank == DesRan){
sum =sum+1;
}
}
printf("\n process %d will receive = %d messege \n",rank,sum);
printf("\n _______________________________________ \n");
fflush(stdout);
MPI_Barrier(MPI_COMM_WORLD);
MPI_Isend(outmsg1, ran1, MPI_INT,DesRan1, 150, MPI_COMM_WORLD,&reque[0]);
MPI_Isend(outmsg2, ran2, MPI_INT,DesRan2, 150, MPI_COMM_WORLD,&reque[1]);
MPI_Barrier(MPI_COMM_WORLD);
k=0;
while (1){
MPI_Iprobe( MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &flag, &stu);
if (flag){
MPI_Get_count(&stu,MPI_INT,&count);
inmsg= (int*) malloc(count * sizeof(int));
k++;
MPI_Recv(inmsg,count , MPI_INT, stu.MPI_SOURCE, stu.MPI_TAG,
MPI_COMM_WORLD, &stu);
printf("\n \n process %d received from source %d for the %d times >>> \n array[%d] = "
,rank,stu.MPI_SOURCE,i,count);
for (j=0;j<count;j++){
printf("%d ",inmsg[j]);
}
}
if (k == sum)
break;
}
MPI_Finalize();
}