First commit. Already outdated
73
Functions.scala
Normal file
@ -0,0 +1,73 @@
|
||||
import java.io._
|
||||
import scala.io.Source
|
||||
import scala.util.Random
|
||||
|
||||
object Functions {
|
||||
def helloworld1: Unit = {
|
||||
println("Hello, functions world!")
|
||||
}
|
||||
|
||||
def splitLine(line: String) = line.split(" ")
|
||||
|
||||
def countOccurrences(target: String)(line: String) = {
|
||||
splitLine(line).map(_ == target).foldLeft(0)((acc, n) => if (n) acc+1 else acc)
|
||||
}
|
||||
|
||||
def time[R](block: => R): R = {
|
||||
time("{}")(block)
|
||||
}
|
||||
|
||||
def time[R](name: String)(block: => R): R = {
|
||||
val formatter = java.text.NumberFormat.getIntegerInstance
|
||||
val t0 = System.nanoTime()
|
||||
val result = block // call-by-name
|
||||
val t1 = System.nanoTime()
|
||||
println("Elapsed time for " + name +": " + formatter.format((t1 - t0)) + "ns. Result: "+result)
|
||||
result
|
||||
}
|
||||
|
||||
def makeFile(size: Integer, name: String): File = {
|
||||
|
||||
val file = new File(name)
|
||||
if (file.exists && file.length >= size) return file
|
||||
val bw = new BufferedWriter(new FileWriter(file))
|
||||
|
||||
for (
|
||||
i <- Stream.continually(Random.nextInt(100)).takeWhile(x => file.length < size)
|
||||
) {
|
||||
val r = scala.util.Random
|
||||
bw.write(i.toString())
|
||||
bw.write("\n")
|
||||
}
|
||||
bw.close()
|
||||
return file
|
||||
}
|
||||
|
||||
def makeBigFile() = {
|
||||
val size = 1024*1024*100
|
||||
makeFile(size, "BigFile.txt")
|
||||
}
|
||||
|
||||
def makeSmallFile() = {
|
||||
val size = 1024*1024
|
||||
makeFile(size, "SmallFile.txt")
|
||||
}
|
||||
|
||||
def applyToFile[R](file: File)(block: String=>R)(parallel: Boolean): Iterator[R] = {
|
||||
if (parallel) {
|
||||
Source.fromFile(file).getLines().grouped(1000).flatMap({ y=>
|
||||
val z = if (parallel) y.toIndexedSeq.par else y;
|
||||
z.map(x=>block(x))})
|
||||
} else {
|
||||
Source.fromFile(file).getLines().map(x=>block(x))
|
||||
}
|
||||
}
|
||||
|
||||
def mapRedFile[R](file: File)(mapper: String=>R)(reducer: (R,R)=>R)(parallel: Boolean): R = {
|
||||
if (parallel)
|
||||
(applyToFile(file)(mapper)(parallel)).grouped(1000).map(x=>x.toIndexedSeq.par.reduce(reducer)).reduce(reducer)
|
||||
else
|
||||
(applyToFile(file)(mapper)(parallel)).reduce(reducer)
|
||||
}
|
||||
|
||||
}
|
34
Makefile
Normal file
@ -0,0 +1,34 @@
|
||||
SOURCES = $(shell ls *.scala)
|
||||
S = scala
|
||||
SC = scalac
|
||||
TARGET = target
|
||||
CP = $(TARGET):scalatest.jar
|
||||
SPEC = scala.RomanSpec
|
||||
TEMP_DIR ?= _LATEX
|
||||
|
||||
all: pdf
|
||||
|
||||
compile: $(SOURCES)
|
||||
@echo "Compiling $(SOURCES)..."
|
||||
@$(SC) -cp . -d $(TARGET) $(SOURCES)
|
||||
|
||||
Parallel.class: $(SOURCES)
|
||||
|
||||
run: Parallel.class
|
||||
@scala -cp $(TARGET) Parallel
|
||||
|
||||
test: compile
|
||||
@$(S) -cp $(CP) org.scalatest.tools.Runner -p . -o -s $(SPEC)
|
||||
|
||||
pdf:
|
||||
@mkdir -p $(TEMP_DIR)
|
||||
xelatex -output-directory ./$(TEMP_DIR) spark1.tex
|
||||
xelatex -output-directory ./$(TEMP_DIR) spark2.tex
|
||||
cp $(TEMP_DIR)/*.pdf .
|
||||
|
||||
clean:
|
||||
@$(RM) -rf $(TEMP_DIR)
|
||||
@$(RM) $(TARGET)/*.class
|
||||
|
||||
clean-all: clean
|
||||
@rm spark*.pdf
|
30
Parallel.scala
Normal file
@ -0,0 +1,30 @@
|
||||
import Functions._
|
||||
|
||||
object Parallel {
|
||||
def main(args: Array[String]): Unit = {
|
||||
time("helloworld"){ helloworld1}
|
||||
val countScala = countOccurrences("scala")(_)
|
||||
time("count"){ countScala("scala is the best language for scalability, says the creator of scala :)")}
|
||||
val bigFile = time("makeFile"){ makeBigFile }
|
||||
val smallFile = time("makeSmall"){ makeSmallFile }
|
||||
|
||||
val example1 = (file: java.io.File, parallel: Boolean) => mapRedFile(file)(x=> (1, Math.sqrt(Math.pow(1.1, x.toInt))))((x1,x2)=>(x1._1+x2._1, x1._2+x2._2))(parallel)
|
||||
|
||||
time("applyToFile Small\tConcurrent"){
|
||||
val (total, sum) = example1(smallFile, false)
|
||||
println("Average: " + sum/total)
|
||||
}
|
||||
time("applyToFile Small\tParallel"){
|
||||
val (total, sum) = example1(smallFile, true)
|
||||
println("Average: " + sum/total)
|
||||
}
|
||||
time("applyToFile Big\tConcurrent"){
|
||||
val (total, sum) = example1(bigFile, false)
|
||||
println("Average: " + sum/total)
|
||||
}
|
||||
time("applyToFile Big\tParallel"){
|
||||
val (total, sum) = example1(bigFile, true)
|
||||
println("Average: " + sum/total)
|
||||
}
|
||||
}
|
||||
}
|
364453
SmallFile.txt
Normal file
BIN
dist/spark1.pdf
vendored
Normal file
BIN
dist/spark2.pdf
vendored
Normal file
BIN
gif/another.gif
Normal file
After Width: | Height: | Size: 2.7 MiB |
BIN
gif/sparkdemo.mp4
Normal file
BIN
gif/sparkdemo.swf
Normal file
BIN
gif/sparkrepl-0.png
Normal file
After Width: | Height: | Size: 339 B |
BIN
gif/sparkrepl-1.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
gif/sparkrepl-10.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
gif/sparkrepl-100.png
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
gif/sparkrepl-101.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
gif/sparkrepl-102.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-103.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-104.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-105.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
gif/sparkrepl-106.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-107.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-108.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-109.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
gif/sparkrepl-11.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
gif/sparkrepl-110.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-111.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
gif/sparkrepl-112.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-113.png
Normal file
After Width: | Height: | Size: 42 KiB |
BIN
gif/sparkrepl-114.png
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
gif/sparkrepl-115.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-116.png
Normal file
After Width: | Height: | Size: 50 KiB |
BIN
gif/sparkrepl-117.png
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
gif/sparkrepl-118.png
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
gif/sparkrepl-119.png
Normal file
After Width: | Height: | Size: 48 KiB |
BIN
gif/sparkrepl-12.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
gif/sparkrepl-120.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-121.png
Normal file
After Width: | Height: | Size: 43 KiB |
BIN
gif/sparkrepl-122.png
Normal file
After Width: | Height: | Size: 41 KiB |
BIN
gif/sparkrepl-123.png
Normal file
After Width: | Height: | Size: 1.1 KiB |
BIN
gif/sparkrepl-13.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
gif/sparkrepl-14.png
Normal file
After Width: | Height: | Size: 13 KiB |
BIN
gif/sparkrepl-15.png
Normal file
After Width: | Height: | Size: 14 KiB |
BIN
gif/sparkrepl-16.png
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
gif/sparkrepl-17.png
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
gif/sparkrepl-18.png
Normal file
After Width: | Height: | Size: 15 KiB |
BIN
gif/sparkrepl-19.png
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
gif/sparkrepl-2.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
gif/sparkrepl-20.png
Normal file
After Width: | Height: | Size: 3.7 KiB |
BIN
gif/sparkrepl-21.png
Normal file
After Width: | Height: | Size: 3.7 KiB |
BIN
gif/sparkrepl-22.png
Normal file
After Width: | Height: | Size: 3.8 KiB |
BIN
gif/sparkrepl-23.png
Normal file
After Width: | Height: | Size: 3.9 KiB |
BIN
gif/sparkrepl-24.png
Normal file
After Width: | Height: | Size: 3.9 KiB |
BIN
gif/sparkrepl-25.png
Normal file
After Width: | Height: | Size: 4.0 KiB |
BIN
gif/sparkrepl-26.png
Normal file
After Width: | Height: | Size: 3.9 KiB |
BIN
gif/sparkrepl-27.png
Normal file
After Width: | Height: | Size: 4.1 KiB |
BIN
gif/sparkrepl-28.png
Normal file
After Width: | Height: | Size: 4.1 KiB |
BIN
gif/sparkrepl-29.png
Normal file
After Width: | Height: | Size: 4.2 KiB |
BIN
gif/sparkrepl-3.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
gif/sparkrepl-30.png
Normal file
After Width: | Height: | Size: 4.2 KiB |
BIN
gif/sparkrepl-31.png
Normal file
After Width: | Height: | Size: 4.3 KiB |
BIN
gif/sparkrepl-32.png
Normal file
After Width: | Height: | Size: 4.2 KiB |
BIN
gif/sparkrepl-33.png
Normal file
After Width: | Height: | Size: 4.2 KiB |
BIN
gif/sparkrepl-34.png
Normal file
After Width: | Height: | Size: 4.3 KiB |
BIN
gif/sparkrepl-35.png
Normal file
After Width: | Height: | Size: 4.4 KiB |
BIN
gif/sparkrepl-36.png
Normal file
After Width: | Height: | Size: 5.6 KiB |
BIN
gif/sparkrepl-37.png
Normal file
After Width: | Height: | Size: 6.6 KiB |
BIN
gif/sparkrepl-38.png
Normal file
After Width: | Height: | Size: 8.2 KiB |
BIN
gif/sparkrepl-39.png
Normal file
After Width: | Height: | Size: 17 KiB |
BIN
gif/sparkrepl-4.png
Normal file
After Width: | Height: | Size: 2.7 KiB |
BIN
gif/sparkrepl-40.png
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
gif/sparkrepl-41.png
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
gif/sparkrepl-42.png
Normal file
After Width: | Height: | Size: 44 KiB |
BIN
gif/sparkrepl-43.png
Normal file
After Width: | Height: | Size: 52 KiB |
BIN
gif/sparkrepl-44.png
Normal file
After Width: | Height: | Size: 54 KiB |
BIN
gif/sparkrepl-45.png
Normal file
After Width: | Height: | Size: 59 KiB |
BIN
gif/sparkrepl-46.png
Normal file
After Width: | Height: | Size: 60 KiB |
BIN
gif/sparkrepl-47.png
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
gif/sparkrepl-48.png
Normal file
After Width: | Height: | Size: 61 KiB |
BIN
gif/sparkrepl-49.png
Normal file
After Width: | Height: | Size: 26 KiB |
BIN
gif/sparkrepl-5.png
Normal file
After Width: | Height: | Size: 4.9 KiB |
BIN
gif/sparkrepl-50.png
Normal file
After Width: | Height: | Size: 29 KiB |
BIN
gif/sparkrepl-51.png
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
gif/sparkrepl-52.png
Normal file
After Width: | Height: | Size: 29 KiB |
BIN
gif/sparkrepl-53.png
Normal file
After Width: | Height: | Size: 28 KiB |
BIN
gif/sparkrepl-54.png
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
gif/sparkrepl-55.png
Normal file
After Width: | Height: | Size: 30 KiB |
BIN
gif/sparkrepl-56.png
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
gif/sparkrepl-57.png
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
gif/sparkrepl-58.png
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
gif/sparkrepl-59.png
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
gif/sparkrepl-6.png
Normal file
After Width: | Height: | Size: 5.7 KiB |
BIN
gif/sparkrepl-60.png
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
gif/sparkrepl-61.png
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
gif/sparkrepl-62.png
Normal file
After Width: | Height: | Size: 33 KiB |
BIN
gif/sparkrepl-63.png
Normal file
After Width: | Height: | Size: 36 KiB |
BIN
gif/sparkrepl-64.png
Normal file
After Width: | Height: | Size: 46 KiB |
BIN
gif/sparkrepl-65.png
Normal file
After Width: | Height: | Size: 56 KiB |
BIN
gif/sparkrepl-66.png
Normal file
After Width: | Height: | Size: 57 KiB |
BIN
gif/sparkrepl-67.png
Normal file
After Width: | Height: | Size: 56 KiB |
BIN
gif/sparkrepl-68.png
Normal file
After Width: | Height: | Size: 57 KiB |
BIN
gif/sparkrepl-69.png
Normal file
After Width: | Height: | Size: 56 KiB |