irgendwo muss noch ein fehler sein, aber beispiel funktioniert

master
leonnicolas 5 years ago
parent 0854a789dd
commit acccbe2ebd

@ -5,9 +5,11 @@ import (
"runtime" "runtime"
) )
const m int64 = 4 const m int64 = 2000
const mm int64 = m * m 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 random [m * m]int64 //this is only 16MB for m = 2000
var R [m][m]int64 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 func getSumV(R *[m][m]int64, start int64, step int64, ch chan Res) { //vertical sum
var r Res var r Res
r.sum = -1000000 r.direction = "V"
for i := 0; i < len(R[0]); i += int(step) { r.threadNUm = int(start)
r.sum = -4000000
for i := int(start); i < len(R[0]); i += int(step) {
var sum int64 = 0 var sum int64 = 0
for j := 0; j < len(R); j++ { for j := 0; j < len(R); j++ {
sum += R[j][i] sum += R[j][i]
} }
if sum > r.sum { if sum > r.sum {
r.sum = sum r.sum = sum
r.direction = "V"
r.threadNUm = int(start)
r.x = 0 r.x = 0
r.y = i 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 func getSumH(R *[m][m]int64, start int64, step int64, ch chan Res) { //horizontal sum
var r Res var r Res
r.sum = -1000000 r.direction = "H"
for i := 0; i < len(R); i += int(step) { r.threadNUm = int(start)
r.sum = -4000000
for i := int(start); i < len(R); i += int(step) {
var sum int64 = 0 var sum int64 = 0
for j := 0; j < len(R[i]); j++ { for j := 0; j < len(R[i]); j++ {
sum += R[i][j] sum += R[i][j]
} }
if sum > r.sum { if sum > r.sum {
r.sum = sum r.sum = sum
r.direction = "H"
r.threadNUm = int(start)
r.x = i r.x = i
r.y = 0 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 func getSumD(R *[m][m]int64, start int64, step int64, ch chan Res) { //diagonal sum
var r Res var r Res
r.sum = -1000000 r.sum = -4000000
r.direction = "D"
r.threadNUm = int(start)
var sum int64 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 sum = 0
for j := 0; j < len(R); j++ { for j := 0; j < len(R); j++ {
if i+j >= len(R) { 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 { if sum > r.sum {
r.sum = sum r.sum = sum
r.direction = "D"
r.threadNUm = int(start)
r.x = i r.x = i
r.y = 0 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 sum = 0
for j := 0; j < len(R); j++ { for j := 0; j < len(R); j++ {
if i+j >= len(R[i]) { 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 { if sum > r.sum {
r.sum = sum r.sum = sum
r.direction = "D"
r.threadNUm = int(start)
r.x = 0 r.x = 0
r.y = i 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 func getSumAD(R *[m][m]int64, start int64, step int64, ch chan Res) { // anti diagnonal sum
var r Res var r Res
r.sum = -1000000 r.sum = -4000000
r.direction = "AD"
r.threadNUm = int(start)
var sum int64 = 0 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 sum = 0
for j := 0; j < len(R); j++ { for j := 0; j < len(R); j++ {
if i-j < 0 { 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 { if sum > r.sum {
r.sum = sum r.sum = sum
r.direction = "AD"
r.threadNUm = int(start)
r.x = i r.x = i
r.y = 0 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 sum = 0
for j := 0; j < len(R); j++ { for j := 0; j < len(R); j++ {
if i+j >= len(R) { 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 { if sum > r.sum {
r.sum = sum r.sum = sum
r.direction = "AD"
r.threadNUm = int(start)
r.x = len(R) r.x = len(R)
r.y = i 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 { func getAllSums(R *[m][m]int64) Res {
ch := make(chan Res) ch := make(chan Res)
var ret Res var ret Res
ret.sum = -1000000 ret.sum = -4000000
numThread := runtime.NumCPU() numThread := runtime.NumCPU()
for i := 0; i < numThread; i++ { for i := 0; i < numThread; i++ {
go getSumH(R, int64(i), int64(numThread), ch) go getSumH(R, int64(i), int64(numThread), ch)
} }
for i := 0; i < numThread; i++ { for i := 0; i < numThread; i++ {
r := <-ch r := <-ch
fmt.Printf("sum = %d\n", r.sum) printRes(r)
if r.sum > ret.sum { if r.sum > ret.sum {
ret = r ret = r
} }
go getSumV(R, int64(i), int64(numThread), ch) 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++ { for i := 0; i < numThread; i++ {
r := <-ch r := <-ch
printRes(r)
if r.sum > ret.sum { if r.sum > ret.sum {
ret = r ret = r
} }
go getSumD(R, int64(i), int64(numThread), ch) go getSumD(R, int64(i), int64(numThread), ch)
} }
fmt.Printf("largest vertical sum = %d\n", ret.sum)
for i := 0; i < numThread; i++ { for i := 0; i < numThread; i++ {
r := <-ch r := <-ch
printRes(r)
if r.sum > ret.sum { if r.sum > ret.sum {
ret = r ret = r
} }
go getSumAD(R, int64(i), int64(numThread), ch) go getSumAD(R, int64(i), int64(numThread), ch)
} }
fmt.Printf("largest diagonal sum = %d\n", ret.sum)
for i := 0; i < numThread; i++ { for i := 0; i < numThread; i++ {
r := <-ch r := <-ch
printRes(r)
if r.sum > ret.sum { if r.sum > ret.sum {
ret = r ret = r
} }
@ -191,16 +192,16 @@ func getAllSums(R *[m][m]int64) Res {
} }
func main() { 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) assignRandomNumbers(&random)
arrayToMatrix(&random, &R) arrayToMatrix(&random, &R)
fmt.Println(R) //fmt.Println(R)
sum := getAllSums(&R) sum := getAllSums(&R)
ch := make(chan Res)
go getSumH(&R, 0, 1, ch) fmt.Printf("Max sum is: %d at ", sum.sum)
hsum := <-ch printRes(sum)
printRes(hsum)
fmt.Printf("Max sum is: %d\n", sum.sum)
} }

Loading…
Cancel
Save