简体   繁体   English

将切片传递给用于执行常规的功能

[英]Passing slice to function used for go routine

I have the following function which takes as parameters two slices of two dimensional arrays of ints (where coreCount is amount of cores available) 我有以下函数作为参数两片二维int数组(其中coreCount是可用的核心数量)

func calculate(slice_1 [][array_size][array_size]int, slice_2 []  [array_size[array_size]int, coreCount int) {
//for each (coreCount*k + i, i = 0, ... , coreCount) matrix from slice_1 take matrix from slice_2 and check some criteria while not changing matrix under check
}

Slices are quite big in size (thousands of two dimensional arrays) so it's good idea to do the checking in parallel. 切片的大小非常大(数千个二维数组),因此最好并行进行检查。 So I simply create (in case of 4 cores compurer) 所以我简单地创建(如果是4核压缩器)

go calculate(slice_1 , slice_2, 4)
go calculate(slice_1 , slice_2, 4)
go calculate(slice_1 , slice_2, 4)
go calculate(slice_1 , slice_2, 4)

But it still calculates not in parallel. 但它仍然没有并行计算。 What's wrong? 怎么了?

There's no need for func in a go statement (just write go funcName(arg1, arg2) ). go语句中不需要func (只需写入go funcName(arg1, arg2) )。

Also, we'll need the whole program (or at least a simplified, working version) to be able to help you. 此外,我们需要整个程序(或至少一个简化的工作版本)才能帮助您。 Have you set GOMAXPROCS? 你有GOMAXPROCS吗?

To do some checking in parallel, your goroutines would need to look at different parts of the input data and from your very brief code sketch, at best you'd run the same calculation four times. 要进行一些并行检查,您的goroutine需要查看输入数据的不同部分以及非常简短的代码草图,最多只运行四次相同的计算。 That is, of course, assuming that you actually spawn four goroutines with the same arguments and nothing to distinguish them from each other. 也就是说,假设您实际上生成了具有相同参数的四个goroutine,并且没有任何东西可以将它们彼此区分开来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM