-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatAddRecursive.go
157 lines (126 loc) · 4.25 KB
/
MatAddRecursive.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
/****************************************************************************/
// MATRIX ADDITION WITH RECURSIVE DIVIDE AND CONQUER
/****************************************************************************/
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
func addMatRecursive(matA [][]int, matB [][]int, matC [][]int, baseSize int,
ch chan bool) {
//Get current size
size := len(matA)
if(size <= baseSize) {
//Do serial addition
for i := 0; i < size; i++ {
for j := 0; j < size; j++ {
matC[i][j] = matA[i][j] + matB[i][j]
}
}
ch <- true
} else {
//Recursive divide and conquer
//End is mid point
end := size / 2
ch1 := make(chan bool)
ch2 := make(chan bool)
go addMatRecursive(matA[0:end], matB[0:end], matC[0:end], baseSize,
ch1)
go addMatRecursive(matA[end:size], matB[end:size], matC[end:size],
baseSize, ch2)
//Wait for additions to complete
<- ch1
<- ch2
ch <-true
}
}
func addMatRecursiveBegin(matA [][]int, matB [][]int, matC [][]int, splits int,
ch chan bool){
size := len(matA)
baseSize := size /splits
chStart := make(chan bool)
go addMatRecursive(matA, matB, matC, baseSize, 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)
/******** ADDITION ***********/
//Do addition
go addMatRecursive(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>")
}
}