spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From falaki <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-2082] stratified sampling in PairRDDFun...
Date Fri, 04 Jul 2014 00:43:21 GMT
Github user falaki commented on a diff in the pull request:

    https://github.com/apache/spark/pull/1025#discussion_r14544074
  
    --- Diff: core/src/main/scala/org/apache/spark/util/random/StratifiedSampler.scala ---
    @@ -0,0 +1,296 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.util.random
    +
    +import scala.collection.{Map, mutable}
    +import scala.collection.mutable.ArrayBuffer
    +import scala.reflect.ClassTag
    +
    +import org.apache.commons.math3.random.RandomDataGenerator
    +import org.apache.spark.{Logging, SparkContext, TaskContext}
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.util.Utils
    +import org.apache.spark.util.random.{PoissonBounds => PB}
    +
    +private[spark] object StratifiedSampler extends Logging {
    +
    +  /**
    +   * A version of {@link #aggregate()} that passes the TaskContext to the function that
does
    +   * aggregation for each partition. This function avoids creating an extra depth in
the RDD
    +   * lineage, as opposed to using mapPartitionsWithId, which results in slightly improved
run time.
    +   */
    +  def aggregateWithContext[U: ClassTag, T: ClassTag](zeroValue: U)
    +      (rdd: RDD[T],
    +       seqOp: ((TaskContext, U), T) => U,
    +       combOp: (U, U) => U): U = {
    +    val sc: SparkContext = rdd.sparkContext
    +    // Clone the zero value since we will also be serializing it as part of tasks
    +    var jobResult = Utils.clone(zeroValue, sc.env.closureSerializer.newInstance())
    +    // pad seqOp and combOp with taskContext to conform to aggregate's signature in TraversableOnce
    +    val paddedSeqOp = (arg1: (TaskContext, U), item: T) => (arg1._1, seqOp(arg1, item))
    +    val paddedcombOp = (arg1: (TaskContext, U), arg2: (TaskContext, U)) =>
    +      (arg1._1, combOp(arg1._2, arg1._2))
    +    val cleanSeqOp = sc.clean(paddedSeqOp)
    +    val cleanCombOp = sc.clean(paddedcombOp)
    +    val aggregatePartition = (tc: TaskContext, it: Iterator[T]) =>
    +      (it.aggregate(tc, zeroValue)(cleanSeqOp, cleanCombOp))._2
    +    val mergeResult = (index: Int, taskResult: U) => jobResult = combOp(jobResult,
taskResult)
    +    sc.runJob(rdd, aggregatePartition, mergeResult)
    +    jobResult
    +  }
    +
    +  /**
    +   * Returns the function used by aggregate to collect sampling statistics for each partition.
    +   */
    +  def getSeqOp[K, V](withReplacement: Boolean,
    +      fractionByKey: (K => Double),
    +      counts: Option[Map[K, Long]]): ((TaskContext, Result[K]),(K, V)) => Result[K]
= {
    --- End diff --
    
    Space after comma


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message