-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tera sort + tests, support custom kwargs in algorithm steps
- Loading branch information
1 parent
52ebaf2
commit 9147400
Showing
4 changed files
with
153 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
from typing import Iterable, Tuple, Any, List | ||
from bisect import bisect_left | ||
import random | ||
import math | ||
|
||
from spark_minimal_algorithms.algorithm import Step, Algorithm | ||
|
||
from pyspark import RDD, Broadcast | ||
|
||
|
||
class TeraSortFirstRound(Step): | ||
p = 0.1 | ||
""" Default value for probability of sampling a point to be a bucket key """ | ||
|
||
@staticmethod | ||
def extract_idx( | ||
partition_idx: int, partition_points: Iterable[Any] | ||
) -> Iterable[Tuple[int, Any]]: | ||
for point in partition_points: | ||
yield partition_idx, point | ||
|
||
@staticmethod | ||
def group(rdd: RDD, **kwargs: Any) -> RDD: | ||
rdd = rdd.mapPartitionsWithIndex(TeraSortFirstRound.extract_idx).groupByKey() | ||
return rdd | ||
|
||
@staticmethod | ||
def emit_by_group(group_key: int, group_items: Iterable[Any], **kwargs: Any) -> Any: | ||
samples = list() | ||
p: float = kwargs.get("p", TeraSortFirstRound.p) | ||
for point in group_items: | ||
if random.random() < p: | ||
samples.append(point) | ||
|
||
return samples | ||
|
||
@staticmethod | ||
def broadcast(emitted_items: List[List[Any]], **kwargs: Any) -> List[Any]: | ||
n_dim = kwargs["n_dim"] | ||
zero_point = tuple(0 for _ in range(n_dim)) | ||
buckets = [zero_point] + [ | ||
point for samples in emitted_items for point in samples | ||
] | ||
return sorted(buckets) | ||
|
||
@staticmethod | ||
def step( # type: ignore | ||
group_key: int, group_items: Iterable[Any], broadcast: Broadcast, **kwargs: Any | ||
) -> Iterable[Tuple[int, Any]]: | ||
for point in group_items: | ||
point_bucket = bisect_left(broadcast.value, point) | ||
yield point_bucket, point | ||
|
||
|
||
class TeraSortFinalRound(Step): | ||
@staticmethod | ||
def group(rdd: RDD) -> RDD: # type: ignore | ||
rdd = rdd.groupByKey().sortByKey() | ||
return rdd | ||
|
||
@staticmethod | ||
def step( # type: ignore | ||
group_key: int, group_items: Iterable[Any], broadcast: Broadcast | ||
) -> Iterable[Any]: | ||
sorted_points = sorted(group_items) | ||
for point in sorted_points: | ||
yield point | ||
|
||
|
||
class TeraSort(Algorithm): | ||
__steps__ = { | ||
"assign_buckets": TeraSortFirstRound, | ||
"sort": TeraSortFinalRound, | ||
} | ||
|
||
def run(self, rdd: RDD, n_dim: int) -> RDD: # type: ignore | ||
rdd = rdd.cache() | ||
|
||
n_points = rdd.count() | ||
m = n_points / self.n_partitions | ||
optimal_p = math.log(n_points * self.n_partitions) / m | ||
|
||
rdd = self.assign_buckets(rdd, p=optimal_p, n_dim=n_dim) # type: ignore | ||
rdd = self.sort(rdd) # type: ignore | ||
|
||
return rdd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from typing import List, Tuple, Any | ||
import random | ||
|
||
import pytest | ||
|
||
from spark_minimal_algorithms.examples.tera_sort import TeraSort | ||
|
||
random.seed(42) | ||
|
||
|
||
def create_test_case(n_points: int, n_dim: int) -> List[Tuple[Any]]: | ||
max_point = 100 * n_points | ||
points = [ | ||
tuple(random.randint(1, max_point) for _ in range(n_dim)) | ||
for _ in range(n_points) | ||
] | ||
return points, sorted(points) | ||
|
||
|
||
TESTS = [ | ||
create_test_case(5, 1), | ||
create_test_case(10, 1), | ||
create_test_case(100, 1), | ||
create_test_case(1_000, 1), | ||
create_test_case(5, 2), | ||
create_test_case(10, 2), | ||
create_test_case(100, 2), | ||
create_test_case(1_000, 2), | ||
create_test_case(5, 3), | ||
create_test_case(10, 3), | ||
create_test_case(100, 3), | ||
create_test_case(1_000, 3), | ||
] | ||
|
||
|
||
@pytest.mark.parametrize("test_case", TESTS) | ||
@pytest.mark.parametrize("n_partitions", [1, 2, 4]) | ||
def test_tera_sort(spark_context, n_partitions, test_case): | ||
points, sorted_points = test_case | ||
n_dim = len(points[0]) | ||
rdd = spark_context.parallelize(points) | ||
|
||
tera_sort = TeraSort(spark_context, n_partitions) | ||
result = tera_sort(rdd=rdd, n_dim=n_dim).collect() | ||
|
||
assert len(result) == len(sorted_points) | ||
assert result == sorted_points |