0
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.

  • =================================================================================== = BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES = PID 2860 RUNNING AT Mustafa = EXIT CODE: 139 = CLEANING UP REMAINING PROCESSES = YOU CAN IGNORE THE BELOW CLEANUP MESSAGES =================================================================================== YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11) This typically refers to a problem with your application. Please see the FAQ page for debugging suggestions – mustafagoksever Mar 26 '18 at 13:32
  • On what line of your source did the error condition occur? – ryyker Mar 26 '18 at 13:41
  • This line: `for(i=rank;i – ryyker Mar 26 '18 at 13:42
  • other than rank 0 does not work in for loops . Rank 0 is working very well. when rank 1 entered in for loop i came across bad termination – mustafagoksever Mar 26 '18 at 13:44
  • for ecample n=10 size 4; it will calculate rank 0 => 0,4,8 rank 1=> 1,5,9 rank 2=> 2,6,10 rank 3=> 3,7 – mustafagoksever Mar 26 '18 at 13:45
  • It does not look like `i` is ever `==` to `1`. It jumps from `0` to `i+size` – ryyker Mar 26 '18 at 13:45

2 Answers2

0

You need to malloc() all your arrays a, b and c on all the ranks (e.g. MPI_Bcast() does not do it for you on non root ranks).

From a performance and memory footprint point of view, you'd rather MPI_Scatterv() the arrays so the operations can be vectorized without striding access.

Gilles Gouaillardet
  • 8,193
  • 11
  • 24
  • 30
0

I am beginner but maybe you could use this:

#include <iostream>
#include <mpi.h>


#define MASTER 0

int main(int argc, char* argv[])
{

    int N = 0;      //array size
    int my_rank, world_size;

    double* a{};
    double* b{};
    double* c{};

    MPI_Init(&argc, &argv);

    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    if (my_rank == MASTER) {
        std::cout << "number of processes = " << world_size << std::endl;
    }

    if (my_rank == MASTER) {
        std::cout << "array size: "; std::cin >> N;
        a = new double[N] {};
        b = new double[N] {};
        c = new double[N] {};
    }

    MPI_Bcast(&N, 1, MPI_INT, MASTER, MPI_COMM_WORLD);

    if (my_rank == MASTER) {
        for (int i = 0; i < N; i++) {
            a[i] = (double)i + 1.0;
            b[i] = (double)i + 1.0;
        }
        for (auto i = 0; i < N; i++) {
            std::cout << "a[" << i << "] = " << a[i] <<
                "\t" << "b[" << i << "] = " << b[i] <<
                std::endl;
        }
        std::cout << "============================" << std::endl;
    }

    int* workloads = new int[world_size];
    int* displs = new int[world_size];
    int increment = 0;

    for (int i = 0; i < world_size; i++) {
        displs[i] = increment;
        workloads[i] = N / world_size;
        if (i < N % world_size) workloads[i]++;
        increment += workloads[i];
    }

    bool nowPrint = false;
    for (int j = 0; j < world_size; j++) {
        if(j == my_rank)
            std::cout << "workload on rank #" << my_rank << " : " << workloads[my_rank] << std::endl;
        if (j == world_size-1) {
            nowPrint = true;
        }    
        MPI_Barrier(MPI_COMM_WORLD);
    }

    int n = workloads[my_rank];
    double* ap(new double[n] {});
    double* bp(new double[n] {});
    double* cp(new double[n] {});

    MPI_Scatterv(a, workloads, displs, MPI_DOUBLE, ap, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
    MPI_Scatterv(b, workloads, displs, MPI_DOUBLE, bp, n, MPI_DOUBLE, 0, MPI_COMM_WORLD);

    for (auto i = 0; i < n; i++) {
        cp[i] = ap[i] + bp[i];
    }
    MPI_Gatherv(cp, n, MPI_DOUBLE, c, workloads, displs, MPI_DOUBLE, 0, MPI_COMM_WORLD);

    if (my_rank == MASTER) {
        if (nowPrint) {
            std::cout << "============================\n";
            for (auto i = 0; i < N; i++) {
                std::cout << "c[" << i << "] = " << c[i] << std::endl;
            }
        }
    }

    // free heap on the root process
    if (my_rank == MASTER) {
        delete[] a;
        a = nullptr;
        delete[] b;
        b = nullptr;
        delete[] c;
        c = nullptr;
    }
    //

    // free heap for all processes
    delete[] ap;
    ap = nullptr;
    delete[] bp;
    bp = nullptr;
    delete[] cp;
    cp = nullptr;
    delete[] workloads;
    workloads = nullptr;
    delete[] displs;
    displs = nullptr;
    //

    MPI_Finalize();

    return 0;  
}
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Ramin
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 06 '22 at 09:33