#include <mpi.h>
#include <iostream>
#include <cmath>
#include <ctime>
#include <iomanip>
using namespace std;
double cpu_time(void);
int prime_number(int n, int id, int numtasks);
int main(int argc, char** argv) {
int rank, size, n_factor = 2, n_lo = 1, n_hi = 100;
double start_time, end_time;
int n, primes, primes_part;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0) {
cout << "Pomiar ilosci liczb pierwszych w powiekszajacych sie " << n_factor << "-krotnie podprzedzialach przedzialu [" << n_lo << " , " << n_hi << " ]" << endl;
cout << "\n";
cout << " N_max Ilosc liczb pierwszych Czas " << endl;
cout << "\n";
}
n = n_lo;
while (n <= n_hi) {
start_time = MPI_Wtime();
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
primes_part = prime_number(n, rank, size);
MPI_Reduce(&primes_part, &primes, 1, MPI_INT, MPI_SUM, 0, MPI_COMM_WORLD);
end_time = MPI_Wtime();
if (rank == 0) {
cout << " " << setw(3) << n
<< " " << setw(18) << primes
<< " " << setw(18) << end_time - start_time << endl;
}
n = n * n_factor;
}
MPI_Finalize();
return 0;
}
double cpu_time() {
return (double)clock() / (double)CLOCKS_PER_SEC
; }
int prime_number(int n, int id, int numtasks) {
int prime;
int total = 0;
for (int i = id + 2; i <= n; i += numtasks) {
prime = 1;
for (int j = 2; j < i; j++) {
if ((i % j) == 0) {
prime = 0;
break;
}
}
total = total + prime;
}
return total;
}
I2luY2x1ZGUgPG1waS5oPgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGN0aW1lPgojaW5jbHVkZSA8aW9tYW5pcD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpkb3VibGUgY3B1X3RpbWUodm9pZCk7CmludCBwcmltZV9udW1iZXIoaW50IG4sIGludCBpZCwgaW50IG51bXRhc2tzKTsKCmludCBtYWluKGludCBhcmdjLCBjaGFyKiogYXJndikgewogICAgaW50IHJhbmssIHNpemUsIG5fZmFjdG9yID0gMiwgbl9sbyA9IDEsIG5faGkgPSAxMDA7CiAgICBkb3VibGUgc3RhcnRfdGltZSwgZW5kX3RpbWU7CiAgICBpbnQgbiwgcHJpbWVzLCBwcmltZXNfcGFydDsKCiAgICBNUElfSW5pdCgmYXJnYywgJmFyZ3YpOwogICAgTVBJX0NvbW1fcmFuayhNUElfQ09NTV9XT1JMRCwgJnJhbmspOwogICAgTVBJX0NvbW1fc2l6ZShNUElfQ09NTV9XT1JMRCwgJnNpemUpOwoKICAgIGlmIChyYW5rID09IDApIHsKICAgICAgICBjb3V0IDw8ICJQb21pYXIgaWxvc2NpIGxpY3piIHBpZXJ3c3p5Y2ggdyBwb3dpZWtzemFqYWN5Y2ggc2llICIgPDwgbl9mYWN0b3IgPDwgIi1rcm90bmllIHBvZHByemVkemlhbGFjaCBwcnplZHppYWx1IFsiIDw8IG5fbG8gPDwgIiAsICIgPDwgbl9oaSA8PCAiIF0iIDw8IGVuZGw7CiAgICAgICAgY291dCA8PCAiXG4iOwogICAgICAgIGNvdXQgPDwgIiAgICBOX21heCAgICAgICAgSWxvc2MgbGljemIgcGllcndzenljaCAgICAgICAgQ3phcyAiIDw8IGVuZGw7CiAgICAgICAgY291dCA8PCAiXG4iOwogICAgfQoKICAgIG4gPSBuX2xvOwoKICAgIHdoaWxlIChuIDw9IG5faGkpIHsKICAgICAgICBzdGFydF90aW1lID0gTVBJX1d0aW1lKCk7CgogICAgICAgIE1QSV9CY2FzdCgmbiwgMSwgTVBJX0lOVCwgMCwgTVBJX0NPTU1fV09STEQpOwogICAgICAgIHByaW1lc19wYXJ0ID0gcHJpbWVfbnVtYmVyKG4sIHJhbmssIHNpemUpOwoKICAgICAgICBNUElfUmVkdWNlKCZwcmltZXNfcGFydCwgJnByaW1lcywgMSwgTVBJX0lOVCwgTVBJX1NVTSwgMCwgTVBJX0NPTU1fV09STEQpOwoKICAgICAgICBlbmRfdGltZSA9IE1QSV9XdGltZSgpOwoKICAgICAgICBpZiAocmFuayA9PSAwKSB7CiAgICAgICAgICAgIGNvdXQgPDwgIiAgICAiIDw8IHNldHcoMykgPDwgbgogICAgICAgICAgICAgICAgIDw8ICIgICAgIiA8PCBzZXR3KDE4KSA8PCBwcmltZXMKICAgICAgICAgICAgICAgICA8PCAiICAgICIgPDwgc2V0dygxOCkgPDwgZW5kX3RpbWUgLSBzdGFydF90aW1lIDw8IGVuZGw7CiAgICAgICAgfQoKICAgICAgICBuID0gbiAqIG5fZmFjdG9yOwogICAgfQoKICAgIE1QSV9GaW5hbGl6ZSgpOwogICAgcmV0dXJuIDA7Cn0KCmRvdWJsZSBjcHVfdGltZSgpIHsKICAgIHJldHVybiAoZG91YmxlKWNsb2NrKCkgLyAoZG91YmxlKUNMT0NLU19QRVJfU0VDOwp9CgppbnQgcHJpbWVfbnVtYmVyKGludCBuLCBpbnQgaWQsIGludCBudW10YXNrcykgewogICAgaW50IHByaW1lOwogICAgaW50IHRvdGFsID0gMDsKCiAgICBmb3IgKGludCBpID0gaWQgKyAyOyBpIDw9IG47IGkgKz0gbnVtdGFza3MpIHsKICAgICAgICBwcmltZSA9IDE7CiAgICAgICAgZm9yIChpbnQgaiA9IDI7IGogPCBpOyBqKyspIHsKICAgICAgICAgICAgaWYgKChpICUgaikgPT0gMCkgewogICAgICAgICAgICAgICAgcHJpbWUgPSAwOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgdG90YWwgPSB0b3RhbCArIHByaW1lOwogICAgfQogICAgcmV0dXJuIHRvdGFsOwp9Cg==