Skip to content

Commit

Permalink
add iteration/apply test
Browse files Browse the repository at this point in the history
Signed-off-by: Jade Abraham <[email protected]>
  • Loading branch information
jabraham17 committed Nov 6, 2024
1 parent dc3f541 commit 65963e3
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
117 changes: 117 additions & 0 deletions test/library/packages/Python/compareIterationPatterns.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@

use Python;
use Time;

config const print = true;
config const time = false;
config const n = 10;

config const lambdaStr = "lambda x,: x + 1 if x % 2 != 0 else x";
proc makeEven(x: int): int do return if x % 2 != 0 then x+1 else x;

//
// Calling a python Lambda function from Chapel serially
//
proc serialPythonApply(interp: borrowed, type t, arr, l) {
var lambdaFunc = new Python.Function(interp, l);
var res: [arr.domain] t;
for i in arr.domain {
res(i) = lambdaFunc(t, arr(i));
}
return res;
}
//
// Calling a python Lambda function from Chapel in parallel
//
proc parallelPythonApply(interp: borrowed, type t, arr, l) {
var lambdaFunc = new Python.Function(interp, l);
var res: [arr.domain] t;
var ts = new threadState();
ts.save();
forall i in arr.domain with (var gil = new Python.GIL()) {
res(i) = lambdaFunc(t, arr(i));
}
ts.restore();
return res;
}


//
// Calling a Chapel FCP from Chapel serially
//
proc serialChapelApply(type t, arr, f) {
var res: [arr.domain] t;
for i in arr.domain {
res(i) = f(arr(i));
}
return res;
}
//
// Calling a Chapel FCP from Chapel in parallel
//
proc parallelChapelApply(type t, arr, f) {
var res: [arr.domain] t;
forall i in arr.domain {
res(i) = f(arr(i));
}
return res;
}

proc main() {
var data: [1..#n] int = 1..#n;
if print then writeln("data: ", data);


var interp = new Python.Interpreter();

{
data = 1..#n;
var s = new stopwatch();
s.start();
var res = serialPythonApply(interp, int, data, lambdaStr);
s.stop();
if time then
writeln("Elapsed time (Serial Python): ", s.elapsed(), " seconds");
if print then
writeln("Serial Python result: ", res);
}

{
data = 1..#n;
var s = new stopwatch();
s.start();
var res = parallelPythonApply(interp, int, data, lambdaStr);
s.stop();
if time then
writeln("Elapsed time (Parallel Python): ", s.elapsed(), " seconds");
if print then
writeln("Parallel Python result: ", res);
}

{
data = 1..#n;
var s = new stopwatch();
s.start();
var res = serialChapelApply(int, data, makeEven);
s.stop();
if time then
writeln("Elapsed time (Serial Chapel): ", s.elapsed(), " seconds");
if print then
writeln("Serial Chapel result: ", res);
}

{
data = 1..#n;
var s = new stopwatch();
s.start();
var res = parallelChapelApply(int, data, makeEven);
s.stop();
if time then
writeln("Elapsed time (Parallel Chapel): ", s.elapsed(), " seconds");
if print then
writeln("Parallel Chapel result: ", res);
}



}
5 changes: 5 additions & 0 deletions test/library/packages/Python/compareIterationPatterns.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data: 1 2 3 4 5 6 7 8 9 10
Serial Python result: 2 2 4 4 6 6 8 8 10 10
Parallel Python result: 2 2 4 4 6 6 8 8 10 10
Serial Chapel result: 2 2 4 4 6 6 8 8 10 10
Parallel Chapel result: 2 2 4 4 6 6 8 8 10 10

0 comments on commit 65963e3

Please sign in to comment.