我做了一个生命游戏的顺序版本,但现在我需要使用 OpenMP 制作我的代码的并行版本,但我遇到了一些问题。如果有人可以帮助我,那就太好了。谢谢。这是我的顺序代码:
// Swapping the two grids
#define SWAP_BOARDS( b1, b2 ) do { \
char* temp = b1; \
b1 = b2; \
b2 = temp; \
} while(0)
// Simplifying access to grid elements
#define BOARD( G, X, Y ) ((G)[NC*(X)+(Y)])
char* sequential_game_of_life (char* outgrid, char* ingrid,
const int nrows, const int ncols, const int gens_max) {
const int NC = ncols;
int curgen, i, j;
for (curgen = 0; curgen < gens_max; curgen++)
{
for (i = 0; i < nrows; i++)
{
for (j = 0; j < ncols; j++)
{
const int inorth = mod (i-1, nrows);
const int isouth = mod (i+1, nrows);
const int jwest = mod (j-1, ncols);
const int jeast = mod (j+1, ncols);
const char neighbor_count =
BOARD (ingrid, inorth, jwest) +
BOARD (ingrid, inorth, j) +
BOARD (ingrid, inorth, jeast) +
BOARD (ingrid, i, jwest) +
BOARD (ingrid, i, jeast) +
BOARD (ingrid, isouth, jwest) +
BOARD (ingrid, isouth, j) +
BOARD (ingrid, isouth, jeast);
BOARD(outgrid, i, j) = alivep (neighbor_count, BOARD (ingrid, i, j));
}
}
SWAP_BOARDS( outgrid, ingrid );
}
return outgrid;
}
我知道我必须平行这 3 个,但我不知道该怎么做。