Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <omp.h>
- #include <time.h>
- int array1[1000][1000];
- int array2[10000][10000];
- /* add to a width index, wrapping around like a cylinder */
- int xadd (int i, int a) {
- i += a;
- while (i < 0) i += 1000;
- while (i >= 1000) i -= 1000;
- return i;
- }
- /* add to a height index, wrapping around */
- int yadd (int i, int a) {
- i += a;
- while (i < 0) i += 1000;
- while (i >= 1000) i -= 1000;
- return i;
- }
- int xadd2 (int i, int a) {
- i += a;
- while (i < 0) i += 10000;
- while (i >= 10000) i -= 10000;
- return i;
- }
- int yadd2 (int i, int a) {
- i += a;
- while (i < 0) i += 10000;
- while (i >= 10000) i -= 10000;
- return i;
- }
- int readFileDataset1(int gen, char *outputfile){
- FILE *fp = fopen("test.txt", "r");
- if(fp == NULL) {
- printf("error in opening file\n");
- return 0;
- }
- char c;
- int row = 0;
- int col = -1;
- while ((c = (char)fgetc(fp)) !=EOF){
- if(c == '#')
- continue;
- if(c == '|'){
- //auxisi stilis.
- col = col + 1;
- }
- else if(c == '*'){
- //zontanos
- array1[row][col] = 1;
- }
- else if(c == ' '){
- //nekros
- array1[row][col] = 0;
- }
- else if(c == '\n'){
- //auxisi grammis.
- row = row + 1;
- col = -1;
- }
- }
- int newboard[1000][1000];
- int i,j,m,a;
- int k, l, count;
- for(m = 0; m < gen; m++){
- #pragma omp parallel for shared(newboard, array1) private(i, j, a, k, l, count) num_threads(1)
- for (i=0; i<1000; i++) for (j=0; j<1000; j++) {
- count = 0;
- /* go around the cell */
- for (k=-1; k<=1; k++) for (l=-1; l<=1; l++)
- /* only count if at least one of k,l isn't zero */
- if (k || l)
- if (array1[xadd(i,k)][yadd(j,l)]) count++;
- if (count == 2) newboard[i][j] = array1[i][j];
- if (count == 3) newboard[i][j] = 1;
- if (count < 2) newboard[i][j] = 0;
- if (count > 3) newboard[i][j] = 0;
- }
- }
- FILE *fwr = fopen(outputfile, "w");
- for (j=0; j<1000; j++) {
- fprintf(fwr, "|");
- /* print each column position... */
- for (i=0; i<1000; i++) {
- if(newboard[j][i] == 0){
- fputs(" ", fwr);
- }
- else if(newboard[j][i] == 1){
- fputs("*", fwr);
- }
- fputs("|", fwr);
- }
- /* followed by a carriage return */
- fputs("\n", fwr);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int readFileDataset2(int gen, char *outputfile){
- FILE *fp = fopen("test.txt", "r");
- if(fp == NULL) {
- printf("error in opening file\n");
- return 0;
- }
- char c;
- int row = 0;
- int col = -1;
- while ((c = (char)fgetc(fp)) !=EOF){
- if(c == '#')
- continue;
- if(c == '|'){
- //auxisi stilis.
- col = col + 1;
- }
- else if(c == '*'){
- //zontanos
- array2[row][col] = 1;
- }
- else if(c == ' '){
- //nekros
- array2[row][col] = 0;
- }
- else if(c == '\n'){
- //auxisi grammis.
- row = row + 1;
- col = -1;
- }
- }
- //sto telos tis while tha exeis ton disdiastato pinaka kai 1 stin thesi opou einai zontanos, 0 opou einai nekros.
- int newboard[10000][10000];
- int i,j,m,a;
- int k, l, count;
- for(m = 0; m < gen; m++){
- #pragma omp parallel for shared(newboard, array2) private(i, j, a, k, l, count) num_threads(1)
- for (i=0; i<10000; i++) for (j=0; j<10000; j++) {
- count = 0;
- /* go around the cell */
- for (k=-1; k<=1; k++) for (l=-1; l<=1; l++)
- /* only count if at least one of k,l isn't zero */
- if (k || l)
- if (array2[xadd2(i,k)][yadd2(j,l)]) count++;
- if (count == 2) newboard[i][j] = array2[i][j];
- if (count == 3) newboard[i][j] = 1;
- if (count < 2) newboard[i][j] = 0;
- if (count > 3) newboard[i][j] = 0;
- }
- }
- FILE *fwr = fopen(outputfile, "w");
- for (j=0; j<10000; j++) {
- fprintf(fwr, "|");
- /* print each column position... */
- for (i=0; i<10000; i++) {
- if(newboard[j][i] == 0){
- fputs(" ", fwr);
- }
- else if(newboard[j][i] == 1){
- fputs("*", fwr);
- }
- fputs("|", fwr);
- }
- /* followed by a carriage return */
- fputs("\n", fwr);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- int main(int argc, char** argv){
- if(argc != 4){
- printf("No right arguments passed!");
- return 0;
- }
- if(!strcmp(argv[1],"grid1")){
- clock_t begin = clock();
- long arg = strtol(argv[2], NULL, 10);
- //kalesma sinartisis me arg ton aritho ton threads
- readFileDataset1(arg, argv[3]);
- clock_t end = clock();
- double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- printf("time to finish program: %lf\n", time_spent);
- }
- else if(!strcmp(argv[1],"grid2")){
- clock_t begin = clock();
- long arg = strtol(argv[2], NULL, 10);
- //kalesma sinartisis me arg ton aritho ton threads
- readFileDataset2(arg, argv[3]);
- clock_t end = clock();
- double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
- printf("time to finish program: %lf\n", time_spent);
- }
- else {
- printf("wrong input grid\n");
- return 0;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement