Skip to content
Snippets Groups Projects
main.c 2.86 KiB
Newer Older
/**
 * Author: Baptiste Coudray
 * School: HEPIA
 * Class: ITI-3
 * Year 2020-2021
 */

#include <stdio.h>
#include "stdlib.h"
#include <mpi.h>
baptiste.coudray's avatar
baptiste.coudray committed
#include "lbm.h"
#include "../futhark_mpi/dispatch.h"

#define INDEX_3D_TO_1D(y, x, z, nb_columns, nb_depths) ((x) + (nb_columns) * ((y) + (nb_depths) * (z)))

#define N_ITERATIONS 200

void init_chunk_lbm(chunk_info_t *ci) {
    int *data32 = ci->data;
    for (int i = 0; i < ci->dimensions[0]; ++i) {
        for (int j = 0; j < ci->dimensions[1]; ++j) {
            for (int k = 0; k < ci->dimensions[2]; ++k) {
                int idx = INDEX_3D_TO_1D(i, j, k, ci->dimensions[1], ci->dimensions[2]);
                data32[idx] = rand() % 2;
            }
        }
    }
}

void compute_next_lbm(struct dispatch_context *dc, struct futhark_context *fc, chunk_info_t *ci) {
    envelope_t *outer_envelope = get_outer_envelope(dc, fc, 1);
baptiste.coudray's avatar
baptiste.coudray committed
    struct futhark_opaque_envelope_3d_f32 *fut_outer_envelope = futhark_outer_envelope_new(dc, fc, outer_envelope,
                                                                                           futhark_restore_opaque_envelope_3d_f32,
                                                                                           FUTHARK_F32);
baptiste.coudray's avatar
baptiste.coudray committed
    struct futhark_f32_3d *fut_chunk_lbm = futhark_new_f32_3d(fc, ci->data, ci->dimensions[0], ci->dimensions[1],
baptiste.coudray's avatar
baptiste.coudray committed
    struct futhark_f32_3d *fut_next_chunk_lbm;
baptiste.coudray's avatar
baptiste.coudray committed
    futhark_entry_next_chunk_lbm(fc, &fut_next_chunk_lbm, fut_chunk_lbm, fut_outer_envelope);
baptiste.coudray's avatar
baptiste.coudray committed
    futhark_values_f32_3d(fc, fut_next_chunk_lbm, ci->data);
baptiste.coudray's avatar
baptiste.coudray committed
    futhark_free_f32_3d(fc, fut_next_chunk_lbm);
    futhark_free_f32_3d(fc, fut_chunk_lbm);
    futhark_free_opaque_envelope_3d_f32(fc, fut_outer_envelope);
    envelope_free(outer_envelope);
}

int main(int argc, char *argv[]) {
    if (argc < 4) {
        printf("usage: mpirun -n <nb_proc> %s <height> <width> <depth>\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();
    struct futhark_context *fut_context = futhark_context_new(fut_config);

    int lbm_dimensions[3] = {atoi(argv[1]), atoi(argv[2]), atoi(argv[3])};

    struct dispatch_context *disp_context = dispatch_context_new(lbm_dimensions, MPI_FLOAT, 3);
    chunk_info_t ci = get_chunk_info(disp_context);
    init_chunk_lbm(&ci);

    for (int i = 0; i < N_ITERATIONS; ++i) {
baptiste.coudray's avatar
baptiste.coudray committed
        compute_next_lbm(disp_context, fut_context, &ci);
    }

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