Last active
October 28, 2016 02:35
-
-
Save vascoosx/8b810df3dbf98c56d2e2f5a14ebf24c0 to your computer and use it in GitHub Desktop.
parallel programs comparisons
This file contains hidden or 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
import org.scalameter._ | |
import common._ | |
import scala.math.pow | |
val standardConfig = config( | |
Key.exec.minWarmupRuns -> 40, | |
Key.exec.maxWarmupRuns -> 80, | |
Key.exec.benchRuns -> 100, | |
Key.verbose -> false | |
) withWarmer(new Warmer.Default) | |
def sum1(a: Array[Int], s: Int, t: Int): Int = { | |
var i= s; var sum: Int = 0 | |
while (i < t) { | |
sum= sum + a(i) | |
i= i + 1 } | |
sum } | |
def sum2(a: Array[Int], p: Double, s: Int, t: Int): Double = { | |
var i= s; var sum: Double = 0 | |
while (i < t) { | |
sum = sum + pow(a(i): Int, p) | |
i= i + 1 } | |
sum } | |
val a = (1 to 100000).toArray | |
val m1 = 100000/4 | |
val m2 = m1 + m1 | |
val m3 = m2 + m1 | |
val m4 = a.length | |
val seqtime = standardConfig measure { | |
val ((sumA, sumB), (sumC, sumD)) = parallel( | |
parallel(sum1(a, 0, m1), sum1(a, m1, m2)), | |
parallel(sum1(a, m2, m3), sum1(a, m3, m4))) | |
} | |
val seqtime2 = standardConfig measure { | |
sum1(a, 0, a.length) | |
} | |
val seqtime3 = standardConfig measure { | |
a.par | |
.aggregate(0)((x,y) => x + y, (x,y) => x + y) | |
} | |
val seqtime4 = standardConfig measure{ | |
val (r1, r2) = parallel(sum1(a,0,m2), sum1(a,m2, m4)) | |
r1 + r2 | |
} | |
val p = 7.35 | |
val seqtime5 = standardConfig measure { | |
val ((sumA, sumB), (sumC, sumD)) = parallel( | |
parallel(sum2(a, p, 0, m1), sum2(a, p, m1, m2)), | |
parallel(sum2(a, p, m2, m3), sum2(a, p, m3, m4))) | |
} | |
val seqtime6 = standardConfig measure { | |
sum2(a, p, 0, a.length) | |
} | |
val seqtime7 = standardConfig measure{ | |
val (r1, r2) = parallel(sum2(a, p, 0,m2), sum2(a, p,m2, m4)) | |
r1 + r2 | |
} | |
val seqtime8 = { | |
a.par | |
.aggregate(0.0)((x, y) => x + pow(y,p) , (x, y) => x + y) | |
} |
When computations are less then fetching the entire array parallel programs are slower.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.