void gameOfLife(char *ruleString, int n, char oldField[][n], char newField[][n], char topology) { // check if ruleString is correct int aliveLength = 0, deadLength = 0, afterSlash = 0; while (*ruleString != '\0') { if (*ruleString != '/') { if (afterSlash) { deadLength++; } else { aliveLength++; } } else { afterSlash++; } ruleString++; } if (afterSlash != 1 || aliveLength > 9 || deadLength > 9) { // ERROR-Exception printf("ERROR: Rule-input should be of the form \"xxx/yyy.\n"); return; } else { ruleString -= aliveLength+deadLength+1; // find rule, e.g. "23/2" means: // living cell with 2 or 3 neighbours stays alive, otherwise dies // dead cell with 3 neighbours becomes alive, otherwise stays dead int ruleAlive[aliveLength], ruleDead[deadLength]; int alive = 0, dead = 0; afterSlash = 0; for (int m=0; m<aliveLength+deadLength+1; m++) { if (ruleString[m] != '/') { if (afterSlash) { ruleDead[dead] = charToInt(ruleString[m]); dead++; } else { ruleAlive[alive] = charToInt(ruleString[m]); alive++; } } else { afterSlash++; } } // e.g.: ruleAlive = [2 3], ruleDead = [3]; aliveLength = 2, deadLength = 1 (length of the rules) // conpute newField int livingCells; for (int row=0; row<n; row++) { for (int column=0; column<n; column++) { livingCells = livingCellsInNeighbourhood(row,column,n,oldField,topology); //printf("living cells in [%d,%d]: %d\n",row,column,livingCells); if (deadOrAlive(livingCells, ruleDead, deadLength, ruleAlive, aliveLength, oldField[row][column])) { newField[row][column] = '1'; } else { newField[row][column] = '0'; } } } } // } closes the else-part return; }
int main(int argc, char *argv[]) { /* initialize */ uint8_t *field_n; // field in next step uint8_t *field; // field in this step uint8_t *field_p1; // field in previous step uint8_t *field_p2; // field in previous previous step field_n = (uint8_t *)calloc(NC*(NY+2), sizeof(uint8_t)); field = (uint8_t *)calloc(NC*(NY+2), sizeof(uint8_t)); field_p1 = (uint8_t *)calloc(NC*(NY+2), sizeof(uint8_t)); field_p2 = (uint8_t *)calloc(NC*(NY+2), sizeof(uint8_t)); struct st_field sf; #if DEAD_OR_ALIVE == 1 pthread_t th_doa; uint8_t *doa; // dead or alive doa = (uint8_t *)calloc(NC*(NY+2), sizeof(uint8_t)); #if PRINT_PBM == 2 sf.field = doa; sf.nt = 0; sf.str = "da"; pthread_create(&th_doa, NULL, printPBM, (void *)&sf); #endif #endif time_t start = clock(); initialize(field); #if PRINT_PBM >= 1 sf.field = field; sf.nt = 0; sf.str = "bw"; pthread_t th_pbm; pthread_create(&th_pbm, NULL, printPBM, (void *)&sf); #endif /* main loop */ #pragma unroll for (int nt = 1; nt <= EXEC_STEP; nt++) { if (!(nt % 100)) { fprintf(stderr, "nt= %06d\n", nt); } setBound1(field); int black = developField(field, field_n); //swap uint8_t *tmp = field_p2; field_p2 = field_p1; field_p1 = field; field = field_n; field_n = tmp; setBound2(field); #if PRINT_PBM >= 1 sf.field = field; sf.nt = nt; sf.str = "bw"; pthread_join(th_pbm, NULL); pthread_create(&th_pbm, NULL, printPBM, (void *)&sf); #endif #if DEAD_OR_ALIVE == 1 int alive = deadOrAlive(field, field_p1, field_p2, doa); setBound2(doa); fprintf(stdout, "%d %d %d", nt, black, alive); #if PRINT_PBM == 2 sf.field = doa; sf.nt = nt; sf.str = "da"; pthread_join(th_doa, NULL); pthread_create(&th_doa, NULL, printPBM, (void *)&sf); #endif #endif fprintf(stdout, "\n"); #if BOX_COUNT > 0 if (nt % BOX_COUNT == 0) { int *cnt_b = boxCount(field); int *cnt_a = NULL; #if DEAD_OR_ALIVE > 0 cnt_a = boxCount(doa); #endif printBoxCount(cnt_b, cnt_a, nt); free(cnt_b); free(cnt_a); } #endif } fprintf(stderr, "TIME: %ld [ms]\n", (clock() - start)*1000/CLOCKS_PER_SEC); #if PRINT_PBM >= 1 pthread_join(th_pbm, NULL); #endif #if DEAD_OR_ALIVE == 1 && PRINT_PBM == 2 pthread_join(th_doa, NULL); #endif free(field_n); free(field); free(field_p1); free(field_p2); return EXIT_SUCCESS; }