Skip to content

Instantly share code, notes, and snippets.

@sdshlanta
Last active October 23, 2017 01:04
Show Gist options
  • Save sdshlanta/7fa991e26acd2c5317bde3f61844be9d to your computer and use it in GitHub Desktop.
Save sdshlanta/7fa991e26acd2c5317bde3f61844be9d to your computer and use it in GitHub Desktop.
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#define SEGMENT 100
#define PRIMEMAX 1000
int comp (const void * elem1, const void * elem2) {
unsigned int f = *((unsigned int*)elem1);
unsigned int s = *((unsigned int*)elem2);
if (f > s) return 1;
if (f < s) return -1;
return 0;
}
int main(int argc, char** argv)
{
int world_rank;
int world_size;
int x, running = 1;
unsigned int k, i;
unsigned int number[2];
int buffSize = 0;
int activeJobs = 0;
MPI_Status status;
MPI_Init(NULL, NULL);
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &world_size);
if (world_rank == 0) {
// If we are rank 0, set the number to 27 and send it to process 1
for(k=0; k<world_size-1; ++k) {
printf("sending to %u\n", k+1);
number[0] = (k * SEGMENT)+3;//lower bound
number[1] = ((k * SEGMENT)+SEGMENT)+3;//upper bound
MPI_Send(&number, 2, MPI_UNSIGNED, k+1, 0, MPI_COMM_WORLD);
++activeJobs;
}
int numOfPrimesFound;
int startIndex = 0;
unsigned int* primeFound = (unsigned int*)malloc(sizeof(unsigned int));
for(k=((world_size-1) * SEGMENT)+3; k<PRIMEMAX; k+=SEGMENT) {
int i = 0;
fflush(stdout);
//printf("waiting for value back\n");
MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
MPI_Get_count(&status, MPI_UNSIGNED, &numOfPrimesFound);
//printf("number of primes: %d\n", numOfPrimesFound);
buffSize += sizeof(unsigned int) * numOfPrimesFound;
// printf("buff size %d\n", buffSize / 4);
primeFound = (unsigned int*)realloc(primeFound, buffSize);
MPI_Recv(primeFound + startIndex , numOfPrimesFound, MPI_UNSIGNED, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
// printf("got value back %u\n", primeFound[startIndex]);
startIndex += numOfPrimesFound;
number[0] = k;//lower bound
number[1] = (k + SEGMENT > PRIMEMAX ? PRIMEMAX : k + SEGMENT) ;//upper bound
MPI_Send(number, 2, MPI_UNSIGNED, status.MPI_SOURCE, 0, MPI_COMM_WORLD);
}
while(activeJobs) {
MPI_Probe(MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
MPI_Get_count(&status, MPI_UNSIGNED, &numOfPrimesFound);
//printf("number of primes: %d\n", numOfPrimesFound);
buffSize += sizeof(unsigned int) * numOfPrimesFound;
//printf("buff size %d\n", buffSize / 4);
primeFound = (unsigned int*)realloc(primeFound, buffSize);
MPI_Recv(primeFound + startIndex , numOfPrimesFound, MPI_UNSIGNED, status.MPI_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
//printf("got value back %u\n", primeFound[startIndex]);
//for(i =0; i<buffSize / 4; ++i){
// printf("mas %u\n", primeFound[i]);
//}
startIndex += numOfPrimesFound;
--activeJobs;
MPI_Send(number, 2, MPI_UNSIGNED, status.MPI_SOURCE, 1, MPI_COMM_WORLD);
}
for(i =0; i<buffSize / 4; ++i){
printf("mas %u\n", primeFound[i]);
}
//sort this crap
qsort(primeFound, buffSize/4, sizeof(unsigned int), comp);
//for(i =0; i<buffSize / 4; ++i){
// printf("mas %u\n", primeFound[i]);
//}
}
else if (world_rank >= 1) {
unsigned int maxDiv;
int isPrime;
int size, buffSize;
int i;
unsigned int* primes;
while(running) {
MPI_Recv(number, 2, MPI_UNSIGNED, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
size = 0;
primes = (unsigned int*)malloc(sizeof(unsigned int) * ((number[1] - number[0]) /2) +1);
if(status.MPI_TAG == 1) {
printf("%d is quitting\n", world_rank);
running = 0;
} else {
printf("Process %d received range [%d, %d] from process 0\n",world_rank, number[0], number[1]);
for(k = number[0]; k < number[1]; ++k){
isPrime = 1;
//printf("checking %d ", k);
if(k % 2) {
maxDiv = ceil(sqrt(k));
for(x = 2; x <= maxDiv; ++x) {
if(k % x == 0) {
isPrime = 0;
break;
}
}
} else {
isPrime = 0;
}
if(isPrime) {
primes[size] = k;
++size;
}
}
//if(primes != NULL)
//printf("found %d primes\n", size);
// for(i = 0; i<size; ++i)
// printf("sub: %u\n", primes[i]);
MPI_Send(primes, size, MPI_UNSIGNED, 0, 0, MPI_COMM_WORLD);
free(primes);
}
}
}
MPI_Finalize();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment