Skip to content

Commit 900069b

Browse files
committed
Add threading test to test/capgen_test
1 parent 9d87be6 commit 900069b

3 files changed

Lines changed: 78 additions & 21 deletions

File tree

test/capgen_test/CMakeLists.txt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,25 @@ add_library(CAPGEN_TESTLIB OBJECT ${SCHEME_FORTRAN_FILES}
7272

7373
# Setup test executable with needed dependencies
7474
add_executable(capgen_host_integration test_capgen_host_integration.F90 ${HOST}.F90)
75+
if(OPENMP)
76+
target_link_libraries(capgen_host_integration PRIVATE OpenMP::OpenMP_Fortran)
77+
endif()
7578
target_link_libraries(capgen_host_integration PRIVATE CAPGEN_TESTLIB test_utils)
7679
target_include_directories(capgen_host_integration PRIVATE "$<TARGET_PROPERTY:test_utils,BINARY_DIR>")
7780

7881
# Add executable to be called with ctest
79-
add_test(NAME ctest_capgen_host_integration COMMAND capgen_host_integration)
82+
add_test(NAME ctest_capgen_host_integration_omp1
83+
COMMAND capgen_host_integration)
84+
85+
add_test(NAME ctest_capgen_host_integration_omp2
86+
COMMAND capgen_host_integration)
87+
88+
set_tests_properties(ctest_capgen_host_integration_omp1
89+
PROPERTIES
90+
ENVIRONMENT "OMP_NUM_THREADS=1"
91+
)
92+
93+
set_tests_properties(ctest_capgen_host_integration_omp2
94+
PROPERTIES
95+
ENVIRONMENT "OMP_NUM_THREADS=2"
96+
)

test/capgen_test/test_host.F90

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ end function check_suite
104104
!!
105105
subroutine test_host(retval, test_suites)
106106

107+
#ifdef _OPENMP
108+
use omp_lib
109+
#endif
107110
use test_host_mod, only: ncols, num_time_steps
108111
use test_host_ccpp_cap, only: test_host_ccpp_physics_register
109112
use test_host_ccpp_cap, only: test_host_ccpp_physics_initialize
@@ -120,6 +123,7 @@ subroutine test_host(retval, test_suites)
120123

121124
logical :: check
122125
integer :: col_start, col_end
126+
integer :: thread_num, num_threads
123127
integer :: index, sind
124128
integer :: time_step
125129
integer :: num_suites
@@ -196,35 +200,55 @@ subroutine test_host(retval, test_suites)
196200
end if
197201
end do
198202

199-
do col_start = 1, ncols, 5
200-
if (errflg /= 0) then
201-
exit
202-
end if
203-
col_end = MIN(col_start + 4, ncols)
204-
205-
do sind = 1, num_suites
203+
run_phase_if_no_error: if (errflg == 0) then
204+
#ifdef _OPENMP
205+
num_threads = omp_get_max_threads()
206+
#else
207+
num_threads = 1
208+
#endif
209+
!$OMP parallel num_threads (num_threads) &
210+
!$OMP default (none) &
211+
!$OMP shared (num_threads, num_suites, test_suites) &
212+
!$OMP private (thread_num, col_start, col_end, errmsg) &
213+
!$OMP reduction (+:errflg)
214+
#ifdef _OPENMP
215+
thread_num = omp_get_thread_num()
216+
#else
217+
thread_num = 0
218+
#endif
219+
!$OMP do
220+
do col_start = 1, ncols, 5
206221
if (errflg /= 0) then
207-
exit
222+
continue
208223
end if
209-
do index = 1, size(test_suites(sind)%suite_parts)
224+
col_end = MIN(col_start + 4, ncols)
225+
do sind = 1, num_suites
210226
if (errflg /= 0) then
211-
exit
227+
continue
212228
end if
213-
if (errflg == 0) then
229+
do index = 1, size(test_suites(sind)%suite_parts)
230+
if (errflg /= 0) then
231+
continue
232+
end if
233+
write(0,'(a,i0,a,i0,5a,i0,a,i0)') 'Thread ', thread_num, '/', num_threads, &
234+
': calling run phase for suite ', trim(test_suites(sind)%suite_name), &
235+
' part ', trim(test_suites(sind)%suite_parts(index)), &
236+
' columns ', col_start, ':', col_end
214237
call test_host_ccpp_physics_run( &
215238
test_suites(sind)%suite_name, &
216239
test_suites(sind)%suite_parts(index), &
217240
col_start, col_end, errmsg, errflg)
218-
end if
219-
if (errflg /= 0) then
220-
write(6, '(5a)') trim(test_suites(sind)%suite_name), &
221-
'/', trim(test_suites(sind)%suite_parts(index)), &
222-
': ', trim(errmsg)
223-
exit
224-
end if
241+
if (errflg /= 0) then
242+
write(6, '(5a)') trim(test_suites(sind)%suite_name), &
243+
'/', trim(test_suites(sind)%suite_parts(index)), &
244+
': ', trim(errmsg)
245+
end if
246+
end do
225247
end do
226248
end do
227-
end do
249+
!$OMP end do
250+
!$OMP end parallel
251+
end if run_phase_if_no_error
228252

229253
do sind = 1, num_suites
230254
if (errflg /= 0) then

test/capgen_test/test_host_mod.F90

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,19 @@ logical function compare_data()
9898
logical :: need_header
9999
real(kind_phys) :: avg
100100
integer, parameter :: cincrements(pcnst) = (/ 1, 0 /)
101+
real(kind_phys) :: total_test
102+
real(kind_phys), parameter :: total_ref = 4230.0_kind_phys
101103

102104
compare_data = .true.
103105

106+
total_test = 0.0_kind_phys
104107
need_header = .true.
105108
do lev = 1, pver
106109
do col = 1, ncols
107110
avg = (tint_save(col,lev) + tint_save(col,lev+1))
108111
avg = 1.0_kind_phys + (avg / 2.0_kind_phys)
109112
avg = avg + (temp_inc * num_time_steps)
113+
total_test = total_test + avg
110114
if (abs((temp_midpoints(col, lev) - avg) / avg) > tolerance) then
111115
if (need_header) then
112116
write(6, '(" COL LEV T MIDPOINTS EXPECTED")')
@@ -126,6 +130,7 @@ logical function compare_data()
126130
do col = 1, ncols
127131
avg = real(offsize + col + (cincrements(cind) * num_time_steps), &
128132
kind=kind_phys)
133+
total_test = total_test + avg
129134
if (abs((phys_state%q(col, lev, cind) - avg) / avg) > &
130135
tolerance) then
131136
if (need_header) then
@@ -140,7 +145,18 @@ logical function compare_data()
140145
end do
141146
end do
142147
end do
143-
148+
if (abs((total_test - total_ref) / total_ref) > tolerance) then
149+
write(6, '(a,e12.4)') 'TOTAL REFERENCE: ', total_ref
150+
write(6, '(a,e12.4)') 'TOTAL TEST: ', total_test
151+
write(6, '(2(a,e12.4))') 'REL.DIFF > TOLERANCE:', &
152+
abs((total_test - total_ref) / total_ref), ' >', tolerance
153+
compare_data = .false.
154+
else
155+
write(0, '(a,e12.4)') 'TOTAL REFERENCE: ', total_ref
156+
write(0, '(a,e12.4)') 'TOTAL TEST: ', total_test
157+
write(0, '(2(a,e12.4))') 'REL.DIFF < TOLERANCE:', &
158+
abs((total_test - total_ref) / total_ref), ' <', tolerance
159+
end if
144160
end function compare_data
145161

146162
end module test_host_mod

0 commit comments

Comments
 (0)