#include "WireCellGenKokkos/KokkosArray.h"
#include "WireCellUtil/Array.h"

#include <iostream>
#include <typeinfo>

using namespace std;
using namespace WireCell;
using namespace WireCell::KokkosArray;

int main(int argc, char* argv[])
{
    int N0 = 2;
    int N1 = 3;
    int group = 2;
    int nrep = 1;
    int print = 0;
    if (argc > 1) N0 = atoi(argv[1]);
    if (argc > 2) N1 = atoi(argv[2]);
    if (argc > 3) group = atoi(argv[3]);
    if (argc > 4) nrep = atoi(argv[4]);
    if (argc > 5) print = atoi(argv[5]);

    Kokkos::initialize(argc, argv);
    while (true)
    {
        Kokkos::Timer timer;

        array_xxf B = Zero<array_xxf>(N0, N1*group);
        Kokkos::parallel_for("modify_B",
                             Kokkos::MDRangePolicy<Kokkos::Rank<2, Kokkos::Iterate::Left>>({0, 0}, {N0, N1*group}),
                             KOKKOS_LAMBDA(const Index& i0, const Index& i1) { B(i0, i1) = i0 * 10 + i1; });
        
        Kokkos::fence();
        timer.reset();
        dft_rc(B);
        Kokkos::fence();
        printf("B: init %f ms.\n", timer.seconds()*1000);

        printf("B dims: %d %d\n", N0, N1*group);
        
        float total_time = 0;
        for (int irep = 0; irep < nrep; ++irep) {
            timer.reset();
            auto tmp = dft_rc(B,0);
            Kokkos::fence();
            if (print) {
                std::cout << "large: " << dump_2d_view(B) << " --> " << dump_2d_view(tmp);
            }
            total_time += timer.seconds();
            printf("irep: %d %f ms.\n", irep, timer.seconds()*1000);
        }
        printf("large: %d : nrep: %d avg: %.2f ms.\n",  N1*group, nrep, total_time/nrep*1000);

        total_time = 0;
        for (int irep = 0; irep < nrep; ++irep) {
            timer.reset();
            auto tmp = dft_rc(B,0,group);
            Kokkos::fence();
            if (print) {
                std::cout << "group: " << dump_2d_view(B) << " --> " << dump_2d_view(tmp);
            }
            total_time += timer.seconds();
            printf("irep: %d %f ms.\n", irep, timer.seconds()*1000);
        }
        printf("group: %d : nrep: %d avg: %.2f ms.\n",  group, nrep, total_time/nrep*1000);



        break;
    }
    Kokkos::finalize();
    return 0;
}
