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

/**
 *
 * 	@file ddss_test_dposv.c
 *
 * 	@brief LASs-DDSs ddss_test_dposv 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 2017-05-04
 * 	@reviewer 
 * 	@modified 
 *
 **/

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

/**
 *
 *	@sa ddss_dposv
 *	@sa kdposv
 *
 **/ 

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

	if( argc < 4 || argc > 4 )
	{ 		
		fprintf( stderr, "Usage: ./test_dposv N NRHS UPLO\n" );
		return NoSuccess;
	}
	
	// Local variables
	int i, j;
	int n, nrhs;
	const char *uplo_input = NULL;
	enum DDSS_UPLO uplo;
	enum DDSS_TRANS trans;
	double error;
	double max_error;
	double count_error;	
	double *A;
	double *A_ref;
	double *A_test;
	double *B;
	double *B_ref;
	double *B_test;
	struct timeval start, end;
	double time;
	double flops;
	double flops_ddss; 
	double flops_ref; 
	enum LASS_RETURN ret;
	lapack_int retval;
	int seed[] = {0, 0, 0 , 1};

	n = atoi( argv[1] );
	nrhs = atoi( argv[2] );
	
	if ( strlen( argv[3] ) != 1 ) 
	{
		fprintf( stderr,"Illegal value of UPLO, UPLO can be L or U\n" );
		return NoSuccess;
	}
	uplo_input = argv[3];	
	
	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;
	}

	if ( uplo_input[0] == 'U' )
	{
		uplo = Upper;
	}
	else if ( uplo_input[0] == 'L' )
	{
		uplo = Lower;
	}
	else
	{
		fprintf( stderr, "Illegal value of UPLO, UPLO can be U or L\n" );
		return NoSuccess;
	}
	
	// Matrices allocation
	A = ( double * ) malloc( sizeof( double ) * n * n );
	A_ref = ( double * ) malloc( sizeof( double ) * n * n );
	A_test = ( 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 );

	// Matrix A initialization
    retval = LAPACKE_dlarnv( 1, seed, n * n, A );
    assert( retval == 0 );
	
	// Matrix B initialization
    retval = LAPACKE_dlarnv( 1, seed, n * nrhs, B );
    assert( retval == 0 );
	
	// Make A matrix symmetric positive definite
	for ( i = 0; i < n; i++ )
	{
		A[ i * n + i ] += n; 
		for ( j = 0; j < n; j++ )
		{
			A[ j * n + i ] = A[ i * n + j ];
		}
	}

	memcpy( A_ref, A, n * n * sizeof( double ) );
	memcpy( A_test, A, n * n * sizeof( double ) );
	memcpy( B_ref, B, n * nrhs * sizeof( double ) );
	memcpy( B_test, B, n * nrhs * sizeof( double ) );

	// DPOTRF FLOPS
	flops = FLOPS_DPOTRF( n ); 
	flops += 2 * FLOPS_DTRSM( Left, n, nrhs ); 
		
	gettimeofday( &start, NULL );

	LAPACKE_dpotrf_work( LAPACK_ROW_MAJOR,
                         uplo_input[0],
                         n, A_ref, n );

	if ( uplo == Lower )
	{
		trans = NoTrans;
	}
	else
	{
		trans = Trans;
	}

	cblas_dtrsm( CblasRowMajor,
				 ( CBLAS_SIDE ) Left, ( CBLAS_UPLO ) uplo,
				 ( CBLAS_TRANSPOSE ) trans,
				 ( CBLAS_DIAG ) NonUnit,
				 n,
				 nrhs,
				 1.0, A_ref, n,
					  B_ref, nrhs );
	
	if ( uplo == Lower )
	{
		trans = Trans;
	}
	else
	{
		trans = NoTrans;
	}
	
	cblas_dtrsm( CblasRowMajor,
				 ( CBLAS_SIDE ) Left, ( CBLAS_UPLO ) uplo,
				 ( CBLAS_TRANSPOSE ) trans,
				 ( CBLAS_DIAG ) NonUnit,
				 n,
				 nrhs,
				 1.0, A_ref, n,
					  B_ref, nrhs );
	
	gettimeofday( &end, NULL );
	
	// FLOPS achieved by the (reference) routine
	flops_ref = (double) flops / (((end.tv_sec * 1e6 + end.tv_usec)
				 - (start.tv_sec * 1e6 + start.tv_usec)) / 1e6);

	ret = ddss_dposv( uplo,
					  n, nrhs, 
					  A_test,    n, 
					  B_test, nrhs );
	
	gettimeofday( &start, NULL );
	
	ret = ddss_dposv( uplo,
					  n, nrhs, 
					  A,    n, 
					  B, nrhs );
	
	gettimeofday( &end, NULL );
	if ( ret == NoSuccess )
		return ret;

	// TIME consumed by ddss_dposv	
	time = ( double ) (((end.tv_sec * 1e6 + end.tv_usec)
				- (start.tv_sec * 1e6 + start.tv_usec)) / 1e6);
	
	// FLOPS achieved by the ddss_dposv routine
	flops_ddss = flops / time;
	
	// Error computation
	for ( i = 0; i < n; i++ )
	{
		for ( j = 0; j < nrhs; j++ )
		{
			error = fabs( B[ i * nrhs + j ] - B_ref[ i * nrhs + j ] );
			if ( max_error < 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 * n ) );
	
	fprintf( stdout, "DDSs_DPOSV TIME(s) = %1.2f\n", time );
	fprintf( stdout, "DDSs_DPOSV GFLOPS = %1.2f\n", flops_ddss / 1e9 );
	fprintf( stdout, "REF. DPOSV GFLOPS = %1.2f\n", flops_ref / 1e9 );

	free( A );
	free( A_ref );
	free( A_test );
	free( B );
	free( B_ref );
	free( B_test );
	
	return Success;

}
