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

/**
 *
 * 	@file kdtpgetrf.c
 *
 * 	@brief LASs-DDSs kdtpgetrf routine.
 *
 * 	LASs-DDSs is a software package provided by:
 * 	Barcelona Supercomputing Center - Centro Nacional de Supercomputacion
 *
 * 	@author Pedro Valero-Lara pedro.valero@bsc.es
 * 	@author Boro Sofranac boro.sofranac@bsc.es
 * 	@date 2018-04-08
 * 	@reviewer 
 * 	@modified 2018-05-08
 *
 **/

/**
 *  
 *	@ingroup DDSS
 *   
 *	Performs the LU factorization with tiled pivoting ( row interchanges )
 *	of a general M-by-N matrix A:
 *
 *		A = P * L * U
 *
 *	where P is a permutation matrix, L is a lower triangular 
 *	( lower trapezoidal if M > N ) matrix with unit diagonal elements and
 *	U is an upper triangular ( upper trapezoidal if M < N ) matrix.
 *
**/

/**
 *	
 *	@param[in]
 *	M       int.
 *          M specifies the number of rows of the matrix A. M >= 0.
 *	
 *	@param[in]
 *	N       int.
 *          N specifies the number of columns of the matrix A. N >= 0.
 *
 *	@param[in,out]
 *  A  		double *.
 *	    	A is a pointer to a regular matrix of dimension M-by-N.
 *          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.    	
 *
 **/

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

/**
 *
 *	@sa ddss_tile
 *	@sa ddss_flat2tiled
 *	@sa ddss_tiled2flat
 *
 **/ 

enum LASS_RETURN kdtpgetrf( int M, int N, double *A, int LDA, int *IPIV )
{

	// Local variables
	int i;
	int mt, nt;
	int mi, mmi, ki, ni;
	int tile_size_m;
	int tile_size_mm;
	int tile_size_n;
	int tile_size_km;
	int tile_size_kn;
	int local_ipiv_size;
	int current_position;
	enum LASS_RETURN ret;
	int	*ipiv_local;

	// Number of tiles
	if ( M % TILE_SIZE == 0 )
	{
		mt = M / TILE_SIZE;
	}
	else
	{
		mt = ( M / TILE_SIZE ) + 1;
	}

	if ( N % TILE_SIZE == 0 )
	{
		nt = N / TILE_SIZE;
	}
	else
	{
		nt = ( N / TILE_SIZE ) + 1;
	}

	/***************************
	--Tile A matrix allocation--
	***************************/
		
	double ( *TILE_A )[nt][TILE_SIZE * TILE_SIZE] = malloc ( mt * nt * 
		TILE_SIZE * TILE_SIZE * sizeof( double ) );
	
	if ( TILE_A == NULL )
	{
		fprintf( stderr, "Failure in kdnpgetrf for matrix TILE_A\n" );
		return NoSuccess;
	}

	ipiv_local = ( int* ) malloc( MIN( M, N ) * sizeof( int ) );
	
	if ( ipiv_local == NULL )
	{
		fprintf( stderr, "Failure in kdnpgetrf for ipiv_local\n" );
		return NoSuccess;
	}

	/************************************************
	// --From flat data layout to tiled data layout--	
	************************************************/
	
	ddss_dflat2tiled( M, N, A, LDA, mt, nt, TILE_A );

	/****************
	--DGETRF tile--
	****************/	
	
	current_position = 0;
	i = 0;
	for ( ki = 0; ki < MIN( mt, nt ); ki++ )
	{
		tile_size_km = ddss_tile_size( M, ki );
		tile_size_kn = ddss_tile_size( N, ki );
		
		#pragma oss task inout( TILE_A[ki][ki] ) \
			inout( ipiv_local ) \
			inout( current_position ) \
			out( local_ipiv_size ) \
			shared( TILE_A ) \
			firstprivate( ki, i ) \
			label( dgetrf )
		{
			LAPACKE_dgetrf( CblasRowMajor,
							tile_size_km, tile_size_kn, 
							TILE_A[ki][ki], tile_size_kn,
							ipiv_local + current_position );
			
			// Update the global ipiv array
			local_ipiv_size = MIN( tile_size_km, tile_size_kn );
			for ( i = 0; i < local_ipiv_size; i++ )
			{
				( IPIV + current_position )[i] =
						( ipiv_local + current_position )[i] + current_position;
			}
			current_position += local_ipiv_size;
		}
		for ( mi = ki + 1; mi < mt; mi++ )
		{
			tile_size_m = ddss_tile_size( M, mi );

			#pragma oss task in( TILE_A[ki][ki] ) \
				inout( TILE_A[mi][ki] ) \
				shared( TILE_A ) \
				firstprivate( mi, ki ) \
				label( dtrsm_below )
			cblas_dtrsm( CblasRowMajor, 
				     	 ( CBLAS_SIDE ) Right, ( CBLAS_UPLO ) Upper, 
				     	 ( CBLAS_TRANSPOSE ) NoTrans, ( CBLAS_DIAG ) NonUnit, 
					 	 tile_size_m, tile_size_kn, 
					 	 1.0, TILE_A[ki][ki], tile_size_kn, 
						  	  TILE_A[mi][ki], tile_size_kn );
		}
		for ( ni = 0; ni < ki; ni++ )
		{		
			tile_size_n = ddss_tile_size( N, ni );
			// Swap the tiles to the left of A[ki][ki]
			#pragma oss task in( ipiv_local ) \
				in( current_position ) \
				in( local_ipiv_size ) \
				inout( TILE_A[ki][ni] ) \
				shared( TILE_A ) \
				firstprivate( ki, ni ) \
				label( dlaswp_left )
			LAPACKE_dlaswp( CblasRowMajor,
							tile_size_n, TILE_A[ki][ni], tile_size_n,
							1, local_ipiv_size,
							ipiv_local + current_position - local_ipiv_size, 
							1 );
			
		}
		for ( ni = ki + 1; ni < nt; ni++ )
		{
			tile_size_n = ddss_tile_size( N, ni );
			// Swap the tiles to the right of A[ki][ki]
			#pragma oss task in( ipiv_local ) \
				in( current_position ) \
				in( local_ipiv_size ) \
				inout( TILE_A[ki][ni] ) \
				shared( TILE_A ) \
				firstprivate( ki, ni ) \
				label( dlaswp_right )
			LAPACKE_dlaswp( CblasRowMajor,
							tile_size_n, TILE_A[ki][ni], tile_size_n,
							1, local_ipiv_size,
							ipiv_local + current_position - local_ipiv_size, 
							1 );

			#pragma oss task in( TILE_A[ki][ki] ) \
				inout( TILE_A[ki][ni] ) \
				shared( TILE_A ) \
				firstprivate( ni, ki ) \
				label( dtrsm_right )
			cblas_dtrsm( CblasRowMajor, 
				     	 ( CBLAS_SIDE ) Left, ( CBLAS_UPLO ) Lower, 
				     	 ( CBLAS_TRANSPOSE ) NoTrans, ( CBLAS_DIAG ) Unit, 
					 	 tile_size_km, tile_size_n, 
					 	 1.0, TILE_A[ki][ki], tile_size_kn, 
						  	  TILE_A[ki][ni], tile_size_n );
				
			for ( mmi = ki + 1; mmi < mt; mmi++ )
			{
				tile_size_mm = ddss_tile_size( M, mmi );
		
				#pragma oss task in( TILE_A[mmi][ki] ) \
					in( TILE_A[ki][ni] ) \
					inout( TILE_A[mmi][ni] ) \
					shared( TILE_A ) \
					firstprivate( ni, mmi, ki ) \
					label( dgemm )
				cblas_dgemm( CblasRowMajor, 
							 ( CBLAS_TRANSPOSE ) NoTrans,
							 ( CBLAS_TRANSPOSE ) NoTrans,
							 tile_size_mm, 
							 tile_size_n, 
							 TILE_SIZE,
							 -1.0, TILE_A[mmi][ki], tile_size_kn,
							 	   TILE_A[ki][ni],  tile_size_n,
							  1.0, TILE_A[mmi][ni], tile_size_n );			
			}
		}
	}
		
	// --From tile data layout to flat data layout--	
	ddss_dtiled2flat( M, N, A, LDA, mt, nt, TILE_A );

	// --Free--
	free( TILE_A );
	free( ipiv_local );
	
	return Success;

}
