SparkPi is slower with more than 1 slice

A relatively new spark, and tried to run the SparkPi example on an autonomous 12-core three-component cluster. What I don't understand is that running this example with a single fragment gives better performance compared to using 12 slices. The same thing happened when I used the parallelization function. Time scales almost linearly with the addition of each slice. Please let me know if I do something wrong. The following is a snippet of code:

val spark = new SparkContext("spark://telecom:7077", "SparkPi",
  System.getenv("SPARK_HOME"), List("target/scala-2.10/sparkpii_2.10-1.0.jar"))
val slices = 1
val n = 10000000 * slices
val count = spark.parallelize(1 to n, slices).map {
  i =>
    val x = random * 2 - 1
    val y = random * 2 - 1
    if (x * x + y * y < 1) 1 else 0
}.reduce(_ + _)
println("Pi is roughly " + 4.0 * count / n)
spark.stop()

Update: the problem was with a random function, since it was a synchronized method, it could not be scaled to several cores.

+4
2

, sparkpi, . , Spark.

+1

, "scala.math.random". "org.apache.spark.util.random.XORShiftRandom", Pi . , SparkPi Spark:

// scalastyle:off println
package org.apache.spark.examples

import org.apache.spark.util.random.XORShiftRandom

import org.apache.spark._

/** Computes an approximation to pi */
object SparkPi {
  def main(args: Array[String]) {
    val conf = new SparkConf().setAppName("Spark Pi").setMaster(args(0))
    val spark = new SparkContext(conf)
    val slices = if (args.length > 1) args(1).toInt else 2
    val n = math.min(100000000L * slices, Int.MaxValue).toInt // avoid overflow
    val rand = new XORShiftRandom()

    val count = spark.parallelize(1 until n, slices).map { i =>
        val x = rand.nextDouble * 2 - 1
        val y = rand.nextDouble * 2 - 1
        if (x*x + y*y < 1) 1 else 0
      }.reduce(_ + _)

    println("Pi is roughly " + 4.0 * count / n)
    spark.stop()
  }
}
// scalastyle:on println

, ​​ local [1] 16 ', 60 . , 8 ("local [*] 16 '), 17 .

0

Source: https://habr.com/ru/post/1537875/


All Articles