gpt4 book ai didi

c - 使用 OpenMP 的生命游戏

转载 作者:太空宇宙 更新时间:2023-11-04 01:36:14 24 4
gpt4 key购买 nike

我制作了生命游戏的顺序版本,但现在我需要使用 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 个 for 并联,但我不知道该怎么做。

最佳答案

我觉得外层循环不能并行化,因为每一代的输入都是上一代的,所以它有一个顺序公式(至少你不能做小的改动!)

如果嵌套循环遍历矩阵或类似的东西,我更喜欢运行从 0ncol*nrow 的单个循环(在你的情况下)和从循环索引中找到 ij

像这样:

// because you are running a parallel codes multiple times in a loop,
// it would be better to make the thread swarm first and schedule the
// tasks in each loop iteration, to avoid multiple creation and destruction
// of working threads
#pragma omp parallel
for (curgen = 0; curgen < gens_max; curgen++)
{
#pragma omp for
for (t = 0; t < nrows*ncols; t++)
{
int i = t / ncols;
int j = t % ncols;
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 );
}

我在配备双核 2.53 GHz CPU 的笔记本电脑上运行此代码,在 1000x1000 矩阵上运行了 1000 代,速度提高了 69%。

关于c - 使用 OpenMP 的生命游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13846477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com