Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include<mpi.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<math.h>
  5.  
  6. int prost(int a)
  7. {
  8.     if(a==1) return 0;
  9.     if(a==2) return 1;
  10.    
  11.     for(int i=2;i<=sqrt(a);i++)
  12.     {
  13.         if(a%i==0) return 0;
  14.     }
  15.     return 1;
  16. }
  17.  
  18. int main(int argc, char *argv[])
  19. {
  20.     int i,err,size,rank,N,prosti,*moji,*svi,ostatak,broj,*sviNovi;
  21.     FILE *f=fopen("greensOut.dat","w");
  22.    
  23.     err=MPI_Init(&argc,&argv);
  24.     MPI_Comm_size(MPI_COMM_WORLD,&size);
  25.     MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  26.    
  27.    
  28.     if(rank==0)
  29.     {
  30.         scanf("%d",&N);
  31.     }
  32.    
  33.     MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD);
  34.    
  35.     moji=(int*)calloc(N,sizeof(int));
  36.     svi=(int*)calloc(N,sizeof(int));
  37.     sviNovi=(int*)calloc(N,sizeof(int));
  38.    
  39.     for(i=2*rank+1;i<N;i+=2*size)
  40.     {
  41.         moji[i]=prost(i);
  42.     }
  43.    
  44.     MPI_Allreduce(moji,svi,N,MPI_INT,MPI_LOR,MPI_COMM_WORLD);
  45.    
  46.     moji[2]=1;
  47.    
  48.     for(i=2*rank+1;i<N;i+=2*size)
  49.     {
  50.         if(svi[i]==1)
  51.         {
  52.             ostatak=0;
  53.             broj=i;
  54.             while(broj>0)
  55.             {
  56.                 ostatak+=broj%10;
  57.                 broj=broj/10;
  58.             }
  59.             if(!prost(ostatak))
  60.                 svi[i]=0;
  61.         }
  62.     }
  63.    
  64.     MPI_Reduce(svi,sviNovi,N,MPI_INT,MPI_LAND,0,MPI_COMM_WORLD);
  65.    
  66.     if(rank==0)
  67.     {
  68.         if(sviNovi[2]==1)
  69.             fprintf(f,"%d\n",2);
  70.         for(i=3;i<N;i+=2)
  71.         {
  72.             if(sviNovi[i]==1)
  73.                 fprintf(f,"%d\n",i);
  74.         }
  75.     }
  76.    
  77.    
  78.     MPI_Finalize();
  79.    
  80.  
  81.    
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement