Advertisement
Guest User

aka2 lab4a

a guest
May 23rd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. time_t start, stop;
  6.  
  7. struct wektor
  8. {
  9. float l1, l2, l3, l4;
  10. };
  11.  
  12. struct wektor A, B;
  13.  
  14. void losowanieLiczb() //uzupelnianie wektora liczbami
  15. {
  16. int przedzial1=1000;
  17. float przedzial2 = 4.12;
  18. srand(time(NULL));
  19. A.l1 = rand()%przedzial1/przedzial2;
  20. A.l2 = rand()%przedzial1/przedzial2;
  21. A.l3 = rand()%przedzial1/przedzial2;
  22. A.l4 = rand()%przedzial1/przedzial2;
  23. B.l1 = rand()%przedzial1/przedzial2;
  24. B.l2 = rand()%przedzial1/przedzial2;
  25. B.l3 = rand()%przedzial1/przedzial2;
  26. B.l4 = rand()%przedzial1/przedzial2;
  27. }
  28.  
  29. float dodawanie(int ile_liczb) //dodawanie
  30. {
  31. struct wektor V;
  32. int q, w;
  33. float czas;
  34. float sredni_czas=0;
  35. for(q=0;q<10;q++){
  36. losowanieLiczb();
  37. start=clock();
  38. for(w=0;w<ile_liczb;w++){
  39. //losowanieLiczb();
  40. //start=clock();
  41.  
  42. //__asm( "movl $A, %eax\n"
  43. // "movl $B, %ebx\n"
  44. // "movups (%eax), %xmm0\n"
  45. // "movups (%ebx), %xmm1\n"
  46. // "addps %xmm1, %xmm0\n"
  47.  
  48. __asm(
  49. "movups A, %xmm0\n"
  50. "movups B, %xmm1\n"
  51. "addps %xmm0, %xmm1\n"
  52. "movups %xmm1, A\n"
  53. );
  54.  
  55. //stop=clock();
  56. //czas= (double)(stop-start)/CLOCKS_PER_SEC;
  57. //sredni_czas= sredni_czas+czas;
  58. }
  59. stop=clock();
  60. czas= (double)(stop-start)/CLOCKS_PER_SEC;
  61. sredni_czas= sredni_czas+czas;
  62. }
  63. return sredni_czas/10;
  64. }
  65.  
  66. double odejmowanie(int ile_liczb) //odejmowanie
  67. {
  68. int q, w;
  69. double czas;
  70. double sredni_czas=0;
  71. for(q=0;q<10;q++){
  72. losowanieLiczb();
  73. start=clock();
  74. for(w=0;w<ile_liczb;w++){
  75.  
  76.  
  77. __asm(
  78. "movups A, %xmm0\n"
  79. "movups B, %xmm1\n"
  80. "subps %xmm0, %xmm1\n"
  81. "movups %xmm1, A\n"
  82. );
  83.  
  84. }
  85. stop=clock();
  86. czas= (double)(stop-start)/CLOCKS_PER_SEC;
  87. sredni_czas= sredni_czas+czas;
  88. }
  89. return sredni_czas/10;
  90. }
  91.  
  92. double mnozenie(int ile_liczb) //mnozenie
  93. {
  94. int q, w;
  95. double czas;
  96. double sredni_czas=0;
  97. for(q=0;q<10;q++){
  98.  
  99. losowanieLiczb();
  100. start=clock();
  101. for(w=0;w<ile_liczb;w++){
  102.  
  103.  
  104. __asm(
  105. "movups A, %xmm0\n"
  106. "movups B, %xmm1\n"
  107. "mulps %xmm0, %xmm1\n"
  108. "movups %xmm1, A\n"
  109. );
  110.  
  111. }
  112. stop=clock();
  113. czas= (double)(stop-start)/CLOCKS_PER_SEC;
  114. sredni_czas= sredni_czas+czas;
  115. }
  116. return sredni_czas/10;
  117. }
  118.  
  119. double dzielenie(int ile_liczb) //dzielenie
  120. {
  121. int q, w;
  122. double czas;
  123. double sredni_czas=0;
  124. for(q=0;q<10;q++){
  125.  
  126. losowanieLiczb();
  127. start=clock();
  128. for(w=0;w<ile_liczb;w++){
  129.  
  130.  
  131. __asm(
  132. "movups A, %xmm0\n"
  133. "movups B, %xmm1\n"
  134. "divps %xmm0, %xmm1\n"
  135. "movups %xmm1, A\n"
  136. );
  137.  
  138.  
  139. }
  140. stop=clock();
  141. czas= (double)(stop-start)/CLOCKS_PER_SEC;
  142. sredni_czas= sredni_czas+czas;
  143. }
  144. return sredni_czas/10;
  145. }
  146.  
  147. int main()
  148. {
  149. dodawanie(2048);
  150. printf("\nTyp obliczen: SIMD\n");
  151. printf("Liczba liczb: %i\n", 2048);
  152. printf("Sredni czas [s]:\n");
  153. printf("+ %f\n", dodawanie(2048/4));
  154. printf("- %f\n", odejmowanie(2048/4));
  155. printf("* %f\n", mnozenie(2048/4));
  156. printf("/ %f\n", dzielenie(2048/4));
  157.  
  158. printf("\nTyp obliczen: SIMD\n");
  159. printf("Liczba liczb: %i\n", 4096);
  160. printf("Sredni czas [s]:\n");
  161. printf("+ %f\n", dodawanie(4096/4));
  162. printf("- %f\n", odejmowanie(4096/4));
  163. printf("* %f\n", mnozenie(4096/4));
  164. printf("/ %f\n", dzielenie(4096/4));
  165.  
  166. printf("\nTyp obliczen: SIMD\n");
  167. printf("Liczba liczb: %i\n", 8196);
  168. printf("Sredni czas [s]:\n");
  169. printf("+ %f\n", dodawanie(8192/4));
  170. printf("- %f\n", odejmowanie(8192/4));
  171. printf("* %f\n", mnozenie(8192/4));
  172. printf("/ %f\n", dzielenie(8192/4));
  173.  
  174. //printf("ilosc liczb= %i\n", liczba*4);
  175. //printf("czas dodawania= %f\n", wynik_add);
  176. //printf("czas odejmowania= %f\n", wynik_sub);
  177. //printf("czas mnozenia= %f\n", wynik_mul);
  178. //printf("czas dzielenia= %f\n\n", wynik_div);
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement