Skip to content
Snippets Groups Projects
main.c 2.78 KiB
Newer Older
baptiste.coudray's avatar
baptiste.coudray committed
/**
 * Author: Baptiste Coudray
 * School: HEPIA
 * Class: ITI-3
 * Year: 2020-2021
baptiste.coudray's avatar
baptiste.coudray committed
 */

#include <stdio.h>
#include "stdlib.h"
#include <mpi.h>
#include "../elementary.h"
#include "../../futhark_mpi/dispatch.h"

#define INDEX_2D_TO_1D(y, x, nb_columns) ((y) * (nb_columns) + (x))

#define ROOT_RANK 0
#define N_MEASURES 15

#if defined(FUTHARK_BACKEND_cuda) || defined(FUTHARK_BACKEND_opencl)
baptiste.coudray's avatar
baptiste.coudray committed
#define N_ITERATIONS 300
#else
#define N_ITERATIONS 100
#endif
baptiste.coudray's avatar
baptiste.coudray committed

void init_chunk_elems(chunk_info_t *ci) {
    int8_t *data8 = ci->data;
    for (int i = 0; i < ci->dimensions[1]; ++i) {
        data8[i] = rand() % 2;
    }
}

baptiste.coudray's avatar
baptiste.coudray committed
void compute_next_chunk_elems(struct dispatch_context *dc, struct futhark_context *fc, chunk_info_t *ci) {
    struct futhark_i8_1d *fut_chunk_with_envelope = get_chunk_with_envelope(dc, fc, 1, futhark_new_i8_1d);
baptiste.coudray's avatar
baptiste.coudray committed

    struct futhark_i8_1d *fut_next_chunk_elems;
    futhark_entry_next_chunk_elems(fc, &fut_next_chunk_elems, fut_chunk_with_envelope);
baptiste.coudray's avatar
baptiste.coudray committed
    futhark_context_sync(fc);

    futhark_values_i8_1d(fc, fut_next_chunk_elems, ci->data);
    futhark_context_sync(fc);

    futhark_free_i8_1d(fc, fut_chunk_with_envelope);
baptiste.coudray's avatar
baptiste.coudray committed
    futhark_free_i8_1d(fc, fut_next_chunk_elems);
}

int main(int argc, char *argv[]) {
    if (argc < 3) {
        printf("usage: mpirun -n <nb_proc> %s <nb_devices> <width>\n", argv[0]);
        return EXIT_FAILURE;
    }
    MPI_Init(&argc, &argv);
    int my_rank;
    int world_size;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    struct futhark_context_config *fut_config = futhark_context_config_new();
    int nb_devices = atoi(argv[1]);
#if defined(FUTHARK_BACKEND_cuda) || defined(FUTHARK_BACKEND_opencl)
#if defined(FUTHARK_BACKEND_opencl)
baptiste.coudray's avatar
baptiste.coudray committed
    futhark_context_config_list_devices(fut_config);
#endif
    char device[4] = {0};
    snprintf(device, sizeof(device), "#%d", my_rank % nb_devices);
    futhark_context_config_set_device(fut_config, device);
#endif
    struct futhark_context *fut_context = futhark_context_new(fut_config);

    int elems_n = atoi(argv[2]);
    int elems_dimensions[1] = {elems_n};

    struct dispatch_context *disp_context = dispatch_context_new(elems_dimensions, MPI_INT8_T, 1);
    chunk_info_t ci = get_chunk_info(disp_context);
    init_chunk_elems(&ci);

    for (int i = 0; i < N_MEASURES; ++i) {
        double start = MPI_Wtime();
        for (int j = 0; j < N_ITERATIONS; ++j) {
baptiste.coudray's avatar
baptiste.coudray committed
            compute_next_chunk_elems(disp_context, fut_context, &ci, i, j);
baptiste.coudray's avatar
baptiste.coudray committed
        }
        double finish = MPI_Wtime();
        if (my_rank == ROOT_RANK) {
            printf("%d;%d;%d;%f\n", world_size, nb_devices, elems_n, finish - start);
        }
    }

    dispatch_context_free(disp_context);
    futhark_context_config_free(fut_config);
    futhark_context_free(fut_context);
    return MPI_Finalize();
}