-
Notifications
You must be signed in to change notification settings - Fork 2
/
ParMatMulMain.go
84 lines (71 loc) · 2.3 KB
/
ParMatMulMain.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"./ParallelMat"
"fmt"
"flag"
"log"
"os"
"runtime"
"runtime/pprof"
"time"
. "./comm"
)
var mat1loc string
var mat2loc string
var NumWorkers int
func init() {
flag.StringVar(&mat1loc, "mat1", "./data1.csv", "Path to the CSV data file.")
flag.StringVar(&mat2loc, "mat2", "./data2.csv", "Path to the CSV data file.")
flag.IntVar(&NumWorkers, "workers", 5, "number of goroutines doing the work")
}
func main() {
runtime.GOMAXPROCS(4)
flag.Parse()
//Memory and CPU Profiling.Use gopprof matmul cpuprofile and gopprof matmul memprofile to see profiling information
var cpuprofile = "cpuprofile"
var memprofile = "memprofile"
f1, err := os.Create(cpuprofile)
if err != nil {
log.Fatal(err)
}
f2, err := os.Create(memprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f1)
defer pprof.StopCPUProfile() //Happens when main() returns
pprof.WriteHeapProfile(f2) //Memory profiler
//Reading the matrices from csv files
start := time.Now()
mat1 := OpenCsv(mat1loc)
mat2 := OpenCsv(mat2loc)
end := time.Now()
if(mat1.Columns!=mat2.Rows){
fmt.Println("These matrices cannot be multiplied, %s has %d columns and %s has %d rows",mat1loc,mat1.Columns,mat2loc,mat2.Rows)
os.Exit(1)
}
rtime := end.Sub(start)
fmt.Printf("\nTime Taken to read Matrices %v s\n", rtime.Seconds())
matres := Matrix{mat1.Rows, mat2.Columns, make([][]int, mat1.Rows)}
InitMatrix(&matres) //matres.initMatrix() make it this way
//ParallelMatMul.CheckResults(mat1.Data,mat2.Data)see what's in the matrices
matValidate := Matrix{mat1.Rows, mat2.Columns, make([][]int, mat1.Rows)} //Matrix for validating the results
InitMatrix(&matValidate)
ParallelMat.NumWorkers = NumWorkers// need not be set, has default
fmt.Println("\nExecuting Parallel Matrix Multiplication")
start = time.Now()
ParallelMat.Mul(mat1, mat2, &matres)
end = time.Now()
mtime := end.Sub(start)
fmt.Printf("\nParallel matrix multiplication done in %v s ", mtime.Seconds())
fmt.Println()
fmt.Printf("\nTotal time taken %v s ", (rtime + mtime).Seconds())
//Validation
fmt.Println("\nChecking for errors using standard matrix multiplication")
SeqMatMult(mat1.Data, mat2.Data, matValidate.Data)
if CheckResults(matres.Data,matValidate.Data) {
fmt.Println("\nNo errors occured")
} else {
fmt.Println("\nError detected\n")
}
}