Blame test/mpi/perf/non_zero_root.c

Packit 0848f5
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
Packit 0848f5
/*
Packit 0848f5
 *  (C) 2008 by Argonne National Laboratory.
Packit 0848f5
 *      See COPYRIGHT in top-level directory.
Packit 0848f5
 */
Packit 0848f5
#include "mpi.h"
Packit 0848f5
#include <stdio.h>
Packit 0848f5
#include <stdlib.h>
Packit 0848f5
#include <sys/time.h>
Packit 0848f5
Packit 0848f5
#define SIZE 100000
Packit 0848f5
#define ITER 1000
Packit 0848f5
Packit 0848f5
#define ERROR_MARGIN 0.5
Packit 0848f5
Packit 0848f5
static int verbose = 0;
Packit 0848f5
Packit 0848f5
int main(int argc, char *argv[])
Packit 0848f5
{
Packit 0848f5
    char *sbuf, *rbuf;
Packit 0848f5
    int i, j;
Packit 0848f5
    double t1, t2, t, ts;
Packit 0848f5
    int rank, size;
Packit 0848f5
    MPI_Status status;
Packit 0848f5
Packit 0848f5
    MPI_Init(&argc, &argv);
Packit 0848f5
    MPI_Comm_rank(MPI_COMM_WORLD, &rank;;
Packit 0848f5
    MPI_Comm_size(MPI_COMM_WORLD, &size);
Packit 0848f5
Packit 0848f5
    if (getenv("MPITEST_VERBOSE"))
Packit 0848f5
        verbose = 1;
Packit 0848f5
Packit 0848f5
    /* Allocate memory regions to communicate */
Packit 0848f5
    sbuf = (char *) malloc(SIZE);
Packit 0848f5
    rbuf = (char *) malloc(size * SIZE);
Packit 0848f5
Packit 0848f5
    /* Touch the buffers to make sure they are allocated */
Packit 0848f5
    for (i = 0; i < SIZE; i++)
Packit 0848f5
        sbuf[i] = '0';
Packit 0848f5
    for (i = 0; i < SIZE * size; i++)
Packit 0848f5
        rbuf[i] = '0';
Packit 0848f5
Packit 0848f5
    /* Time when rank 0 gathers the data */
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    t1 = MPI_Wtime();
Packit 0848f5
    for (i = 0; i < ITER; i++) {
Packit 0848f5
        MPI_Gather(sbuf, SIZE, MPI_BYTE, rbuf, SIZE, MPI_BYTE, 0, MPI_COMM_WORLD);
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
    t2 = MPI_Wtime();
Packit 0848f5
    t = (t2 - t1) / ITER;
Packit 0848f5
Packit 0848f5
    /* Time when rank 1 gathers the data */
Packit 0848f5
    MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    t1 = MPI_Wtime();
Packit 0848f5
    for (j = 0; j < ITER; j++) {
Packit 0848f5
        MPI_Gather(sbuf, SIZE, MPI_BYTE, rbuf, SIZE, MPI_BYTE, 1, MPI_COMM_WORLD);
Packit 0848f5
        MPI_Barrier(MPI_COMM_WORLD);
Packit 0848f5
    }
Packit 0848f5
    t2 = MPI_Wtime();
Packit 0848f5
    ts = (t2 - t1) / ITER;
Packit 0848f5
Packit 0848f5
    if (rank == 1)
Packit 0848f5
        MPI_Send(&ts, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
Packit 0848f5
    if (rank == 0)
Packit 0848f5
        MPI_Recv(&ts, 1, MPI_DOUBLE, 1, 0, MPI_COMM_WORLD, &status);
Packit 0848f5
Packit 0848f5
    /* Print out the results */
Packit 0848f5
    if (!rank) {
Packit 0848f5
        if ((ts / t) > (1 + ERROR_MARGIN)) {    /* If the difference is more than 10%, it's an error */
Packit 0848f5
            printf("%.3f\t%.3f\n", 1000000.0 * ts, 1000000.0 * t);
Packit 0848f5
            printf("Too much difference in performance\n");
Packit 0848f5
        }
Packit 0848f5
        else
Packit 0848f5
            printf(" No Errors\n");
Packit 0848f5
    }
Packit 0848f5
Packit 0848f5
    MPI_Finalize();
Packit 0848f5
Packit 0848f5
    return 0;
Packit 0848f5
}