summaryrefslogtreecommitdiff
path: root/test/benchmarks/src/scala/collection/parallel/benchmarks/generic/ParallelBenches.scala
blob: c39c3849277ccfaff2c6d80e6cb96594fa61b901 (plain) (blame)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package scala.collection.parallel
package benchmarks
package generic



import scala.collection.SeqView



trait ParIterableBenches[T, Coll <: ParIterable[T]] {
self =>
  
  def createSequential(sz: Int, p: Int): Iterable[T]
  def createParallel(sz: Int, p: Int): Coll
  def nameOfCollection: String
  def operators: Operators[T]
    
  trait IterableBenchCompanion extends BenchCompanion {
    def collectionName = self.nameOfCollection
  }
  
  trait IterableBench extends collection.parallel.benchmarks.Bench {
    protected var seqcoll: Iterable[T] = null
    protected var parcoll: Coll = null.asInstanceOf[Coll]
    
    reset
    
    def reset = runWhat match {
      case "seq" => this.seqcoll = createSequential(size, parallelism)
      case "par" => this.parcoll = createParallel(size, parallelism)
      case _ =>
    }
    
    def nameOfCollection = self.nameOfCollection
    def operators = self.operators
    def createSequential(sz: Int, p: Int) = self.createSequential(size, parallelism)
    def createParallel(sz: Int, p: Int) = self.createParallel(size, parallelism)
    def forkJoinPool: scala.concurrent.forkjoin.ForkJoinPool = self.forkJoinPool
    
    override def printResults {
      println(" --- Fork join pool state --- ")
      println("Parallelism: " + forkJoinPool.getParallelism)
      println("Active threads: " + forkJoinPool.getActiveThreadCount)
      println("Work stealings: "  + forkJoinPool.getStealCount)
    }
    
  }
  
  def forkJoinPool: scala.concurrent.forkjoin.ForkJoinPool
    
}


trait ParSeqBenches[T, Coll <: ParSeq[T]] extends ParIterableBenches[T, Coll] {
self =>
  
  def createSequential(sz: Int, p: Int): Seq[T]
  
  trait SeqBenchCompanion extends BenchCompanion {
    def collectionName = self.nameOfCollection
  }
  
  trait SeqBench extends IterableBench {
    def seqcollAsSeq = seqcoll.asInstanceOf[Seq[T]]
    override def createSequential(sz: Int, p: Int) = self.createSequential(sz, p)
  }
  
}




/** Standard benchmarks for collections.
 */ 
trait StandardParIterableBenches[T, Coll <: ParIterable[T]] extends ParIterableBenches[T, Coll] {
  
  object Reduce extends IterableBenchCompanion {
    override def defaultSize = 50000
    def benchName = "reduce";
    def apply(sz: Int, p: Int, w: String) = new Reduce(sz, p, w)
  }
  
  class Reduce(val size: Int, val parallelism: Int, val runWhat: String)
  extends IterableBench {
    def comparisonMap = collection.Map()
    def runseq = this.seqcoll.reduceLeft(operators.reducer)
    def runpar = this.parcoll.reduce(operators.reducer)
    def companion = Reduce
  }
  
  object ReduceMedium extends IterableBenchCompanion {
    override def defaultSize = 5000
    def benchName = "reduce-medium";
    def apply(sz: Int, p: Int, w: String) = new ReduceMedium(sz, p, w)
  }
  
  class ReduceMedium(val size: Int, val parallelism: Int, val runWhat: String)
  extends IterableBench {
    def comparisonMap = collection.Map()
    def runseq = this.seqcoll.reduceLeft(operators.mediumreducer)
    def runpar = this.parcoll.reduce(operators.mediumreducer)
    def companion = ReduceMedium
  }
  
  object Map extends IterableBenchCompanion {
    override def defaultSize = 5000
    def benchName = "map";
    def apply(sz: Int, p: Int, w: String) = new Map(sz, p, w)
  }
  
  class Map(val size: Int, val parallelism: Int, val runWhat: String)
  extends IterableBench {
    def comparisonMap = collection.Map()
    def runseq = this.seqcoll.map(operators.mapper)
    def runpar = this.parcoll.map(operators.mapper)
    def companion = Map
  }
  
  object Filter extends IterableBenchCompanion {
    override def defaultSize = 5000
    def benchName = "filter";
    def apply(sz: Int, p: Int, w: String) = new Filter(sz, p, w)
  }
  
  class Filter(val size: Int, val parallelism: Int, val runWhat: String)
  extends IterableBench {
    def comparisonMap = collection.Map()
    def runseq = this.seqcoll.filter(operators.filterer)
    def runpar = this.parcoll.filter(operators.filterer)
    def companion = Filter
  }
  
  object FlatMap extends IterableBenchCompanion {
    override def defaultSize = 5000
    def benchName = "flatmap";
    def apply(sz: Int, p: Int, w: String) = new FlatMap(sz, p, w)
  }
  
  class FlatMap(val size: Int, val parallelism: Int, val runWhat: String)
  extends IterableBench {
    def comparisonMap = collection.Map()
    def runseq = this.seqcoll.flatMap(operators.flatmapper)
    def runpar = this.parcoll.flatMap(operators.flatmapper)
    def companion = FlatMap
  }  
  
}



/** Benchmarks for sequence views.
 */
trait ParSeqViewBenches[T, Coll <: ParSeqView[T, ParSeq[T], CollSeq], CollSeq] extends ParSeqBenches[T, Coll] {
self =>
  
  trait SeqViewBench extends SeqBench {
    lazy val seqview: SeqView[T, Seq[T]] = createSeqView(size, parallelism)
    
    def createSeqView(sz: Int, p: Int) = self.createSeqView(sz, p)
  }
  
  def createSeqView(sz: Int, p: Int): SeqView[T, Seq[T]]
    
  object Iteration extends SeqBenchCompanion {
    override def defaultSize = 250000
    def benchName = "iter"
    def apply(sz: Int, p: Int, w: String) = new Iteration(sz, p, w)
  }
  
  class Iteration(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    def comparisonMap = collection.Map("seqview" -> runseqview _)
    def runseq = this.seqcoll.foreach(operators.eachFun)
    def runpar = this.parcoll.pforeach(operators.eachFun)
    def runseqview = {
      this.seqview.foreach(operators.eachFun)
    }
    def companion = Iteration
  }
  
  object IterationS extends SeqBenchCompanion {
    override def defaultSize = 250000
    def benchName = "iter-s"
    def apply(sz: Int, p: Int, w: String) = new IterationS(sz, p, w)
  }
  
  class IterationS(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    def comparisonMap = collection.Map("seqview" -> runseqview _)
    def runseq = this.seqcoll.slice(0, size / 2).foreach(operators.eachFun)
    def runpar = this.parcoll.slice(0, size / 2).pforeach(operators.eachFun)
    def runseqview = this.seqview.slice(0, size / 2).foreach(operators.eachFun)
    def companion = IterationS
  }

  object IterationM extends SeqBenchCompanion {
    override def defaultSize = 100000
    def benchName = "iter-m"
    def apply(sz: Int, p: Int, w: String) = new IterationM(sz, p, w)
  }
  
  class IterationM(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    def comparisonMap = collection.Map("seqview" -> runseqview _)
    def runseq = this.seqcoll.map(operators.mapper).foreach(operators.eachFun)
    def runpar = this.parcoll.map(operators.mapper).pforeach(operators.eachFun)
    def runseqview = this.seqview.map(operators.mapper).foreach(operators.eachFun)
    def companion = IterationM
  }
  
  object IterationA extends SeqBenchCompanion {
    override def defaultSize = 50000
    def benchName = "iter-a"
    def apply(sz: Int, p: Int, w: String) = new IterationA(sz, p, w)
  }
  
  class IterationA(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    val appended = operators.sequence(size)
    val sqappended = appended.toSeq
    def comparisonMap = collection.Map("seqview" -> runseqview _)
    def runseq = {
      val withapp = this.seqcoll.++(sqappended)
      withapp.foreach(operators.eachFun)
    }
    def runpar = this.parcoll.++(appended).pforeach(operators.eachFun)
    def runseqview = this.seqview.++(appended).foreach(operators.eachFun)
    def companion = IterationA
  }
  
  object IterationZ extends SeqBenchCompanion {
    override def defaultSize = 50000
    def benchName = "iter-z"
    def apply(sz: Int, p: Int, w: String) = new IterationZ(sz, p, w)
  }
  
  class IterationZ(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    val zipped = operators.sequence(size)
    def comparisonMap = collection.Map("seqview" -> runseqview _)
    def runseq = {
      val withzip = this.seqcoll.zip(zipped)
      withzip.foreach(operators.eachPairFun)
    }
    def runpar = this.parcoll.zip(zipped).pforeach(operators.eachPairFun)
    def runseqview = this.seqview.zip(zipped).foreach(operators.eachPairFun)
    def companion = IterationZ
  }
  
  object IterationP extends SeqBenchCompanion {
    override def defaultSize = 50000
    def benchName = "iter-p"
    def apply(sz: Int, p: Int, w: String) = new IterationP(sz, p, w)
  }
  
  class IterationP(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    val patch = operators.sequence(size / 4)
    val sqpatch = patch.toSeq
    def comparisonMap = collection.Map("seqview" -> runseqview _)
    def runseq = {
      val withpatch = this.seqcollAsSeq.patch(size / 4, sqpatch, size / 2)
      withpatch.foreach(operators.eachFun)
    }
    def runpar = this.parcoll.patch(size / 4, patch, size / 2).pforeach(operators.eachFun)
    def runseqview = this.seqview.patch(size / 4, patch, size / 2).foreach(operators.eachFun)
    def companion = IterationP
  }
    
  object Reduce extends SeqBenchCompanion {
    override def defaultSize = 50000
    def benchName = "reduce";
    def apply(sz: Int, p: Int, w: String) = new Reduce(sz, p, w)
  }
  
  class Reduce(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    def comparisonMap = collection.Map()
    def runseq = this.seqcoll.reduceLeft(operators.reducer)
    def runpar = this.parcoll.reduce(operators.reducer)
    def companion = Reduce
  }
  
  object MediumReduce extends SeqBenchCompanion {
    override def defaultSize = 50000
    def benchName = "reduce-medium";
    def apply(sz: Int, p: Int, w: String) = new MediumReduce(sz, p, w)
  }
  
  class MediumReduce(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    def comparisonMap = collection.Map()
    def runseq = this.seqcoll.reduceLeft(operators.mediumreducer)
    def runpar = this.parcoll.reduce(operators.mediumreducer)
    def companion = Reduce
  }
  
  object ModifyThenReduce extends SeqBenchCompanion {
    override def defaultSize = 20000
    def benchName = "modify-then-reduce";
    def apply(sz: Int, p: Int, w: String) = new ModifyThenReduce(sz, p, w)
  }
  
  class ModifyThenReduce(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    val toadd = createSequential(size, parallelism)
    def comparisonMap = collection.Map()
    def runseq = {
      val modified = (seqcoll ++ toadd).drop(size).map(operators.mapper).++(toadd).take(size)
      modified.reduceLeft(operators.reducer)
    }
    def runpar = {
      val modified = (parcoll ++ toadd).drop(size).map(operators.mapper).++(toadd).take(size)
      modified.reduce(operators.reducer)
    }
    def companion = ModifyThenReduce
  }
  
  object ModifyThenForce extends SeqBenchCompanion {
    override def defaultSize = 20000
    def benchName = "modify-then-force";
    def apply(sz: Int, p: Int, w: String) = new ModifyThenForce(sz, p, w)
  }
  
  class ModifyThenForce(val size: Int, val parallelism: Int, val runWhat: String)
  extends SeqBench with SeqViewBench {
    val toadd = createSequential(size, parallelism)
    def comparisonMap = collection.Map()
    def runseq = (seqcoll ++ toadd).drop(size).map(operators.mapper).++(toadd).take(size)
    def runpar = {
      val r: ParSeqView[T, ParSeq[T], Seq[T]] = (parcoll ++ toadd).drop(size).map(operators.mapper).++(toadd).take(size)
      r.force
    }
    def companion = ModifyThenForce
  }
  
}