irgendwo muss noch ein fehler sein, aber beispiel funktioniert

master
leonnicolas 5 years ago
parent 0854a789dd
commit acccbe2ebd

@ -5,9 +5,11 @@ import (
"runtime"
)
const m int64 = 4
const m int64 = 2000
const mm int64 = m * m
var test = [4][4]int64{{-2, 5, 3, 2}, {9, -6, 5, 1}, {3, 2, 7, 3}, {-1, 8, -4, 8}}
var random [m * m]int64 //this is only 16MB for m = 2000
var R [m][m]int64
@ -37,16 +39,16 @@ func arrayToMatrix(ar *[m * m]int64, R *[m][m]int64) {
}
func getSumV(R *[m][m]int64, start int64, step int64, ch chan Res) { //vertical sum
var r Res
r.sum = -1000000
for i := 0; i < len(R[0]); i += int(step) {
r.direction = "V"
r.threadNUm = int(start)
r.sum = -4000000
for i := int(start); i < len(R[0]); i += int(step) {
var sum int64 = 0
for j := 0; j < len(R); j++ {
sum += R[j][i]
}
if sum > r.sum {
r.sum = sum
r.direction = "V"
r.threadNUm = int(start)
r.x = 0
r.y = i
}
@ -55,16 +57,16 @@ func getSumV(R *[m][m]int64, start int64, step int64, ch chan Res) { //vertical
}
func getSumH(R *[m][m]int64, start int64, step int64, ch chan Res) { //horizontal sum
var r Res
r.sum = -1000000
for i := 0; i < len(R); i += int(step) {
r.direction = "H"
r.threadNUm = int(start)
r.sum = -4000000
for i := int(start); i < len(R); i += int(step) {
var sum int64 = 0
for j := 0; j < len(R[i]); j++ {
sum += R[i][j]
}
if sum > r.sum {
r.sum = sum
r.direction = "H"
r.threadNUm = int(start)
r.x = i
r.y = 0
}
@ -73,9 +75,11 @@ func getSumH(R *[m][m]int64, start int64, step int64, ch chan Res) { //horizonta
}
func getSumD(R *[m][m]int64, start int64, step int64, ch chan Res) { //diagonal sum
var r Res
r.sum = -1000000
r.sum = -4000000
r.direction = "D"
r.threadNUm = int(start)
var sum int64
for i := 0; i < len(R); i += int(step) { //start at left column going right and down
for i := int(start); i < len(R); i += int(step) { //start at left column going right and down
sum = 0
for j := 0; j < len(R); j++ {
if i+j >= len(R) {
@ -85,13 +89,11 @@ func getSumD(R *[m][m]int64, start int64, step int64, ch chan Res) { //diagonal
}
if sum > r.sum {
r.sum = sum
r.direction = "D"
r.threadNUm = int(start)
r.x = i
r.y = 0
}
}
for i := 0; i < len(R[0]); i += int(step) { //start at to top row going right and down
for i := int(start); i < len(R[0]); i += int(step) { //start at to top row going right and down
sum = 0
for j := 0; j < len(R); j++ {
if i+j >= len(R[i]) {
@ -101,8 +103,6 @@ func getSumD(R *[m][m]int64, start int64, step int64, ch chan Res) { //diagonal
}
if sum > r.sum {
r.sum = sum
r.direction = "D"
r.threadNUm = int(start)
r.x = 0
r.y = i
}
@ -111,9 +111,11 @@ func getSumD(R *[m][m]int64, start int64, step int64, ch chan Res) { //diagonal
}
func getSumAD(R *[m][m]int64, start int64, step int64, ch chan Res) { // anti diagnonal sum
var r Res
r.sum = -1000000
r.sum = -4000000
r.direction = "AD"
r.threadNUm = int(start)
var sum int64 = 0
for i := 0; i < len(R); i += int(step) { //start at the left column going right and up
for i := int(start); i < len(R); i += int(step) { //start at the left column going right and up
sum = 0
for j := 0; j < len(R); j++ {
if i-j < 0 {
@ -123,13 +125,11 @@ func getSumAD(R *[m][m]int64, start int64, step int64, ch chan Res) { // anti di
}
if sum > r.sum {
r.sum = sum
r.direction = "AD"
r.threadNUm = int(start)
r.x = i
r.y = 0
}
}
for i := 0; i < len(R[0]); i += int(step) { //start at bottom row going right and up
for i := int(start); i < len(R[0]); i += int(step) { //start at bottom row going right and up
sum = 0
for j := 0; j < len(R); j++ {
if i+j >= len(R) {
@ -139,8 +139,6 @@ func getSumAD(R *[m][m]int64, start int64, step int64, ch chan Res) { // anti di
}
if sum > r.sum {
r.sum = sum
r.direction = "AD"
r.threadNUm = int(start)
r.x = len(R)
r.y = i
}
@ -150,39 +148,42 @@ func getSumAD(R *[m][m]int64, start int64, step int64, ch chan Res) { // anti di
func getAllSums(R *[m][m]int64) Res {
ch := make(chan Res)
var ret Res
ret.sum = -1000000
ret.sum = -4000000
numThread := runtime.NumCPU()
for i := 0; i < numThread; i++ {
go getSumH(R, int64(i), int64(numThread), ch)
}
for i := 0; i < numThread; i++ {
r := <-ch
fmt.Printf("sum = %d\n", r.sum)
printRes(r)
if r.sum > ret.sum {
ret = r
}
go getSumV(R, int64(i), int64(numThread), ch)
}
fmt.Printf("largest horizontal sum = %d\n", ret.sum)
printRes(ret)
//printRes(ret)
for i := 0; i < numThread; i++ {
r := <-ch
printRes(r)
if r.sum > ret.sum {
ret = r
}
go getSumD(R, int64(i), int64(numThread), ch)
}
fmt.Printf("largest vertical sum = %d\n", ret.sum)
for i := 0; i < numThread; i++ {
r := <-ch
printRes(r)
if r.sum > ret.sum {
ret = r
}
go getSumAD(R, int64(i), int64(numThread), ch)
}
fmt.Printf("largest diagonal sum = %d\n", ret.sum)
for i := 0; i < numThread; i++ {
r := <-ch
printRes(r)
if r.sum > ret.sum {
ret = r
}
@ -191,16 +192,16 @@ func getAllSums(R *[m][m]int64) Res {
}
func main() {
//test[0]=[-2,5,3,2]
//test1 := [[-2,5,3,2],[9,-6,5,1],[3,2,7,3],[-1,8,-4,8]]
assignRandomNumbers(&random)
arrayToMatrix(&random, &R)
fmt.Println(R)
//fmt.Println(R)
sum := getAllSums(&R)
ch := make(chan Res)
go getSumH(&R, 0, 1, ch)
hsum := <-ch
printRes(hsum)
fmt.Printf("Max sum is: %d\n", sum.sum)
fmt.Printf("Max sum is: %d at ", sum.sum)
printRes(sum)
}

Loading…
Cancel
Save