forked from saddle/saddle
-
Notifications
You must be signed in to change notification settings - Fork 11
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 specialization and loop optis benchmark suites #436
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
saddle-jmh/src/main/scala/org/saddle/LoopOptimizations.scala
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,134 @@ | ||
package org.saddle | ||
|
||
import org.openjdk.jmh.annotations._ | ||
|
||
/** Benchmark suite comparing different loop patterns and the impact of loop optimizations. | ||
* https://wiki.openjdk.java.net/pages/viewpage.action?pageId=20415918 | ||
*/ | ||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 10) | ||
@Measurement(iterations = 10) | ||
@Fork(1) | ||
@Threads(1) | ||
class LoopOptimisations { | ||
@Param(Array("10000")) | ||
var size: Int = _ | ||
var arr: Array[Double] = _ | ||
var vec1: Vec[Double] = _ | ||
var b: Double = _ | ||
|
||
@Setup(Level.Iteration) | ||
def setup() = { | ||
arr = vec.rand(size).toArray | ||
vec1 = Vec(arr) | ||
b = scala.util.Random.nextDouble() | ||
} | ||
|
||
@Benchmark | ||
/** Simplest variant, should trigger vectorization and other optimisations. | ||
*/ | ||
def arrayAdd(): Array[Double] = { | ||
var i = 0 | ||
while (i < arr.length) { | ||
arr(i) += b | ||
i += 1 | ||
} | ||
arr | ||
} | ||
|
||
@Benchmark | ||
def arrayAddExpandedAssignmentOperator(): Array[Double] = { | ||
var i = 0 | ||
while (i < arr.length) { | ||
// assignement operator seems to perform worse | ||
arr(i) = arr(i) + b | ||
i += 1 | ||
} | ||
arr | ||
} | ||
|
||
|
||
/** hand-unrolled loops prevent vectorization and potentially other | ||
* optimisations | ||
*/ | ||
@Benchmark | ||
def arrayAddHandUnrolled2(): Array[Double] = { | ||
var i = 0 | ||
val length = arr.length | ||
val unrolledStride = 2 | ||
val preloopIterations = length % unrolledStride | ||
while (i < preloopIterations) { | ||
arr(i) += b | ||
i += 1 | ||
} | ||
while (i < length) { | ||
arr(i + 0) += b | ||
arr(i + 1) += b | ||
i += unrolledStride | ||
} | ||
arr | ||
} | ||
|
||
@Benchmark | ||
def arrayAddHandUnrolled5(): Array[Double] = { | ||
var i = 0 | ||
val length = arr.length | ||
val unrolledStride = 5 | ||
val preloopIterations = length % unrolledStride | ||
while (i < preloopIterations) { | ||
arr(i) += b | ||
i += 1 | ||
} | ||
while (i < length) { | ||
arr(i + 0) += b | ||
arr(i + 1) += b | ||
arr(i + 2) += b | ||
arr(i + 3) += b | ||
arr(i + 4) += b | ||
i += unrolledStride | ||
} | ||
arr | ||
} | ||
|
||
@Benchmark | ||
def arrayDiv(): Array[Double] = { | ||
var i = 0 | ||
while (i < arr.length) { | ||
arr(i) /= b | ||
i += 1 | ||
} | ||
arr | ||
} | ||
|
||
@Benchmark | ||
def arrayDivHandUnrolled2(): Array[Double] = { | ||
var i = 0 | ||
val length = arr.length | ||
val unrolledStride = 2 | ||
val preloopIterations = length % unrolledStride | ||
while (i < preloopIterations) { | ||
arr(i) /= b | ||
i += 1 | ||
} | ||
while (i < length) { | ||
arr(i + 0) /= b | ||
arr(i + 1) /= b | ||
i += unrolledStride | ||
} | ||
arr | ||
} | ||
|
||
@Benchmark | ||
def vecBinOps(): Vec[Double] = { | ||
import ops.BinOps._ | ||
vec1 += b | ||
vec1 | ||
} | ||
|
||
@Benchmark | ||
def vecBinOpsMacros(): Vec[Double] = { | ||
import macros.BinOps._ | ||
vec1 += b | ||
vec1 | ||
} | ||
} |
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,47 @@ | ||
package org.saddle | ||
|
||
import org.saddle.scalar.ScalarTag | ||
import org.saddle.vec.VecDefault | ||
|
||
import org.openjdk.jmh.annotations._ | ||
|
||
class VecBoxed[T: ScalarTag](values: Array[T]) | ||
extends VecDefault[T](values, implicitly[ScalarTag[T]]) | ||
|
||
/** Benchmark suite illustrating the performance boost of specialization. | ||
*/ | ||
@State(Scope.Benchmark) | ||
@Warmup(iterations = 10) | ||
@Measurement(iterations = 10) | ||
@Fork(1) | ||
@Threads(1) | ||
class Specialization { | ||
@Param(Array("10", "10000")) | ||
var size: Int = _ | ||
|
||
var v1: Vec[Double] = _ | ||
var v2: VecBoxed[Double] = _ | ||
var b: Double = _ | ||
|
||
@Setup(Level.Iteration) | ||
def setup() = { | ||
v1 = vec.rand(size) | ||
v2 = new VecBoxed(v1.toArray) | ||
b = scala.util.Random.nextDouble() | ||
} | ||
|
||
@Benchmark | ||
def vecSpecialized(): Vec[Double] = { | ||
import org.saddle.ops.BinOps._ | ||
v1 += b | ||
v1 | ||
} | ||
|
||
@Benchmark | ||
def vecBoxed(): Vec[Double] = { | ||
import org.saddle.ops.BinOps._ | ||
v2 += b | ||
v2 | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly, assignment operators seem to harm performance. Here
arr(i) += b
performs worse.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I must have imagined this, can't reproduce it anymore.