Skip to content

Pragma_Examples: fix Fortran min/max/sum array reduction test discarding its device result - #191

Closed
dannybaths wants to merge 1 commit into
amd:mainfrom
dannybaths:fortran-reduction-array-combined-construct
Closed

Pragma_Examples: fix Fortran min/max/sum array reduction test discarding its device result#191
dannybaths wants to merge 1 commit into
amd:mainfrom
dannybaths:fortran-reduction-array-combined-construct

Conversation

@dannybaths

@dannybaths dannybaths commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

What

Pragma_Examples/OpenMP/Fortran/5_reduction_array/2_reduction_array_min_max_sum/reduction.f90
reports the wrong answer for all eight of its sub-tests. Each one reports the
initialiser of its result variable rather than the reduction: Test 1 int MAX
prints the value of -huge(imax_result), Test 4 int SUM prints 0, and so on.

All eight sub-tests use a bare !$omp target with the reduction on an inner
!$omp teams loop:

!$omp target
!$omp teams loop collapse(1) default(shared) private(i) reduction(MAX: imax_result)
...
!$omp end teams loop
!$omp end target

A scalar referenced in a target region with no map or defaultmap clause is
firstprivate, so the value is copied in and never copied back. The device
performs the reduction correctly and the host then discards the result.

This change uses the combined construct instead, which is what the passing
sibling test OpenMP_Language_Constructs_Fortran_Reduction_Array already does:

!$omp target teams loop collapse(1) default(shared) private(i) reduction(MAX: imax_result)
...
!$omp end target teams loop

The change also removes this annotation above the test in tests/CMakeLists.txt:

# Expected failure -- not implemented in amdflang-new yet

That comment is a mis-diagnosis. The construct is implemented and the reduction is
computed correctly; only the data mapping was wrong.

Why

The test currently fails for a reason that has nothing to do with compiler
support, while an annotation in the test list attributes the failure to a missing
compiler feature. Anyone reading it learns the wrong lesson twice: that this
reduction form is unsupported, and that the code as written is correct.

Validation

MI300A / gfx942, HSA_XNACK=1, amdflang from $ROCM_PATH/bin. Focused run on
upstream main (20e93223):

git clone https://github.com/amd/HPCTrainingExamples.git
cd HPCTrainingExamples/tests
export HSA_XNACK=1
export ROCM_GPU=gfx942
export FC="${ROCM_PATH}/bin/amdflang"
rm -rf build && cmake . -B build
ctest -V -R '^OpenMP_Language_Constructs_Fortran_Reduction_Array_Min_Max_Sum$' --test-dir build

On many raw clones the CTest driver does not pick up amdflang unless FC is set
explicitly as above.

Unpatched upstream: 1/1 FAIL. All eight sub-tests report wrong answers
(initialiser values); CTest misses the ALL TESTS PASSED regex.

With this change: 1/1 PASS. Example lines:

Test 1  int MAX (n=4):       result=320  expected=320
Test 4  int SUM (n=100):     result=5050  expected=5050
Test 7  dp  SUM (n=1000):    result=     1000.00  expected=     1000.00
Test 8  int MAX cond (n=4):  result=320  expected=320
ALL TESTS PASSED

AAC7 proof: current-main CTest on ROCm 10.0.0 — unpatched FAIL, patched PASS.

AAC6 no-regression: MI300A, Lmod rocm/7.14.0, Slurm job 19739 on
ppac-pl1-s24-16 — 1/1 PASS, ALL TESTS PASSED, with
FC=$ROCM_PATH/bin/amdflang.

…ts device result

Signed-off-by: Daniele Bagni <30289343+dannybaths@users.noreply.github.com>
@gcapodagAMD

Copy link
Copy Markdown
Collaborator

@dannybaths please make sure to always mention the environment you are in so we can reproduce 1-1.
A simple "module list" would work in most cases

@gcapodagAMD

Copy link
Copy Markdown
Collaborator

please make sure to test the same rocm versions when you test on aac7 and aac6 so we compare apples to apples

@gcapodagAMD

Copy link
Copy Markdown
Collaborator

it would also be very helpful to test across 3 rocm versions because the compiler moves very fast so maybe check ROCm 7.2.4 ROCm 7.14 and ROCm 10, and the AFARs too since those are the ones that have the latest on the Fortran compiler

@gcapodagAMD

Copy link
Copy Markdown
Collaborator

Bob already changed this test so I am closing this PR, thanks @dannybaths

@bobrobey

Copy link
Copy Markdown
Collaborator

Good catch. I got the same analysis and posted in the compiler channel. I rewrite the test to fix the problem. It is kind of a strange corner case that is unexpected.

@bobrobey

Copy link
Copy Markdown
Collaborator

There is one item here that may be glossed over. That is the setting of the FC compiler. The test script sets:
if [[ -n "$CRAYPE_VERSION" || -f /etc/cray-release ]]; then
if [ -z "$CXX" ]; then
export CXX=which CC
fi
if [ -z "$CC" ]; then
export CC=which cc
fi
if [ -z "$FC" ]; then
export FC=which ftn
fi

The analysis set FC to point to export FC="${ROCM_PATH}/bin/amdflang". We need to confirm that the ftn is set to point to the amdflang compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants