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

/**
 *
 * 	@file ddss_test_dpotrf.c
 *
 * 	@brief LASs-DDSs ddss_test_dgemm 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-08-16
 * 	@reviewer 
 * 	@modified 
 *
 **/

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

/**
 *
 *	@sa ddss_dpotrf
 *	@sa kdpotrf
 *
 **/ 

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

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

	
	// Local variables
	int i, j;
	int n;
	const char *uplo_input = NULL;
	enum DDSS_UPLO uplo = Lower;
	double error;
	double max_error;
	double count_error;	
	double *A;
	double *A_test;
	double *A_ref;
	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] );
	
	if ( strlen( argv[2] ) != 1 ) 
	{
		fprintf( stderr,"Illegal value of UPLO, UPLO can be L or U\n" );
		return NoSuccess;
	}
	uplo_input = argv[2];	
	
	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 ( 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 );

	// Matrix A initialization
    retval = LAPACKE_dlarnv( 1, seed, n * n, A );
    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 ) );

	// DPOTRF FLOPS
	flops = FLOPS_DPOTRF( n ); 
		
	gettimeofday( &start, NULL );

	LAPACKE_dpotrf_work( LAPACK_ROW_MAJOR, 
						 uplo_input[0],
        				 n, A_ref, n );
	
	gettimeofday( &end, NULL );
	
	// FLOPS achieved by the (reference) cblas_dgemm routine
	flops_ref = (double) flops / (((end.tv_sec * 1e6 + end.tv_usec)
				 - (start.tv_sec * 1e6 + start.tv_usec)) / 1e6);

	ret = ddss_dpotrf( uplo, n, A_test, n );
	
	gettimeofday( &start, NULL );
	
	ret = ddss_dpotrf( uplo, n, A, n );
	
	gettimeofday( &end, NULL );
	if ( ret == NoSuccess )
		return ret;

	// TIME consumed by ddss_dpotrf	
	time = ( double ) (((end.tv_sec * 1e6 + end.tv_usec)
				- (start.tv_sec * 1e6 + start.tv_usec)) / 1e6);
	
	// FLOPS achieved by the ddsst_dpotrf routine
	flops_ddss = flops / time;
	
	// Error computation
	for ( i = 0; i < n; i++ )
	{
		for ( j = 0; j < n; j++ )
		{
			error = fabs( A[ i * n + j ] - A_ref[ i * n + 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_DPOTRF TIME(s) = %1.2f\n", time );
	fprintf( stdout, "DDSs_DPOTRF GFLOPS = %1.2f\n", flops_ddss / 1e9 );
	fprintf( stdout, "REF. DPOTRF GFLOPS = %1.2f\n", flops_ref / 1e9 );

	free( A );
	free( A_test );
	free( A_ref );
	
	return Success;

}
