-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jade Abraham <[email protected]>
- Loading branch information
1 parent
dc3f541
commit 65963e3
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
test/library/packages/Python/compareIterationPatterns.chpl
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
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); | ||
} | ||
|
||
|
||
|
||
} |
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
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 |