/* This file was automatically generated by pyNLControl.*/


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

{%for h in headers%}
#include "{{h}}"{%endfor%}

#define BILLION 1000000000.0
#define COL {{1 + sizeIn | sum() }}

int main(int argc, char const *argv[])
{
    if (argc < 2) {
        printf("Not sufficient input arguments!!!\n");
        printf("Call as: ./EXECUTABLE NROW DATAIN DATAOUT\n");
        return -1;
    }
    int i, j, k, k_prev = 0, IDX_END = 0;

    int ROW = atoi(argv[1]);

    struct timespec start, end;

    FILE *ptr;

    double *DataIn = (double *)malloc(ROW * COL * sizeof(double));

    ptr = fopen(argv[2], "rb");
    fread(DataIn, sizeof(double), ROW * COL, ptr);
    fclose(ptr);

    double t_COMPUTE_TIME = 0.0;

    {%for k in range(varsIn | length)%}
    double *{{varsIn[loop.index0]}} = (double *)malloc({{sizeIn[loop.index0]}} * sizeof(double));{%endfor%}

    {%for k in range(varsOut | length)%}
    double *{{varsOut[loop.index0]}} = (double *)malloc({{sizeOut[loop.index0]}} * sizeof(double));{%endfor%}

    ptr = fopen(argv[3], "w");

    fprintf(ptr, "time,");
    {%for k in range(varsOut | length)%}
    for (i=0; i<{{sizeOut[loop.index0]}}; i++)
        fprintf(ptr, "{{varsOut[loop.index0]}}_%d,", i+1);
    {%endfor%}

    fprintf(ptr, "Computation_time\n");

    for (k = 0; k < ROW; k++)
    {   {%set IDX_Start = namespace(value=0)%}
        {%for k in range(varsIn | length)%}
        for (i = 0; i < {{sizeIn[loop.index0]}}; i++)
            {{varsIn[loop.index0]}}[i] = DataIn[k*COL+{{IDX_Start.value}}+i+1];
            {%set IDX_Start.value = IDX_Start.value + sizeIn[loop.index0]%} 
        {%endfor%}
    
        clock_gettime(CLOCK_REALTIME, &start);
        {{callFuncName}}({%for xin in varsIn%}{{xin}}, {%endfor%}{%for xout in varsOut%}{{xout}}{% if not loop.last %}, {% endif %} {%endfor%});
        clock_gettime(CLOCK_REALTIME, &end);
        t_COMPUTE_TIME = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / BILLION;

        fprintf(ptr, "%f,", DataIn[k*COL]);
        {%set IDX_Start = namespace(value=0)%}
        {%for k in range(varsOut | length)%}
        for (i = 0; i < {{sizeOut[loop.index0]}}; i++)
            fprintf(ptr, "%f,", {{varsOut[loop.index0]}}[i]);
        {%endfor%}

        fprintf(ptr, "%.10f\n", t_COMPUTE_TIME);

        if ((float)(k - k_prev) / ROW * 100 >= 10) {
            printf("%.0f%% complete!!!\n", (float)k / ROW * 100);
            k_prev = k;
        }
    }

    if ((float)k_prev / ROW * 100 < 100)
    {
        printf("100%% complete!!!\n");
    }

    fclose(ptr);

    return 0;

}