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

时间:2022-04-01 07:08:50

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?

但它仍然没有并行计算。怎么了?

2 个解决方案

#1


1  

There's no need for func in a go statement (just write go funcName(arg1, arg2)).

go语句中不需要func(只需写入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吗?

#2


1  

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. That is, of course, assuming that you actually spawn four goroutines with the same arguments and nothing to distinguish them from each other.

要进行一些并行检查,您的goroutine需要查看输入数据的不同部分以及非常简短的代码草图,最多只运行四次相同的计算。也就是说,当然,假设您实际上生成了具有相同参数的四个goroutine,并且没有任何东西可以将它们彼此区分开来。

#1


1  

There's no need for func in a go statement (just write go funcName(arg1, arg2)).

go语句中不需要func(只需写入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吗?

#2


1  

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. That is, of course, assuming that you actually spawn four goroutines with the same arguments and nothing to distinguish them from each other.

要进行一些并行检查,您的goroutine需要查看输入数据的不同部分以及非常简短的代码草图,最多只运行四次相同的计算。也就是说,当然,假设您实际上生成了具有相同参数的四个goroutine,并且没有任何东西可以将它们彼此区分开来。