简体   繁体   中英

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.
Just 2 extra moves makes a real difference.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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