int main(int argc, char **argv){ int i, j; int x, y, size; int w_breeding, s_breeding, w_starvation, gen_num; char type_code; FILE * input_file; if(argc <= 5){ printf("ERROR: Expected 5 arguments provided %d.\n", argc); printf("Expected:\n./wolves-squirrels-serial <InputFile> <WolfBreedingPeriod> <SquirrelBreedingPeriod> <WolfStarvationPerior> <Generations>\n"); return -1; } omp_set_num_threads(4); w_breeding = atoi(argv[2]); s_breeding = atoi(argv[3]); w_starvation = atoi(argv[4]); gen_num = atoi(argv[5]); input_file = fopen(argv[1], "r"); if(input_file == NULL){ printf("ERROR: A valid input file is expected. %s is not a valid file.\n", argv[1]); return -1; } fscanf(input_file, "%d", &size); initWorld(size); while(fscanf(input_file, "%d %d %c", &x, &y, &type_code) != EOF){ world[x][y].type = type_code; if(world[x][y].type == wolf){ world[x][y].breeding_period = w_breeding; world[x][y].starvation_period = w_starvation; } else if(world[x][y].type == squirrel){ world[x][y].breeding_period = s_breeding; } } fclose(input_file); #ifdef VERBOSE start = omp_get_wtime(); #endif /* Generate */ while(gen_num != 0){ #pragma omp parallel sections { #pragma omp section { /* 1st sub-generation - RED */ #pragma omp parallel for private(i, j) schedule(guided, size/8) for(i=0; i<size; i++){ for(j = i%2 == 0 ? 0 : 1 ; j<size; j+=2){ computeCell(i, j, s_breeding, w_breeding, w_starvation, size); } } } #pragma omp section { /* 2nd sub-generation */ #pragma omp parallel for private(i, j) schedule(guided, size/8) for(i=0; i<size; i++){ for(j = i%2 == 0 ? 1 : 0 ; j<size; j+=2){ computeCell(i, j, s_breeding, w_breeding, w_starvation, size); } } } } fixWorld(size, w_starvation, w_breeding); gen_num--; } #ifdef VERBOSE end = omp_get_wtime(); printf("Elapsed time: %lf\n", end-start); #endif /* Output */ printWorldFormatted(size); return 0; }
int main(int argc, char **argv) { int i, j; int x, y, size; int w_breeding, s_breeding, w_starvation, gen_num; char type_code; FILE * input_file; w_number = 0; if(argc <= 5) { printf("ERROR: Expected 5 arguments provided %d.\n", argc); printf("Expected:\n./wolves-squirrels-serial <InputFile> <WolfBreedingPeriod> <SquirrelBreedingPeriod> <WolfStarvationPerior> <Generations>\n"); return -1; } w_breeding = atoi(argv[2]); s_breeding = atoi(argv[3]); w_starvation = atoi(argv[4]); gen_num = atoi(argv[5]); input_file = fopen(argv[1], "r"); if(input_file == NULL) { printf("ERROR: A valid input file is expected. %s is not a valid file.\n", argv[1]); return -1; } fscanf(input_file, "%d", &size); initWorld(size); while(fscanf(input_file, "%d %d %c", &x, &y, &type_code) != EOF) { world[0][x][y].type = type_code; world[1][x][y].type = type_code; if(world[0][x][y].type == wolf) { world[0][x][y].breeding_period = w_breeding; world[0][x][y].starvation_period = w_starvation; } else if(world[0][x][y].type == squirrel || world[0][x][y].type == squirrel_on_tree) { world[0][x][y].breeding_period = s_breeding; } } fclose(input_file); #ifdef VERBOSE printf("INITIAL WORLD - w_number = %d\n", w_number); printWorldDetailed(size); printf("\n"); #endif /* Generate */ while(gen_num != 0) { cleanWorld(size); /* 1st sub-generation - RED */ for(i=0; i<size; i++) { for(j = i%2 == 0 ? 0 : 1 ; j<size; j+=2) { computeCell(i, j, s_breeding, w_breeding, w_starvation, size); } } /* 2nd sub-generation */ for(i=0; i<size; i++) { for(j = i%2 == 0 ? 1 : 0 ; j<size; j+=2) { computeCell(i, j, s_breeding, w_breeding, w_starvation, size); } } #ifdef VERBOSE printf("\n\nIteration %d:\n", gen_num); printWorldDetailed(size); printf("\n"); #endif gen_num--; w_number = (w_number+1) % 2; } /* Output */ printWorldFormatted(size); return 0; }