Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parallel iterators for map.keys and map.values #26519

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions modules/internal/ChapelHashtable.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -304,31 +304,31 @@ module ChapelHashtable {
}

iter allSlots() {
for slot in _allSlots(tableSize) {
foreach slot in _allSlots(tableSize) {
yield slot;
}
}

iter allSlots(param tag: iterKind)
where tag == iterKind.standalone {

for slot in _allSlots(tableSize, tag=tag) {
foreach slot in _allSlots(tableSize, tag=tag) {
yield slot;
}
}

iter allSlots(param tag: iterKind)
where tag == iterKind.leader {

for followThis in _allSlots(tableSize, tag=tag) {
foreach followThis in _allSlots(tableSize, tag=tag) {
yield followThis;
}
}

iter allSlots(followThis, param tag: iterKind)
where tag == iterKind.follower {

for i in _allSlots(tableSize, followThis, tag=tag) {
foreach i in _allSlots(tableSize, followThis, tag=tag) {
yield i;
}
}
Expand Down
45 changes: 43 additions & 2 deletions modules/standard/Map.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,27 @@ module Map {
yield table.table[slot].key;
}
}
@chpldoc.nodoc
iter keys(param tag: iterKind) const ref where tag == iterKind.standalone {
foreach slot in table.allSlots(tag) {
if table.isSlotFull(slot) then
yield table.table[slot].key;
}
}
@chpldoc.nodoc
iter keys(param tag: iterKind) where tag == iterKind.leader {
var space = 0..#table.tableSize;
foreach followThis in space.these(tag) {
yield followThis;
}
}
@chpldoc.nodoc
iter keys(param tag: iterKind, followThis) const ref where tag == iterKind.follower {
foreach idx in followThis(0) {
if table.isSlotFull(idx) then
yield table.table[idx].key;
}
}

/*
Iterates over the key-value pairs of this map.
Expand Down Expand Up @@ -623,13 +644,33 @@ module Map {

:yields: A reference to one of the values contained in this map.
*/
iter values() ref
{
iter values() ref {
foreach slot in table.allSlots() {
if table.isSlotFull(slot) then
yield table.table[slot].val;
}
}
@chpldoc.nodoc
iter values(param tag: iterKind) ref where tag == iterKind.standalone {
foreach slot in table.allSlots(tag) {
if table.isSlotFull(slot) then
yield table.table[slot].val;
}
}
@chpldoc.nodoc
iter values(param tag: iterKind) where tag == iterKind.leader {
var space = 0..#table.tableSize;
foreach followThis in space.these(tag) {
yield followThis;
}
}
@chpldoc.nodoc
iter values(param tag: iterKind, followThis) ref where tag == iterKind.follower {
foreach idx in followThis(0) {
if table.isSlotFull(idx) then
yield table.table[idx].val;
}
}

@chpldoc.nodoc
proc ref _readHelper(r: fileReader, ref deserializer) throws {
Expand Down
17 changes: 17 additions & 0 deletions test/library/standard/Map/reduceOverKeysAndValues.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use Map;

var m: map(int, int);

for i in 1..100 do m[i] = i;

{
var s = 0;
forall k in m.keys() with (+ reduce s) do s += k;
writeln(s);
}

{
var s = 0;
forall v in m.values() with (+ reduce s) do s += v;
writeln(s);
}
2 changes: 2 additions & 0 deletions test/library/standard/Map/reduceOverKeysAndValues.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5050
5050
32 changes: 29 additions & 3 deletions test/library/standard/Map/testIterators.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,42 @@ var A: [1..fac.size] (int, FacInt);
for item in zip(fac.keys(), fac.values()) {
A[item(0)] = item;
}
writeln(sorted(A));
writeln("A: ", sorted(A));

var B: [1..fac.size] FacInt;
for (val, i) in zip(fac.values(), 1..) {
B[i] = val;
}
writeln(sorted(B));
writeln("B: ", sorted(B));

var C: [1..fac.size] (int, FacInt);
for key in fac.keys() {
C[key] = (key, fac[key]);
}
writeln(sorted(C));
writeln("C: ", sorted(C));

var D: [1..fac.size] (int, FacInt);
forall item in zip(fac.keys(), fac.values()) {
D[item(0)] = item;
}
writeln("D: ", sorted(D));

var E: [1..fac.size] (FacInt, int);
forall item in zip(fac.values(), fac.keys()) {
E[item(1)] = item;
}
writeln("E: ", sorted(E));

var F: [1..fac.size] (int, FacInt);
forall key in fac.keys() {
F[key] = (key, fac[key]);
}
writeln("F: ", sorted(F));

var G: [1..fac.size] FacInt;
var Hidx: atomic int = 1;
forall val in fac.values() {
var i = Hidx.fetchAdd(1);
G[i] = val;
}
writeln("G: ", sorted(G));
10 changes: 7 additions & 3 deletions test/library/standard/Map/testIterators.good
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
(1, (n = 1, nFac = 1)) (2, (n = 2, nFac = 2)) (3, (n = 3, nFac = 6)) (4, (n = 4, nFac = 24)) (5, (n = 5, nFac = 120)) (6, (n = 6, nFac = 720)) (7, (n = 7, nFac = 5040)) (8, (n = 8, nFac = 40320)) (9, (n = 9, nFac = 362880)) (10, (n = 10, nFac = 3628800)) (11, (n = 11, nFac = 39916800)) (12, (n = 12, nFac = 479001600)) (13, (n = 13, nFac = 6227020800)) (14, (n = 14, nFac = 87178291200)) (15, (n = 15, nFac = 1307674368000))
(n = 1, nFac = 1) (n = 2, nFac = 2) (n = 3, nFac = 6) (n = 4, nFac = 24) (n = 5, nFac = 120) (n = 6, nFac = 720) (n = 7, nFac = 5040) (n = 8, nFac = 40320) (n = 9, nFac = 362880) (n = 10, nFac = 3628800) (n = 11, nFac = 39916800) (n = 12, nFac = 479001600) (n = 13, nFac = 6227020800) (n = 14, nFac = 87178291200) (n = 15, nFac = 1307674368000)
(1, (n = 1, nFac = 1)) (2, (n = 2, nFac = 2)) (3, (n = 3, nFac = 6)) (4, (n = 4, nFac = 24)) (5, (n = 5, nFac = 120)) (6, (n = 6, nFac = 720)) (7, (n = 7, nFac = 5040)) (8, (n = 8, nFac = 40320)) (9, (n = 9, nFac = 362880)) (10, (n = 10, nFac = 3628800)) (11, (n = 11, nFac = 39916800)) (12, (n = 12, nFac = 479001600)) (13, (n = 13, nFac = 6227020800)) (14, (n = 14, nFac = 87178291200)) (15, (n = 15, nFac = 1307674368000))
A: (1, (n = 1, nFac = 1)) (2, (n = 2, nFac = 2)) (3, (n = 3, nFac = 6)) (4, (n = 4, nFac = 24)) (5, (n = 5, nFac = 120)) (6, (n = 6, nFac = 720)) (7, (n = 7, nFac = 5040)) (8, (n = 8, nFac = 40320)) (9, (n = 9, nFac = 362880)) (10, (n = 10, nFac = 3628800)) (11, (n = 11, nFac = 39916800)) (12, (n = 12, nFac = 479001600)) (13, (n = 13, nFac = 6227020800)) (14, (n = 14, nFac = 87178291200)) (15, (n = 15, nFac = 1307674368000))
B: (n = 1, nFac = 1) (n = 2, nFac = 2) (n = 3, nFac = 6) (n = 4, nFac = 24) (n = 5, nFac = 120) (n = 6, nFac = 720) (n = 7, nFac = 5040) (n = 8, nFac = 40320) (n = 9, nFac = 362880) (n = 10, nFac = 3628800) (n = 11, nFac = 39916800) (n = 12, nFac = 479001600) (n = 13, nFac = 6227020800) (n = 14, nFac = 87178291200) (n = 15, nFac = 1307674368000)
C: (1, (n = 1, nFac = 1)) (2, (n = 2, nFac = 2)) (3, (n = 3, nFac = 6)) (4, (n = 4, nFac = 24)) (5, (n = 5, nFac = 120)) (6, (n = 6, nFac = 720)) (7, (n = 7, nFac = 5040)) (8, (n = 8, nFac = 40320)) (9, (n = 9, nFac = 362880)) (10, (n = 10, nFac = 3628800)) (11, (n = 11, nFac = 39916800)) (12, (n = 12, nFac = 479001600)) (13, (n = 13, nFac = 6227020800)) (14, (n = 14, nFac = 87178291200)) (15, (n = 15, nFac = 1307674368000))
D: (1, (n = 1, nFac = 1)) (2, (n = 2, nFac = 2)) (3, (n = 3, nFac = 6)) (4, (n = 4, nFac = 24)) (5, (n = 5, nFac = 120)) (6, (n = 6, nFac = 720)) (7, (n = 7, nFac = 5040)) (8, (n = 8, nFac = 40320)) (9, (n = 9, nFac = 362880)) (10, (n = 10, nFac = 3628800)) (11, (n = 11, nFac = 39916800)) (12, (n = 12, nFac = 479001600)) (13, (n = 13, nFac = 6227020800)) (14, (n = 14, nFac = 87178291200)) (15, (n = 15, nFac = 1307674368000))
E: ((n = 1, nFac = 1), 1) ((n = 2, nFac = 2), 2) ((n = 3, nFac = 6), 3) ((n = 4, nFac = 24), 4) ((n = 5, nFac = 120), 5) ((n = 6, nFac = 720), 6) ((n = 7, nFac = 5040), 7) ((n = 8, nFac = 40320), 8) ((n = 9, nFac = 362880), 9) ((n = 10, nFac = 3628800), 10) ((n = 11, nFac = 39916800), 11) ((n = 12, nFac = 479001600), 12) ((n = 13, nFac = 6227020800), 13) ((n = 14, nFac = 87178291200), 14) ((n = 15, nFac = 1307674368000), 15)
F: (1, (n = 1, nFac = 1)) (2, (n = 2, nFac = 2)) (3, (n = 3, nFac = 6)) (4, (n = 4, nFac = 24)) (5, (n = 5, nFac = 120)) (6, (n = 6, nFac = 720)) (7, (n = 7, nFac = 5040)) (8, (n = 8, nFac = 40320)) (9, (n = 9, nFac = 362880)) (10, (n = 10, nFac = 3628800)) (11, (n = 11, nFac = 39916800)) (12, (n = 12, nFac = 479001600)) (13, (n = 13, nFac = 6227020800)) (14, (n = 14, nFac = 87178291200)) (15, (n = 15, nFac = 1307674368000))
G: (n = 1, nFac = 1) (n = 2, nFac = 2) (n = 3, nFac = 6) (n = 4, nFac = 24) (n = 5, nFac = 120) (n = 6, nFac = 720) (n = 7, nFac = 5040) (n = 8, nFac = 40320) (n = 9, nFac = 362880) (n = 10, nFac = 3628800) (n = 11, nFac = 39916800) (n = 12, nFac = 479001600) (n = 13, nFac = 6227020800) (n = 14, nFac = 87178291200) (n = 15, nFac = 1307674368000)
Loading