mpi application performace get worse when adding more process

52 Views Asked by At

i write parallel binary search algorithm with MPI it works as expected in term of searching for a value but when the -n is 1 (serial) the total time is much lower than any value above that like 2, 4, 8, etc....

when i increase number of process it take longer time when i expect the time to be lower than the 1 process. what is the problem or can anyone help me solve this. Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>

void BinarySearch(int local_x[], int search, int lower, int heigher, int rank, int comm_sz);
int* create_array(int n);

int index = -1;
int found_rank = -1;

void main() {
    int search = 7;
    
    MPI_Init(NULL, NULL);

    int my_rank, comm_sz;
    double start = 0;
    double finish = 0;
    MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    int size =  10 * comm_sz;
    int* x = nullptr;

    int local_size = (size / comm_sz); //+ 1 
    int* local_x = new int[local_size];

    MPI_Barrier(MPI_COMM_WORLD);
    start = MPI_Wtime();
    if (my_rank == 0)
    {
        printf("size per process = %d \n", local_size);
        x = create_array(size);
        for (int i = 0; i < size; i++)
        {
            printf("%d ", x[i]);
        }

        //  printf("\n Please input number: \n");
        //getchar();
    }

    MPI_Scatter(x, local_size, MPI_INT, local_x, local_size, MPI_INT, 0, MPI_COMM_WORLD);

    BinarySearch(local_x, search, 0, local_size, my_rank, comm_sz);


    MPI_Barrier(MPI_COMM_WORLD);
    finish = MPI_Wtime();


    if (my_rank == 0) {
        printf("\n total time = %g \n", finish - start);

        if (found_rank == -1 || index == -1) {
            printf("Not found");
        }else
        printf("\n value %d located at index %d at rank %d \n", search, index,found_rank );

    }


    MPI_Finalize();
}

void BinarySearch(int local_x[], int search, int lower, int heigher, int rank, int comm_sz) {
    int mid = -1;
    int size = heigher;
    int correct_rank = -1;
    for (int i = lower; lower < heigher ; i++)
    {
        mid = (lower + heigher) / 2;

        if (local_x[mid] > search) {

            heigher = mid - 1;
        }

        if (local_x[mid] < search) {
            lower = (mid + 1);
        }

        if (local_x[mid] == search) {
            break;
        }

    }
    int value = local_x[mid];
    if (value == search) {
        mid = (rank * size) + mid;
        correct_rank = rank;
    }
    else {
        mid = -1;
    }

    MPI_Reduce(&mid, &index, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
    MPI_Reduce(&correct_rank, &found_rank, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);




}

int* create_array(int n) {
    int* tmp = (int*)calloc(n, sizeof(int));
    for (int i = 0; i < n; i++)
    {
        tmp[i] = i + 1 ;
    }
    return (tmp);
}
0

There are 0 best solutions below