/*
*===============================================================================
*
* d/wilson_d520/dirac_test.c
*
*-------------------------------------------------------------------------------
*
* Copyright (C) 2010 Thomas Streuer 
*
* This file is part of BQCD -- Berlin Quantum ChromoDynamics program
*
* BQCD is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BQCD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BQCD.  If not, see <http:*www.gnu.org/licenses/>.
*
*-------------------------------------------------------------------------------
*/
#include "common.h"
#include "globals.h"
#include "memory.h"
#include "neighb.h"

#include <mpi.h>
#include <time.h>
#include <common/bgp_personality.h>
#include <common/bgp_personality_inlines.h>
#include <spi/DMA_Counter.h>
#include <spi/DMA_Fifo.h>

void multg5(double* psi)
{
  for (unsigned x=0; x<3*volume; ++x, psi+=8)
  {
    for (unsigned a=0; a<4; ++a)
    {
      double tmp = psi[a];
      psi[a]     = psi[a+4];
      psi[a+4]   = tmp;
    }
  }
}

void multg5f(double* psid)
{
  float* psi = (float*) psid;
  for (unsigned x=0; x<3*volume; ++x, psi+=8)
  {
    for (unsigned a=0; a<4; ++a)
    {
      double tmp = psi[a];
      psi[a]     = psi[a+4];
      psi[a+4]   = tmp;
    }
  }
}

void whoami(int rank)
{
  _BGP_Personality_t personality;
  Kernel_GetPersonality(&personality, sizeof(personality));
  printf("[%3i]\t%2i/%2i/%2i/%2i\n", 
	 rank,
	 personality.Network_Config.Xcoord,
	 personality.Network_Config.Ycoord,
	 personality.Network_Config.Zcoord,
	 Kernel_PhysicalProcessorID());
}


int main(int argc, char** argv)
{
  int rank, size;
  MPI_Init(&argc, &argv);
  MPI_Comm_size(MPI_COMM_WORLD, &size);
  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
  int lat[4], grid[4];
  for (unsigned i=0; i<4; ++i)
    lat[i] = atoi(argv[1+i]);
  for (unsigned i=0; i<4; ++i)
    grid[i] = atoi(argv[5+i]);
  int n = atoi(argv[9]);
  int dagger = 0;
  if (argv[10][0] == 'd')
    dagger = 1;
  mesh_correct = atoi(argv[11]);
  int single = 0;
  if (argv[12][0] == 's')
    single = 1;
  init(lat, grid);
  init_shmem();
  double* U    = new_gc_dbl();
  double* in   = new_vec_shmem();
  double* out  = new_vec();

  double* inf  = new_vec_shmem();
  double* outf = new_vec();
  for (unsigned i=0; i<24*volume; ++i)
  {
    out[i]  = 0.;
    outf[i] = 0.;
  }
  read_gc("conf", U);
  read_vec("input", in);

  for (unsigned oe=0; oe<2; ++oe)
  {
    const unsigned d = 8 * sizeof(uint16_t) / sizeof(double);
    for (unsigned x=0; x<half_volume[oe]; ++x)
      for (unsigned i=0; i<144; ++i)
	wilson_data.w_data[0][oe][0].U[(144 + d) * x + i + d] =
	  U[(144 + d) * (x + oe * half_volume[0]) + i + d];
  }

  if (T0_LAST)
  {
    const unsigned d = 8 * sizeof(uint16_t) / sizeof(double);
    double* tmp_U = malloc(sizeof(*tmp_U) * (volume_slice + 1) * 72);
    assert(tmp_U);
    for (unsigned oe=0; oe<2; ++oe)
    {
      for (unsigned x=0; x<half_slice[oe]; ++x)
	for (unsigned i=0; i<144; ++i)
	  tmp_U[144 * x + i] = wilson_data.w_data[0][oe][0].U[(144 + d) * x + d + i];
      for (unsigned x=0; x<half_volume[oe]-half_slice[oe]; ++x)
	for (unsigned i=0; i<144; ++i)
	  wilson_data.w_data[0][oe][0].U[(144 + d) * x + d + i] =
	    wilson_data.w_data[0][oe][0].U[(144 + d) * (x + half_slice[oe]) + d + i];
      for (unsigned x=0; x<half_slice[oe]; ++x)
	for (unsigned i=0; i<144; ++i)
	  wilson_data.w_data[0][oe][0].U[(144 + d) * (x + half_volume[oe] - half_slice[oe]) + d + i] = tmp_U[144 * x + i];
    }
    free(tmp_U);
  }

  if (single)
    for (unsigned oe=0; oe<2; ++oe)
    {
      const unsigned dd = 8 * sizeof(uint16_t) / sizeof(double);
      const unsigned df = 8 * sizeof(uint16_t) / sizeof(float);
      for (unsigned x=0; x<half_volume[oe]; ++x)
	for (unsigned i=0; i<144; ++i)
	  ((float*) (wilson_data.w_data[1][oe][0].U))[(144 + df) * x + i + df] =
	    wilson_data.w_data[1][oe][0].U[(144 + dd) * x + i + dd];
    }  

  printf("[%i]\tU %p (% e)\tin %p (% e)\tout %p\tcb %p %p\n", rank, wilson_data.w_data[0][0][0].U, wilson_data.w_data[0][0][0].U[2], in, *in, out,
	 wilson_data.w_data[0][0][0].comm_buffer, wilson_data.w_data[0][0][0].U);
  printf("[%i]\tW %p\tN %p %p\n", rank, wilson_data, wilson_data.w_data[0][0][0].neighb, wilson_data.w_data[0][1][0].neighb);
  if (my_rank == 0)
    printf("[%i]\tdagger %i, mesh_correct %i\n", rank, dagger, mesh_correct);
  int zero = 0;
  int one  = 1;

  if (single)
    for (unsigned i=0; i<24*volume; ++i)
      ((float*) inf)[i] = in[i];

  MPI_Barrier(MPI_COMM_WORLD);
  if (dagger)
    if (!single)
      multg5(in);
    else
      multg5f(inf);
  time_t t = time(0);
  if (!single)
    for (unsigned i=0; i<n; ++i)
      bgp_wilson_hop(in  + 24 * half_volume[0], out, &zero, &dagger, &single);
  else
    for (unsigned i=0; i<n; ++i)
      bgp_wilson_hop(inf + 12 * half_volume[0], out, &zero, &dagger, &single);
  t = time(0)-t;

  if (!single)
    bgp_wilson_hop(in, out  + 24 * half_volume[0], &one, &dagger, &single);
  else
    bgp_wilson_hop(in, outf + 12 * half_volume[0], &one, &dagger, &single);

  if (dagger)
    if (!single)
      multg5(out);
    else
      multg5f(outf);
  if (single)
    for (unsigned i=0; i<24*volume; ++i)
      out[i] = ((float*) outf)[i];
  if (rank == 0)
    printf("T %i\n", (int) t);
  MPI_Barrier(MPI_COMM_WORLD);
  write_vec("output", out);
  print_debug_buffer();

  MPI_Barrier(MPI_COMM_WORLD);
  MPI_Finalize();
  return 0;
}
