0
votes

I tried to write the code 'finding prime number'. However MPI_Gather function cannot get the value of 'c'(prime number). It's not work even I declare the other arrays. What is the problem of my code source? It works well when I delete the part of MPI_Gather function. Do I need to use the other type of function in order to get the value?

This is my code:

#include<stdio.h>
#include<mpi.h>
#include<stdlib.h>
#define MAXSIZE 1000

int main(int argc, char *argv[]){
    int my_rank,p,low,high;
    int x,data[MAXSIZE],a=0;
    int num,t,result,i=0;
    int w=0,b=0,c=0,d=0;
    int * results, *resultss;

    MPI_Request req;
    MPI_Status status;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD, &p);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);


    if( my_rank == 0){
        printf("Input(50~1000) : ");
        fflush(stdout);
        scanf("%d",&w);
        for(i=0; i<w; i++) data[i] = i+1;
    }

    MPI_Bcast(&w, 1, MPI_INT,0,MPI_COMM_WORLD);
    MPI_Bcast(data, w, MPI_INT,0,MPI_COMM_WORLD);


if( my_rank != 0){
    x = w/(p-1);
    low = (my_rank-1)*x;
    high = low+x-1;
    for(num = data[low]; num <= data[high];num++){
        result = 0;
        t=1;
        while(num>=t){
            if(num%t==0){
                result = result +1;
            }
                t += 1;
        }
        if(result==2){
            if(i%5){
                c=num;
              //  printf("%d\t",c);
            }
            else{
                c=num;
             //   printf("%d\n",c);
            }
        }

     else {
    results = (int *)malloc(p * sizeof(int));
        }
    }
}

MPI_Gather(&c, 1, MPI_INT, results, 1, MPI_INT, 0, MPI_COMM_WORLD);
  int j=0;
for(j=1; j< p; j++){
if(my_rank == 0){
    printf("%d\n",results[j]);
    }
}

MPI_Finalize();

}

Here is the error message:

* Process received signal *

 Signal: Segmentation fault: 11 (11)
 Signal code: Address not mapped (1)
 Failing at address: 0x0
 [ 0] 0   libsystem_platform.dylib            0x00007fff8af90b3a _sigtramp + 26
 [ 1] 0   ???                                 0x000000010cc560cb 0x0 + 4509229259
 [ 2] 0   libopen-pal.40.dylib                0x00000001052771a5 non_overlap_copy_content_same_ddt + 885
 [ 3] 0   libmpi.40.dylib                     0x00000001050cc97c ompi_datatype_sndrcv + 444
 [ 4] 0   libmpi.40.dylib                     0x00000001051401ff ompi_coll_base_gather_intra_basic_linear + 159
 [ 5] 0   libmpi.40.dylib                     0x00000001050d9ac4 MPI_Gather + 564
 [ 6] 0   prog3                               0x0000000105095dfb main + 923
 [ 7] 0   libdyld.dylib                       0x00007fff8ad81235 start + 1
 [ 8] 0   ???                                 0x0000000000000001 0x0 + 1

* End of error message *


Primary job terminated normally, but 1 process returned a non-zero exit code. Per user-direction, the job has been aborted.

mpirun noticed that process rank 0 with PID 0 on node ****-ui-MacBook Proexited on signal 11 (Segmentation fault: 11).

1

1 Answers

0
votes

The error is you do not allocate results at the right place.

it should be something like

if( my_rank != 0){
    /* compute how many prime numbers there are */
} else {
    results = (int *)malloc(p * sizeof(int));
}