/* Two wrappers to make Fortran-style dgesv_ and sgesv_ call
 * cusolverDnDDgesv and cusolverDnSSgesv from cuSOLVER
 *
 * At link time need -lcusolver -lcudart
 *
 * MJR 11/2025
 */

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <cuda_runtime.h>
#include <cublas_v2.h>
#include <cusolverDn.h>

void dgesv_(int *n, int *nrhs, double *a, int *lda, int *ipiv, double *b,
	   int *ldb, int *info);

void sgesv_(int *n, int *nrhs, float *a, int *lda, int *ipiv, float *b,
	   int *ldb, int *info);


void dgesv_(int *n, int *nrhs, double *a, int *lda, int *ipiv, double *b,
	   int *ldb, int *info){

  double *GPU_a, *GPU_b,*GPU_x,*GPU_work;
  int *GPU_ipiv,llda,lldb,lldx,niters,*GPU_dinfo,dinfo;
  size_t lwork;
  cudaError_t status;
  cusolverStatus_t cu_status;
  cusolverDnHandle_t handle ;

  status=cudaMalloc((void**)(&GPU_a),(*n)*(*lda)*sizeof(double));
  assert (status==0);
  status=cudaMalloc((void**)(&GPU_b),(*n)*(*nrhs)*sizeof(double));
  assert (status==0);
  status=cudaMalloc((void**)(&GPU_x),(*n)*(*nrhs)*sizeof(double));
  assert (status==0);
  status=cudaMalloc((void**)(&GPU_ipiv),(*n)*sizeof(int));
  assert (status==0);

  printf("Memory reserved on GPU\n");
  
  status=cudaMemcpy(GPU_a,a,(*n)*(*lda)*sizeof(double),cudaMemcpyHostToDevice);
  assert (status==0);
  status=cudaMemcpy(GPU_b,b,(*n)*(*nrhs)*sizeof(double),cudaMemcpyHostToDevice);
  assert (status==0);

  printf("Arrays copied to GPU\n");
  
  cusolverDnCreate(&handle);

  printf("Handle obtained\n");

  llda=*lda;
  lldb=*ldb;
  lldx=*n;
  
  cusolverDnDDgesv_bufferSize(handle,*n,*nrhs,NULL,llda,NULL,NULL,
			      lldb,NULL,lldx,NULL,&lwork);
  printf("Workspace size requested %ld\n",(long)lwork);
  status=cudaMalloc((void**)(&GPU_work),lwork);
  assert (status==0);
  status=cudaMalloc((void**)(&GPU_dinfo),sizeof(int));
  assert (status==0);

  printf("Workspace reserved on GPU, calling solver\n");
  
  cu_status=cusolverDnDDgesv(handle,*n,*nrhs,GPU_a,llda,GPU_ipiv,GPU_b,lldb,
			     GPU_x,lldx,GPU_work,lwork,&niters,GPU_dinfo);

  if (status!=0){
    printf("Status=%d\n",(int)cu_status);
    printf("niters=%d\n",niters);

    status=cudaMemcpy(&dinfo,GPU_dinfo,sizeof(int),cudaMemcpyDeviceToHost);
    assert (status==0);
    printf("dinfo=%d\n",dinfo);
  }

  
  status=cudaMemcpy(b,GPU_x,(*n)*(*nrhs)*sizeof(double),cudaMemcpyDeviceToHost);
  assert (status==0);

  cudaFree(GPU_dinfo);
  cudaFree(GPU_work);
  cudaFree(GPU_ipiv);
  cudaFree(GPU_x);
  cudaFree(GPU_b);
  cudaFree(GPU_a);

  cusolverDnDestroy(handle);
  
}



void sgesv_(int *n, int *nrhs, float *a, int *lda, int *ipiv, float *b,
	   int *ldb, int *info){

  float *GPU_a, *GPU_b,*GPU_x,*GPU_work;
  int *GPU_ipiv,llda,lldb,lldx,niters,*GPU_dinfo,dinfo;
  size_t lwork;
  cudaError_t status;
  cusolverStatus_t cu_status;
  cusolverDnHandle_t handle ;

  status=cudaMalloc((void**)(&GPU_a),(*n)*(*lda)*sizeof(float));
  assert (status==0);
  status=cudaMalloc((void**)(&GPU_b),(*n)*(*nrhs)*sizeof(float));
  assert (status==0);
  status=cudaMalloc((void**)(&GPU_x),(*n)*(*nrhs)*sizeof(float));
  assert (status==0);
  status=cudaMalloc((void**)(&GPU_ipiv),(*n)*sizeof(int));
  assert (status==0);

  printf("Memory reserved on GPU\n");
  
  status=cudaMemcpy(GPU_a,a,(*n)*(*lda)*sizeof(float),cudaMemcpyHostToDevice);
  assert (status==0);
  status=cudaMemcpy(GPU_b,b,(*n)*(*nrhs)*sizeof(float),cudaMemcpyHostToDevice);
  assert (status==0);

  printf("Arrays copied to GPU\n");
  
  cusolverDnCreate(&handle);

  printf("Handle obtained\n");

  llda=*lda;
  lldb=*ldb;
  lldx=*n;
  
  cusolverDnSSgesv_bufferSize(handle,*n,*nrhs,NULL,llda,NULL,NULL,
			      lldb,NULL,lldx,NULL,&lwork);
  printf("Workspace size requested %ld\n",(long)lwork);
  status=cudaMalloc((void**)(&GPU_work),lwork);
  assert (status==0);
  status=cudaMalloc((void**)(&GPU_dinfo),sizeof(int));
  assert (status==0);

  printf("Workspace reserved on GPU, calling solver\n");
  
  cu_status=cusolverDnSSgesv(handle,*n,*nrhs,GPU_a,llda,GPU_ipiv,GPU_b,lldb,
			     GPU_x,lldx,GPU_work,lwork,&niters,GPU_dinfo);

  if (status!=0){
    printf("Status=%d\n",(int)cu_status);
    printf("niters=%d\n",niters);

    status=cudaMemcpy(&dinfo,GPU_dinfo,sizeof(int),cudaMemcpyDeviceToHost);
    assert (status==0);
    printf("dinfo=%d\n",dinfo);
  }

  
  status=cudaMemcpy(b,GPU_x,(*n)*(*nrhs)*sizeof(float),cudaMemcpyDeviceToHost);
  assert (status==0);

  cudaFree(GPU_dinfo);
  cudaFree(GPU_work);
  cudaFree(GPU_ipiv);
  cudaFree(GPU_x);
  cudaFree(GPU_b);
  cudaFree(GPU_a);

  cusolverDnDestroy(handle);
  
}
