Unable to increase performance in MPI code

Viewed 45

I'm learning MPI and I have been running some programs that obtain some sort of speedup. However, I can't understand why the code below has no difference when running on 1, 2 or 4 processors.

What it does is simply allocate memory on master rank (0) based on command line input (n). Then it distributes the data among all processes, perform some computation and reduces. Finally master rank prints the global answer.

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

#define MAX_KEY 1

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

    MPI_Init(NULL, NULL);
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);

    srand(0);

    int *v;
    int n;
    int ROOT = 0;

    if (my_rank == ROOT)  {
        // Handle args
        if (argc != 2) {
            fprintf(stderr, "Usage: mpiexec -n <p> %s <N>\n", argv[0]);
            exit(1);
        }
        else {
            n = atoi(argv[1]);
            v = malloc(sizeof(int) * n);
            
            for (int i = 0; i < n; i++)
                v[i] = rand() % (MAX_KEY + 1); // Gen num in range [0, MAX_KEY]
        }
    }

    MPI_Bcast(&n, 1, MPI_INT, ROOT, MPI_COMM_WORLD);

    int local_n = n/comm_sz;
    int *local_v = malloc(sizeof(int) * local_n);
    MPI_Scatter(v, local_n, MPI_INT, local_v, local_n, MPI_INT, ROOT, MPI_COMM_WORLD);
    long long local_x = 0, global_x = 0;
   
    // Dummy computation
    for (int i = 0; i < local_n; i++)
        local_x += local_v[i];

    MPI_Reduce(&local_x, &global_x, 1, MPI_INT, MPI_SUM, ROOT, MPI_COMM_WORLD);
    
    if (my_rank == ROOT) {
        printf("x is %lld\n", global_x);
        free(v);
    }
    
    free(local_v);
    MPI_Finalize();
    return 0;
}

Running this code gave me some results like

$ time mpiexec -n 1 ./a.out 500000000
x is 249987030
mpiexec -n 1 ./a.out 500000000  7,48s user 1,28s system 99% cpu 8,761 total

$ time mpiexec -n 2 ./a.out 500000000
x is 249987030
mpiexec -n 2 ./a.out 500000000  14,90s user 1,46s system 199% cpu 8,187 total

$ time mpiexec -n 4 ./a.out 500000000
x is 249987030
mpiexec -n 4 ./a.out 500000000  29,95s user 2,28s system 399% cpu 8,071 total

I have ran into similar programs and problems and a almost linear speedup could be noticed in those cases. What's going on in this particular code?

0 Answers
Related