First commit. Already outdated

master
J. Fernando Sánchez 6 years ago
commit 4aef771062

@ -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)
}
}

@ -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

@ -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)
}
}
}

File diff suppressed because it is too large Load Diff

BIN
dist/spark1.pdf vendored

Binary file not shown.

BIN
dist/spark2.pdf vendored

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 339 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 46 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save