101602 – [F2018] local and local_init are not supported in DO CONCURRENT (original) (raw)

| Description Jeff Hammond 2021-07-23 15:01:34 UTC Fortran 2018 (https://j3-fortran.org/doc/year/18/18-007r1.pdf) has three locality specifiers: shared, local and local_init. GCC Fortran does not support any of these. This breaks user experience for Fortran programmers using DO CONCURRENT code that works with other compilers. OpenMP supports equivalent locality specifiers already so the internal capability for this surely exists in GCC. program bug implicit none integer :: i, j, k integer, dimension(100) :: x j = 20 k = 30 x = 7 do concurrent (i=1:10) shared(x) k = k + x(i) j = k end do do concurrent (i=1:10) local(j) k = k + x(i) j = k end do do concurrent (i=1:10) local_init(k) k = k + x(i) j = k end do print*,k end program bug % gfortran-11 bug.F bug.F:8:31: 8 | do concurrent (i=1:10) shared(x) | 1 Error: Syntax error in DO statement at (1) bug.F:11:11: 11 | end do | 1 Error: Expecting END PROGRAM statement at (1) bug.F:12:31: 12 | do concurrent (i=1:10) local(j) | 1 Error: Syntax error in DO statement at (1) bug.F:15:11: 15 | end do | 1 Error: Expecting END PROGRAM statement at (1) bug.F:16:31: 16 | do concurrent (i=1:10) local_init(k) | 1 Error: Syntax error in DO statement at (1) bug.F:19:11: 19 | end do | 1 Error: Expecting END PROGRAM statement at (1) % gfortran-11 --version GNU Fortran (Homebrew GCC 11.1.0_1) 11.1.0 Copyright (C) 2021 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Comment 1 Harald Anlauf 2021-11-06 19:53:54 UTC Confirmed. See also F2018:R1130. Note that Intel warns that locality information is ignored unless OpenMP or other form of parallelization is active. Comment 2 Marshall Ward 2023-08-14 20:12:52 UTC I've tested this in 13.0.0 and it appears that `local`, `local_init`, `shared` are still not supported in `do concurrent` and produce syntax errors. Has there been any activity on this issue recently? If not, could anyone comment on the proposal by Jeff Hammond about utilizing the analogous OpenMP constructs? Would this be a feasible option? Comment 3 Michael Klemm 2023-08-15 05:06:59 UTC The locality specifiers cannot directly map to the OpenMP data-sharing clauses. While SHARED and LOCAL can be mapped, LOCAL_INIT cannot. The latter needs to initialize the variable anew for each iteration of the DO CONCURRENT loop, while FIRSTPRIVATE will initialize the variable only once per thread that executes chunks of said loop. So, the code transformation for that case will have to be more involved. There has been discussions in the OpenMP language committee if a LINEAR(x:0) clause can substitute LOCAL_INIT(x). That might be one solution to reduce the implementation burden and map everything to OpenMP constructs. Comment 4 Marshall Ward 2023-08-16 15:37:28 UTC Thank you Michael, that is very informative, particularly with respect to LOCAL_INIT vs FIRSTPRIVATE. If we could just get support for LOCAL, then we may be to start using do-concurrent in our production codes. Perhaps there is a possibility of only adding support for LOCAL and SHARED, and raising an error if LOCAL_INIT appears? I have no experience with the GCC source, but I can see where this should appear in the parser: diff --git a/gcc/fortran/match.cc b/gcc/fortran/match.cc index ba23bcd9692..439839295a1 100644 --- a/gcc/fortran/match.cc +++ b/gcc/fortran/match.cc @@ -2642,36 +2642,38 @@ gfc_match_do (void) if (gfc_match (" concurrent") == MATCH_YES) { gfc_forall_iterator *head; gfc_expr *mask; if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C")) return MATCH_ERROR; mask = NULL; head = NULL; m = match_forall_header (&head, &mask); if (m == MATCH_NO) return m; if (m == MATCH_ERROR) goto concurr_cleanup; + /* TODO: Parse local, local_init, shared */ + if (gfc_match_eos () != MATCH_YES) goto concurr_cleanup; if (label != NULL && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET)) goto concurr_cleanup; new_st.label1 = label; new_st.op = EXEC_DO_CONCURRENT; new_st.expr1 = mask; new_st.ext.forall_iterator = head; return MATCH_YES; concurr_cleanup: gfc_syntax_error (ST_DO); gfc_free_expr (mask); gfc_free_forall_iterator (head); I'm a little lost on how to associate these blocks with OpenMP constructs, however. Comment 5 Tobias Burnus 2025-01-25 21:26:22 UTC Basic support for the locality specifier has now been implemented in commit r15-6887-g20b8500cfa522e: commit 20b8500cfa522ebe0fcf756d5b32816da7f904dd Author: Anuj Mohite <anujmohite001@gmail.com> Date: Mon Jan 13 16:28:57 2025 -0800 Fortran: Add LOCALITY support for DO_CONCURRENT This patch provided by Anuj Mohite as part of the GSoC project. It is modified slightly by Jerry DeLisle for minor formatting. The patch provides front-end parsing of the LOCALITY specs in DO_CONCURRENT and adds numerous test cases. gcc/fortran/ChangeLog: * dump-parse-tree.cc (show_code_node): Updated to use c->ext.concur.forall_iterator instead of c->ext.forall_iterator. * frontend-passes.cc (index_interchange): Updated to use c->ext.concur.forall_iterator instead of c->ext.forall_iterator. (gfc_code_walker): Likewise. * gfortran.h (enum locality_type): Added new enum for locality types in DO CONCURRENT constructs. * match.cc (match_simple_forall): Updated to use new_st.ext.concur.forall_iterator instead of new_st.ext.forall_iterator. (gfc_match_forall): Likewise. (gfc_match_do): Implemented support for matching DO CONCURRENT locality specifiers (LOCAL, LOCAL_INIT, SHARED, DEFAULT(NONE), and REDUCE). * parse.cc (parse_do_block): Updated to use new_st.ext.concur.forall_iterator instead of new_st.ext.forall_iterator. * resolve.cc (struct check_default_none_data): Added struct check_default_none_data. (do_concur_locality_specs_f2023): New function to check compliance with F2023's C1133 constraint for DO CONCURRENT. (check_default_none_expr): New function to check DEFAULT(NONE) compliance. (resolve_locality_spec): New function to resolve locality specs. (gfc_count_forall_iterators): Updated to use code->ext.concur.forall_iterator. (gfc_resolve_forall): Updated to use code->ext.concur.forall_iterator. * st.cc (gfc_free_statement): Updated to free locality specifications and use p->ext.concur.forall_iterator. * trans-stmt.cc (gfc_trans_forall_1): Updated to use code->ext.concur.forall_iterator. gcc/testsuite/ChangeLog: * gfortran.dg/do_concurrent_10.f90: New test. * gfortran.dg/do_concurrent_8_f2018.f90: New test. * gfortran.dg/do_concurrent_8_f2023.f90: New test. * gfortran.dg/do_concurrent_9.f90: New test. * gfortran.dg/do_concurrent_all_clauses.f90: New test. * gfortran.dg/do_concurrent_basic.f90: New test. * gfortran.dg/do_concurrent_constraints.f90: New test. * gfortran.dg/do_concurrent_local_init.f90: New test. * gfortran.dg/do_concurrent_locality_specs.f90: New test. * gfortran.dg/do_concurrent_multiple_reduce.f90: New test. * gfortran.dg/do_concurrent_nested.f90: New test. * gfortran.dg/do_concurrent_parser.f90: New test. * gfortran.dg/do_concurrent_reduce_max.f90: New test. * gfortran.dg/do_concurrent_reduce_sum.f90: New test. * gfortran.dg/do_concurrent_shared.f90: New test. Signed-off-by: Anuj <anujmohite001@gmail.com> Comment 6 Tobias Burnus 2025-01-25 21:44:06 UTC Created attachment 60279 [details] Draft patch - see comment 6 for known issues ... this includes REDUCE. Note that no real concurrency except for SIMD / vectorization is supported so far. But in particular: * * * NOTE: The current implementation does not handle LOCAL or LOCAL_INIT, which fail with 'sorry, unimplemented'. * * * Attached is a draft patch which adds basic support for LOCAL and LOCAL_INIT; however, the current implementation does not handle: (A) LOCAL and default values of derived-types -> testsuite/gfortran.dg/do_concurrent_12.f90 something like deferred initialization seems to be missing (B) (no testcases) assumed-size arrays, which need to be handle in a special way. Thinking about it, maybe using gfc_omp_clause_default_ctor / gfc_omp_clause_copy_ctor instead or in additional could make sense here. And if there are issues, possibly those should be improved as well. Comment 7 Jerry DeLisle 2025-01-26 01:30:59 UTC (In reply to Tobias Burnus from comment #6) > Created attachment 60279 [details] > Draft patch - see comment 6 for known issues > > ... this includes REDUCE. > Note that no real concurrency except for SIMD / vectorization is supported > so far. But in particular: > > * * * > > NOTE: The current implementation does not handle LOCAL or LOCAL_INIT, > which fail with 'sorry, unimplemented'. > > * * * > > Attached is a draft patch which adds basic support for LOCAL and LOCAL_INIT; > however, the current implementation does not handle: > > (A) LOCAL and default values of derived-types > -> testsuite/gfortran.dg/do_concurrent_12.f90 > something like deferred initialization seems to be missing > > (B) (no testcases) assumed-size arrays, which need to be handle in a > special way. > > Thinking about it, maybe using > gfc_omp_clause_default_ctor / gfc_omp_clause_copy_ctor > instead or in additional could make sense here. And if there are issues, > possibly those should be improved as well. Thanks Tobias! I will see if I can do something with this to help you. Comment 8 Jerry DeLisle 2025-01-26 02:02:43 UTC I have the patch applied here. do_concurrent_12.f90 has six failures that look like related to optimization. I will see if I can figure this out. Running /home/jerry/dev/trunk/gcc/testsuite/gfortran.dg/dg.exp ... FAIL: gfortran.dg/do_concurrent_12.f90 -O0 execution test FAIL: gfortran.dg/do_concurrent_12.f90 -O1 execution test FAIL: gfortran.dg/do_concurrent_12.f90 -O2 execution test FAIL: gfortran.dg/do_concurrent_12.f90 -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions execution test FAIL: gfortran.dg/do_concurrent_12.f90 -O3 -g execution test FAIL: gfortran.dg/do_concurrent_12.f90 -Os execution test === gfortran Summary === # of expected passes 6 # of unexpected failures 6 /home/jerry/dev/objdir/gcc/gfortran version 15.0.1 20250126 (experimental) (GCC) Comment 9 GCC Commits 2025-04-09 06:22:53 UTC The master branch has been updated by Tobias Burnus <burnus@gcc.gnu.org>: https://gcc.gnu.org/g:2d7e1d6e40a13a5f160b584336795b80f193ec3b commit r15-9326-g2d7e1d6e40a13a5f160b584336795b80f193ec3b Author: Tobias Burnus <tburnus@baylibre.com> Date: Wed Apr 9 08:21:19 2025 +0200 Fortran: Add code gen for do,concurrent's LOCAL/LOCAL_INIT [PR101602] Implement LOCAL and LOCAL_INIT; we locally replace the tree declaration by a local declaration of the outer variable. The 'local_init' then assigns the value at the beginning of each loop iteration from the outer declaration. Note that the current implementation does not handle LOCAL with types that have a default initializer and LOCAL/LOCAL_INIT for assumed-shape arrays; this is diagnosed with a sorry error. PR fortran/101602 gcc/fortran/ChangeLog: * resolve.cc (resolve_locality_spec): Remove 'sorry, unimplemented'. * trans-stmt.cc (struct symbol_and_tree_t): New. (gfc_trans_concurrent_locality_spec): New. (gfc_trans_forall_1): Call it; update to handle local and local_init. * trans-decl.cc (gfc_start_saved_local_decls, gfc_stop_saved_local_decls): New; moved code from ... (gfc_process_block_locals): ... here. Call it. * trans.h (gfc_start_saved_local_decls, gfc_stop_saved_local_decls): Declare. gcc/testsuite/ChangeLog: * gfortran.dg/do_concurrent_8_f2023.f90: Update for removed 'sorry, unimplemented'. * gfortran.dg/do_concurrent_9.f90: Likewise. * gfortran.dg/do_concurrent_all_clauses.f90: Likewise. * gfortran.dg/do_concurrent_local_init.f90: Likewise. * gfortran.dg/do_concurrent_locality_specs.f90: Likewise. * gfortran.dg/do_concurrent_11.f90: New test. * gfortran.dg/do_concurrent_12.f90: New test. * gfortran.dg/do_concurrent_13.f90: New test. * gfortran.dg/do_concurrent_14.f90: New test. * gfortran.dg/do_concurrent_15.f90: New test. Comment 10 GCC Commits 2025-04-12 09:01:45 UTC The trunk branch has been updated by Thomas Schwinge <tschwinge@gcc.gnu.org>: https://gcc.gnu.org/g:f417af3f9f94538c2600e78e6c60b61c29fdbf99 commit r15-9393-gf417af3f9f94538c2600e78e6c60b61c29fdbf99 Author: Thomas Schwinge <tschwinge@baylibre.com> Date: Sat Apr 12 10:53:14 2025 +0200 Fortran: Add code gen for do,concurrent's LOCAL/LOCAL_INIT: Fix 'static_assert' [PR101602] Fix-up for commit 2d7e1d6e40a13a5f160b584336795b80f193ec3b "Fortran: Add code gen for do,concurrent's LOCAL/LOCAL_INIT [PR101602]": ../../source-gcc/gcc/fortran/trans-stmt.cc: In function âvoid gfc_trans_concurrent_locality_spec(bool, stmtblock_t*, std::vector<symbol_and_tree_t>*, gfc_expr_list**)â: ../../source-gcc/gcc/fortran/trans-stmt.cc:5157:59: error: expected â,â before â)â token static_assert (LOCALITY_LOCAL_INIT - LOCALITY_LOCAL == 1); ^ ../../source-gcc/gcc/fortran/trans-stmt.cc:5157:59: error: expected string-literal before â)â token make[2]: *** [Makefile:1210: fortran/trans-stmt.o] Error 1 PR fortran/101602 gcc/fortran/ * trans-stmt.cc (gfc_trans_concurrent_locality_spec): Fix 'static_assert'. | | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------- | ------ | ------------------------------------------------------------------ | ------------------------------- | --------------------------------------------------------------- | ------ | ------------------------------------------------------------------ | ------------------------------------- | --------------------------------------------------------------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |