Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <omp.h>
  4.  
  5. int lookForPrimes(int xmin, int xmax);
  6. int isPrime(int suspect);
  7.  
  8. int main(int argc, char* argv[]){
  9. // sprawdzenie wymagań formalnych
  10. if(argc != 3){
  11. printf("Złe dane wejściowe. Podaj tylko xmin i xmax przedziału szukania liczb pierwszych.\n");
  12. return -1;
  13. }
  14.  
  15. // deklaracja zmiennych przedziału
  16. int xmin = atoi(argv[1]);
  17. int xmax = atoi(argv[2]);
  18.  
  19. // sprawdzenie, czy uzytkownik dobrze podał przedziały.
  20. if(xmin < 1 || xmax < 1){
  21. printf("Złe dane wejściowe. Podaj liczby naturalne większe od 1.\n");
  22. return -1;
  23. }
  24.  
  25. if(xmin == xmax){
  26. printf("Złe dane wejściowe. Podaj przedział zawierający co najmniej 2 liczby.\n");
  27. return -1;
  28. }
  29.  
  30. if(xmin > xmax){
  31. int temp = xmin;
  32. xmin = xmax;
  33. xmax = temp;
  34. }
  35.  
  36. printf("Szukam liczb pierwszych w przedziale <%d, %d>\n", xmin, xmax);
  37.  
  38.  
  39. // START ALGORYTMU
  40. omp_set_nested(1); // zrownoleglenie zagniezdzonych petli.
  41. lookForPrimes(xmin, xmax);
  42. // KONIEC ALGORYTMU
  43. }
  44.  
  45. int lookForPrimes(int xmin, int xmax){
  46. // deklaracja zmiennych pomocnicznych
  47. int count = 0;
  48. int x = (xmin%2 == 0) ? xmin + 1 : xmin;
  49. double stop, start;
  50.  
  51. printf ( "Liczba obecnie wykorzystywanych wątków\t= %d\n", omp_get_max_threads());
  52. printf ( "Maksymalna liczba dostępnych wątków\t= %d\n", omp_get_num_procs());
  53.  
  54. #pragma omp
  55. {
  56. start = omp_get_wtime();
  57. #pragma for simd \
  58. shared(xmax) \
  59. firstprivate(x, start) \
  60. lastprivate(stop) \
  61. reduction (+:count) \
  62. schedule(dynamic)
  63. for(x; x<=xmax; x+=2){
  64. if(isPrime(x)){
  65. count++;
  66. }
  67. }
  68. stop = omp_get_wtime( ) - start;
  69. }
  70.  
  71. printf("Znalezienie %d liczb pierwszych w przedziale <%d, %d> zajęło %5.1f [s].\n", count, xmin, xmax, stop);
  72. return 0;
  73. }
  74.  
  75. int isPrime(int suspect){
  76. int i = 3;
  77. while(i <= (suspect/2 + 1)){
  78. if(suspect%i == 0){
  79. return 0;
  80. }
  81. i+=2;
  82. }
  83. return 1;
  84. }
  85.  
  86. #include <stdio.h>
  87. #include <stdlib.h>
  88. #include <omp.h>
  89.  
  90. int lookForPrimes(int xmin, int xmax);
  91. int isPrime(int suspect);
  92.  
  93. int main(int argc, char* argv[]){
  94. // sprawdzenie wymagań formalnych
  95. if(argc != 3){
  96. printf("Złe dane wejściowe. Podaj tylko xmin i xmax przedziału szukania liczb pierwszych.\n");
  97. return -1;
  98. }
  99.  
  100. // deklaracja zmiennych przedziału
  101. int xmin = atoi(argv[1]);
  102. int xmax = atoi(argv[2]);
  103.  
  104. // sprawdzenie, czy uzytkownik dobrze podał przedziały.
  105. if(xmin < 1 || xmax < 1){
  106. printf("Złe dane wejściowe. Podaj liczby naturalne większe od 1.\n");
  107. return -1;
  108. }
  109.  
  110. if(xmin == xmax){
  111. printf("Złe dane wejściowe. Podaj przedział zawierający co najmniej 2 liczby.\n");
  112. return -1;
  113. }
  114.  
  115. if(xmin > xmax){
  116. int temp = xmin;
  117. xmin = xmax;
  118. xmax = temp;
  119. }
  120.  
  121. printf("Szukam liczb pierwszych w przedziale <%d, %d>\n", xmin, xmax);
  122.  
  123.  
  124. // START ALGORYTMU
  125. omp_set_nested(1); // zrownoleglenie zagniezdzonych petli.
  126. lookForPrimes(xmin, xmax);
  127. // KONIEC ALGORYTMU
  128. }
  129.  
  130. int lookForPrimes(int xmin, int xmax){
  131. // deklaracja zmiennych pomocnicznych
  132. int count = 0;
  133. int x = (xmin%2 == 0) ? xmin + 1 : xmin;
  134. double stop, start;
  135.  
  136. printf ( "Liczba obecnie wykorzystywanych wątków\t= %d\n", omp_get_max_threads());
  137. printf ( "Maksymalna liczba dostępnych wątków\t= %d\n", omp_get_num_procs());
  138.  
  139. #pragma omp
  140. {
  141. start = omp_get_wtime();
  142. #pragma for simd \
  143. shared(xmax) \
  144. firstprivate(x, start) \
  145. lastprivate(stop) \
  146. reduction (+:count) \
  147. schedule(dynamic)
  148. for(x; x<=xmax; x+=2){
  149. if(isPrime(x)){
  150. count++;
  151. }
  152. }
  153. stop = omp_get_wtime( ) - start;
  154. }
  155.  
  156. printf("Znalezienie %d liczb pierwszych w przedziale <%d, %d> zajęło %5.1f [s].\n", count, xmin, xmax, stop);
  157. return 0;
  158. }
  159.  
  160. int isPrime(int suspect){
  161. int i = 3;
  162. while(i <= (suspect/2 + 1)){
  163. if(suspect%i == 0){
  164. return 0;
  165. }
  166. i+=2;
  167. }
  168. return 1;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement