-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathProgram.fs
More file actions
216 lines (171 loc) · 6.44 KB
/
Copy pathProgram.fs
File metadata and controls
216 lines (171 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
module Program
open System.Linq
open System.Linq.Expressions
open System
open System.Threading.Tasks
open System.Threading
open System.Numerics
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
open BenchmarkDotNet.Configs
open BenchmarkDotNet.Jobs
open SIMDArrayUtils
open BenchmarkDotNet.Diagnostics.Windows
open System.Collections.Generic
module Array =
let inline zeroCreateUnchecked (count: int) = Array.zeroCreate count
let inline subUnchecked startIndex count (array: 'T[]) = Array.sub array startIndex count
// Almost every array function calls this, so mock it with
// the exact same code
let inline checkNonNull argName arg =
match box arg with
| null -> nullArg argName
| _ -> ()
let empty = [||]
let inline indexNotFound () = raise (Exception())
let partition f (array: _[]) =
checkNonNull "array" array
let res = Array.zeroCreateUnchecked array.Length
let mutable upCount = 0
let mutable downCount = array.Length - 1
for x in array do
if f x then
res.[upCount] <- x
upCount <- upCount + 1
else
res.[downCount] <- x
downCount <- downCount - 1
let res1 = Array.subUnchecked 0 upCount res
let res2 = Array.zeroCreateUnchecked (array.Length - upCount)
downCount <- array.Length - 1
for i = 0 to res2.Length - 1 do
res2.[i] <- res.[downCount]
downCount <- downCount - 1
res1, res2
[<GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)>]
[<CategoriesColumn>]
[<MemoryDiagnoser>]
type CoreBenchmark() =
let mutable arrayInt = [||]
let mutable arrayInt2 = [||]
let mutable arrayFloat = [||]
let mutable arrayFloat2 = [||]
[<Params(100, 1000, 1000000)>]
member val public Length = 0 with get, set
member val public Half = Int32.MaxValue / 2
[<GlobalSetup>]
member self.SetupData() =
let r = Random(self.Length)
arrayInt <- Array.init self.Length (fun i -> r.Next())
arrayInt2 <- Array.init self.Length (fun i -> r.Next())
arrayFloat <- Array.init self.Length (fun i -> r.NextDouble())
arrayFloat2 <- Array.init self.Length (fun i -> r.NextDouble())
[<BenchmarkCategory("MapSquare", "Int")>]
[<Benchmark(Baseline = true)>]
member self.MapSquare() = arrayInt |> Array.map (fun x -> x * x)
[<BenchmarkCategory("MapSquare", "Int")>]
[<Benchmark>]
member self.MapSquareSIMD() =
arrayInt |> Array.SIMD.map (fun x -> x * x) (fun x -> x * x)
[<BenchmarkCategory("MapSquare", "Float")>]
[<Benchmark(Baseline = true)>]
member self.MapSquareFloat() =
arrayFloat |> Array.map (fun x -> x * x)
[<BenchmarkCategory("MapSquare", "Float")>]
[<Benchmark>]
member self.MapSquareSIMDFloat() =
arrayFloat |> Array.SIMD.map (fun x -> x * x) (fun x -> x * x)
[<BenchmarkCategory("Dot")>]
[<Benchmark(Baseline = true)>]
member self.Dot() =
arrayInt |> Array.fold2 (fun a x y -> a + x * y) 0 arrayInt2
[<BenchmarkCategory("Dot")>]
[<Benchmark>]
member self.DotSIMD() = arrayInt |> Array.SIMD.dot arrayInt2
[<BenchmarkCategory("Max", "Int")>]
[<Benchmark(Baseline = true)>]
member self.Max() = arrayInt |> Array.max
[<BenchmarkCategory("Max", "Int")>]
[<Benchmark>]
member self.MaxSIMD() = arrayInt |> Array.SIMD.max
[<BenchmarkCategory("Max", "Float")>]
[<Benchmark(Baseline = true)>]
member self.MaxFloat() = arrayFloat |> Array.max
[<BenchmarkCategory("Max", "Float")>]
[<Benchmark>]
member self.MaxSIMDFloat() = arrayFloat |> Array.SIMD.max
[<BenchmarkCategory("MaxBy")>]
[<Benchmark(Baseline = true)>]
member self.MaxBy() =
arrayInt |> Array.maxBy (fun x -> x * x)
[<BenchmarkCategory("MaxBy")>]
[<Benchmark>]
member self.MaxBySIMD() =
arrayInt |> Array.SIMD.maxBy (fun x -> x * x) (fun x -> x * x)
[<BenchmarkCategory("Min")>]
[<Benchmark(Baseline = true)>]
member self.Min() = arrayInt |> Array.min
[<BenchmarkCategory("Min")>]
[<Benchmark>]
member self.MinSIMD() = arrayInt |> Array.SIMD.min
[<BenchmarkCategory("MinBy")>]
[<Benchmark(Baseline = true)>]
member self.MinBy() =
arrayInt |> Array.minBy (fun x -> x - 82)
[<BenchmarkCategory("MinBy")>]
[<Benchmark>]
member self.MinBySIMD() =
arrayInt |> Array.SIMD.minBy (fun x -> x - Vector<int>(82)) (fun x -> x - 82)
[<BenchmarkCategory("Map", "Int")>]
[<Benchmark(Baseline = true)>]
member self.Map() =
arrayInt |> Array.map (fun x -> x + 2 * x)
[<BenchmarkCategory("Map", "Int")>]
[<Benchmark>]
member self.MapSIMD() =
arrayInt |> Array.SIMD.map (fun x -> x + 2 * x) (fun x -> x + 2 * x)
[<BenchmarkCategory("Map", "Float")>]
[<Benchmark(Baseline = true)>]
member self.MapFloat() =
arrayFloat |> Array.map (fun x -> x + 2. * x)
[<BenchmarkCategory("Map", "Float")>]
[<Benchmark>]
member self.MapSIMDFloat() =
arrayFloat |> Array.SIMD.map (fun x -> x + 2. * x) (fun x -> x + 2. * x)
[<BenchmarkCategory("Fold")>]
[<Benchmark(Baseline = true)>]
member self.Fold() =
(0, arrayInt) ||> Array.fold (fun acc x -> x + acc)
[<BenchmarkCategory("Fold")>]
[<Benchmark>]
member self.FoldSIMD() =
let inline fn acc x = x + acc
(0, arrayInt) ||> Array.SIMD.fold fn fn (+)
[<BenchmarkCategory("Partition")>]
[<Benchmark(Baseline = true)>]
member self.Partition() =
arrayInt |> Array.partition (fun x -> x > self.Half)
[<BenchmarkCategory("Partition")>]
[<Benchmark>]
member self.PartitionPerformance() =
arrayInt |> Array.Performance.partitionUnordered (fun x -> x > self.Half)
[<BenchmarkCategory("Filter", "Int")>]
[<Benchmark(Baseline = true)>]
member self.Filter() =
arrayInt |> Array.filter (fun x -> x % 2 = 0)
[<BenchmarkCategory("Filter", "Int")>]
[<Benchmark>]
member self.FilterPerformance() =
arrayInt |> Array.Performance.filterSimplePredicate (fun x -> x % 2 = 0)
[<BenchmarkCategory("Filter", "Float")>]
[<Benchmark(Baseline = true)>]
member self.FilterFloat() =
arrayFloat |> Array.filter (fun x -> x % 2. = 0)
[<BenchmarkCategory("Filter", "Float")>]
[<Benchmark>]
member self.FilterPerformanceFloat() =
arrayFloat |> Array.Performance.filterSimplePredicate (fun x -> x % 2. = 0)
[<EntryPoint>]
let main _argv =
let _ = BenchmarkRunner.Run<CoreBenchmark>()
0