this is my code, it cause segmentation fault when I run this.. no compilation errors. can anyone please check this code?
#include<mpi.h>
#include<stdio.h>
#include<math.h>
int isPrime(int n);
int main(int argc, char* argv[])
{
int myRank, size;
int flag=0;
int source;
MPI_Status status;
int index=0;
int arr[90002]; //initial array
int temp[90000];
int primes[90000]; //resulting array
int numworkers;
int count=0;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myRank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
numworkers=size-1;
count=sizeof(arr)/numworkers;
if(myRank==0)
{
int i;
for (i = 0; i < 90000; i++) {
arr[i] =i+10000;
}
index=0;
for(myRank= 1; myRank <=numworkers; myRank++) {
// start=floor((myRank*90000)/size);
// end=floor(((myRank+1)*90000)/size)-1;
// num_to_send = end - start + 1;
MPI_Send(&index, 1 , MPI_INT, myRank, 123, MPI_COMM_WORLD);
MPI_Send(&arr[index], count, MPI_INT,myRank, 321, MPI_COMM_WORLD);
index=index+count;
}
int j;
for (j=1;j<=numworkers;j++)
{
source=j;
MPI_Recv(&index, 1, MPI_INT, source, 1, MPI_COMM_WORLD, &status);
MPI_Recv(&primes[index] , count, MPI_INT, source, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
for(i=0;i<sizeof(primes)/sizeof(primes[0]);i++)
printf("%d ", primes[i]);
printf("\n");
}
//worker nodes task
if (myRank>0){
int i;
source=0;
// for(myRank= 1; myRank < size; myRank++) {
MPI_Recv( &index, 1, MPI_INT, source, 123, MPI_COMM_WORLD, &status);
MPI_Recv( &temp[index], count, MPI_INT, source, 321, MPI_COMM_WORLD, &status);
for(i = index; i < index+count; i++) {
flag=isPrime(temp[i]);
if(flag==0)
{
primes[i]= temp[i];
}
}
MPI_Send(&index,1,MPI_INT,source,1,MPI_COMM_WORLD);
MPI_Send( &primes[index], count, MPI_INT,source, 0, MPI_COMM_WORLD);
//}
}
MPI_Finalize();
return 0;
}
int isPrime(int n)
{
int flag, i;
if (n == 0 || n == 1)
flag=1;
for (i = 2; i <= n / 2;i++) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
return flag;
}