/*
 * Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
 * All rights reserved. This file is part of nusif-solver.
 * Use of this source code is governed by a MIT style
 * license that can be found in the LICENSE file.
 */
#ifndef __COMM_H_
#define __COMM_H_
#include <mpi.h>

enum direction { LEFT = 0, RIGHT, BOTTOM, TOP, NDIRS };
enum dimension { JDIM = 0, IDIM, NDIMS };
enum layer { HALO = 0, BULK };
enum op { MAX = 0, SUM };

typedef struct {
    int rank;
    int size;
    MPI_Comm comm;
    MPI_Datatype sbufferTypes[NDIRS];
    MPI_Datatype rbufferTypes[NDIRS];
    int neighbours[NDIRS];
    int coords[NDIMS], dims[NDIMS];
    int imaxLocal, jmaxLocal;
} Comm;

extern void commInit(Comm* c, int jmax, int imax);
extern void commPrintConfig(Comm*);
extern void commExchange(Comm*, double*);
extern void commShift(Comm* c, double* f, double* g);
extern void commReduction(double* v, int op);
extern int commIsBoundary(Comm* c, int direction);
extern void commCollectResult(Comm* c,
    double* ug,
    double* vg,
    double* pg,
    double* u,
    double* v,
    double* p,
    int jmax,
    int imax);

static inline int commIsMaster(Comm* c) { return c->rank == 0; }
#endif // __COMM_H_
