-
Notifications
You must be signed in to change notification settings - Fork 0
/
PhuongTrinhNhiet1D_C_openmpi.c
109 lines (81 loc) · 2.75 KB
/
PhuongTrinhNhiet1D_C_openmpi.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
#include <stdio.h>
#include <malloc.h>
#include <mpi.h>
#include <math.h>
#include <sys/time.h>
#define M 20
#define Time 1
#define dx 0.1
#define dt 0.01
#define D 0.1
int i;
int rank, size;
float t = 0, *T, *dT, *Ts, *dTs;
MPI_Status stat;
void DHB2(float *Ts, float Tl, float Tr, float *dTs, int ms) {
int i;
float c, l, r;
for (i = 0; i < ms; i++) {
// ignore last point
c = *(Ts + i);
l = (i == 0) ? Tl : *(Ts + i - 1);
r = (i == ms - 1) ? Tr : *(Ts + i + 1);
*(dTs + i) = D * (l - 2 * c + r) / (dx * dx);
}
}
int main(int argc, char** argv) {
// Khai bao bien
T = (float *) malloc((M) * sizeof (float));
dT = (float *) malloc((M) * sizeof (float));
float Tl;
float Tr;
// Khoi tao
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
int mc = M / size;
for (i = 0; i < M; i++) *(T + i) = 25.0;
Ts = T + rank*mc;
dTs = dT + rank*mc;
while (t < Time) {
//Truyền thông phần tử Tl
// rank = 0 ung voi truong hop o ngoai cung ben trai nen ko nhan ve gi
// Tl cua rank 0 mac dinh bang 0
if (rank == 0) {
Tl = 100;
MPI_Send(Ts + mc - 1, 1, MPI_FLOAT, rank + 1, rank, MPI_COMM_WORLD);
} else if (rank == size - 1) {
MPI_Recv(&Tl, 1, MPI_FLOAT, rank - 1, rank - 1, MPI_COMM_WORLD, &stat);
} else {
MPI_Send(Ts + mc - 1, 1, MPI_FLOAT, rank + 1, rank, MPI_COMM_WORLD);
MPI_Recv(&Tl, 1, MPI_FLOAT, rank - 1, rank - 1, MPI_COMM_WORLD, &stat);
}
// Truyền thông phần tử Tr
// rank size - 1 ung voi core cuoi cung nen ko co Tr
// Tr cua rank size-1 bang 25
if (rank == size - 1) {
Tr = 25;
MPI_Send(Ts, 1, MPI_FLOAT, rank - 1, rank, MPI_COMM_WORLD);
} else if (rank == 0) {
MPI_Recv(&Tr, 1, MPI_FLOAT, rank + 1, rank + 1, MPI_COMM_WORLD, &stat);
} else {
MPI_Send(Ts, 1, MPI_FLOAT, rank - 1, rank, MPI_COMM_WORLD);
MPI_Recv(&Tr, 1, MPI_FLOAT, rank + 1, rank + 1, MPI_COMM_WORLD, &stat);
}
DHB2(Ts, Tl, Tr, dTs, mc);
for (i = 0; i < mc; i++)
*(Ts + i) = *(Ts + i) + *(dTs + i) * dt;
t = t + dt;
}
// ham gather: mc la kich thuoc cua du lieu gui di va nhan ve
MPI_Gather(Ts, mc, MPI_INT, T, mc, MPI_INT, 0, MPI_COMM_WORLD);
if (rank == 0)
for (i = 0; i < M; i++) printf("%f \n", *(T + i));
MPI_Finalize();
return 0;
}