Skip to content

Commit 4c3a05e

Browse files
author
Jesse Lentz
committed
Replace do loops with reshape intrinsic
Replace nested do loops in mpp_pack.fh with calls to Fortran's `reshape` intrinsic. Additionally, the new `mpp_global_field` tests have been enabled.
1 parent 77897cf commit 4c3a05e

2 files changed

Lines changed: 25 additions & 92 deletions

File tree

mpp/include/mpp_pack.fh

Lines changed: 25 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
integer, allocatable, dimension(:) :: lb, ub !< These are 1 and shape(arr) respectively for all dimensions
3737
!! except the domain-decomposed dimensions, for which they are
3838
!! set according to the is,ie,js,je options.
39-
integer :: n, m
39+
integer :: ndims, n
4040
integer :: i1, i2, i3, i4, i5
4141

42-
n = rank(arr)
43-
allocate (lb(n), ub(n))
42+
ndims = rank(arr)
43+
allocate (lb(ndims), ub(ndims))
4444

4545
lb = 1
4646
ub = shape(arr)
@@ -51,48 +51,17 @@
5151
ub(xdim) = ie
5252
ub(ydim) = je
5353

54-
m = 0
54+
n = product(ub - lb + 1)
55+
5556
select rank(arr)
5657
rank (2)
57-
do i2=lb(2),ub(2)
58-
do i1=lb(1),ub(1)
59-
m = m + 1
60-
vec(m) = arr(i1, i2)
61-
enddo
62-
enddo
58+
vec(1:n) = reshape(arr(lb(1):ub(1), lb(2):ub(2)), [n])
6359
rank (3)
64-
do i3=lb(3),ub(3)
65-
do i2=lb(2),ub(2)
66-
do i1=lb(1),ub(1)
67-
m = m + 1
68-
vec(m) = arr(i1, i2, i3)
69-
enddo
70-
enddo
71-
enddo
60+
vec(1:n) = reshape(arr(lb(1):ub(1), lb(2):ub(2), lb(3):ub(3)), [n])
7261
rank (4)
73-
do i4=lb(4),ub(4)
74-
do i3=lb(3),ub(3)
75-
do i2=lb(2),ub(2)
76-
do i1=lb(1),ub(1)
77-
m = m + 1
78-
vec(m) = arr(i1, i2, i3, i4)
79-
enddo
80-
enddo
81-
enddo
82-
enddo
62+
vec(1:n) = reshape(arr(lb(1):ub(1), lb(2):ub(2), lb(3):ub(3), lb(4):ub(4)), [n])
8363
rank (5)
84-
do i5=lb(5),ub(5)
85-
do i4=lb(4),ub(4)
86-
do i3=lb(3),ub(3)
87-
do i2=lb(2),ub(2)
88-
do i1=lb(1),ub(1)
89-
m = m + 1
90-
vec(m) = arr(i1, i2, i3, i4, i5)
91-
enddo
92-
enddo
93-
enddo
94-
enddo
95-
enddo
64+
vec(1:n) = reshape(arr(lb(1):ub(1), lb(2):ub(2), lb(3):ub(3), lb(4):ub(4), lb(5):ub(5)), [n])
9665
end select
9766
end subroutine ARR2VEC_
9867

@@ -116,11 +85,11 @@
11685
integer, allocatable, dimension(:) :: lb, ub !< These are 1 and shape(arr) respectively for all dimensions
11786
!! except the domain-decomposed dimensions, for which they are
11887
!! set according to the is,ie,js,je options.
119-
integer :: n, m
88+
integer :: ndims, n
12089
integer :: i1, i2, i3, i4, i5
12190

122-
n = rank(arr)
123-
allocate (lb(n), ub(n))
91+
ndims = rank(arr)
92+
allocate (lb(ndims), ub(ndims))
12493

12594
lb = 1
12695
ub = shape(arr)
@@ -131,49 +100,19 @@
131100
ub(xdim) = ie
132101
ub(ydim) = je
133102

134-
m = 0
135-
select rank(arr)
136-
rank (2)
137-
do i2=lb(2),ub(2)
138-
do i1=lb(1),ub(1)
139-
m = m + 1
140-
arr(i1, i2) = vec(m)
141-
enddo
142-
enddo
143-
rank (3)
144-
do i3=lb(3),ub(3)
145-
do i2=lb(2),ub(2)
146-
do i1=lb(1),ub(1)
147-
m = m + 1
148-
arr(i1, i2, i3) = vec(m)
149-
enddo
150-
enddo
151-
enddo
152-
rank (4)
153-
do i4=lb(4),ub(4)
154-
do i3=lb(3),ub(3)
155-
do i2=lb(2),ub(2)
156-
do i1=lb(1),ub(1)
157-
m = m + 1
158-
arr(i1, i2, i3, i4) = vec(m)
159-
enddo
160-
enddo
161-
enddo
162-
enddo
163-
rank (5)
164-
do i5=lb(5),ub(5)
165-
do i4=lb(4),ub(4)
166-
do i3=lb(3),ub(3)
167-
do i2=lb(2),ub(2)
168-
do i1=lb(1),ub(1)
169-
m = m + 1
170-
arr(i1, i2, i3, i4, i5) = vec(m)
171-
enddo
172-
enddo
173-
enddo
174-
enddo
175-
enddo
176-
end select
103+
associate (s => ub - lb + 1)
104+
n = product(s)
105+
select rank(arr)
106+
rank (2)
107+
arr(lb(1):ub(1), lb(2):ub(2)) = reshape(vec(1:n), s(1:2))
108+
rank (3)
109+
arr(lb(1):ub(1), lb(2):ub(2), lb(3):ub(3)) = reshape(vec(1:n), s(1:3))
110+
rank (4)
111+
arr(lb(1):ub(1), lb(2):ub(2), lb(3):ub(3), lb(4):ub(4)) = reshape(vec(1:n), s(1:4))
112+
rank (5)
113+
arr(lb(1):ub(1), lb(2):ub(2), lb(3):ub(3), lb(4):ub(4), lb(5):ub(5)) = reshape(vec(1:n), s(1:5))
114+
end select
115+
end associate
177116
end subroutine VEC2ARR_
178117

179118
!> @brief Initialize an assumed-rank array to `val`. This is used when initializing an assumed-rank array

test_fms/mpp/test_mpp_global_field.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,6 @@
2626
# Set common test settings.
2727
. ../test-lib.sh
2828

29-
# TODO: Enable these tests once generalized indices work is complete
30-
SKIP_TESTS="test_mpp_global_field.1 \
31-
test_mpp_global_field.2 \
32-
test_mpp_global_field.3 \
33-
test_mpp_global_field.4"
34-
3529
touch input.nml
3630

3731
for datatype in r4 r8 i4 i8

0 commit comments

Comments
 (0)