/* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.ke
 * All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found in the LICENSE file.    */
#include <stdio.h>
#include <stdlib.h>

#include "likwid-marker.h"
#include "parameter.h"
#include "solver.h"
#include "timing.h"
#include "omp.h"

#define LIKWID_PROFILE(tag, call)                                                        \
    startTime = getTimeStamp();                                                          \
    LIKWID_MARKER_START(#tag);                                                           \
    call(&solver);                                                                       \
    LIKWID_MARKER_STOP(#tag);                                                            \
    endTime = getTimeStamp();

enum VARIANT { SOR = 1, RB, RBA };

int main(int argc, char** argv)
{
    int volatile dummy = 0;
    int variant = RB;
    double startTime, endTime;
    Parameter params;
    Solver solver;
    initParameter(&params);
    LIKWID_MARKER_INIT;
    #pragma omp parallel
    {
        if(dummy==1 || omp_get_thread_num()==0)
        printf("OMP_THREADS_DETECTED: %d\n",omp_get_num_threads());
    }
    if (argc < 2) {
        printf("Usage: %s <configFile>\n", argv[0]);
        exit(EXIT_SUCCESS);
    }

    readParameter(&params, argv[1]);
    // printParameter(&params);
    if (argc == 3) {
        variant = atoi(argv[2]);
    }
    if (argc == 4) {
        sscanf("%lf", argv[3], &params.omg);
    }

    initSolver(&solver, &params, 2);
    writeResult(&solver, "p-0.dat");

    switch (variant) {
    case SOR:
        printf("Plain SOR\n");
        fflush(stdout);
        LIKWID_PROFILE("SOR", solve);
        break;
    case RB:
        printf("Red-black SOR\n");
        fflush(stdout);
        LIKWID_PROFILE("RB", solveRB);
        break;
    case RBA:
        printf("Red-black SOR with acceleration\n");
        fflush(stdout);
        LIKWID_PROFILE("RBA", solveRBA);
        break;
    }
    printf(" %.2fs\n", endTime - startTime);
    writeResult(&solver, "p-final.dat");

    LIKWID_MARKER_CLOSE;
    return EXIT_SUCCESS;
}
