Skip to content

Commit cd701c2

Browse files
committed
CO_REDUCE_PREFIX_INCLUSIVE: Add segmented sum example, based on MPI_SCAN
1 parent ecd5b3c commit cd701c2

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

drafts/25-WIP-collective-edits.txt

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,44 @@ ERRMSG (optional) shall be a noncoindexed default character scalar. It
268268

269269
The semantics of TEAM, COMPLETION, STAT and ERRMSG are described in 16.6.
270270

271-
<<Example.>>
271+
<<Example.>> This example uses a user-defined operation to produce a
272+
segmented prefix sum. A segmented prefix sum takes, as input, an ordered
273+
list of values and corresponding list of logicals, and the logicals
274+
delineate the various segments of the prefix sum. For example:
272275

273-
*** TODO ***
276+
values: 1 2 4 5 6 7 8 9
277+
logicals: F F T T T F F T
278+
result: 1 3 4 9 15 7 15 9
279+
280+
Note the segmented_sum operation used below is noncommutative.
281+
282+
SUBROUTINE co_prefix_segment_sum(value, flag)
283+
REAL, INTENT(INOUT) :: value
284+
LOGICAL, INTENT(IN) :: flag
285+
286+
TYPE :: tuple
287+
REAL :: value
288+
LOGICAL :: flag
289+
END TYPE
290+
TYPE(tuple) :: t
291+
292+
t = tuple(value, flag)
293+
CALL CO_REDUCE_PREFIX_INCLUSIVE(t, OPERATION=segmented_sum)
294+
value = t%value
295+
296+
CONTAINS
297+
PURE FUNCTION segmented_sum(lhs,rhs) RESULT(sum)
298+
TYPE(tuple), INTENT(IN) :: lhs,rhs
299+
TYPE(tuple) :: sum
300+
301+
IF (lhs%flag .eqv. rhs%flag) THEN
302+
sum%value = lhs%value + rhs%value
303+
ELSE
304+
sum%value = rhs%value
305+
END IF
306+
sum%flag = rhs%flag
307+
END FUNCTION segmented_sum
308+
END SUBROUTINE co_prefix_segment_sum
274309

275310
-------------------------------------------------------------------------
276311
[412:4+] In 16.9 Specifications of the standard intrinsic procedures,

drafts/coll-edit-notes.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
Collective subroutines edits TODO:
22

3-
* Write REDUCE examples
3+
* Find better ways to test the CO_REDUCE_PREFIX examples
44

55
Examples:
66
EXCLUSIVE
7-
UNTESTED with multi-image
87
MAXLOC over a derived type of real value and integer image ID
98
computes max value in prefix and the image that provided it
109
INCLUSIVE
1110
derived type: value and boolean
12-
operation on boolean flag is XOR
1311
illustrates segmented prefix reduction
1412
MPI Example 6.24.
1513

0 commit comments

Comments
 (0)