Open main menu

CDOT Wiki β

GPU621/Go Kimchi

Revision as of 15:52, 3 August 2021 by Ywon7 (talk | contribs) (Implementation)

Project Name

Parallel Programming in Go & Java

Group Members

Yuseok Won Minsu Kim

Project Description

This project will look at the performance difference between /Go/Java using a classic matrix multiplication algorithm. It will present how to do parallel program in each language and show the different syntax codes that's using the similar algorithm. We will gather the results of running the code and show the graphical result to clearly show to performance difference between the two programming language. It will also briefly cover the history of each language, key term of parallel programming, look at the fundamental difference between two languages to see what may have caused the performance difference.

Progress

What's GO Language?


Go Language is a open source programming language that is designed at Google by Robert Griesemer, Ken Thompson, and Rob Pike.

Go lang is a Fast and Pretty language. Go language is a compiler language which makes its execution fast. Go is faster than Python & JavaScript. In some cases, Go may be faster than Java or as fast as Java.
Go also has a relatively easy syntax which makes it easy to learn and fast to develop.
Is Go Popular?
GO is used by Google, Youtube, Google Download Server, Uber replaced Node.JS to Go, Twitch's trance-coding process Go & C++.




What's Java Language?


Java is open source and Object-oriented programming language developed by Sun Microsystems in 1995. The founder is James Gosling. In 2010, Oracle acquired Sun Microsystems and owned Java's copyright.

Java is simple programming language, easy to learn, clean and easy to understand. This is because syntax of Java is based on C++ that has removed many confusing and rarely used functionality by user (e.g. operator overloading, explicit pointer, automatic garbage collection etc.)

Java is the platform independent language. Programs can run on several different types of computer as long as computer has a Java Runtime Environment(JRE) installed.





Parallel Programming in GO & Java


Parallelism and Concurrency

Rob Pike who is developer of Go language has defined parallelism and concurrency in his speech "Concurrency is not Parallelism":

Parallelism defined as

     Parallelism is about doing lots of things at once.

Concurrency defined as

     Concurrency is about dealing with lots of things at once.

Concurrency allows to execute multiple tasks in partial order or out of order. The executions must finished before the next execution can start. While, parallelism has simultaneous execution on a multicore per CPU.

If we combine two concepts:

  Concurrency                 Concurrency + parallelism
  (Single-Core CPU)           (Multi-Core CPU)
   ___                         ___ ___
  |th1|                       |th1|th2|
  |   |                       |   |___|
  |___|___                    |   |___
      |th2|                   |___|th2|
   ___|___|                    ___|___|
  |th1|                       |th1|
  |___|___                    |   |___
      |th2|                   |   |th2|

diagram cited by Pithikos in Stack Overflow

Go language focuses on concurrency and parallelism by implementing its own Goroutine instead of regular threading. Goroutines uses the built-in scheduler to conduct the threads in behind and hide many complexity of thread management from the user to specify which function to run and execute them with “Go” command.

Java language can implement thread in two different methods. The first method is to create a class which extends the “Thread” class that gives the user to have control of the lifecycle of the thread. But Java language only allows single inheritance which prevents the thread from being extend other base class. The second method is using ‘Runnable’ interface. This method allows to user to extend other base class while still being run as a new thread. However, this method does not give user to control lifecycle of thread.

Java version 5.0 and later provides concurrency to simplify thread creation and processing. The "Executor" that implement by java.util.concurrent provides APIs for creating and processing new threads using reusable threads.

Implementation


Installation of Go
Go to https://golang.org/dl/ & Download Go accordingly to your OS.

Installation of Java
Go to https://www.java.com/ko/download/manual.jsp & Download Java accordingly to your OS.

Multi-Thread Program in Go

   package main
   import (
       "bufio"
       "fmt"
       "log"
       "os"
       "runtime"
       "runtime/debug"
       "strconv"
       "strings"
       "sync"
       "time"
   )
   func multiply(A [][]int, B [][]int) [][]int { sizeA := len(A)
       sizeB := len(B)
       n := make([][]int, sizeA)
       for i := range n {
           n[i] = make([]int, sizeB)
       }
       for i := 0; i < sizeA; i++ {
           for k := 0; k < sizeB; k++ {
               temp := A[i][k]
               for j := 0; j < sizeB; j++ {
                   n[i][j] += temp * B[k][j]
               }
           } 		

}

       return n 
   }
   func splitMatrix(nrOfThreads int, matrix [][]int) (matrixes [][][]int) {
       splitter := len(matrix) / nrOfThreads
       for i := 0; i < nrOfThreads; i++ {
           matrixes = append(matrixes, matrix[splitter*i:(splitter*(i+1))])
       }
       return
   }
   func multiplyStuff(finalMatrix *[][][]int, matrix1 [][]int, matrix2 [][]int, i int) {
       (*finalMatrix)[i] = multiply(matrix1, matrix2) 
   }
   func readFile(filePath string) (matrix1 [][]int, matrix2 [][]int) {
       file, err := os.Open(filePath)
       if err != nil {
           log.Fatal(err)
       }
       defer file.Close()
       var temp []int
       matrixNr := 1
       scanner := bufio.NewScanner(file)
       for scanner.Scan() {
           words := strings.Fields(scanner.Text()) 
           if len(words) != 0 {
               for _, element := range words {
                   i, err := strconv.Atoi(element)
                   if err != nil {
                       log.Fatal(err)
                   }
                   temp = append(temp, i)
               }
               if matrixNr == 1 {
                   matrix1 = append(matrix1, temp)
               } else {
                   matrix2 = append(matrix2, temp)
               }
               temp = nil
           } else {
               matrixNr = 2 
    	    }
       }	
       if err := scanner.Err(); err != nil {
           log.Fatal(err)
       }
       return
   }
   func main() {
       file := os.Args[1]
       nrOfThreads, err := strconv.Atoi(os.Args[2]) 
       if err != nil {  
           log.Fatal("USAGE: " + os.Args[0] + " <file> <nrOfThreads>")
       }
       debug.SetGCPercent(-1)
       if nrOfThreads <= 0 {
           runtime.GOMAXPROCS(1)
       } else if nrOfThreads >= 16 {
           runtime.GOMAXPROCS(8)
       } else {
           runtime.GOMAXPROCS(nrOfThreads)
       }
       var wg sync.WaitGroup
       finishedMatrix := make([][][]int, nrOfThreads)
       matrix1, matrix2 := readFile(file)
       if len(matrix1) != len(matrix2) || (nrOfThreads != 0 && len(matrix1)%nrOfThreads != 0) {
           log.Fatal("USAGE: " + os.Args[0] + " <file> <nrOfThreads>")
       }
       var start int64
       if nrOfThreads == 0 {
           start = time.Now().UnixNano()
           multiply(matrix1, matrix2)
       } else {
           matrixes := splitMatrix(nrOfThreads, matrix1)
           start = time.Now().UnixNano()
           for i := 0; i < nrOfThreads; i++ {
               wg.Add(1)
               go func(index int) {
                   defer wg.Done()
                   multiplyStuff(&finishedMatrix, matrixes[index], matrix2, index)
               }(i)

}

       wg.Wait() 
       }
       end := time.Now().UnixNano()
       fmt.Printf("Execution took %d ns\n", (end - start)) 
       runtime.GC()
   }


Multi-Thread Program in Java

Result


Analysis