求助,为什么内存溢出了。我想并发执行,然后输出结果,但是报错

Dench · · 403 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。
``` // test project main.go package main import ( "fmt" "math/big" "os" "runtime" "sync" "time" ) var file, _ = os.Create("20-1.txt") var round = 1 var input = 3 var size = 1048576 var block = 8192 var s_box = [16][16]int{ {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, //0 {0, 0, 0, 4, 0, 0, 0, 4, 0, 4, 0, 0, 0, 4, 0, 0}, //1 {0, 0, 0, 2, 0, 4, 2, 0, 0, 0, 2, 0, 2, 2, 2, 0}, //2 {0, 2, 0, 2, 2, 0, 4, 2, 0, 0, 2, 2, 0, 0, 0, 0}, //3 {0, 0, 0, 0, 0, 4, 2, 2, 0, 2, 2, 0, 2, 0, 2, 0}, //4 {0, 2, 0, 0, 2, 0, 0, 0, 0, 2, 2, 2, 4, 2, 0, 0}, //5 {0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 0, 4, 2, 0, 0, 4}, //6 {0, 4, 2, 0, 0, 0, 2, 0, 2, 0, 0, 0, 2, 0, 0, 4}, //7 {0, 0, 0, 2, 0, 0, 0, 2, 0, 2, 0, 4, 0, 2, 0, 4}, //8 {0, 0, 2, 0, 4, 0, 2, 0, 2, 0, 0, 0, 2, 0, 4, 0}, //9 {0, 0, 2, 2, 0, 4, 0, 0, 2, 0, 2, 0, 0, 2, 2, 0}, //A {0, 2, 0, 0, 2, 0, 0, 0, 4, 2, 2, 2, 0, 2, 0, 0}, //B {0, 0, 2, 0, 0, 4, 0, 2, 2, 2, 2, 0, 0, 0, 2, 0}, //C {0, 2, 4, 2, 2, 0, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0}, //D {0, 0, 2, 2, 0, 0, 2, 2, 2, 2, 0, 0, 2, 2, 0, 0}, //E {0, 4, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4}} //F func SPN(a []big.Rat, b [][]big.Rat, base int) { for k := base * block; k < (base+1)*block; k++ { //fmt.Println(k) if a[k].Cmp(big.NewRat(0, 1)) > 0 { first := k / 65536 second := k / 4096 % 16 third := k / 256 % 16 fourth := k / 16 % 16 fifth := k % 16 for m := 0; m < 16; m++ { for n := 0; n < 16; n++ { for x := 0; x < 16; x++ { for y := 0; y < 16; y++ { for z := 0; z < 16; z++ { per := int64(s_box[first][m] * s_box[second][n] * s_box[third][x] * s_box[fourth][y] * s_box[fifth][z]) if per > 0 { temp := big.NewRat(per, 1048576) temp.Mul(temp, &a[k]) tt := m*65536 + n*4096 + x*256 + y*16 + z var pos [20]int for i := 0; i < 20; i++ { pos[i] = tt % 2 tt >>= 1 tt = 524288*pos[19] + 16384*pos[18] + 512*pos[17] + 16*pos[16] + 262144*pos[15] + 8192*pos[14] + 256*pos[13] + 8*pos[12] + 131072*pos[11] + 4096*pos[10] + 128*pos[9] + 4*pos[8] + 65536*pos[7] + 2048*pos[6] + 64*pos[5] + 2*pos[4] + 32768*pos[3] + 1024*pos[2] + 32*pos[1] + 1*pos[0] b[base][tt].Add(temp, &b[base][tt]) } } } } } } } } } } func main() { start := time.Now() runtime.GOMAXPROCS(128) var wg sync.WaitGroup var a = make([]big.Rat, size) var b = make([][]big.Rat, 128) var c = make([]big.Rat, size) var dot string for i := 0; i < 128; i++ { b[i] = make([]big.Rat, size) } a[input].SetFrac(big.NewInt(1), big.NewInt(1)) change := a[:] fmt.Printf("%v", a[input].String()) for i := 0; i < round; i++ { for j := 0; j < 128; j++ { //fmt.Printf("%d", j) wg.Add(1) go func(wg *sync.WaitGroup, j int) { //SPN(a, b[j], j) SPN(a, b, j) for i := block * j; i < block*(j+1); i++ { a[i].SetFrac(big.NewInt(0), big.NewInt(1)) } for i := 0; i < size; i++ { c[i].Add(&c[i], &b[j][i]) b[j][i].SetFrac(big.NewInt(0), big.NewInt(1)) } defer wg.Done() }(&wg, j) } wg.Wait() change = a a = c c = change /*for i := 0; i < 128; i++ { for j := 0; j < size; j++ { a[j].Add(&a[j], &b[i][j]) } } for i := 0; i < 128; i++ { for j := 0; j < size; j++ { b[i][j].SetFrac(big.NewInt(0), big.NewInt(1)) } }*/ } for i := 1; i < size; i++ { dot = a[i].FloatString(20) file.WriteString(dot + "\r\n") } elapsed := time.Since(start) fmt.Printf("\nTook %s\n", elapsed) } ``` 错误如下 ![image.png](https://static.studygolang.com/180921/5ead36255c5293d9c2a6520a3d7c4582.png)

入群交流(和以上内容无关):Go中文网 QQ 交流群:798786647 或加微信入微信群:274768166 备注:入群;关注公众号:Go语言中文网

403 次点击  
加入收藏 微博
5 回复  |  直到 2018-09-23 12:46:40
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传