#include "../include/lass.h"

/**
 *
 * 	@file ddss_test_dtpgesv.c
 *
 * 	@brief LASs-DDSs ddss_test_dtpgesv routine.
 *
 * 	LASs-DDSs is a software package provided by:
 * 	Barcelona Supercomputing Center - Centro Nacional de Supercomputacion
 *
 * 	@author Pedro Valero-Lara pedro.valero@bsc.es
 * 	@date 2018-09-22
 * 	@reviewer 
 * 	@modified 
 *
 **/

/**
 *  
 *	@ingroup DDSs
 *   
 *  main:
 *	Performs the testing of the ddss_dtpgesv routine. 
 *
**/

/**
 *
 *	@sa ddss_dtpgesv
 *	@sa kdtpgesv
 *
 **/

int main( int argc, char const *argv[] )
{

	if ( argc < 3 || argc > 3 )
	{ 		
		fprintf(stderr, "Usage: ./test_dtpgesv N NRHS\n" );
		return NoSuccess;
	}

	// Local variables
	int i, j;
	int n, nrhs;
	int ldA, ldB;
	double error;
	double max_error;
	double count_error;	
	double *A;
	double *A_ref;
	double *A_warmup;
	double *B;
	double *B_ref;
	double *B_test;
	double *B_warmup;
	double *L;
	double *U;
	struct timeval start, end;
	double time;
	double flops;
	double flops_ref;
	double flops_ddss;
	enum LASS_RETURN ret;
	lapack_int retval;
	int *ipiv;
	int *ipiv_ref;
	int *ipiv_warmup;
	int seed[] = {0, 0, 0 , 1};

	n 	 = atoi( argv[1] );
	nrhs = atoi( argv[2] );
	
	// Row mayor order
	ldA = n; 
	ldB = nrhs; 
	
	max_error = 0.0;
	count_error = 0.0;

	// Checking inputs
	if ( n < 0 )
	{
		fprintf(stderr, "Illegal value of N, N must be >= 0\n");
		return NoSuccess;
	}
	if ( nrhs < 0 )
	{
		fprintf(stderr, "Illegal value of NRHS, NRHS must be >= 0\n");
		return NoSuccess;
	}
	
	// Matrices allocation
	A = ( double * ) malloc( sizeof( double ) * n * n );
	A_ref = ( double * ) malloc( sizeof( double ) * n * n );
	A_warmup = ( double * ) malloc( sizeof( double ) * n * n );
	B = ( double * ) malloc( sizeof( double ) * n * nrhs );
	B_ref = ( double * ) malloc( sizeof( double ) * n * nrhs );
	B_test = ( double * ) malloc( sizeof( double ) * n * nrhs );
	B_warmup = ( double * ) malloc( sizeof( double ) * n * nrhs );
	L = ( double * ) calloc(  n * n, sizeof( double ) );
	U = ( double * ) calloc(  n * n, sizeof( double ) );
	ipiv = ( int* ) malloc( n * sizeof( int ) );
	ipiv_ref = ( int* ) malloc( n * sizeof( int ) );
	ipiv_warmup = ( int* ) malloc( n * sizeof( int ) );

	// Matrix A initialization
    retval = LAPACKE_dlarnv( 1, seed, n * n, A );
    assert( retval == 0 );
    retval = LAPACKE_dlarnv( 1, seed, n * nrhs, B );
    assert( retval == 0 );
   
	// Copy matrices for testing purpose
	memcpy( A_ref, A, n * n * sizeof( double ) );
    memcpy( A_warmup, A, n * n * sizeof( double ) );
	memcpy( B_ref, B, n * nrhs * sizeof( double ) );
    memcpy( B_test, B, n * nrhs * sizeof( double ) );
    memcpy( B_warmup, B, n * nrhs * sizeof( double ) );
			
	// DGESV FLOPS
	flops = FLOPS_DGETRF( n, n );
	flops += 2 * FLOPS_DTRSM( Left, n, nrhs );
	
	gettimeofday( &start, NULL );

	/*	
	LAPACKE_dgetrf( CblasRowMajor,
					n, n, 
					A_ref, ldA, 
					ipiv_ref );

	
	LAPACKE_dlaswp( CblasRowMajor,
				    nrhs, B_ref, ldB,
					1, n,
					ipiv_ref, 1 );

	cblas_dtrsm( CblasRowMajor,
                 ( CBLAS_SIDE ) Left, ( CBLAS_UPLO ) Lower,
                 ( CBLAS_TRANSPOSE ) NoTrans,
                 ( CBLAS_DIAG ) Unit,
                 n,
                 nrhs,
                 1.0, A_ref, n,
                      B_ref, nrhs );

    cblas_dtrsm( CblasRowMajor,
                 ( CBLAS_SIDE ) Left, ( CBLAS_UPLO ) Upper,
                 ( CBLAS_TRANSPOSE ) NoTrans,
                 ( CBLAS_DIAG ) NonUnit,
                 n,
                 nrhs,
                 1.0, A_ref, n,
                      B_ref, nrhs );
	*/
	
	gettimeofday( &end, NULL );
	
	flops_ref = ( double ) flops / ( ( ( end.tv_sec * 1e6 + end.tv_usec )
			    - ( start.tv_sec * 1e6 + start.tv_usec ) ) / 1e6 );
	
	ret = ddss_dtpgesv( n, nrhs,
					    A_warmup, ldA,
					    ipiv_warmup,
					    B_warmup, ldB );
	
	gettimeofday( &start, NULL );
	
	ret = ddss_dtpgesv( n, nrhs,
					    A, ldA,
					    ipiv,
					    B, ldB );
	
	gettimeofday( &end, NULL );
	if ( ret == NoSuccess )
		return ret;
	
	// Execution time
	time = ( double ) ( ( ( end.tv_sec * 1e6 + end.tv_usec ) 
			- ( start.tv_sec * 1e6 + start.tv_usec ) ) / 1e6 );

	// FLOPS achieved by the ddss_dgetrf routine
	flops_ddss = flops / time;

	/*
	LAPACKE_dlaswp( CblasRowMajor,
				    nrhs, B_test, ldB,
					1, n,
					ipiv, 1 );
	
	cblas_dtrsm( CblasRowMajor,
                 ( CBLAS_SIDE ) Left, ( CBLAS_UPLO ) Lower,
                 ( CBLAS_TRANSPOSE ) NoTrans,
                 ( CBLAS_DIAG ) Unit,
                 n,
                 nrhs,
                 1.0, A, n,
                      B_test, nrhs );

    cblas_dtrsm( CblasRowMajor,
                 ( CBLAS_SIDE ) Left, ( CBLAS_UPLO ) Upper,
                 ( CBLAS_TRANSPOSE ) NoTrans,
                 ( CBLAS_DIAG ) NonUnit,
                 n,
                 nrhs,
                 1.0, A, n,
                      B_test, nrhs );

	// Error computation
	for ( int i = 0; i < n; i++ ) 
	{
		for ( int j = 0; j < nrhs; j++ ) 
		{
			error = fabs( B[i * nrhs + j] - B_test[i * nrhs + j] );
			if ( error > max_error )
				max_error = error;
			count_error += error;
		}
	}
	*/
	
	fprintf( stdout, "Max. error = %1.2f\n", max_error );
	fprintf( stdout, "Av. error = %1.2f\n", count_error / ( n * nrhs ) );
	
	fprintf( stdout, "DDSs_DTPGESV TIME(s) = %1.2f\n", time );
	fprintf( stdout, "DDSs_DTPGESV GFLOPS = %1.2f\n", flops_ddss / 1e9 );
	fprintf( stdout, "REF. DGESV GFLOPS = %1.2f\n", flops_ref / 1e9 );
	
	free( A );
	free( A_ref );
	free( A_warmup );
	free( B );
	free( B_ref );
	free( B_test );
	free( B_warmup );
	free( U );
	free( L );
	free( ipiv );
	free( ipiv_ref );
	free( ipiv_warmup );

	return Success;

}
