Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <omp.h>
- int lookForPrimes(int xmin, int xmax);
- int isPrime(int suspect);
- int main(int argc, char* argv[]){
- // sprawdzenie wymagań formalnych
- if(argc != 3){
- printf("Złe dane wejściowe. Podaj tylko xmin i xmax przedziału szukania liczb pierwszych.\n");
- return -1;
- }
- // deklaracja zmiennych przedziału
- int xmin = atoi(argv[1]);
- int xmax = atoi(argv[2]);
- // sprawdzenie, czy uzytkownik dobrze podał przedziały.
- if(xmin < 1 || xmax < 1){
- printf("Złe dane wejściowe. Podaj liczby naturalne większe od 1.\n");
- return -1;
- }
- if(xmin == xmax){
- printf("Złe dane wejściowe. Podaj przedział zawierający co najmniej 2 liczby.\n");
- return -1;
- }
- if(xmin > xmax){
- int temp = xmin;
- xmin = xmax;
- xmax = temp;
- }
- printf("Szukam liczb pierwszych w przedziale <%d, %d>\n", xmin, xmax);
- // START ALGORYTMU
- omp_set_nested(1); // zrownoleglenie zagniezdzonych petli.
- lookForPrimes(xmin, xmax);
- // KONIEC ALGORYTMU
- }
- int lookForPrimes(int xmin, int xmax){
- // deklaracja zmiennych pomocnicznych
- int count = 0;
- int x = (xmin%2 == 0) ? xmin + 1 : xmin;
- double stop, start;
- printf ( "Liczba obecnie wykorzystywanych wątków\t= %d\n", omp_get_max_threads());
- printf ( "Maksymalna liczba dostępnych wątków\t= %d\n", omp_get_num_procs());
- #pragma omp
- {
- start = omp_get_wtime();
- #pragma for simd \
- shared(xmax) \
- firstprivate(x, start) \
- lastprivate(stop) \
- reduction (+:count) \
- schedule(dynamic)
- for(x; x<=xmax; x+=2){
- if(isPrime(x)){
- count++;
- }
- }
- stop = omp_get_wtime( ) - start;
- }
- printf("Znalezienie %d liczb pierwszych w przedziale <%d, %d> zajęło %5.1f [s].\n", count, xmin, xmax, stop);
- return 0;
- }
- int isPrime(int suspect){
- int i = 3;
- while(i <= (suspect/2 + 1)){
- if(suspect%i == 0){
- return 0;
- }
- i+=2;
- }
- return 1;
- }
- #include <stdio.h>
- #include <stdlib.h>
- #include <omp.h>
- int lookForPrimes(int xmin, int xmax);
- int isPrime(int suspect);
- int main(int argc, char* argv[]){
- // sprawdzenie wymagań formalnych
- if(argc != 3){
- printf("Złe dane wejściowe. Podaj tylko xmin i xmax przedziału szukania liczb pierwszych.\n");
- return -1;
- }
- // deklaracja zmiennych przedziału
- int xmin = atoi(argv[1]);
- int xmax = atoi(argv[2]);
- // sprawdzenie, czy uzytkownik dobrze podał przedziały.
- if(xmin < 1 || xmax < 1){
- printf("Złe dane wejściowe. Podaj liczby naturalne większe od 1.\n");
- return -1;
- }
- if(xmin == xmax){
- printf("Złe dane wejściowe. Podaj przedział zawierający co najmniej 2 liczby.\n");
- return -1;
- }
- if(xmin > xmax){
- int temp = xmin;
- xmin = xmax;
- xmax = temp;
- }
- printf("Szukam liczb pierwszych w przedziale <%d, %d>\n", xmin, xmax);
- // START ALGORYTMU
- omp_set_nested(1); // zrownoleglenie zagniezdzonych petli.
- lookForPrimes(xmin, xmax);
- // KONIEC ALGORYTMU
- }
- int lookForPrimes(int xmin, int xmax){
- // deklaracja zmiennych pomocnicznych
- int count = 0;
- int x = (xmin%2 == 0) ? xmin + 1 : xmin;
- double stop, start;
- printf ( "Liczba obecnie wykorzystywanych wątków\t= %d\n", omp_get_max_threads());
- printf ( "Maksymalna liczba dostępnych wątków\t= %d\n", omp_get_num_procs());
- #pragma omp
- {
- start = omp_get_wtime();
- #pragma for simd \
- shared(xmax) \
- firstprivate(x, start) \
- lastprivate(stop) \
- reduction (+:count) \
- schedule(dynamic)
- for(x; x<=xmax; x+=2){
- if(isPrime(x)){
- count++;
- }
- }
- stop = omp_get_wtime( ) - start;
- }
- printf("Znalezienie %d liczb pierwszych w przedziale <%d, %d> zajęło %5.1f [s].\n", count, xmin, xmax, stop);
- return 0;
- }
- int isPrime(int suspect){
- int i = 3;
- while(i <= (suspect/2 + 1)){
- if(suspect%i == 0){
- return 0;
- }
- i+=2;
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement