#include #include #include #include #include using namespace std; int prime_number(int n, int id, int numtask); int main(int argc, char **argv) { int n_factor=2; //krok int n_lo=1; //poczatek przedzialu int n_hi=100;//koniec przedzialu int nodeId,totalNode,len; char hostname[MPI_MAX_PROCESSOR_NAME]; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &totalNode); MPI_Comm_rank(MPI_COMM_WORLD, &nodeId); MPI_Get_processor_name(hostname, &len); if(nodeId == 0 ) { cout << "NODEID 0: (ID: "<< nodeId <<") NODY: " << totalNode << endl; cout << " N_max Ilosc liczb pierwszych Czas " << endl; cout << "n"; } int n,p; int primes,primes_part; double startwtime,endwtime; n = n_lo; while (n <= n_hi) { if ( nodeId == 0 ) { //ROZPOCZYNAM POMIAR CZASU startwtime = MPI_Wtime(); } //ROZSYLANIE MPI_Bcast( &n, 1, MPI_INT, 0, MPI_COMM_WORLD ); primes_part = prime_number( n, nodeId, totalNode ); MPI_Reduce( &primes_part, &primes, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD ); if ( nodeId == 0 ) { endwtime = MPI_Wtime(); cout << " " << setw(3) << n << " " << setw(18) << primes << " " << setw(18) << (endwtime-startwtime) << endl; } n = n*n_factor; } MPI_Finalize(); return 0; } int prime_number(int n, int id, int numtask) { int prime; int total= 0; for (int i=2+id ; i<=n ; i=i+numtask) { prime = 1; for (int j = 2; j < i; j++) { if ((i%j) == 0) { prime = 0; break; } } total = total + prime; } return total; }