From 9105048537ac8ed8f0b83b08fe7191527b8088a2 Mon Sep 17 00:00:00 2001 From: Hans Pabst Date: Wed, 16 Oct 2024 10:21:45 +0200 Subject: [PATCH] #857: conditionally rely on PARALLEL WORKSHARE - Avoid IF-condition as part of the directive. --- src/data/dbcsr_ptr_util.F | 24 ++++++++++------ src/mpi/dbcsr_mpiwrap.F | 12 ++++++-- src/ops/dbcsr_operations.F | 56 +++++++++++++++++++++----------------- 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/data/dbcsr_ptr_util.F b/src/data/dbcsr_ptr_util.F index eb9d25da071..1e76b0e6c63 100644 --- a/src/data/dbcsr_ptr_util.F +++ b/src/data/dbcsr_ptr_util.F @@ -28,7 +28,7 @@ MODULE dbcsr_ptr_util mp_deallocate #include "base/dbcsr_base_uses.f90" -!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads +!$ USE OMP_LIB, ONLY: omp_in_parallel IMPLICIT NONE @@ -294,14 +294,18 @@ SUBROUTINE mem_copy_${nametype1}$ (dst, src, n) !! length of copy ${type1}$, DIMENSION(1:n), INTENT(OUT) :: dst !! destination memory - ${type1}$, DIMENSION(1:n), INTENT(IN) :: src + ${type1}$, DIMENSION(1:n), INTENT(IN) :: src !! source memory #if !defined(__DBCSR_DISABLE_WORKSHARE) -!$OMP PARALLEL WORKSHARE DEFAULT(none) SHARED(dst,src) +!$ IF (.NOT. omp_in_parallel()) THEN +!$OMP PARALLEL WORKSHARE DEFAULT(none) SHARED(dst,src) + dst(:) = src(:) +!$OMP END PARALLEL WORKSHARE +!$ ELSE #endif - dst(:) = src(:) + dst(:) = src(:) #if !defined(__DBCSR_DISABLE_WORKSHARE) -!$OMP END PARALLEL WORKSHARE +!$ END IF #endif END SUBROUTINE mem_copy_${nametype1}$ @@ -313,11 +317,15 @@ SUBROUTINE mem_zero_${nametype1}$ (dst, n) ${type1}$, DIMENSION(1:n), INTENT(OUT) :: dst !! destination memory #if !defined(__DBCSR_DISABLE_WORKSHARE) -!$OMP PARALLEL WORKSHARE DEFAULT(none) SHARED(dst) +!$ IF (.NOT. omp_in_parallel()) THEN +!$OMP PARALLEL WORKSHARE DEFAULT(none) SHARED(dst) + dst(:) = ${zero1}$ +!$OMP END PARALLEL WORKSHARE +!$ ELSE #endif - dst(:) = ${zero1}$ + dst(:) = ${zero1}$ #if !defined(__DBCSR_DISABLE_WORKSHARE) -!$OMP END PARALLEL WORKSHARE +!$ END IF #endif END SUBROUTINE mem_zero_${nametype1}$ diff --git a/src/mpi/dbcsr_mpiwrap.F b/src/mpi/dbcsr_mpiwrap.F index f5393630eb9..09e77487db2 100644 --- a/src/mpi/dbcsr_mpiwrap.F +++ b/src/mpi/dbcsr_mpiwrap.F @@ -95,6 +95,8 @@ MODULE dbcsr_mpiwrap #define MPI_STATUS_EXTRACT(X) (X) #endif +!$ USE OMP_LIB, ONLY: omp_in_parallel + IMPLICIT NONE PRIVATE @@ -5183,11 +5185,15 @@ SUBROUTINE mp_rget_${nametype1}$v(base, source, win, win_data, myproc, disp, req #endif IF (do_local_copy) THEN #if !defined(__DBCSR_DISABLE_WORKSHARE) -!$OMP PARALLEL WORKSHARE DEFAULT(none) SHARED(base,win_data,disp_aint,len) +!$ IF (.NOT. omp_in_parallel()) THEN +!$OMP PARALLEL WORKSHARE DEFAULT(none) SHARED(base,win_data,disp_aint,len) + base(:) = win_data(disp_aint + 1:disp_aint + len) +!$OMP END PARALLEL WORKSHARE +!$ ELSE #endif - base(:) = win_data(disp_aint + 1:disp_aint + len) + base(:) = win_data(disp_aint + 1:disp_aint + len) #if !defined(__DBCSR_DISABLE_WORKSHARE) -!$OMP END PARALLEL WORKSHARE +!$ END IF #endif request = mp_request_null ierr = 0 diff --git a/src/ops/dbcsr_operations.F b/src/ops/dbcsr_operations.F index e0a59a92e48..e95d7c03142 100644 --- a/src/ops/dbcsr_operations.F +++ b/src/ops/dbcsr_operations.F @@ -94,7 +94,7 @@ MODULE dbcsr_operations mp_sum #include "base/dbcsr_base_uses.f90" -!$ USE OMP_LIB, ONLY: omp_get_max_threads, omp_get_thread_num, omp_get_num_threads +!$ USE OMP_LIB, ONLY: omp_get_thread_num, omp_get_num_threads, omp_in_parallel IMPLICIT NONE @@ -315,35 +315,41 @@ SUBROUTINE dbcsr_zero(matrix_a) INTEGER :: handle CALL timeset(routineN, handle) - SELECT CASE (dbcsr_get_data_type(matrix_a)) -#if defined(__DBCSR_DISABLE_WORKSHARE) - CASE (dbcsr_type_complex_4) - matrix_a%data_area%d%c_sp = (0.0, 0.0) - CASE (dbcsr_type_complex_8) - matrix_a%data_area%d%c_dp = (0.0_dp, 0.0_dp) - CASE (dbcsr_type_real_4) - matrix_a%data_area%d%r_sp = 0.0 - CASE (dbcsr_type_real_8) - matrix_a%data_area%d%r_dp = 0.0_dp -#else - CASE (dbcsr_type_complex_4) -!$OMP PARALLEL WORKSHARE DEFAULT(NONE), SHARED(matrix_a) - matrix_a%data_area%d%c_sp = (0.0, 0.0) +#if !defined(__DBCSR_DISABLE_WORKSHARE) +!$ IF (.NOT. omp_in_parallel()) THEN + SELECT CASE (dbcsr_get_data_type(matrix_a)) + CASE (dbcsr_type_complex_4) +!$OMP PARALLEL WORKSHARE DEFAULT(NONE) SHARED(matrix_a) + matrix_a%data_area%d%c_sp = (0.0, 0.0) !$OMP END PARALLEL WORKSHARE - CASE (dbcsr_type_complex_8) -!$OMP PARALLEL WORKSHARE DEFAULT(NONE), SHARED(matrix_a) - matrix_a%data_area%d%c_dp = (0.0_dp, 0.0_dp) + CASE (dbcsr_type_complex_8) +!$OMP PARALLEL WORKSHARE DEFAULT(NONE) SHARED(matrix_a) + matrix_a%data_area%d%c_dp = (0.0_dp, 0.0_dp) !$OMP END PARALLEL WORKSHARE - CASE (dbcsr_type_real_4) -!$OMP PARALLEL WORKSHARE DEFAULT(NONE), SHARED(matrix_a) - matrix_a%data_area%d%r_sp = 0.0 + CASE (dbcsr_type_real_4) +!$OMP PARALLEL WORKSHARE DEFAULT(NONE) SHARED(matrix_a) + matrix_a%data_area%d%r_sp = 0.0 !$OMP END PARALLEL WORKSHARE - CASE (dbcsr_type_real_8) -!$OMP PARALLEL WORKSHARE DEFAULT(NONE), SHARED(matrix_a) - matrix_a%data_area%d%r_dp = 0.0_dp + CASE (dbcsr_type_real_8) +!$OMP PARALLEL WORKSHARE DEFAULT(NONE) SHARED(matrix_a) + matrix_a%data_area%d%r_dp = 0.0_dp !$OMP END PARALLEL WORKSHARE + END SELECT +!$ ELSE +#endif + SELECT CASE (dbcsr_get_data_type(matrix_a)) + CASE (dbcsr_type_complex_4) + matrix_a%data_area%d%c_sp = (0.0, 0.0) + CASE (dbcsr_type_complex_8) + matrix_a%data_area%d%c_dp = (0.0_dp, 0.0_dp) + CASE (dbcsr_type_real_4) + matrix_a%data_area%d%r_sp = 0.0 + CASE (dbcsr_type_real_8) + matrix_a%data_area%d%r_dp = 0.0_dp + END SELECT +#if !defined(__DBCSR_DISABLE_WORKSHARE) +!$ END IF #endif - END SELECT CALL timestop(handle) END SUBROUTINE dbcsr_zero