Skip to content
skeleton.c 1.53 KiB
Newer Older
Jussi Enkovaara's avatar
Jussi Enkovaara committed
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>

int main(int argc, char *argv[])
{
    int i, myid, ntasks;
    int size = 10000000;
    int *message;
    int *receiveBuffer;
    MPI_Status status;

    double t0, t1;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &ntasks);
    MPI_Comm_rank(MPI_COMM_WORLD, &myid);

    /* Allocate message buffers */
    message = malloc(sizeof(int) * size);
    receiveBuffer = malloc(sizeof(int) * size);
    /* Initialize message */
    for (i = 0; i < size; i++) {
        message[i] = myid;
    }

    /* Start measuring the time spent in communication */
    MPI_Barrier(MPI_COMM_WORLD);
    t0 = MPI_Wtime();

    /* TODO start */
    /* Send and receive messages as defined in exercise */
    if (myid < ntasks - 1) {
        printf("Sender: %d. Sent elements: %d. Tag: %d. Receiver: %d\n",
               myid, size, myid + 1, myid + 1);
    }

    if (myid > 0) {

        printf("Receiver: %d. first element %d.\n",
               myid, receiveBuffer[0]);
    }

    /* TODO end */

    /* Finalize measuring the time and print it out */
    t1 = MPI_Wtime();
    MPI_Barrier(MPI_COMM_WORLD);
    fflush(stdout);

    if (myid == 0) {
        printf("\n");
        printf("Timings:\n");
        printf("Time elapsed in rank %2d: %6.3f\n", myid, t1 - t0);
    }
    MPI_Barrier(MPI_COMM_WORLD);
    fflush(stdout);
    if (myid == ntasks - 1) {
        printf("Time elapsed in rank %2d: %6.3f\n", myid, t1 - t0);
    }

    free(message);
    free(receiveBuffer);
    MPI_Finalize();
    return 0;
}