/*
 *   -- micMAGMA (version 0.1) --
 *      Univ. of Tennessee, Knoxville
 *      Univ. of California, Berkeley
 *      Univ. of Colorado, Denver
 *      April 2012
 *
 * @precisions mixed zc -> ds
 */

// defines stub functions, if no GPU implementation is available.
#include <stdio.h>
#include <stdlib.h>
#include "magma.h"

#ifdef HAVE_MIC

#include "magma_mic_bindings.h"


magma_err_t
magma_zlag2c( magma_int_t m, magma_int_t n, 
              magmaDoubleComplex_ptr dA, size_t dA_offset, magma_int_t lda,
              magmaFloatComplex_ptr dAs, size_t dAs_offset, magma_int_t ldas,
              magma_queue_t queue)
{
  magma_event_t event;
  int err;
  magma_mic_zlag2c_param zlag2c_param;

  zlag2c_param.m                  =       m;
  zlag2c_param.n                  =       n;
  zlag2c_param.dA                 =       dA + dA_offset;
  zlag2c_param.lda                =       lda;
  zlag2c_param.dAs                =       dAs + dAs_offset;
  zlag2c_param.ldas               =       ldas;

  int control_msg         =       magma_mic_ZLAG2C;
  if ((err = scif_send(gEpd, &control_msg, sizeof(control_msg), 1)) <= 0) {
    err = errno;
    printf("scif_send failed with err %d\n", errno);
    fflush(stdout);
  }

  if ((err = scif_send(gEpd, &zlag2c_param, sizeof(zlag2c_param), 1)) <= 0) {
    err = errno;
    printf("scif_send failed with err %d\n", errno);
    fflush(stdout);
  }
  return MAGMA_SUCCESS;
}

magma_err_t
magma_zcgesv(magma_int_t N, magma_int_t NRHS,
             magmaDoubleComplex_ptr dA, size_t dA_offset, magma_int_t ldda,
             magma_int_t *dIPIV,
             magmaDoubleComplex_ptr dB, size_t dB_offset, magma_int_t lddb,
             magmaDoubleComplex_ptr dX, size_t dX_offset, magma_int_t lddx,
             magmaDoubleComplex_ptr dworkd, magmaFloatComplex_ptr dworks,
             magma_queue_t queue )
{
  magma_event_t event;
  int err;
  magma_mic_zcgesv_param zcgesv_param;

  zcgesv_param.n      = N;
  zcgesv_param.nrhs   = NRHS;
  zcgesv_param.d_A    = dA + dA_offset;
  zcgesv_param.lda    = ldda;
  zcgesv_param.d_ipiv = dIPIV;
  zcgesv_param.d_B    = dB + dB_offset;
  zcgesv_param.ldb    = lddb;
  zcgesv_param.d_X    = dX + dX_offset;
  zcgesv_param.ldx    = lddx;
  zcgesv_param.d_work = dworkd;
  zcgesv_param.d_swork= dworks;

  int control_msg     = magma_mic_ZCGESV;
  if ((err = scif_send(gEpd, &control_msg, sizeof(control_msg), 1)) <= 0) {
    err = errno;
    printf("scif_send failed with err %d\n", errno);
    fflush(stdout);
  }

  if ((err = scif_send(gEpd, &zcgesv_param, sizeof(zcgesv_param), 1)) <= 0) {
    err = errno;
    printf("scif_send failed with err %d\n", errno);
    fflush(stdout);
  }

  magma_mic_synchronize_kernels();

  return MAGMA_SUCCESS;
}


#endif // HAVE_MIC
