/* * Prime1.c * prost ciji je prvi neparni naslednik takodje prost * * Created on: Aug 20, 2010 * Author: djordje */ #include "mpi.h" #include #include #include const int A = 10000000; int prime(int n) { if((n == 0) || (n == 1)) return 0; if(n == 2) return 1; for(int i = 2; i <= sqrt(n); i++) if((n % i) == 0) return 0; return 1; } int main(int argc, char *argv[]) { int i, solutions, globalSolution; int id; //Process rank int p; //Num of processes double elapsed_time; int *primes, *globalPrimes; MPI_Init(&argc, &argv); MPI_Barrier(MPI_COMM_WORLD); elapsed_time = - MPI_Wtime(); MPI_Comm_rank(MPI_COMM_WORLD, &id); MPI_Comm_size(MPI_COMM_WORLD, &p); primes = (int*) calloc(A, sizeof( int )); for(i = 2*id+1; i < A; i += 2*p) primes[i] = prime(i); globalPrimes = (int*) malloc(A * sizeof( int )); MPI_Allreduce(primes, globalPrimes, A, MPI_INT, MPI_LOR, MPI_COMM_WORLD); solutions = 0; for(i = 2 * id + 1; i < A; i += 2*p) if(globalPrimes[i] && globalPrimes[i+2]) solutions++; MPI_Reduce(&solutions, &globalSolution, 1 , MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD); elapsed_time += MPI_Wtime(); MPI_Finalize(); if(id == 0)printf("%d, Elapsed time is %f\n", globalSolution, elapsed_time); return 0; }