Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stddef.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <math.h>
  8. #include <ctype.h>
  9. #include <complex.h>
  10.  
  11. int lines, n_threads, degree, *block_limit;
  12. char **attractor;
  13. char **convergence;
  14. char *color[10];
  15. char *it_color[99];
  16. double _Complex *root;
  17. int max_it = 98;
  18. int *done;
  19.  
  20.  
  21. //methods called in newton
  22.  
  23. static inline double _Complex compute_power(double _Complex z0, int power){
  24. double _Complex tmp = z0;
  25. for(int i=1; i<power; i++){
  26. z0 *=tmp;
  27. }
  28. return z0;
  29. }
  30.  
  31. static inline double _Complex newton_step(double _Complex x_loc){
  32.  
  33. switch(degree){
  34.  
  35. case 1:
  36. x_loc = 1;
  37. break;
  38. case 2:
  39. x_loc = (x_loc+1/x_loc)/2;
  40. break;
  41.  
  42. default:
  43. x_loc = x_loc - x_loc/degree + 1/(degree *compute_power(x_loc,degree-1));
  44. }
  45. return x_loc;
  46. }
  47.  
  48.  
  49. void* write(){
  50.  
  51. char str1[26], str2[27];
  52. sprintf(str1, "newton_attractors_x%i.ppm", degree);
  53. sprintf(str2, "newton_convergence_x%i.ppm", degree);
  54.  
  55. FILE * fp1 = fopen (str1,"w");
  56. FILE * fp2 = fopen (str2,"w");
  57.  
  58. char form[] = "P3 \n";
  59. char *dim = (char*)malloc(lines*2+2);
  60. sprintf(dim,"%d %d \n", lines, lines);
  61. char *numcol = (char*)malloc(4);
  62. sprintf(numcol,"%d \n", 9);
  63.  
  64. fwrite(form, 1, strlen(form), fp1);
  65. fwrite(dim, 1, strlen(dim), fp1);
  66. fwrite(numcol, 1, strlen(numcol), fp1);
  67.  
  68. char *maxcolor = (char*)malloc(5);
  69. sprintf(maxcolor,"%d \n", max_it);
  70.  
  71. fwrite(form, 1, strlen(form), fp2);
  72. fwrite(dim, 1, strlen(dim), fp2);
  73. fwrite(maxcolor, 1, strlen(maxcolor), fp2);
  74.  
  75. struct timespec tim, tim2;
  76. tim.tv_sec = 0;
  77. tim.tv_nsec = 100;
  78.  
  79. for(int ix=0; ix<lines*lines; ix++){
  80. while(done[ix]==0){
  81. nanosleep(&tim,&tim2);
  82. }
  83. fwrite(attractor[ix], sizeof(attractor[ix])-2 ,1, fp1);
  84. fwrite(convergence[ix],sizeof(convergence[ix])+1,1, fp2);
  85. }
  86.  
  87. free(dim);
  88. free(numcol);
  89. free(maxcolor);
  90. fclose(fp1);
  91. fclose(fp2);
  92.  
  93. return NULL;
  94. }
  95.  
  96. void* newton(void *restrict arg) {
  97. int x = ((int*)arg)[0];
  98. free(arg);
  99.  
  100. int ix, dv;
  101.  
  102. if(degree == 1){
  103. for (int i=0; i<lines; i++){
  104. for(int j=0; j<lines; j++){
  105. attractor[i*lines+j]="2 2 2 ";
  106. convergence[i*lines+j]= "01 01 01 ";
  107. done[i*lines+j]=1;
  108. }}
  109. }
  110. else{
  111. int low_block_limit=block_limit[x]; int up_block_limit=block_limit[x+1];
  112. double _Complex x_loc, x_tmp;
  113. double tolerance = 0.000001;
  114. double max = 10000000000;
  115. int max_it_loc = max_it;
  116. double increment = 4.0/(lines-1);
  117.  
  118.  
  119. for (int i=low_block_limit; i<up_block_limit; i++){
  120. for(int j=0; j<lines; j++){
  121. x_loc = -2 + j*increment + 2*I - i*increment*I;
  122.  
  123. for (ix=1, dv= 0; ; ix++){
  124. x_loc=newton_step(x_loc);
  125. if (creal(x_loc)*creal(x_loc)+cimag(x_loc)*cimag(x_loc) <tolerance || creal(x_loc) > max || creal(x_loc) < -max ||cimag(x_loc) > max || cimag(x_loc) < -max){
  126. attractor[i*lines+j] = color[9];
  127. convergence[i*lines+j] = it_color[ix];
  128. done[i*lines+j]=1;
  129. break;
  130. }
  131. if (ix > max_it_loc){
  132. attractor[i*lines+j] = color[9];
  133. convergence[i*lines+j] = it_color[max_it_loc];
  134. done[i*lines+j]=1;
  135. break;
  136. }
  137.  
  138. for (int jx=0; jx<degree; jx++){
  139. x_tmp = x_loc-root[jx];
  140. if(creal(x_tmp)*creal(x_tmp)+cimag(x_tmp)*cimag(x_tmp)<tolerance){
  141. dv=1;
  142. attractor[i*lines+j] = color[jx];
  143. convergence[i*lines+j] = it_color[ix];
  144. done[i*lines+j]=1;
  145. break;
  146. }
  147. }
  148. if (dv !=0){
  149. break;
  150. }
  151. }
  152. }
  153.  
  154. }}
  155. return NULL;
  156. }
  157.  
  158. int main(int argc, char *argv[]){
  159.  
  160. //assigning comands line arguments to varibles
  161. char tmp1[10], tmp2[10];
  162.  
  163. if (argc != 4 || argv[1][0] != '-' || argv[2][0] !='-'){
  164. printf("Invalid of arguments \n");
  165. return 0;
  166. }
  167. else if (argv[1][1] == 'l' && argv[2][1] == 't' ) {
  168. memset(tmp1, 0, sizeof(tmp1));
  169. memcpy(tmp1, &argv[1][2], strlen(argv[1]) - 2);
  170. memset(tmp2, 0, sizeof(tmp2));
  171. memcpy(tmp2, &argv[2][2], strlen(argv[2]) - 2);
  172. }
  173. else if (argv[1][1] == 't' && argv[2][1] == 'l' ) {
  174. memset(tmp2, 0, sizeof(tmp2));
  175. memcpy(tmp2, &argv[1][2], strlen(argv[1]) - 2);
  176. memset(tmp1, 0, sizeof(tmp1));
  177. memcpy(tmp1, &argv[2][2], strlen(argv[2]) - 2);
  178. }
  179.  
  180. lines = (size_t) strtol(tmp1, NULL, 10);
  181. n_threads = (size_t) strtol(tmp2, NULL, 10);
  182. degree = (size_t) strtol(argv[3], NULL, 10);
  183. int *block_size = (int*)malloc(sizeof(int)*(n_threads+1));
  184. block_limit = (int*)malloc(sizeof(int)*(n_threads+1));
  185.  
  186. //creating blocks
  187.  
  188. int even = lines%n_threads;
  189. int block = lines/n_threads;
  190. int ix, jx;
  191.  
  192. for (ix =0; ix<n_threads+1; ix++){
  193. block_limit[ix] =block*ix;
  194. }
  195.  
  196. if (even != 0){
  197. for (ix=1; ix<even+1; ix++){
  198. block_limit[ix] +=ix;
  199. }
  200. for (; ix<n_threads+1; ix++){
  201. block_limit[ix] += even;
  202. }}
  203.  
  204. done=(int*)malloc(sizeof(int)*lines*lines);
  205. for(int i=0; i<lines*lines; i++){
  206. done[i]=0;
  207. }
  208.  
  209. // defining strings for colors
  210. color[0]="2 5 0 ";
  211. color[1]="3 0 0 ";
  212. color[2]="2 0 3 ";
  213. color[3]="2 4 7 ";
  214. color[4]="5 7 3 ";
  215. color[5]="2 5 5 ";
  216. color[6]="3 4 5 ";
  217. color[7]="0 8 0 ";
  218. color[8]="7 6 4 ";
  219. color[9]="2 2 2 ";
  220.  
  221. char tmp[99][11];
  222. for(int i=0; i<99; i++){
  223. sprintf(tmp[i], "%d%d %d%d %d%d ", i/10, i%10, i/10, i%10, i/10, i%10);
  224. it_color[i] =tmp[i];
  225. }
  226. //initializing matrices
  227.  
  228.  
  229. attractor = (char**)malloc(sizeof(char*)*lines*lines);
  230. convergence = (char**)malloc(sizeof(char*)*lines*lines);
  231.  
  232. //defining true roots
  233. root = (double _Complex*)malloc(sizeof(double _Complex)*degree);
  234. double tmproot;
  235. for (int ix = 0; ix < degree; ix ++) {
  236. tmproot = ix * 2 * M_PI / degree;
  237. root[ix] = cos(tmproot) + sin(tmproot) * I;
  238. }
  239.  
  240. //creating threads
  241.  
  242. size_t tx;
  243. int ret;
  244. pthread_t threads[n_threads];
  245.  
  246. for (tx=0; tx < n_threads; tx++) {
  247. double ** arg = malloc(sizeof(tx));
  248. arg[0] = (double*)tx;
  249. if ((ret = pthread_create(threads+tx, NULL, newton, (void*)arg))) {
  250. printf("Error creating thread: %d\n", ret);
  251. exit(1);
  252. }}
  253.  
  254. if ((ret = pthread_create(threads+n_threads, NULL, write, NULL))) {
  255. printf("Error creating thread: %d\n", ret);
  256. exit(1);
  257. }
  258.  
  259. //joining threads
  260.  
  261. for (tx=0; tx < n_threads; ++tx) {
  262. if ((ret = pthread_join(threads[tx], NULL))) {
  263. printf("Error joining thread: %d\n", ret);
  264. exit(1);
  265. }
  266. }
  267. if ((ret = pthread_join(threads[n_threads], NULL))) {
  268. printf("Error joining thread: %d\n", ret);
  269. exit(1);
  270. }
  271.  
  272.  
  273. free(attractor);
  274. free(convergence);
  275. free(root);
  276. free(block_limit);
  277. free(block_size);
  278. return 0;
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement