Skip to content

Commit 73df70e

Browse files
committed
Merge pull request #1294 from dgrove-oss/benchmark-sequential-integrate
Add sequential variant of Integrate benchmark
2 parents a7dea79 + 8fa5378 commit 73df70e

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ set(SWIFT_BENCH_MODULES
4646
single-source/Hanoi
4747
single-source/Hash
4848
single-source/Histogram
49+
single-source/Integrate
4950
single-source/Join
5051
single-source/LinkedList
5152
single-source/MapReduce
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//===--- Integrate.swift --------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
// A micro-benchmark for recursive divide and conquer problems.
16+
// The program performs integration via Gaussian Quadrature
17+
18+
class Integrate {
19+
static let epsilon = 1.0e-9;
20+
21+
let fun:(Double)->Double;
22+
23+
init (f:(Double)->Double) {
24+
fun = f;
25+
}
26+
27+
private func recEval(l:Double, fl:Double, r:Double, fr:Double, a:Double)->Double {
28+
let h = (r - l) / 2
29+
let hh = h / 2
30+
let c = l + h
31+
let fc = fun(c)
32+
let al = (fl + fc) * hh
33+
let ar = (fr + fc) * hh
34+
let alr = al + ar
35+
let error = abs(alr-a)
36+
if (error < Integrate.epsilon) {
37+
return alr
38+
} else {
39+
let a1 = recEval(c, fl:fc, r:r, fr:fr, a:ar)
40+
let a2 = recEval(l, fl:fl, r:c, fr:fc, a:al)
41+
return a1 + a2
42+
}
43+
}
44+
45+
@inline(never)
46+
func computeArea(left:Double, right:Double)->Double {
47+
return recEval(left, fl:fun(left), r:right, fr:fun(right), a:0)
48+
}
49+
}
50+
51+
@inline(never)
52+
public func run_Integrate(N: Int) {
53+
let obj = Integrate(f: { x in (x*x + 1.0) * x});
54+
let left = 0.0
55+
let right = 10.0
56+
let ref_result = 2550.0
57+
let bound = 0.0001
58+
var result = 0.0
59+
for _ in 1...N {
60+
result = obj.computeArea(left, right:right)
61+
if abs(result - ref_result) > bound {
62+
break
63+
}
64+
}
65+
66+
CheckResults(abs(result - ref_result) < bound,
67+
"Incorrect results in Integrate: abs(\(result) - \(ref_result)) > \(bound)")
68+
}

benchmark/utils/main.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import GlobalClass
5151
import Hanoi
5252
import Hash
5353
import Histogram
54+
import Integrate
5455
import Join
5556
import LinkedList
5657
import MapReduce
@@ -73,12 +74,12 @@ import RC4
7374
import RGBHistogram
7475
import RangeAssignment
7576
import RecursiveOwnedParameter
76-
import StaticArray
7777
import SetTests
7878
import SevenBoom
7979
import Sim2DArray
8080
import SortLettersInPlace
8181
import SortStrings
82+
import StaticArray
8283
import StrComplexWalk
8384
import StrToInt
8485
import StringBuilder
@@ -126,6 +127,7 @@ precommitTests = [
126127
"Hanoi": run_Hanoi,
127128
"HashTest": run_HashTest,
128129
"Histogram": run_Histogram,
130+
"Integrate": run_Integrate,
129131
"Join": run_Join,
130132
"LinkedList": run_LinkedList,
131133
"MapReduce": run_MapReduce,
@@ -149,7 +151,6 @@ precommitTests = [
149151
"RGBHistogram": run_RGBHistogram,
150152
"RangeAssignment": run_RangeAssignment,
151153
"RecursiveOwnedParameter": run_RecursiveOwnedParameter,
152-
"StaticArray": run_StaticArray,
153154
"SetExclusiveOr": run_SetExclusiveOr,
154155
"SetIntersect": run_SetIntersect,
155156
"SetIsSubsetOf": run_SetIsSubsetOf,
@@ -158,6 +159,7 @@ precommitTests = [
158159
"Sim2DArray": run_Sim2DArray,
159160
"SortLettersInPlace": run_SortLettersInPlace,
160161
"SortStrings": run_SortStrings,
162+
"StaticArray": run_StaticArray,
161163
"StrComplexWalk": run_StrComplexWalk,
162164
"StrToInt": run_StrToInt,
163165
"StringBuilder": run_StringBuilder,

0 commit comments

Comments
 (0)