MPI_Init(&argc, &argv);
int size,rank,i,*a,*b,*c,N;
double t1,t2;
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
srand(time(NULL));
if(rank==0)
{
printf("Input an integer for arrays size\n");
scanf("%d",&N);
a = (int*) malloc((N)* sizeof(int));
b = (int*) malloc((N) * sizeof(int));
c = (int*) malloc((N)* sizeof(int));
for(i=0;i<N;i++)
{
a[i]=rand()%100+1;
b[i]=rand()%100+1;
c[i]=0;
printf("c[%d]= %d , a[i]= %d , b[i] = %d \n",i, c[i],a[i],b[i]);
}
}
t1=MPI_Wtime();
MPI_Bcast(&N,1,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(&a,N,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(&b,N,MPI_INT,0,MPI_COMM_WORLD);
MPI_Bcast(&c,N,MPI_INT,0,MPI_COMM_WORLD);
printf("\n\n\n\n");
for(i=rank;i<N;i+=size)
{
printf("entered rank %d \n",rank);
c[i]=a[i]+b[i];
printf("c[%d]= %d , a[i]= %d , b[i] = %d \n",i, c[i],a[i],b[i]);
}
t2=MPI_Wtime();
if(rank==0)
printf("time elapsed %.8f \n" ,t2-t1);
MPI_Finalize();
}
I want to write a parallel program with MPI Library. When I run this code with just one core no problem. but one more than core I came across an run time error something like that is shown below. rank 0 is working very well. other ranks do not work.