-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
420 lines (375 loc) · 9.42 KB
/
main.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package main
import (
"fmt"
"math/rand"
"runtime"
"sync"
"time"
)
//Matrix is used as a type of 2d array
type Matrix [SIZE][SIZE]int
var (
regularTime time.Duration
//Solution1Time Naive-based execution time
Solution1Time time.Duration
//Solution2Time Strassen-based execution time
Solution2Time time.Duration
//Solution3Time Cannon-based execution time
Solution3Time time.Duration
bestTime time.Duration
)
const (
//SIZE of matrix ie SIZE x SIZE
SIZE = 12
//RAND used for random matrix generation
RAND = 100
)
func rowCount(inM Matrix) int {
rc := len(inM)
return rc
}
func colCount(inM Matrix) int {
cc := len(inM[0])
return cc
}
func printMat(inM Matrix, name string) {
fmt.Println("Matrix: ", name)
for _, i := range inM {
for _, j := range i {
fmt.Print(" ", j)
}
fmt.Println()
}
fmt.Println()
rc := rowCount(inM)
fmt.Println("Row Count: ", rc)
cc := colCount(inM)
fmt.Println("Column Count: ", cc)
fmt.Println()
}
func printIntro() {
fmt.Println()
fmt.Println("A long time ago in a galaxy far, far away....")
fmt.Println()
fmt.Println(" ┌───── •✧✧• ─────┐ ")
fmt.Println("-------------- CONCURRENT WARS ---------------")
fmt.Println(" └───── •✧✧• ─────┘ ")
fmt.Println(" _.=+._ ")
fmt.Println(" :.\\`--._/[_/~|;\\_.--'/.::: ")
fmt.Println(" ::.`. ` __`\\.-.( .'.:::: ")
fmt.Println(" ::::.`-:.`'..`-'/\\'.:::::: ")
fmt.Println(" :::::::.\\ `--')/ ) :::::: ")
fmt.Println(" `--' ")
fmt.Println()
fmt.Println("By: Ayoub Jdair | Luke Kellett Murray")
fmt.Println("Student IDs: 18266401 | 18250785")
fmt.Println("Going for Grade: A1")
fmt.Println()
}
func printOutro() {
fmt.Println(" ┌───── •✧✧• ─────┐ ")
fmt.Println("------------- THE END -------------")
fmt.Println(" └───── •✧✧• ─────┘ ")
fmt.Println()
fmt.Println(" _.=+._ ")
fmt.Println(" :.\\`--._/[_/~|;\\_.--'/.::: ")
fmt.Println(" ::.`. ` __`\\.-.( .'.:::: ")
fmt.Println(" ::::.`-:.`'..`-'/\\'.:::::: ")
fmt.Println(" :::::::.\\ `--')/ ) :::::: ")
fmt.Println(" `--' ")
fmt.Println()
fmt.Println("Written and Directed by George Lucas")
fmt.Println("Student IDs: 18266401 | 18250785")
fmt.Println("Going for Grade: A1")
fmt.Println()
}
func printResults(procs int) {
println()
println("The results are in!")
println()
println("GOMAXPROCS set to: ", procs)
println()
println("Solution 0 time: ", regularTime.Microseconds(), "Microseconds")
println("Solution 1 time: ", Solution1Time.Microseconds(), "Microseconds")
println("Solution 2 time: ", Solution2Time.Microseconds(), "Microseconds")
println("Solution 3 time: ", Solution3Time.Microseconds(), "Microseconds")
println()
}
func createRandomMatrix() (Matrix, Matrix) {
var a Matrix
var b Matrix
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
a[i][j] = rand.Intn(RAND)
b[i][j] = rand.Intn(RAND)
}
}
return a, b
}
//Solution0 Based on non-concurrent method provided
func Solution0(inA Matrix, inB Matrix, procs int) Matrix {
fmt.Println("Solution 0: Calculating Non-Concurrently")
runtime.GOMAXPROCS(procs)
var i, j int
k := 0
total := 0
var nM Matrix
start := time.Now()
for i = 0; i < SIZE; i++ {
for j = 0; j < SIZE; j++ {
for k = 0; k < SIZE; k++ {
total = total + inA[i][k]*inB[k][j]
}
nM[i][j] = total
total = 0
}
}
fmt.Println()
elapsed := time.Since(start)
regularTime = elapsed
fmt.Printf("Solution 0: Time taken to calculate Non-Concurrently %s ", elapsed)
fmt.Println()
fmt.Println("Calculation Result = ")
printMat(nM, "C")
fmt.Println()
return nM
}
//Solution1 Based on naive method
func Solution1(inA Matrix, inB Matrix, procs int) Matrix {
fmt.Println("Solution 1: Calculating Concurrently")
runtime.GOMAXPROCS(procs)
var i, j int
k := 0
total := 0
var wg sync.WaitGroup
var mutex sync.RWMutex
var nM Matrix
start := time.Now()
for i = 0; i < SIZE; i++ {
wg.Add(1)
go func(i int) {
fmt.Println("Go Routine: ", i)
for j = 0; j < SIZE; j++ {
for k = 0; k < SIZE; k++ {
mutex.Lock()
total = total + inA[i][k]*inB[k][j]
mutex.Unlock()
}
mutex.Lock()
nM[i][j] = total
total = 0
mutex.Unlock()
}
wg.Done()
}(i)
}
fmt.Println()
wg.Wait()
elapsed := time.Since(start)
Solution1Time = elapsed
fmt.Printf("Solution 1: Time taken to calculate concurrently %s ", elapsed)
fmt.Println("Solution 1: Calculation Result = ")
printMat(nM, "C")
fmt.Println()
return nM
}
//Based on Strassen Method
func multiply(A Matrix, B Matrix) Matrix {
var n Matrix
for i := 0; i < SIZE; i++ {
for j := 0; j < SIZE; j++ {
sum := 0
for k := 0; k < SIZE; k++ {
sum += A[i][k] * B[k][j]
}
n[i][j] = sum
}
}
return n
}
func multiply2(A [SIZE / 2][SIZE / 2]int, B [SIZE / 2][SIZE / 2]int) [SIZE / 2][SIZE / 2]int {
var n [SIZE / 2][SIZE / 2]int
var wg sync.WaitGroup
var mutex sync.RWMutex
for i := 0; i < SIZE/2; i++ {
wg.Add(1)
go func(i int) {
for j := 0; j < SIZE/2; j++ {
sum := 0
for k := 0; k < SIZE/2; k++ {
mutex.Lock()
sum += A[i][k] * B[k][j]
mutex.Unlock()
}
n[i][j] = sum
}
wg.Done()
}(i)
}
wg.Wait()
return n
}
func add(A [SIZE / 2][SIZE / 2]int, B [SIZE / 2][SIZE / 2]int) [SIZE / 2][SIZE / 2]int {
var n [SIZE / 2][SIZE / 2]int
var wg sync.WaitGroup
for i := 0; i < SIZE/2; i++ {
wg.Add(1)
go func(i int) {
for j := 0; j < SIZE/2; j++ {
n[i][j] += A[i][j] + B[i][j]
}
wg.Done()
}(i)
}
wg.Wait()
return n
}
func sub(A [SIZE / 2][SIZE / 2]int, B [SIZE / 2][SIZE / 2]int) [SIZE / 2][SIZE / 2]int {
var n [SIZE / 2][SIZE / 2]int
var wg sync.WaitGroup
for i := 0; i < SIZE/2; i++ {
wg.Add(1)
go func(i int) {
for j := 0; j < SIZE/2; j++ {
n[i][j] += A[i][j] - B[i][j]
}
wg.Done()
}(i)
}
wg.Wait()
return n
}
//Solution2 Based on Strassen divide and conquer
func Solution2(A Matrix, B Matrix, procs int) Matrix {
fmt.Println("Solution 2: Calculating Concurrently")
runtime.GOMAXPROCS(procs)
var n Matrix
var wg sync.WaitGroup
var wg2 sync.WaitGroup
var A11 [SIZE / 2][SIZE / 2]int
var A12 [SIZE / 2][SIZE / 2]int
var A21 [SIZE / 2][SIZE / 2]int
var A22 [SIZE / 2][SIZE / 2]int
var B11 [SIZE / 2][SIZE / 2]int
var B12 [SIZE / 2][SIZE / 2]int
var B21 [SIZE / 2][SIZE / 2]int
var B22 [SIZE / 2][SIZE / 2]int
start := time.Now()
for i := 0; i < SIZE/2; i++ {
wg.Add(1)
go func(i int) {
for j := 0; j < SIZE/2; j++ {
A11[i][j] = A[i][j]
A12[i][j] = A[i][j+SIZE/2]
A21[i][j] = A[i+SIZE/2][j]
A22[i][j] = A[i+SIZE/2][j+SIZE/2]
B11[i][j] = B[i][j]
B12[i][j] = B[i][j+SIZE/2]
B21[i][j] = B[i+SIZE/2][j]
B22[i][j] = B[i+SIZE/2][j+SIZE/2]
}
wg.Done()
}(i)
}
wg.Wait()
M1 := multiply2(add(A11, A22), add(B11, B22))
M2 := multiply2(add(A21, A22), B11)
M3 := multiply2(A11, sub(B12, B22))
M4 := multiply2(A22, sub(B21, B11))
M5 := multiply2(add(A11, A12), B22)
M6 := multiply2(sub(A21, A11), add(B11, B12))
M7 := multiply2(sub(A12, A22), add(B21, B22))
for i := 0; i < SIZE/2; i++ {
wg2.Add(1)
go func(i int) {
for j := 0; j < SIZE/2; j++ {
n[i][j] = M1[i][j] + M4[i][j] - M5[i][j] + M7[i][j]
n[i][j+SIZE/2] = M3[i][j] + M5[i][j]
n[i+SIZE/2][j] = M2[i][j] + M4[i][j]
n[i+SIZE/2][j+SIZE/2] = M1[i][j] - M2[i][j] + M3[i][j] + M6[i][j]
}
wg2.Done()
}(i)
}
wg2.Wait()
elapsed := time.Since(start)
Solution2Time = elapsed
fmt.Printf("Solution 2: Time taken to calculate concurrently %s ", elapsed)
fmt.Println("Solution 2: Calculation Result = ")
printMat(n, "C")
fmt.Println()
return n
}
//ShiftL used on rows to move left with wrap-around
func ShiftL(matrix Matrix, i, count int) {
ind := 0
for ind < count {
temp := matrix[i][0]
indl := len(matrix[i]) - 1
for j := 0; j < indl; j++ {
matrix[i][j] = matrix[i][j+1]
matrix[i][indl] = temp
ind++
}
}
}
//ShiftU used for columns to shift up with wrap-around
func ShiftU(matrix Matrix, j, count int) {
ind := 0
for i := 0; i < count; i++ {
temp := matrix[0][j]
indl := len(matrix) - 1
for i := 0; i < indl; i++ {
matrix[i][j] = matrix[i+1][j]
matrix[indl][j] = temp
ind++
}
}
}
//Solution3 Based on Cannon algorithm
func Solution3(A Matrix, B Matrix, procs int) Matrix {
println("Solution 3: Calculating Concurrently")
runtime.GOMAXPROCS(procs)
var nM Matrix
start := time.Now()
var wg sync.WaitGroup
var mutex sync.RWMutex
for k := 0; k < len(A); k++ {
wg.Add(1)
go func(k int) {
fmt.Println("Go Routine: ", k)
for i := 0; i < len(A); i++ {
for j := 0; j < len(B); j++ {
mutex.Lock()
m := (i + j + k) % len(B)
nM[i][j] += A[i][m] * B[m][j]
mutex.Unlock()
ShiftL(A, i, 1)
ShiftU(B, j, 1)
}
}
wg.Done()
}(k)
}
wg.Wait()
elapsed := time.Since(start)
Solution3Time = elapsed
fmt.Printf("Solution 3: Time taken to calculate concurrently %s ", elapsed)
fmt.Println("Solution 3: Calculation Result = ")
printMat(nM, "C")
fmt.Println()
return nM
}
func main() {
printIntro()
a, b := createRandomMatrix()
procs := 2
printMat(a, "A")
printMat(b, "B")
Solution0(a, b, procs)
Solution1(a, b, procs)
Solution2(a, b, procs)
Solution3(a, b, procs)
printResults(procs)
printOutro()
}