简体   繁体   English

C++ 并行性。 在二维数组上实现并行循环

[英]C++ parallelism. Implementing parallelism looping on a 2D array

Ok, alphaBetaMiniMax is recursive and loops over a 2d board This is a bit of a simplification here, but any improvement in execution time is a big win in terms of how many moves ahead it can see.好的,alphaBetaMiniMax 是递归的并在 2d 板上循环 这在这里有点简化,但是执行时间的任何改进对于它可以看到的前进步数来说都是一个巨大的胜利。
Just 2 extra moves makes a real difference.只需 2 个额外的动作就可以产生真正的不同。

The problem.问题。 The 2D array itself represents a checkered board and needs to be optimized to be parallel.二维阵列本身代表一个方格板,需要优化为平行。 I am aware of several options for splitting the outer for loop up, but the interior for loop is complex and may need to remain a vanilla for.我知道有几种用于拆分外部 for 循环的选项,但内部 for 循环很复杂,可能需要保持原版 for。

foo() {
  ....
  for (i=0; i<8; i++) {
    for (j=calibrate(i); j<8; j=j+2) {
      // to account for checkerboard pattern, 
      // calibrate offsets board color where applicable
      makeMove(i,j)
      foo();
    }
  }

This is an oversimplification and I (think I) have the code structured and set up to avoid data races and deadlocks.这是一种过度简化,我(认为我)对代码进行了结构化和设置,以避免数据竞争和死锁。 That is omitted for here.此处省略。

My problem is what to do with the outer loop.我的问题是如何处理外循环。

for_each has parallel execution options, but I don't know if it will work with the interior for, plus I need each thread to know what row (what i) it is on, or who knows what happens to each board. for_each 有并行执行选项,但我不知道它是否适用于内部,另外我需要每个线程知道它在哪一行(我是什么),或者谁知道每个板会发生什么。 I have theories on how to accomplish that, but they're just that.我有关于如何实现这一目标的理论,但仅此而已。

I just need a nudge in the right direction.我只需要朝着正确的方向轻推。

This header-only library might help you.这个只有头文件的库可能会帮助你。 It takes care of thread management on your behalf.它代表您处理线程管理。 If each row i is independent from other rows, you can maybe wrap the inner loop as a function, let the library run it in parallel for each row and collect the results as shown in examples in Github.如果每一行i都独立于其他行,您可以将内部循环包装为一个函数,让库为每一行并行运行它并收集结果,如 Github 中的示例所示。

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

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