Advertisement
Guest User

Untitled

a guest
May 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #define ITERATIONS_NUM 10
  5. #define RANDOM_MAX 100
  6. #define RANDOM_MIN 1
  7. #define ARRAY_SIZE 8192
  8. #define MIN_SIZE 2048
  9.  
  10. typedef struct
  11. {
  12. float a, b, c, d;
  13. } vector;
  14.  
  15. clock_t start, stop;
  16. double time_result;
  17. vector vecs_arr[ARRAY_SIZE];
  18.  
  19. //randomize values for vectors
  20. void randomizer() //N = 2048 || 4096 || 8192
  21. {
  22. for(int i = 0; i < ARRAY_SIZE; ++i)
  23. {
  24. vecs_arr[i].a = rand()%RANDOM_MAX+RANDOM_MIN;
  25. vecs_arr[i].b = rand()%RANDOM_MAX+RANDOM_MIN;
  26. vecs_arr[i].c = rand()%RANDOM_MAX+RANDOM_MIN;
  27. vecs_arr[i].d = rand()%RANDOM_MAX+RANDOM_MIN;
  28. }
  29. }
  30.  
  31. //SIMD
  32. double addSIMD(vector *vec1, vector *vec2)
  33. {
  34. vector vec;
  35. start = clock();
  36. asm("movups %1, %%xmm0 \n"
  37. "movups %2, %%xmm1 \n"
  38. "addps %%xmm1, %%xmm0 \n"
  39. "movups %%xmm0, %0 \n"
  40.  
  41. : "=m" (vec)
  42. : "m" (vec1),"m" (vec2)
  43. );
  44. stop = clock();
  45. time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
  46. return time_result;
  47. }
  48.  
  49. double subSIMD(vector *vec1, vector *vec2)
  50. {
  51. vector vec;
  52. start = clock();
  53. asm("movups %1, %%xmm0 \n"
  54. "movups %2, %%xmm1 \n"
  55. "subps %%xmm1, %%xmm0 \n"
  56. "movups %%xmm0, %0 \n"
  57.  
  58. : "=m" (vec)
  59. : "m" (vec1),"m" (vec2)
  60. );
  61. stop = clock();
  62. time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
  63. return time_result;
  64. }
  65.  
  66. double mulSIMD(vector *vec1, vector *vec2)
  67. {
  68. vector vec;
  69. start = clock();
  70. asm("movups %1, %%xmm0 \n"
  71. "movups %2, %%xmm1 \n"
  72. "mulps %%xmm1, %%xmm0 \n"
  73. "movups %%xmm0, %0 \n"
  74.  
  75. : "=m" (vec)
  76. : "m" (vec1),"m" (vec2)
  77. );
  78. stop = clock();
  79. time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
  80. return time_result;
  81. }
  82.  
  83. double divSIMD(vector *vec1, vector *vec2)
  84. {
  85. vector vec;
  86. start = clock();
  87. asm("movups %1, %%xmm0 \n"
  88. "movups %2, %%xmm1 \n"
  89. "divps %%xmm1, %%xmm0 \n"
  90. "movups %%xmm0, %0 \n"
  91.  
  92. : "=m" (vec)
  93. : "m" (vec1),"m" (vec2)
  94. );
  95. stop = clock();
  96. time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
  97. return time_result;
  98. }
  99.  
  100. //SISD
  101. double addSISD(float *a, float *b)
  102. {
  103. float res;
  104. start = clock();
  105. asm("fld %1 \n"
  106. "fadd %2 \n"
  107. "fstp %0 \n"
  108.  
  109. : "=m" (res)
  110. : "m" (a),"m" (b)
  111. );
  112. stop = clock();
  113. time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
  114. return time_result;
  115. }
  116.  
  117. double subSISD(float *a, float *b)
  118. {
  119. float res;
  120. start = clock();
  121. asm("fld %1 \n"
  122. "fsub %2 \n"
  123. "fstp %0 \n"
  124.  
  125. : "=m" (res)
  126. : "m" (a),"m" (b)
  127. );
  128. stop = clock();
  129. time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
  130. return time_result;
  131. }
  132.  
  133. double mulSISD(float *a, float *b)
  134. {
  135. float res;
  136. start = clock();
  137. asm("fld %1 \n"
  138. "fmul %2 \n"
  139. "fstp %0 \n"
  140.  
  141. : "=m" (res)
  142. : "m" (a),"m" (b)
  143. );
  144. stop = clock();
  145. time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
  146. return time_result;
  147. }
  148.  
  149. double divSISD(float *a, float *b)
  150. {
  151. float res;
  152. start = clock();
  153. asm("fld %1 \n"
  154. "fdiv %2 \n"
  155. "fstp %0 \n"
  156.  
  157. : "=m" (res)
  158. : "m" (a),"m" (b)
  159. );
  160. stop = clock();
  161. time_result = ((double)(stop - start))/ CLOCKS_PER_SEC;
  162. return time_result;
  163. }
  164.  
  165. int main (int argc, char* argv[])
  166. {
  167. double add_time_simd, sub_time_simd, mul_time_simd, div_time_simd;
  168. double add_time_sisd, sub_time_sisd, mul_time_sisd, div_time_sisd;
  169.  
  170. FILE *fp;
  171. if ((fp=fopen("test.txt", "w"))==NULL) {
  172. printf ("Cannot open file!\n");
  173. exit(1);
  174. }
  175.  
  176. for(int i = MIN_SIZE; i < ARRAY_SIZE+1; i+=i)
  177. {
  178. randomizer();
  179.  
  180. add_time_simd = 0; sub_time_simd = 0; mul_time_simd = 0; div_time_simd = 0;
  181. add_time_sisd = 0; sub_time_sisd = 0; mul_time_sisd = 0; div_time_sisd = 0;
  182.  
  183. for(int j = 0; j < ITERATIONS_NUM; ++j)
  184. {
  185. //SIMD
  186. for(int k = 0; k < i; ++k)
  187. {
  188. add_time_simd += addSIMD(&vecs_arr[k], &vecs_arr[k]);
  189. sub_time_simd += subSIMD(&vecs_arr[k], &vecs_arr[k]);
  190. mul_time_simd += mulSIMD(&vecs_arr[k], &vecs_arr[k]);
  191. div_time_simd += divSIMD(&vecs_arr[k], &vecs_arr[k]);
  192. }
  193.  
  194. //SISD - ile liczb to 8192? 16tys czy 8tys?
  195. for(int k = 0; k < i; ++k)
  196. {
  197. add_time_sisd += addSISD(&vecs_arr[k].a, &vecs_arr[k].a);
  198. add_time_sisd += addSISD(&vecs_arr[k].b, &vecs_arr[k].b);
  199. add_time_sisd += addSISD(&vecs_arr[k].c, &vecs_arr[k].c);
  200. add_time_sisd += addSISD(&vecs_arr[k].d, &vecs_arr[k].d);
  201.  
  202. sub_time_sisd += subSISD(&vecs_arr[k].a, &vecs_arr[k].a);
  203. sub_time_sisd += subSISD(&vecs_arr[k].b, &vecs_arr[k].b);
  204. sub_time_sisd += subSISD(&vecs_arr[k].c, &vecs_arr[k].c);
  205. sub_time_sisd += subSISD(&vecs_arr[k].d, &vecs_arr[k].d);
  206.  
  207. mul_time_sisd += mulSISD(&vecs_arr[k].a, &vecs_arr[k].a);
  208. mul_time_sisd += mulSISD(&vecs_arr[k].b, &vecs_arr[k].b);
  209. mul_time_sisd += mulSISD(&vecs_arr[k].c, &vecs_arr[k].c);
  210. mul_time_sisd += mulSISD(&vecs_arr[k].d, &vecs_arr[k].d);
  211.  
  212. div_time_sisd += divSISD(&vecs_arr[k].a, &vecs_arr[k].a);
  213. div_time_sisd += divSISD(&vecs_arr[k].b, &vecs_arr[k].b);
  214. div_time_sisd += divSISD(&vecs_arr[k].c, &vecs_arr[k].c);
  215. div_time_sisd += divSISD(&vecs_arr[k].d, &vecs_arr[k].d);
  216. }
  217.  
  218. }
  219.  
  220. fprintf (fp, "TYP OBLICZEN: SIMD \n Liczba liczb: %d \n Sredni czas:\n +%f\n -%f\n *%f\n /%f\n",
  221. i, add_time_simd/10, sub_time_simd/10, mul_time_simd/10, div_time_simd/10);
  222.  
  223. fprintf (fp, "TYP OBLICZEN: SISD \n Liczba liczb: %d \n Sredni czas:\n +%f\n -%f\n *%f\n /%f\n",
  224. i, add_time_sisd/10, sub_time_sisd/10, mul_time_sisd/10, div_time_sisd/10);
  225. }
  226.  
  227. fclose (fp);
  228. return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement