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

/**
 *
 * 	@file ddss_dtpgesv.c
 *
 * 	@brief LASs-DDSs ddss_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-20
 * 	@reviewer 
 * 	@modified 
 *
 **/

/**
 *  
 *	@ingroup DDSS
 *   
 *	Solves a system of linear equations A \times X = B, where A is a N-by-N
 *	general matrix and X and B are N-by-NRHS matrices. The matrix A is 
 *	factorized using the LU descomposition with tiled-pivoting.
 *	The matrix A is descomposed as:
 *
 *		A = P * L * U
 *
 *	where P is a permutation matrix, L is a lower triangular matrix with unit 
 *  diagonal elements and U is an upper triangular matrix.
 *
**/

/**
 *
 *	@param[in]
 *	N       int.
 *          N specifies the order of the square matrix A. N >= 0.
 *	
 *	NRHS    int.
 *          NRHS specifies the number of right-hand-sides (number 
 *			of columns of B). NRHS >= 0.
 *
 *	@param[in,out]
 *  A  		double *.
 *	    	A is a pointer to a regular matrix of dimension N-by-LDA.
 *          On exit, if return value is Success, the matrix A is overwriten by 
 *          the factors L and U. The unit diagonal elements of L are not stored.
 *   
 *	@param[in]
 *  LDA     int.
 * 	    	LDA specifies the number of columns of A ( row-major order ).
 * 	    	LDA must be at least max( 1, N ).
 * 	    	
 * 	@param[out]
 * 	IPIV    int *.
 * 			ipiv is a pointer to an array of dimesion at least 
 *			max( 1, min ( M, N ) ). ipiv( i ) = j, 1 <= i <= min( M, N ) implies
 *			that rows i and j have been interchanged.    	
 * 
 *	@param[in,out]
 *  B  		double *.
 *	    	B is a pointer to a matrix of dimension N by NRHS, which stores the 
 *			right-hand-sides of the systems of linear equations.  
 *			(row-major order). On exit, if return value is Success, the matrix B
 * 			is overwriten by the solution matrix X.
 *   
 *	@param[in]
 *  LDB     int.
 * 	    	LDB specifies the number of columns of B ( row-major order ).
 * 	    	LDB must be at least max( 1, NRHS ).
 *
 **/

/**
 *
 *	@retval Success successful exit
 *	@retval NoSuccess unsuccessful exit
 *
 **/

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

int ddss_dtpgesv( int N, int NRHS, 
	double *A, int LDA, 
	int *IPIV, 
	double *B, int LDB )
{

	// Argument checking
	if ( N < 0 )
	{
		fprintf( stderr, "Illegal value of N, in ddss_dtpgesv code\n" );
		return NoSuccess;
	}

	if ( NRHS < 0 )
	{
		fprintf( stderr, "Illegal value of NRHS, in ddss_dtpgesv code\n" );
		return NoSuccess;
	}

	if ( LDA < MAX( 1, N ) )
	{
		fprintf( stderr, "Illegal value of LDA, in ddss_dtpgesv code\n" );
		return NoSuccess;
	}

	if ( LDB < MAX( 1, NRHS ) )
	{
		fprintf( stderr, "Illegal value of LDB, in ddss_dtpgesv code\n" );
		return NoSuccess;
	}

	// Quick return
	if ( MAX( N, 0 ) == 0 || MAX( NRHS, 0 ) == 0 )
	{
		return Success;
	}

	return kdtpgesv( N, NRHS, A, LDA, IPIV, B, LDB );

} 
