-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatMultStatic.go
171 lines (134 loc) · 4.54 KB
/
MatMultStatic.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
166
167
168
169
170
171
/****************************************************************************/
//MATRIX MULTIPLICATION WITH STATIC WORK DIVISION
/****************************************************************************/
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
//Allocates Memory and returns matrix
/*
Calculates addition of the matrices A and B for row
numbers [start, end)
*/
func multMatBlock(matA [][]int, matB [][]int, matC [][]int, startRow int,
endRow int, ch chan bool) {
cols := len(matA)
for i := startRow; i < endRow; i++ {
for j := 0; j < cols; j++ {
matC[i][j] = 0
for k:=0; k < cols; k++ {
matC[i][j] += ( matA[i][k] * matB[k][j] )
}
}
//fmt.Println("multMatBlock row i = ", i, " done");
}
ch <- true
}
/*
Breaks down matrix A and B into 'size / split' chunks.
Calls 'multMatBlock' split number of times.
*/
func multMatStatic(matA [][]int, matB [][]int, matC [][]int, splits int,
ch chan bool){
size := len(matA) //number of rows and columns
chBool := make(chan bool)
if size < splits {
splits = size
}
chunkSize := size / splits //rows to compute by 1 go routine
i := 0
for ; i <= (splits-2); i++ {
start := i*chunkSize
end := start + chunkSize
go multMatBlock(matA, matB, matC, start, end, chBool)
}
//handle last split separately
start := i*chunkSize
go multMatBlock(matA, matB, matC, start, size, chBool)
for i:= 0; i < splits; i++ {
<- chBool
}
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 multMatStatic(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>")
}
}