To: J3 J3/25-186r2 From: Brandon Cook & Dan Bonachea Subject: Specifications and Syntax for Local Prefix Operation Intrinsics Date: 2025-October-15 References: J3/25-145r1, WG5/N-2249 1. Background ============= The Fortran 202Y work list (WG5/N-2249) includes work item US20: "Add Intrinsic and collective subroutines for prefix operations" Paper J3/25-145r1 "Requirements for US20: Local Prefix Operation Intrinsics" presents illustrative use cases and requirements for local prefix operation intrinsics. That paper was passed at J3 meeting #236 in June 2025. This document focuses on specifications and syntax exclusively for the local prefix reduction variant, refining previous concepts based on community and WG5 feedback. By focusing exclusively on the local variant our aim is to allow consideration independent of the closely related but distinct collective subroutines. 2. SUM_PREFIX ============= 2.1 Syntax ----------- For reference, the syntax for the existing SUM intrinsic is: SUM(ARRAY, DIM [, MASK]) or SUM(ARRAY [, MASK]) The new intrinsics are: SUM_PREFIX_INCLUSIVE(ARRAY [, MASK]) or SUM_PREFIX_INCLUSIVE(ARRAY, DIM [, MASK]) SUM_PREFIX_EXCLUSIVE(ARRAY, [, MASK]) or SUM_PREFIX_EXCLUSIVE(ARRAY, DIM [, MASK]) 2.2 Specifications ------------------ Arguments --------- S01. ARRAY shall be an array of numeric type. S03. DIM shall be an integer scalar with a value in the range 1 <= DIM <= n, where n is the rank of ARRAY. S05. MASK (optional) shall be of type logical and shall be conformable with ARRAY. Result Characteristics ---------------------- S07. The result shall have the same type, rank, shape and kind parameter as ARRAY. Result Value: SUM_PREFIX_INCLUSIVE ---------------------------------- S09. The inclusive prefix sum of a sequence S with n elements s_1, s_2, ..., s_n, is the sequence R with n elements r_1, r_2, ..., r_n, where r_i = \sum_{j=1}^i s_j. S11. The result of SUM_PREFIX_INCLUSIVE(ARRAY) is the inclusive prefix sum of the sequence of elements of ARRAY in array element order, with the result sequence provided in array element order. Example: If A is | 1 2 3 |, SUM_PREFIX_INCLUSIVE(A) is | 1 3 6 |. S13. The MASK argument affects the prefix sum as if ARRAY elements corresponding to false elements of MASK had been replaced by 0. Specifically, the result of SUM_PREFIX_INCLUSIVE(ARRAY, MASK) is equal to SUM_PREFIX_INCLUSIVE(MERGE(ARRAY, 0, MASK)). Example: If A is | 1 2 3| and M is | T F T |, SUM_PREFIX_INCLUSIVE(A, MASK = M) is SUM_PREFIX_INCLUSIVE( | 1 0 3 | ) == | 1 1 4 |. S15. If ARRAY has rank one, the result of SUM_PREFIX_INCLUSIVE(ARRAY, DIM = DIM [, MASK = MASK]) has a value equal to SUM_PREFIX_INCLUSIVE(ARRAY [, MASK = MASK]). Otherwise, the result of SUM_PREFIX_INCLUSIVE(ARRAY, DIM = DIM [, MASK = MASK]) is defined by the semantics of S13 applied independently along each sequence of ARRAY elements in dimension DIM. More specifically, the value of result elements (i_1, i_2, ..., i_{DIM-1}, :, i_{DIM+1}, ..., i_n) of SUM_PREFIX_INCLUSIVE(ARRAY, DIM = DIM [, MASK = MASK]) is equal to SUM_PREFIX_INCLUSIVE( ARRAY(i_1, i_2, ..., i_{DIM-1}, :, i_{DIM+1}, ..., i_n), DIM = 1 [, MASK(i_1, i_2, ..., i_{DIM-1}, :, i_{DIM+1}, ..., i_n)]) Example: If A is | 1 2 3 | | 4 5 6 |, SUM_PREFIX_INCLUSIVE(A, DIM=2) is the array | SUM_PREFIX_INCLUSIVE(A(1,:)) | | SUM_PREFIX_INCLUSIVE(A(2,:)) | which is | 1 3 6 | | 4 9 15 |. Result Value: SUM_PREFIX_EXCLUSIVE ---------------------------------- S19. The exclusive prefix sum of a sequence S with n elements s_1, s_2, ..., s_n, is the sequence R with n elements r_1, r_2, ..., r_n, where r_i = \sum_{j=0}^{i-1} s_j with s_0 = 0. S21. The result of SUM_PREFIX_EXCLUSIVE(ARRAY) is the exclusive prefix sum of the sequence of elements of ARRAY in array element order, with the result sequence provided in array element order. Example: If A is | 1 2 3 |, SUM_PREFIX_EXCLUSIVE(A) is | 0 1 3 |. S23. The MASK argument affects the prefix sum as if ARRAY elements corresponding to false elements of MASK had been replaced by 0. Specifically, the result of SUM_PREFIX_EXCLUSIVE(ARRAY, MASK) is equal to SUM_PREFIX_EXCLUSIVE(MERGE(ARRAY, 0, MASK)). Example: If A is | 1 2 3| and M is | T F T |, SUM_PREFIX_EXCLUSIVE(A, MASK = M) is SUM_PREFIX_EXCLUSIVE( | 1 0 3 | ) == | 0 1 1 |. S25. If ARRAY has rank one, the result of SUM_PREFIX_EXCLUSIVE(ARRAY, DIM = DIM [, MASK = MASK]) has a value equal to SUM_PREFIX_EXCLUSIVE(ARRAY [, MASK = MASK]). Otherwise, the result of SUM_PREFIX_EXCLUSIVE(ARRAY, DIM = DIM [, MASK = MASK]) is defined by the semantics of S23 applied independently along each sequence of ARRAY elements in dimension DIM. More specifically, the value of result elements (i_1, i_2, ..., i_{DIM-1}, :, i_{DIM+1}, ..., i_n) of SUM_PREFIX_EXCLUSIVE(ARRAY, DIM = DIM [, MASK = MASK]) is equal to SUM_PREFIX_EXCLUSIVE( ARRAY(i_1, i_2, ..., i_{DIM-1}, :, i_{DIM+1}, ..., i_n), DIM = 1 [, MASK(i_1, i_2, ..., i_{DIM-1}, :, i_{DIM+1}, ..., i_n)]) Example: If A is | 1 2 3 | | 4 5 6 |, SUM_PREFIX_EXCLUSIVE(A, DIM=2) is the array | SUM_PREFIX_EXCLUSIVE(A(1,:)) | | SUM_PREFIX_EXCLUSIVE(A(2,:)) | which is | 0 1 3 | | 0 4 9 |