-
Notifications
You must be signed in to change notification settings - Fork 0
/
amgx_construct.f90
215 lines (179 loc) · 4.86 KB
/
amgx_construct.f90
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
SUBROUTINE amgx_construct_matrix(x_min, &
x_max, &
y_min, &
y_max, &
nnz, &
rx, ry, &
b_mesh, &
b_rhs, &
x, &
Kx, &
Ky, &
A_cols, A_rows, A_data) bind(C, name="amgx_construct_matrix")
USE, INTRINSIC::ISO_C_BINDING
IMPLICIT NONE
! TODO make sure this works with PGI/Cray?
INTEGER(C_INT):: x_min,x_max,y_min,y_max, nnz
REAL(C_DOUBLE), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: Kx, Ky, b_mesh
REAL(C_DOUBLE), DIMENSION(x_max*y_max) :: b_rhs, x
REAL(C_DOUBLE) :: rx, ry
INTEGER(C_INT), DIMENSION(x_max*y_max+1) :: A_rows
INTEGER(C_INT), DIMENSION(x_max*y_max*5) :: A_cols
REAL(C_DOUBLE), DIMENSION(x_max*y_max*5) :: A_data
INTEGER :: ctr, j, k, idx
REAL(KIND=8) :: left, right, up, down, centre
!
! | | 4 |
! | ---+---+--
! | 1 | 2 | 3
! k| ---+---+--
! | | 0 |
! |
! +-------------
! j
!
! 0 1 2 3 4
! 0 1 2 3 4
! 0 1 2 3 4
! etc
!
nnz = 0
ctr = 1
DO k=y_min, y_max
DO j=x_min, x_max
ctr = (x_max)*(k-1) + j
A_rows(ctr) = nnz
! 0
if (k .gt. y_min) then
nnz = nnz + 1
A_cols(nnz) = ctr - x_max
endif
! 1
if (j .gt. x_min) then
nnz = nnz + 1
A_cols(nnz) = ctr - 1
endif
! 2
nnz = nnz + 1
A_cols(nnz) = ctr
! 3
if (j .lt. x_max) then
nnz = nnz + 1
A_cols(nnz) = ctr + 1
endif
! 4
if (k .lt. y_max) then
nnz = nnz + 1
A_cols(nnz) = ctr + x_max
endif
ctr = ctr + 1
ENDDO
ENDDO
a_rows(ctr) = nnz
! construct matrix
!$OMP PARALLEL PRIVATE(j, idx, left, right, up, down, centre, ctr)
!$OMP DO
DO k=y_min, y_max
DO j=x_min, x_max
left = -Kx(j, k)*rx
right = -Kx(j+1, k)*rx
down = -Ky(j, k)*ry
up = -Ky(j, k+1)*ry
if (k .le. y_min) then
down = 0.0_8
endif
if (j .le. x_min) then
left = 0.0_8
endif
if (j .ge. x_max) then
right = 0.0_8
endif
if (k .ge. y_max) then
up = 0.0_8
endif
centre = 1.0_8 - left - right - up - down
! need to index sort of like C
! #define FTNREF2D(i_index,j_index,i_size,i_lb,j_lb)
! ((i_size)*(j_index-(j_lb))+(i_index)-(i_lb))
! x_host[ctr] = b_mesh[FTNREF2D(j ,k ,x_max+4,x_min-2,y_min-2)];
idx = (x_max)*(k-1) + j
idx = A_rows(idx)
ctr = 0
! 0
if (k .gt. y_min) then
ctr = ctr + 1
A_data(idx + ctr) = down
endif
! 1
if (j .gt. x_min) then
ctr = ctr + 1
A_data(idx + ctr) = left
endif
! 2
ctr = ctr + 1
A_data(idx + ctr) = centre
! 3
if (j .lt. x_max) then
ctr = ctr + 1
A_data(idx + ctr) = right
endif
! 4
if (k .lt. y_max) then
ctr = ctr + 1
A_data(idx + ctr) = up
endif
ENDDO
ENDDO
!$OMP END DO
!$OMP END PARALLEL
END SUBROUTINE
SUBROUTINE amgx_read_mesh(x_min, &
x_max, &
y_min, &
y_max, &
b_mesh, &
b_rhs, &
x) bind(C, name="amgx_read_mesh")
USE, INTRINSIC::ISO_C_BINDING
IMPLICIT NONE
! TODO make sure this works with PGI/Cray?
INTEGER(C_INT):: x_min,x_max,y_min,y_max, nnz
REAL(C_DOUBLE), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: b_mesh
REAL(C_DOUBLE), DIMENSION(x_max*y_max) :: b_rhs, x
INTEGER :: ctr, j, k, idx
!$OMP PARALLEL PRIVATE(ctr)
!$OMP DO
DO k=y_min, y_max
DO j=x_min, x_max
ctr = (x_max)*(k-1) + j
x(ctr) = b_mesh(j, k)
b_rhs(ctr) = b_mesh(j, k)
ENDDO
ENDDO
!$OMP END DO
!$OMP END PARALLEL
end subroutine
SUBROUTINE amgx_writeback(x_min, &
x_max, &
y_min, &
y_max, &
b_mesh, &
x) bind(C, name="amgx_writeback")
USE, INTRINSIC::ISO_C_BINDING
IMPLICIT NONE
INTEGER(C_INT):: x_min,x_max,y_min,y_max
REAL(C_DOUBLE), DIMENSION(x_min-2:x_max+2,y_min-2:y_max+2) :: b_mesh
REAL(C_DOUBLE), DIMENSION(x_max*y_max) :: x
INTEGER :: ctr, j, k
! write back
!$OMP PARALLEL PRIVATE(ctr)
!$OMP DO
DO k=y_min, y_max
DO j=x_min, x_max
ctr = (x_max)*(k-1) + j
b_mesh(j, k) = x(ctr)
ENDDO
ENDDO
!$OMP END DO
!$OMP END PARALLEL
end subroutine