Created
March 27, 2020 00:46
-
-
Save omar-3/8c0489a32d91f82c2f977b22cf87253e to your computer and use it in GitHub Desktop.
serial and parallel implementation of iterator starmap in Chapel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use RangeChunk only; | |
// serial | |
iter starmap(array, function) { | |
var iterable = array; | |
for i in iterable { | |
yield function((...i)); // tuple expansion ... THANK YOU CHAPEL | |
} | |
} | |
// standalone | |
iter starmap(param tag: iterKind, array, function) | |
where tag == iterKind.standalone { | |
var iterable = array; | |
var numTasks = here.maxTaskPar; | |
var Range = iterable.domain.low..iterable.domain.high; | |
coforall tid in 0..#numTasks { | |
var nowRange = RangeChunk.chunk(Range, numTasks, tid); | |
for i in nowRange { | |
yield function((...iterable[i])); | |
} | |
} | |
} | |
// exponentiation - serial | |
writeln("exponentiation serial"); | |
var used_1 = [(1,2), (3,2), (2,5), (10,2), (9,1)]; | |
proc funcUsed_1(a: int, b: int) : int { | |
return a**b; | |
} | |
var result_1 = starmap(used_1, funcUsed_1); | |
writeln(result_1); // ==> [1, 9, 32, 100, 9] | |
// add all the elements in a given tuple - serial | |
writeln("addition serial"); | |
var used_2 = [(1,2,3,4), (5,5,5,5), (5,3,5,3), (1,1,1,1)]; | |
proc funcUsed_2(a: int, b: int, c: int, d: int) : int { | |
return a + b + c + d; | |
} | |
var result_2 = starmap(used_2, funcUsed_2); | |
writeln(result_2); | |
// exponentiation - parallel | |
writeln("exponentiation parallel"); | |
forall i in starmap(used_1, funcUsed_1) { | |
writeln(i); | |
} | |
// add all the elements in a given tuple - parallel | |
writeln("addition parallel"); | |
forall i in starmap(used_2, funcUsed_2) { | |
writeln(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment