在 Stackoverflow.com 上使用以前代码中的代码的问题 - C 中的 OpenMp 生命游戏
Issues from using code from a previous code here on Stackoverflow.com - Game of life in C with OpenMp
我试图在我的 visual studio IDE 中复制此 post 中的代码,但我遇到了一些错误,即使我没有更改代码的任何部分。代码来自这个post - Game of Life with OpenMP
这是来自 post -
的代码
// 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;
}
我收到以下错误 -
Errors
char* temp = b1 `--> expression must have a constant value`
b1 = b2; --> `expression must have a constant value`
b2 = temp; --> `expression must have a constant value`
此外,代码的最后一行显示错误
“无法识别的令牌”:
SWAP_BOARDS( outgrid, ingrid ); --> `unrecognized token`
请问我该如何解决这些问题?感谢您的时间和帮助。
在这里,我在评论中回答你的问题。 Game of Life 的棋盘应该是一个无限的、二维的正交正方形网格。通过连接棋盘的边缘可以'approximated'无穷大。为此,mod
函数应定义为模函数。
int mod(int x, int y){return x%y;}
- 任何有两个或三个活邻居的活细胞都会存活下来。
- 任何有三个活邻居的死细胞都会成为活细胞。
- 所有其他活细胞在下一代死亡。同样,所有其他死亡细胞保持死亡状态。
函数alivep
就是上述规则的定义。有 2 个输入参数:存活邻居的数量和细胞状态(存活或死亡)。 return值是cell在下一个周期的状态。假设 0
表示死了,1
表示活着,下面是一个如何实现它的例子:
char alivep(int neighbours, char alive){
if (alive && (neighbours==2 || neighbours==3)) return 1;
else {
if (neighbours==3) return 1; else return 0;
}
}
放在一起,定义main
函数,添加一个print_board
函数来显示棋盘,下面是一个例子:
#include <string.h>
#include <stdio.h>
int mod(int x, int y){return x%y;}
char alivep(int neighbours, char alive){
if (alive && (neighbours==2 || neighbours==3)) return 1;
else {
if (neighbours==3) return 1; else return 0;
}
}
// Swapping the two grids
#define SWAP_BOARDS( b1, b2 ) { \
char* temp = b1; \
b1 = b2; \
b2 = temp; \
}
// Simplifying access to grid elements
#define BOARD( G, X, Y ) ((G)[ncols*(X)+(Y)])
char* sequential_game_of_life (char* outgrid, char* ingrid,
const int nrows, const int ncols, const int gens_max) {
for (int curgen = 0; curgen < gens_max; curgen++){
for (int i = 0; i < nrows; i++){
for (int 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;
}
void print_board(char* ingrid, int nrows, int ncols)
{
for(int i=0;i<nrows;++i)
{
for(int j=0;j<ncols;++j)
{
printf("%c", BOARD(ingrid,i,j)==0 ? '.' : '*');
}
printf("\n");
}
}
int main(){
const int NR=15;
const int NC=15;
const int GENS_MAX=20;
char ingrid[NR*NC];
char outgrid[NR*NC];
memset(ingrid,0,NR*NC*sizeof(ingrid[0]));
//draw an example pattern
ingrid[5*NC+5]=1;
ingrid[5*NC+6]=1;
ingrid[5*NC+7]=1;
ingrid[5*NC+8]=1;
ingrid[6*NC+5]=1;
ingrid[7*NC+5]=1;
ingrid[8*NC+5]=1;
ingrid[9*NC+5]=1;
printf("Initial board\n");
print_board(ingrid, NR, NC);
char* out=sequential_game_of_life (outgrid, ingrid, NR, NC, GENS_MAX);
printf("Final board\n");
print_board(out, NR, NC);
}
我试图在我的 visual studio IDE 中复制此 post 中的代码,但我遇到了一些错误,即使我没有更改代码的任何部分。代码来自这个post - Game of Life with OpenMP
这是来自 post -
的代码// 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;
}
我收到以下错误 -
Errors
char* temp = b1 `--> expression must have a constant value`
b1 = b2; --> `expression must have a constant value`
b2 = temp; --> `expression must have a constant value`
此外,代码的最后一行显示错误 “无法识别的令牌”:
SWAP_BOARDS( outgrid, ingrid ); --> `unrecognized token`
请问我该如何解决这些问题?感谢您的时间和帮助。
在这里,我在评论中回答你的问题。 Game of Life 的棋盘应该是一个无限的、二维的正交正方形网格。通过连接棋盘的边缘可以'approximated'无穷大。为此,mod
函数应定义为模函数。
int mod(int x, int y){return x%y;}
- 任何有两个或三个活邻居的活细胞都会存活下来。
- 任何有三个活邻居的死细胞都会成为活细胞。
- 所有其他活细胞在下一代死亡。同样,所有其他死亡细胞保持死亡状态。
函数alivep
就是上述规则的定义。有 2 个输入参数:存活邻居的数量和细胞状态(存活或死亡)。 return值是cell在下一个周期的状态。假设 0
表示死了,1
表示活着,下面是一个如何实现它的例子:
char alivep(int neighbours, char alive){
if (alive && (neighbours==2 || neighbours==3)) return 1;
else {
if (neighbours==3) return 1; else return 0;
}
}
放在一起,定义main
函数,添加一个print_board
函数来显示棋盘,下面是一个例子:
#include <string.h>
#include <stdio.h>
int mod(int x, int y){return x%y;}
char alivep(int neighbours, char alive){
if (alive && (neighbours==2 || neighbours==3)) return 1;
else {
if (neighbours==3) return 1; else return 0;
}
}
// Swapping the two grids
#define SWAP_BOARDS( b1, b2 ) { \
char* temp = b1; \
b1 = b2; \
b2 = temp; \
}
// Simplifying access to grid elements
#define BOARD( G, X, Y ) ((G)[ncols*(X)+(Y)])
char* sequential_game_of_life (char* outgrid, char* ingrid,
const int nrows, const int ncols, const int gens_max) {
for (int curgen = 0; curgen < gens_max; curgen++){
for (int i = 0; i < nrows; i++){
for (int 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;
}
void print_board(char* ingrid, int nrows, int ncols)
{
for(int i=0;i<nrows;++i)
{
for(int j=0;j<ncols;++j)
{
printf("%c", BOARD(ingrid,i,j)==0 ? '.' : '*');
}
printf("\n");
}
}
int main(){
const int NR=15;
const int NC=15;
const int GENS_MAX=20;
char ingrid[NR*NC];
char outgrid[NR*NC];
memset(ingrid,0,NR*NC*sizeof(ingrid[0]));
//draw an example pattern
ingrid[5*NC+5]=1;
ingrid[5*NC+6]=1;
ingrid[5*NC+7]=1;
ingrid[5*NC+8]=1;
ingrid[6*NC+5]=1;
ingrid[7*NC+5]=1;
ingrid[8*NC+5]=1;
ingrid[9*NC+5]=1;
printf("Initial board\n");
print_board(ingrid, NR, NC);
char* out=sequential_game_of_life (outgrid, ingrid, NR, NC, GENS_MAX);
printf("Final board\n");
print_board(out, NR, NC);
}