ruby-spark 1.1.0.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +37 -0
- data/Gemfile +47 -0
- data/Guardfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +252 -0
- data/Rakefile +35 -0
- data/TODO.md +6 -0
- data/benchmark/aggregate.rb +33 -0
- data/benchmark/bisect.rb +88 -0
- data/benchmark/comparison/prepare.sh +18 -0
- data/benchmark/comparison/python.py +156 -0
- data/benchmark/comparison/r.r +69 -0
- data/benchmark/comparison/ruby.rb +167 -0
- data/benchmark/comparison/run-all.sh +160 -0
- data/benchmark/comparison/scala.scala +181 -0
- data/benchmark/custom_marshal.rb +94 -0
- data/benchmark/digest.rb +150 -0
- data/benchmark/enumerator.rb +88 -0
- data/benchmark/serializer.rb +82 -0
- data/benchmark/sort.rb +43 -0
- data/benchmark/sort2.rb +164 -0
- data/benchmark/take.rb +28 -0
- data/bin/ruby-spark +8 -0
- data/example/pi.rb +28 -0
- data/example/website_search.rb +83 -0
- data/ext/ruby_c/extconf.rb +3 -0
- data/ext/ruby_c/murmur.c +158 -0
- data/ext/ruby_c/murmur.h +9 -0
- data/ext/ruby_c/ruby-spark.c +18 -0
- data/ext/ruby_java/Digest.java +36 -0
- data/ext/ruby_java/Murmur2.java +98 -0
- data/ext/ruby_java/RubySparkExtService.java +28 -0
- data/ext/ruby_java/extconf.rb +3 -0
- data/ext/spark/build.sbt +73 -0
- data/ext/spark/project/plugins.sbt +9 -0
- data/ext/spark/sbt/sbt +34 -0
- data/ext/spark/src/main/scala/Exec.scala +91 -0
- data/ext/spark/src/main/scala/MLLibAPI.scala +4 -0
- data/ext/spark/src/main/scala/Marshal.scala +52 -0
- data/ext/spark/src/main/scala/MarshalDump.scala +113 -0
- data/ext/spark/src/main/scala/MarshalLoad.scala +220 -0
- data/ext/spark/src/main/scala/RubyAccumulatorParam.scala +69 -0
- data/ext/spark/src/main/scala/RubyBroadcast.scala +13 -0
- data/ext/spark/src/main/scala/RubyConstant.scala +13 -0
- data/ext/spark/src/main/scala/RubyMLLibAPI.scala +55 -0
- data/ext/spark/src/main/scala/RubyMLLibUtilAPI.scala +21 -0
- data/ext/spark/src/main/scala/RubyPage.scala +34 -0
- data/ext/spark/src/main/scala/RubyRDD.scala +392 -0
- data/ext/spark/src/main/scala/RubySerializer.scala +14 -0
- data/ext/spark/src/main/scala/RubyTab.scala +11 -0
- data/ext/spark/src/main/scala/RubyUtils.scala +15 -0
- data/ext/spark/src/main/scala/RubyWorker.scala +257 -0
- data/ext/spark/src/test/scala/MarshalSpec.scala +84 -0
- data/lib/ruby-spark.rb +1 -0
- data/lib/spark.rb +198 -0
- data/lib/spark/accumulator.rb +260 -0
- data/lib/spark/broadcast.rb +98 -0
- data/lib/spark/build.rb +43 -0
- data/lib/spark/cli.rb +169 -0
- data/lib/spark/command.rb +86 -0
- data/lib/spark/command/base.rb +158 -0
- data/lib/spark/command/basic.rb +345 -0
- data/lib/spark/command/pair.rb +124 -0
- data/lib/spark/command/sort.rb +51 -0
- data/lib/spark/command/statistic.rb +144 -0
- data/lib/spark/command_builder.rb +141 -0
- data/lib/spark/command_validator.rb +34 -0
- data/lib/spark/config.rb +238 -0
- data/lib/spark/constant.rb +14 -0
- data/lib/spark/context.rb +322 -0
- data/lib/spark/error.rb +50 -0
- data/lib/spark/ext/hash.rb +41 -0
- data/lib/spark/ext/integer.rb +25 -0
- data/lib/spark/ext/io.rb +67 -0
- data/lib/spark/ext/ip_socket.rb +29 -0
- data/lib/spark/ext/module.rb +58 -0
- data/lib/spark/ext/object.rb +24 -0
- data/lib/spark/ext/string.rb +24 -0
- data/lib/spark/helper.rb +10 -0
- data/lib/spark/helper/logger.rb +40 -0
- data/lib/spark/helper/parser.rb +85 -0
- data/lib/spark/helper/serialize.rb +71 -0
- data/lib/spark/helper/statistic.rb +93 -0
- data/lib/spark/helper/system.rb +42 -0
- data/lib/spark/java_bridge.rb +19 -0
- data/lib/spark/java_bridge/base.rb +203 -0
- data/lib/spark/java_bridge/jruby.rb +23 -0
- data/lib/spark/java_bridge/rjb.rb +41 -0
- data/lib/spark/logger.rb +76 -0
- data/lib/spark/mllib.rb +100 -0
- data/lib/spark/mllib/classification/common.rb +31 -0
- data/lib/spark/mllib/classification/logistic_regression.rb +223 -0
- data/lib/spark/mllib/classification/naive_bayes.rb +97 -0
- data/lib/spark/mllib/classification/svm.rb +135 -0
- data/lib/spark/mllib/clustering/gaussian_mixture.rb +82 -0
- data/lib/spark/mllib/clustering/kmeans.rb +118 -0
- data/lib/spark/mllib/matrix.rb +120 -0
- data/lib/spark/mllib/regression/common.rb +73 -0
- data/lib/spark/mllib/regression/labeled_point.rb +41 -0
- data/lib/spark/mllib/regression/lasso.rb +100 -0
- data/lib/spark/mllib/regression/linear.rb +124 -0
- data/lib/spark/mllib/regression/ridge.rb +97 -0
- data/lib/spark/mllib/ruby_matrix/matrix_adapter.rb +53 -0
- data/lib/spark/mllib/ruby_matrix/vector_adapter.rb +57 -0
- data/lib/spark/mllib/stat/distribution.rb +12 -0
- data/lib/spark/mllib/vector.rb +185 -0
- data/lib/spark/rdd.rb +1377 -0
- data/lib/spark/sampler.rb +92 -0
- data/lib/spark/serializer.rb +79 -0
- data/lib/spark/serializer/auto_batched.rb +59 -0
- data/lib/spark/serializer/base.rb +63 -0
- data/lib/spark/serializer/batched.rb +84 -0
- data/lib/spark/serializer/cartesian.rb +13 -0
- data/lib/spark/serializer/compressed.rb +27 -0
- data/lib/spark/serializer/marshal.rb +17 -0
- data/lib/spark/serializer/message_pack.rb +23 -0
- data/lib/spark/serializer/oj.rb +23 -0
- data/lib/spark/serializer/pair.rb +41 -0
- data/lib/spark/serializer/text.rb +25 -0
- data/lib/spark/sort.rb +189 -0
- data/lib/spark/stat_counter.rb +125 -0
- data/lib/spark/storage_level.rb +39 -0
- data/lib/spark/version.rb +3 -0
- data/lib/spark/worker/master.rb +144 -0
- data/lib/spark/worker/spark_files.rb +15 -0
- data/lib/spark/worker/worker.rb +200 -0
- data/ruby-spark.gemspec +47 -0
- data/spec/generator.rb +37 -0
- data/spec/inputs/lorem_300.txt +316 -0
- data/spec/inputs/numbers/1.txt +50 -0
- data/spec/inputs/numbers/10.txt +50 -0
- data/spec/inputs/numbers/11.txt +50 -0
- data/spec/inputs/numbers/12.txt +50 -0
- data/spec/inputs/numbers/13.txt +50 -0
- data/spec/inputs/numbers/14.txt +50 -0
- data/spec/inputs/numbers/15.txt +50 -0
- data/spec/inputs/numbers/16.txt +50 -0
- data/spec/inputs/numbers/17.txt +50 -0
- data/spec/inputs/numbers/18.txt +50 -0
- data/spec/inputs/numbers/19.txt +50 -0
- data/spec/inputs/numbers/2.txt +50 -0
- data/spec/inputs/numbers/20.txt +50 -0
- data/spec/inputs/numbers/3.txt +50 -0
- data/spec/inputs/numbers/4.txt +50 -0
- data/spec/inputs/numbers/5.txt +50 -0
- data/spec/inputs/numbers/6.txt +50 -0
- data/spec/inputs/numbers/7.txt +50 -0
- data/spec/inputs/numbers/8.txt +50 -0
- data/spec/inputs/numbers/9.txt +50 -0
- data/spec/inputs/numbers_0_100.txt +101 -0
- data/spec/inputs/numbers_1_100.txt +100 -0
- data/spec/lib/collect_spec.rb +42 -0
- data/spec/lib/command_spec.rb +68 -0
- data/spec/lib/config_spec.rb +64 -0
- data/spec/lib/context_spec.rb +165 -0
- data/spec/lib/ext_spec.rb +72 -0
- data/spec/lib/external_apps_spec.rb +45 -0
- data/spec/lib/filter_spec.rb +80 -0
- data/spec/lib/flat_map_spec.rb +100 -0
- data/spec/lib/group_spec.rb +109 -0
- data/spec/lib/helper_spec.rb +19 -0
- data/spec/lib/key_spec.rb +41 -0
- data/spec/lib/manipulation_spec.rb +122 -0
- data/spec/lib/map_partitions_spec.rb +87 -0
- data/spec/lib/map_spec.rb +91 -0
- data/spec/lib/mllib/classification_spec.rb +54 -0
- data/spec/lib/mllib/clustering_spec.rb +35 -0
- data/spec/lib/mllib/matrix_spec.rb +32 -0
- data/spec/lib/mllib/regression_spec.rb +116 -0
- data/spec/lib/mllib/vector_spec.rb +77 -0
- data/spec/lib/reduce_by_key_spec.rb +118 -0
- data/spec/lib/reduce_spec.rb +131 -0
- data/spec/lib/sample_spec.rb +46 -0
- data/spec/lib/serializer_spec.rb +88 -0
- data/spec/lib/sort_spec.rb +58 -0
- data/spec/lib/statistic_spec.rb +170 -0
- data/spec/lib/whole_text_files_spec.rb +33 -0
- data/spec/spec_helper.rb +38 -0
- metadata +389 -0
data/benchmark/bisect.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require "benchmark"
|
2
|
+
|
3
|
+
def bisect_left1(a, x, opts={})
|
4
|
+
return nil if a.nil?
|
5
|
+
return 0 if a.empty?
|
6
|
+
|
7
|
+
lo = (opts[:lo] || opts[:low]).to_i
|
8
|
+
hi = opts[:hi] || opts[:high] || a.length
|
9
|
+
|
10
|
+
while lo < hi
|
11
|
+
mid = (lo + hi) / 2
|
12
|
+
v = a[mid]
|
13
|
+
if v < x
|
14
|
+
lo = mid + 1
|
15
|
+
else
|
16
|
+
hi = mid
|
17
|
+
end
|
18
|
+
end
|
19
|
+
return lo
|
20
|
+
end
|
21
|
+
|
22
|
+
def bisect_left2(list, item)
|
23
|
+
count = 0
|
24
|
+
list.each{|i|
|
25
|
+
return count if i >= item
|
26
|
+
count += 1
|
27
|
+
}
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def bisect_left3(list, item, lo = 0, hi = list.size)
|
32
|
+
while lo < hi
|
33
|
+
i = (lo + hi - 1) >> 1
|
34
|
+
|
35
|
+
if 0 <= (list[i] <=> item)
|
36
|
+
hi = i
|
37
|
+
else
|
38
|
+
lo = i + 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
return hi
|
42
|
+
end
|
43
|
+
|
44
|
+
array = Array.new(1000000) { rand(0..1000000) };
|
45
|
+
to_find = Array.new(500) { rand(0..10000) };
|
46
|
+
|
47
|
+
Benchmark.bm(20) do |x|
|
48
|
+
x.report("bisect_left1") do
|
49
|
+
to_find.each do |item|
|
50
|
+
bisect_left1(array, item)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
x.report("bisect_left2") do
|
55
|
+
to_find.each do |item|
|
56
|
+
bisect_left2(array, item)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
x.report("bisect_left3") do
|
61
|
+
to_find.each do |item|
|
62
|
+
bisect_left3(array, item)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
array = Array.new(100000) { Array.new(rand(1..10)){(97+rand(26)).chr}.join };
|
68
|
+
to_find = Array.new(500) { (97+rand(26)).chr };
|
69
|
+
|
70
|
+
Benchmark.bm(20) do |x|
|
71
|
+
x.report("bisect_left1") do
|
72
|
+
to_find.each do |item|
|
73
|
+
bisect_left1(array, item)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
x.report("bisect_left2") do
|
78
|
+
to_find.each do |item|
|
79
|
+
bisect_left2(array, item)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
x.report("bisect_left3") do
|
84
|
+
to_find.each do |item|
|
85
|
+
bisect_left3(array, item)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
# Current dir
|
4
|
+
cd "$(dirname "$0")"
|
5
|
+
|
6
|
+
# Exit immediately if a pipeline returns a non-zero status.
|
7
|
+
set -e
|
8
|
+
|
9
|
+
# Spark
|
10
|
+
wget "http://d3kbcqa49mib13.cloudfront.net/spark-1.3.0-bin-hadoop2.4.tgz" -O spark.tgz
|
11
|
+
tar xvzf spark.tgz
|
12
|
+
mv spark-1.3.0-bin-hadoop2.4 spark
|
13
|
+
rm spark.tgz
|
14
|
+
|
15
|
+
# RSpark (only for 1.3.0)
|
16
|
+
git clone git@github.com:amplab-extras/SparkR-pkg.git rspark
|
17
|
+
cd rspark
|
18
|
+
SPARK_VERSION=1.3.0 ./install-dev.sh
|
@@ -0,0 +1,156 @@
|
|
1
|
+
import os
|
2
|
+
import math
|
3
|
+
from time import time
|
4
|
+
from random import random
|
5
|
+
from operator import add
|
6
|
+
from pyspark import SparkContext
|
7
|
+
|
8
|
+
sc = SparkContext(appName="Python", master="local[*]")
|
9
|
+
|
10
|
+
log_file = open(os.environ.get('PYTHON_LOG'), 'w')
|
11
|
+
|
12
|
+
def log(*values):
|
13
|
+
values = map(lambda x: str(x), values)
|
14
|
+
log_file.write(';'.join(values))
|
15
|
+
log_file.write('\n')
|
16
|
+
|
17
|
+
workers = int(os.environ.get('WORKERS'))
|
18
|
+
numbers_count = int(os.environ.get('NUMBERS_COUNT'))
|
19
|
+
text_file = os.environ.get('TEXT_FILE')
|
20
|
+
|
21
|
+
numbers = range(numbers_count)
|
22
|
+
floats = [float(i) for i in numbers]
|
23
|
+
with open(text_file) as t:
|
24
|
+
strings = t.read().split("\n")
|
25
|
+
|
26
|
+
|
27
|
+
# =============================================================================
|
28
|
+
# Serialization
|
29
|
+
# =============================================================================
|
30
|
+
|
31
|
+
t = time()
|
32
|
+
rdd_numbers = sc.parallelize(numbers, workers)
|
33
|
+
t = time() - t
|
34
|
+
log('NumbersSerialization', t)
|
35
|
+
|
36
|
+
|
37
|
+
t = time()
|
38
|
+
rdd_floats = sc.parallelize(floats, workers)
|
39
|
+
t = time() - t
|
40
|
+
log('FloatsSerialization', t)
|
41
|
+
|
42
|
+
|
43
|
+
t = time()
|
44
|
+
rdd_strings = sc.parallelize(strings, workers)
|
45
|
+
t = time() - t
|
46
|
+
log('StringsSerialization', t)
|
47
|
+
|
48
|
+
|
49
|
+
# =============================================================================
|
50
|
+
# Computing
|
51
|
+
# =============================================================================
|
52
|
+
|
53
|
+
|
54
|
+
# --- Is prime? ---------------------------------------------------------------
|
55
|
+
|
56
|
+
def is_prime(x):
|
57
|
+
if x < 2:
|
58
|
+
return [x, False]
|
59
|
+
elif x == 2:
|
60
|
+
return [x, True]
|
61
|
+
elif x % 2 == 0:
|
62
|
+
return [x, False]
|
63
|
+
else:
|
64
|
+
upper = int(math.sqrt(float(x)))
|
65
|
+
result = True
|
66
|
+
|
67
|
+
i = 3
|
68
|
+
while i <= upper:
|
69
|
+
if x % i == 0:
|
70
|
+
result = False
|
71
|
+
break
|
72
|
+
|
73
|
+
i += 2
|
74
|
+
|
75
|
+
return [x, result]
|
76
|
+
|
77
|
+
t = time()
|
78
|
+
rdd_numbers.map(is_prime).collect()
|
79
|
+
t = time() - t
|
80
|
+
|
81
|
+
log('IsPrime', t)
|
82
|
+
|
83
|
+
|
84
|
+
# --- Matrix multiplication ---------------------------------------------------
|
85
|
+
|
86
|
+
matrix_size = int(os.environ.get('MATRIX_SIZE'))
|
87
|
+
|
88
|
+
matrix = []
|
89
|
+
for row in range(matrix_size):
|
90
|
+
matrix.append([])
|
91
|
+
for col in range(matrix_size):
|
92
|
+
matrix[row].append(row+col)
|
93
|
+
|
94
|
+
def multiplication_func(matrix):
|
95
|
+
matrix = list(matrix)
|
96
|
+
size = len(matrix)
|
97
|
+
|
98
|
+
new_matrix = []
|
99
|
+
for row in range(size):
|
100
|
+
new_matrix.append([])
|
101
|
+
for col in range(size):
|
102
|
+
|
103
|
+
result = 0
|
104
|
+
for i in range(size):
|
105
|
+
result += matrix[row][i] * matrix[col][i]
|
106
|
+
new_matrix[row].append(result)
|
107
|
+
|
108
|
+
return new_matrix
|
109
|
+
|
110
|
+
t = time()
|
111
|
+
rdd = sc.parallelize(matrix, 1)
|
112
|
+
rdd.mapPartitions(multiplication_func).collect()
|
113
|
+
t = time() - t
|
114
|
+
|
115
|
+
log('MatrixMultiplication', t)
|
116
|
+
|
117
|
+
|
118
|
+
# --- Pi digits ---------------------------------------------------------------
|
119
|
+
# http://rosettacode.org/wiki/Pi#Python
|
120
|
+
|
121
|
+
pi_digit = int(os.environ.get('PI_DIGIT'))
|
122
|
+
|
123
|
+
def pi_func(size):
|
124
|
+
size = size.next()
|
125
|
+
result = ''
|
126
|
+
|
127
|
+
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
|
128
|
+
while size > 0:
|
129
|
+
if 4*q+r-t < n*t:
|
130
|
+
result += str(n)
|
131
|
+
size -= 1
|
132
|
+
nr = 10*(r-n*t)
|
133
|
+
n = ((10*(3*q+r))//t)-10*n
|
134
|
+
q *= 10
|
135
|
+
r = nr
|
136
|
+
else:
|
137
|
+
nr = (2*q+r)*l
|
138
|
+
nn = (q*(7*k)+2+(r*l))//(t*l)
|
139
|
+
q *= k
|
140
|
+
t *= l
|
141
|
+
l += 2
|
142
|
+
k += 1
|
143
|
+
n = nn
|
144
|
+
r = nr
|
145
|
+
|
146
|
+
return [result]
|
147
|
+
|
148
|
+
t = time()
|
149
|
+
rdd = sc.parallelize([pi_digit], 1)
|
150
|
+
rdd.mapPartitions(pi_func).collect()
|
151
|
+
t = time() - t
|
152
|
+
|
153
|
+
log('PiDigit', t)
|
154
|
+
|
155
|
+
|
156
|
+
log_file.close()
|
@@ -0,0 +1,69 @@
|
|
1
|
+
library(SparkR)
|
2
|
+
sc <- sparkR.init(master="local[*]")
|
3
|
+
|
4
|
+
logFile <- file(Sys.getenv("R_LOG"), "w")
|
5
|
+
|
6
|
+
logInfo <- function(...){
|
7
|
+
args <- list(...)
|
8
|
+
line <- paste(args, collapse = ";")
|
9
|
+
writeLines(line, logFile)
|
10
|
+
}
|
11
|
+
|
12
|
+
workers <- as.integer(Sys.getenv('WORKERS'))
|
13
|
+
numbersCount <- as.integer(Sys.getenv('NUMBERS_COUNT'))
|
14
|
+
textFile <- Sys.getenv('TEXT_FILE')
|
15
|
+
|
16
|
+
|
17
|
+
# =============================================================================
|
18
|
+
# Serialization
|
19
|
+
# =============================================================================
|
20
|
+
|
21
|
+
time <- proc.time()
|
22
|
+
rddNumbers <- parallelize(sc, as.numeric(seq(0, numbersCount)), workers)
|
23
|
+
time <- as.double(proc.time()-time)[3]
|
24
|
+
|
25
|
+
logInfo('NumbersSerialization', time)
|
26
|
+
|
27
|
+
|
28
|
+
# =============================================================================
|
29
|
+
# Computing
|
30
|
+
# =============================================================================
|
31
|
+
|
32
|
+
isPrime = function(x) {
|
33
|
+
if(x < 2){
|
34
|
+
c(x, FALSE)
|
35
|
+
}
|
36
|
+
else if(x == 2){
|
37
|
+
c(x, TRUE)
|
38
|
+
}
|
39
|
+
else if(x %% 2 == 0){
|
40
|
+
c(x, FALSE)
|
41
|
+
}
|
42
|
+
else{
|
43
|
+
upper <- as.numeric(sqrt(as.double(x)))
|
44
|
+
result <- TRUE
|
45
|
+
|
46
|
+
i <- 3
|
47
|
+
while(i <= upper){
|
48
|
+
if(x %% i == 0){
|
49
|
+
result = FALSE
|
50
|
+
break
|
51
|
+
}
|
52
|
+
|
53
|
+
i <- i+2
|
54
|
+
}
|
55
|
+
|
56
|
+
c(x, result)
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
60
|
+
time <- proc.time()
|
61
|
+
rdd <- map(rddNumbers, isPrime)
|
62
|
+
capture.output(collect(rdd), file='/dev/null')
|
63
|
+
time <- as.double(proc.time()-time)[3]
|
64
|
+
|
65
|
+
logInfo('IsPrime', time)
|
66
|
+
|
67
|
+
|
68
|
+
close(logFile)
|
69
|
+
sparkR.stop()
|
@@ -0,0 +1,167 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
lib = File.expand_path(File.dirname(__FILE__) + '/../../lib')
|
4
|
+
$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require 'ruby-spark'
|
7
|
+
require 'benchmark'
|
8
|
+
|
9
|
+
Spark.start
|
10
|
+
sc = Spark.context
|
11
|
+
|
12
|
+
$log_file = File.open(ENV['RUBY_LOG'], 'w')
|
13
|
+
|
14
|
+
def log(*values)
|
15
|
+
$log_file.puts(values.join(';'))
|
16
|
+
end
|
17
|
+
|
18
|
+
workers = ENV['WORKERS'].to_i
|
19
|
+
numbers_count = ENV['NUMBERS_COUNT'].to_i
|
20
|
+
text_file = ENV['TEXT_FILE']
|
21
|
+
|
22
|
+
numbers = (0...numbers_count).to_a
|
23
|
+
floats = numbers.map(&:to_f)
|
24
|
+
strings = File.read(text_file).split("\n")
|
25
|
+
|
26
|
+
|
27
|
+
# =============================================================================
|
28
|
+
# Serialization
|
29
|
+
# =============================================================================
|
30
|
+
|
31
|
+
time = Benchmark.realtime do
|
32
|
+
@rdd_numbers = sc.parallelize(numbers, workers)
|
33
|
+
end
|
34
|
+
|
35
|
+
log('NumbersSerialization', time)
|
36
|
+
|
37
|
+
|
38
|
+
time = Benchmark.realtime do
|
39
|
+
@rdd_floats = sc.parallelize(floats, workers)
|
40
|
+
end
|
41
|
+
|
42
|
+
log('FloatsSerialization', time)
|
43
|
+
|
44
|
+
|
45
|
+
time = Benchmark.realtime do
|
46
|
+
@rdd_strings = sc.parallelize(strings, workers)
|
47
|
+
end
|
48
|
+
|
49
|
+
log('StringsSerialization', time)
|
50
|
+
|
51
|
+
|
52
|
+
# =============================================================================
|
53
|
+
# Computing
|
54
|
+
# =============================================================================
|
55
|
+
|
56
|
+
|
57
|
+
# --- Is prime? ---------------------------------------------------------------
|
58
|
+
|
59
|
+
is_prime = Proc.new do |x|
|
60
|
+
case
|
61
|
+
when x < 2
|
62
|
+
[x, false]
|
63
|
+
when x == 2
|
64
|
+
[x, true]
|
65
|
+
when x % 2 == 0
|
66
|
+
[x, false]
|
67
|
+
else
|
68
|
+
upper = Math.sqrt(x.to_f).to_i
|
69
|
+
result = true
|
70
|
+
|
71
|
+
i = 3
|
72
|
+
while i <= upper
|
73
|
+
if x % i == 0
|
74
|
+
result = false
|
75
|
+
break
|
76
|
+
end
|
77
|
+
|
78
|
+
i += 2
|
79
|
+
end
|
80
|
+
|
81
|
+
[x, result]
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
time = Benchmark.realtime do
|
86
|
+
@rdd_numbers.map(is_prime).collect
|
87
|
+
end
|
88
|
+
|
89
|
+
log('IsPrime', time)
|
90
|
+
|
91
|
+
|
92
|
+
# --- Matrix multiplication ---------------------------------------------------
|
93
|
+
|
94
|
+
matrix_size = ENV['MATRIX_SIZE'].to_i
|
95
|
+
|
96
|
+
matrix = Array.new(matrix_size) do |row|
|
97
|
+
Array.new(matrix_size) do |col|
|
98
|
+
row+col
|
99
|
+
end
|
100
|
+
end;
|
101
|
+
|
102
|
+
multiplication_func = Proc.new do |matrix|
|
103
|
+
size = matrix.size
|
104
|
+
|
105
|
+
Array.new(size) do |row|
|
106
|
+
Array.new(size) do |col|
|
107
|
+
matrix[row]
|
108
|
+
|
109
|
+
result = 0
|
110
|
+
size.times do |i|
|
111
|
+
result += matrix[row][i] * matrix[col][i]
|
112
|
+
end
|
113
|
+
result
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
time = Benchmark.realtime do
|
119
|
+
rdd = sc.parallelize(matrix, 1)
|
120
|
+
rdd.map_partitions(multiplication_func).collect
|
121
|
+
end
|
122
|
+
|
123
|
+
log('MatrixMultiplication', time)
|
124
|
+
|
125
|
+
|
126
|
+
# --- Pi digits ---------------------------------------------------------------
|
127
|
+
# http://rosettacode.org/wiki/Pi#Ruby
|
128
|
+
|
129
|
+
pi_digit = ENV['PI_DIGIT'].to_i
|
130
|
+
|
131
|
+
pi_func = Proc.new do |size|
|
132
|
+
size = size.first
|
133
|
+
result = ''
|
134
|
+
|
135
|
+
q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
|
136
|
+
while size > 0
|
137
|
+
if 4*q+r-t < n*t
|
138
|
+
result << n.to_s
|
139
|
+
size -= 1
|
140
|
+
nr = 10*(r-n*t)
|
141
|
+
n = ((10*(3*q+r)) / t) - 10*n
|
142
|
+
q *= 10
|
143
|
+
r = nr
|
144
|
+
else
|
145
|
+
nr = (2*q+r) * l
|
146
|
+
nn = (q*(7*k+2)+r*l) / (t*l)
|
147
|
+
q *= k
|
148
|
+
t *= l
|
149
|
+
l += 2
|
150
|
+
k += 1
|
151
|
+
n = nn
|
152
|
+
r = nr
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
[result]
|
157
|
+
end
|
158
|
+
|
159
|
+
time = Benchmark.realtime do
|
160
|
+
rdd = sc.parallelize([pi_digit], 1)
|
161
|
+
rdd.map_partitions(pi_func).collect
|
162
|
+
end
|
163
|
+
|
164
|
+
log('PiDigit', time)
|
165
|
+
|
166
|
+
|
167
|
+
$log_file.close
|