-
Notifications
You must be signed in to change notification settings - Fork 0
/
MatMultRecursive.go
165 lines (131 loc) · 4.52 KB
/
MatMultRecursive.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/****************************************************************************/
//MATRIX MULTIPLICATION WITH RECURSIVE WORK DIVISION
/****************************************************************************/
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
func multMatRecursive(matA [][]int, matB [][]int, matC [][]int, baseSize int,
startRow int, rowCount int, ch chan bool) {
//Get current size
size := len(matA)
if(rowCount <= baseSize) {
//Do serial multiplication
endRow := startRow + rowCount
for i := startRow; i < endRow; i++ {
for j := 0; j < size; j++ {
matC[i][j] = 0
for k:=0; k < size; k++ {
matC[i][j] += ( matA[i][k] * matB[k][j] )
}
}
}
ch <- true
} else {
//Recursive divide and conquer
ch1 := make(chan bool)
ch2 := make(chan bool)
go multMatRecursive(matA, matB, matC, baseSize, startRow,
rowCount/2, ch1)
go multMatRecursive(matA, matB, matC, baseSize, startRow + (rowCount/2),
rowCount - (rowCount /2), ch2)
//Wait for multiplications to complete
<- ch1
<- ch2
ch <-true
}
}
func multMatRecursiveBegin(matA [][]int, matB [][]int, matC [][]int,
splits int, ch chan bool) {
size := len(matA)
if size < splits {
splits = size
}
baseSize := size /splits
chStart := make(chan bool)
go multMatRecursive(matA, matB, matC, baseSize, 0, size, chStart)
<- chStart
ch <- true
}
func allocateMat(size int, ch chan [][]int) {
// Allocate Memory
mat := make([][]int, size)
for i := range mat {
mat[i] = make([]int, size)
}
ch <- mat
}
func initializeMat(mat [][]int, ch chan bool, b bool){
size := len(mat)
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
if b {
mat[i][j] = rand.Intn(size);
} else {
mat[i][j] = -1
}
}
}
ch <- true
}
func print(mat [][]int, size int) {
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
fmt.Printf("ary[%d][%d] = %d ", i, j, mat[i][j]);
}
fmt.Println("");
}
}
func main() {
t1 := time.Now()
chA := make(chan [][]int) //Creating channel for matrix A
chB := make(chan [][]int) //Creating channel for matrix B
chC := make(chan [][]int) //Creating channel for matrix C
//Accept matrix size as input
//Commnad Line Arguments for splits and size of matrix
if len(os.Args) == 3{
size, _ := strconv.Atoi(os.Args[2])
splits, _ := strconv.Atoi(os.Args[1])
//Allcoating Memory of Matrices concurrently
go allocateMat(size, chA)
go allocateMat(size, chB)
go allocateMat(size, chC)
//Wait for allocation
matA := <-chA
matB := <-chB
matC := <-chC
chBool := make(chan bool)
// chBBool := make(chan bool)
// chCBool := make(chan bool)
// Initialize matrix A
go initializeMat(matA, chBool, true)
go initializeMat(matB, chBool, true)
go initializeMat(matC, chBool, false)
//Wait for initialization
<- chBool
<- chBool
<- chBool
//Print matrix A
fmt.Println("\nMatrix A loaded with random numbers:\n")
print(matA, size)
fmt.Println("\nMatrix B loaded with random numbers:\n")
print(matB, size)
fmt.Println("\nMatrix C loaded with -1s:\n")
print(matC, size)
/******** MULTIPLICATION ***********/
//Do multiplication
go multMatRecursiveBegin(matA, matB, matC, splits, chBool)
<- chBool
fmt.Println("\nMatrix C holds result after A[] * B[]:\n")
print(matC, size)
duration := time.Since(t1)
seconds := duration.Seconds()
fmt.Println("\nRunning time = ", seconds, " s\n")
} else {
fmt.Println("usage is ./executable <splits> <size>")
}
}