Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5. #include <omp.h>
  6. #include <time.h>
  7.  
  8. int array1[1000][1000];
  9. int array2[10000][10000];
  10.  
  11. /* add to a width index, wrapping around like a cylinder */
  12.  
  13. int xadd (int i, int a) {
  14. i += a;
  15. while (i < 0) i += 1000;
  16. while (i >= 1000) i -= 1000;
  17. return i;
  18. }
  19.  
  20. /* add to a height index, wrapping around */
  21.  
  22. int yadd (int i, int a) {
  23. i += a;
  24. while (i < 0) i += 1000;
  25. while (i >= 1000) i -= 1000;
  26. return i;
  27. }
  28.  
  29. int xadd2 (int i, int a) {
  30. i += a;
  31. while (i < 0) i += 10000;
  32. while (i >= 10000) i -= 10000;
  33. return i;
  34. }
  35.  
  36. int yadd2 (int i, int a) {
  37. i += a;
  38. while (i < 0) i += 10000;
  39. while (i >= 10000) i -= 10000;
  40. return i;
  41. }
  42.  
  43. int readFileDataset1(int gen, char *outputfile){
  44.  
  45. FILE *fp = fopen("test.txt", "r");
  46. if(fp == NULL) {
  47. printf("error in opening file\n");
  48. return 0;
  49. }
  50.  
  51. char c;
  52. int row = 0;
  53. int col = -1;
  54.  
  55. while ((c = (char)fgetc(fp)) !=EOF){
  56. if(c == '#')
  57. continue;
  58.  
  59. if(c == '|'){
  60. //auxisi stilis.
  61. col = col + 1;
  62. }
  63. else if(c == '*'){
  64. //zontanos
  65. array1[row][col] = 1;
  66. }
  67. else if(c == ' '){
  68. //nekros
  69. array1[row][col] = 0;
  70. }
  71. else if(c == '\n'){
  72. //auxisi grammis.
  73. row = row + 1;
  74. col = -1;
  75. }
  76. }
  77.  
  78. int newboard[1000][1000];
  79. int i,j,m,a;
  80. int k, l, count;
  81.  
  82. for(m = 0; m < gen; m++){
  83. #pragma omp parallel for shared(newboard, array1) private(i, j, a, k, l, count) num_threads(1)
  84. for (i=0; i<1000; i++) for (j=0; j<1000; j++) {
  85.  
  86. count = 0;
  87. /* go around the cell */
  88. for (k=-1; k<=1; k++) for (l=-1; l<=1; l++)
  89. /* only count if at least one of k,l isn't zero */
  90. if (k || l)
  91. if (array1[xadd(i,k)][yadd(j,l)]) count++;
  92.  
  93. if (count == 2) newboard[i][j] = array1[i][j];
  94. if (count == 3) newboard[i][j] = 1;
  95. if (count < 2) newboard[i][j] = 0;
  96. if (count > 3) newboard[i][j] = 0;
  97. }
  98. }
  99.  
  100. FILE *fwr = fopen(outputfile, "w");
  101.  
  102. for (j=0; j<1000; j++) {
  103. fprintf(fwr, "|");
  104. /* print each column position... */
  105. for (i=0; i<1000; i++) {
  106. if(newboard[j][i] == 0){
  107. fputs(" ", fwr);
  108. }
  109. else if(newboard[j][i] == 1){
  110. fputs("*", fwr);
  111. }
  112. fputs("|", fwr);
  113. }
  114. /* followed by a carriage return */
  115. fputs("\n", fwr);
  116. }
  117.  
  118.  
  119. }
  120.  
  121. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  122. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  123.  
  124. int readFileDataset2(int gen, char *outputfile){
  125.  
  126. FILE *fp = fopen("test.txt", "r");
  127. if(fp == NULL) {
  128. printf("error in opening file\n");
  129. return 0;
  130. }
  131.  
  132. char c;
  133. int row = 0;
  134. int col = -1;
  135.  
  136. while ((c = (char)fgetc(fp)) !=EOF){
  137. if(c == '#')
  138. continue;
  139.  
  140. if(c == '|'){
  141. //auxisi stilis.
  142. col = col + 1;
  143. }
  144. else if(c == '*'){
  145. //zontanos
  146. array2[row][col] = 1;
  147. }
  148. else if(c == ' '){
  149. //nekros
  150. array2[row][col] = 0;
  151. }
  152. else if(c == '\n'){
  153. //auxisi grammis.
  154. row = row + 1;
  155. col = -1;
  156. }
  157. }
  158. //sto telos tis while tha exeis ton disdiastato pinaka kai 1 stin thesi opou einai zontanos, 0 opou einai nekros.
  159.  
  160. int newboard[10000][10000];
  161. int i,j,m,a;
  162. int k, l, count;
  163.  
  164. for(m = 0; m < gen; m++){
  165. #pragma omp parallel for shared(newboard, array2) private(i, j, a, k, l, count) num_threads(1)
  166. for (i=0; i<10000; i++) for (j=0; j<10000; j++) {
  167.  
  168. count = 0;
  169. /* go around the cell */
  170. for (k=-1; k<=1; k++) for (l=-1; l<=1; l++)
  171. /* only count if at least one of k,l isn't zero */
  172. if (k || l)
  173. if (array2[xadd2(i,k)][yadd2(j,l)]) count++;
  174.  
  175. if (count == 2) newboard[i][j] = array2[i][j];
  176. if (count == 3) newboard[i][j] = 1;
  177. if (count < 2) newboard[i][j] = 0;
  178. if (count > 3) newboard[i][j] = 0;
  179. }
  180. }
  181.  
  182. FILE *fwr = fopen(outputfile, "w");
  183.  
  184. for (j=0; j<10000; j++) {
  185. fprintf(fwr, "|");
  186. /* print each column position... */
  187. for (i=0; i<10000; i++) {
  188. if(newboard[j][i] == 0){
  189. fputs(" ", fwr);
  190. }
  191. else if(newboard[j][i] == 1){
  192. fputs("*", fwr);
  193. }
  194. fputs("|", fwr);
  195. }
  196. /* followed by a carriage return */
  197. fputs("\n", fwr);
  198. }
  199.  
  200. }
  201.  
  202. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  204.  
  205. int main(int argc, char** argv){
  206.  
  207. if(argc != 4){
  208. printf("No right arguments passed!");
  209. return 0;
  210. }
  211.  
  212. if(!strcmp(argv[1],"grid1")){
  213. clock_t begin = clock();
  214. long arg = strtol(argv[2], NULL, 10);
  215. //kalesma sinartisis me arg ton aritho ton threads
  216. readFileDataset1(arg, argv[3]);
  217. clock_t end = clock();
  218.  
  219. double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  220.  
  221. printf("time to finish program: %lf\n", time_spent);
  222. }
  223. else if(!strcmp(argv[1],"grid2")){
  224. clock_t begin = clock();
  225. long arg = strtol(argv[2], NULL, 10);
  226. //kalesma sinartisis me arg ton aritho ton threads
  227. readFileDataset2(arg, argv[3]);
  228. clock_t end = clock();
  229.  
  230. double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  231.  
  232. printf("time to finish program: %lf\n", time_spent);
  233.  
  234. }
  235. else {
  236. printf("wrong input grid\n");
  237. return 0;
  238. }
  239.  
  240. return 0;
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement