-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.fs
194 lines (148 loc) · 4.63 KB
/
Program.fs
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
// Learn more about F# at http://docs.microsoft.com/dotnet/fsharp
open System
open SliceMapPerformance.Domain
open SliceMapPerformance.SliceMap
open BenchmarkDotNet.Attributes
open BenchmarkDotNet.Running
[<Measure>] type City
[<Measure>] type Truck
let rng = Random 123
let numberOfCities = 1_000
let numberOfTrucks = 1_000
let cities = [| for c in 1 .. numberOfCities -> LanguagePrimitives.Int32WithMeasure<City> c |]
let trucks = [| for t in 1 .. numberOfTrucks -> LanguagePrimitives.Int32WithMeasure<Truck> t|]
module Dense =
let cityTruckPairs =
[|
for c in cities do
for t in trucks ->
c, t
|]
let costs =
SliceMap [|
for t in trucks ->
t, 100.0 + 10.0 * rng.NextDouble()
|]
let capacity =
SliceMap [|
for t in trucks ->
t, 1_000.0 + 10.0 * rng.NextDouble ()
|]
let decisions =
SliceMap2D [|
for (c, t) in cityTruckPairs ->
let decisionName = DecisionName ($"{c.ToString()}_{t.ToString()}")
c, t, { Name = decisionName; Type = DecisionType.Boolean }
|]
let loop () =
let mutable result = LanguagePrimitives.GenericZero
for _ = 1 to 10 do
for c in cities do
let total = sum (capacity .* decisions.[c, All] .* costs)
result <- total
result
module MediumSparsity =
let densityPercent = 0.10
let cityTruckPairs =
[|
for c in cities do
for t in trucks ->
if rng.NextDouble() < densityPercent then
Some (c, t)
else
None
|] |> Array.choose id
let costs =
SliceMap [|
for t in trucks ->
t, 100.0 + 10.0 * rng.NextDouble()
|]
let capacity =
SliceMap [|
for t in trucks ->
t, 1_000.0 + 10.0 * rng.NextDouble ()
|]
let decisions =
SliceMap2D [|
for (c, t) in cityTruckPairs ->
let decisionName = DecisionName ($"{c.ToString()}_{t.ToString()}")
c, t, { Name = decisionName; Type = DecisionType.Boolean }
|]
let loop () =
let mutable result = LanguagePrimitives.GenericZero
for _ = 1 to 10 do
for c in cities do
let total = sum (capacity .* decisions.[c, All] .* costs)
result <- total
result
module HighSparsity =
let densityPercent = 0.01
let cityTruckPairs =
[|
for c in cities do
for t in trucks ->
if rng.NextDouble() < densityPercent then
Some (c, t)
else
None
|] |> Array.choose id
let costs =
SliceMap [|
for t in trucks ->
t, 100.0 + 10.0 * rng.NextDouble()
|]
let capacity =
SliceMap [|
for t in trucks ->
t, 1_000.0 + 10.0 * rng.NextDouble ()
|]
let decisions =
SliceMap2D [|
for (c, t) in cityTruckPairs ->
let decisionName = DecisionName ($"{c.ToString()}_{t.ToString()}")
c, t, { Name = decisionName; Type = DecisionType.Boolean }
|]
let loop () =
let mutable result = LanguagePrimitives.GenericZero
for _ = 1 to 10 do
for c in cities do
let total = sum (capacity .* decisions.[c, All] .* costs)
result <- total
result
[<MemoryDiagnoser>]
type Benchmarks () =
[<Benchmark>]
member _.DenseData () =
Dense.loop ()
[<Benchmark>]
member _.MediumSparsity () =
MediumSparsity.loop ()
[<Benchmark>]
member _.HighSparsity () =
HighSparsity.loop ()
[<EntryPoint>]
let main argv =
match argv.[0].ToLower() with
| "benchmark" ->
let summary = BenchmarkRunner.Run<Benchmarks>()
()
| "profiledense" ->
let iterations = int argv.[1]
printfn "Starting loops"
for _ = 1 to iterations do
let _ = Dense.loop ()
()
| "profilemedium" ->
let iterations = int argv.[1]
printfn "Starting loops"
for _ = 1 to iterations do
let _ = MediumSparsity.loop ()
()
| "profilesparse" ->
let iterations = int argv.[1]
printfn "Starting loops"
for _ = 1 to iterations do
let _ = HighSparsity.loop ()
()
| _ -> failwith "Invalid workload"
0 // return an integer exit code