/*
    -- MAGMA (version 1.1) --
       Univ. of Tennessee, Knoxville
       Univ. of California, Berkeley
       Univ. of Colorado, Denver
       November 2011

       @generated s Fri Nov 22 14:21:36 2013

*/

// includes, system
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

// includes, project
#include "flops.h"
#include "magma.h"
#include "magma_lapack.h"
#include "magma_types.h"
#include "testings.h"

#define PRECISION_s

/* ////////////////////////////////////////////////////////////////////////////
   -- Testing ssytrd
*/
int main( int argc, char** argv)
{
    /* Initialize */
    magma_queue_t  queue;
    magma_device_t device[ MagmaMaxDevices ];
    int num = 0;
    magma_err_t err;

    magma_init();

    err = magma_get_devices( device, MagmaMaxDevices, &num );
    if ( err != 0 || num < 1 ) {
      fprintf( stderr, "magma_get_devices failed: %d\n", err );
      exit(-1);
    }
    err = magma_queue_create( device[0], &queue );
    if ( err != 0 ) {
      fprintf( stderr, "magma_queue_create failed: %d\n", err );
      exit(-1);
    }

    real_Double_t    gflops, mic_perf, cpu_perf, mic_time, cpu_time;
    float           eps;
    float *h_A, *h_R, *h_Q, *h_work, *work;
    float *tau;
    float          *diag, *offdiag;
    float           result[2] = {0., 0.};
    magma_int_t N, n2, lda, lwork, info, nb;
    magma_int_t ione     = 1;
    magma_int_t itwo     = 2;
    magma_int_t ithree   = 3;
    magma_int_t ISEED[4] = {0,0,0,1};
    
    #if defined(PRECISION_z) || defined(PRECISION_c)
    float *rwork;
    #endif

    eps = lapackf77_slamch( "E" );

    magma_opts opts;
    parse_opts( argc, argv, &opts );
    
    printf("  N     CPU GFlop/s (sec)   MIC GFlop/s (sec)   |A-QHQ'|/N|A|   |I-QQ'|/N\n");
    printf("===========================================================================\n");
    for( int i = 0; i < opts.ntest; ++i ) {
        for( int iter = 0; iter < opts.niter; ++iter ) {
            N = opts.nsize[i];
            lda    = N;
            n2     = N*lda;
            nb     = magma_get_ssytrd_nb(N);
            lwork  = N*nb; /* We suppose the magma nb is bigger than lapack nb */
            gflops = FLOPS_SSYTRD( N ) / 1e9;
            
            TESTING_MALLOC_HOST( h_A,     float, lda*N );
            TESTING_MALLOC_HOST( h_R,     float, lda*N );
            TESTING_MALLOC_HOST( h_work,  float, lwork );
            TESTING_MALLOC_HOST( tau,     float, N     );
            TESTING_MALLOC_HOST( diag,    float, N   );
            TESTING_MALLOC_HOST( offdiag, float, N-1 );
            
            if ( opts.check ) {
                TESTING_MALLOC_HOST( h_Q,  float, lda*N );
                TESTING_MALLOC_HOST( work, float, 2*N*N );
                #if defined(PRECISION_z) || defined(PRECISION_c)
                TESTING_MALLOC_HOST( rwork, float, N );
                #endif
            }
            
            /* ====================================================================
               Initialize the matrix
               =================================================================== */
            lapackf77_slarnv( &ione, ISEED, &n2, h_A );
            magma_smake_hermitian( N, h_A, lda );
            lapackf77_slacpy( MagmaUpperLowerStr, &N, &N, h_A, &lda, h_R, &lda );
            
            /* ====================================================================
               Performs operation using MAGMA
               =================================================================== */
            mic_time = magma_wtime();
            magma_ssytrd( opts.uplo, N, h_R, lda, diag, offdiag,
                          tau, h_work, lwork, &info, queue );
            mic_time = magma_wtime() - mic_time;
            mic_perf = gflops / mic_time;
            if (info != 0)
                printf("magma_ssytrd returned error %d: %s.\n",
                       (int) info, magma_strerror( info ));
            
            /* =====================================================================
               Check the factorization
               =================================================================== */
            if ( opts.check ) {
                lapackf77_slacpy( lapack_const(opts.uplo), &N, &N, h_R, &lda, h_Q, &lda );
                lapackf77_sorgtr( lapack_const(opts.uplo), &N, h_Q, &lda, tau, h_work, &lwork, &info );
                
                #if defined(PRECISION_z) || defined(PRECISION_c)
                lapackf77_ssyt21( &itwo, lapack_const(opts.uplo), &N, &ione, 
				  h_A, &lda, diag, offdiag,
                                  h_Q, &lda, h_R, &lda,
                                  tau, work, rwork, &result[0] );
                
                lapackf77_ssyt21( &ithree, lapack_const(opts.uplo), &N, &ione,
                                  h_A, &lda, diag, offdiag,
                                  h_Q, &lda, h_R, &lda,
                                  tau, work, rwork, &result[1] );
                #else
                lapackf77_ssyt21( &itwo, lapack_const(opts.uplo), &N, &ione,
                                  h_A, &lda, diag, offdiag,
                                  h_Q, &lda, h_R, &lda,
                                  tau, work, &result[0] );
                
                lapackf77_ssyt21( &ithree, lapack_const(opts.uplo), &N, &ione,
                                  h_A, &lda, diag, offdiag,
                                  h_Q, &lda, h_R, &lda,
                                  tau, work, &result[1] );
                #endif
            }
            
            /* =====================================================================
               Performs operation using LAPACK
               =================================================================== */
            if ( opts.lapack ) {
                cpu_time = magma_wtime();
                lapackf77_ssytrd( lapack_const(opts.uplo), &N, h_A, &lda, diag, offdiag, tau,
                                  h_work, &lwork, &info );
                cpu_time = magma_wtime() - cpu_time;
                cpu_perf = gflops / cpu_time;
                if (info != 0)
                    printf("lapackf77_ssytrd returned error %d: %s.\n",
                           (int) info, magma_strerror( info ));
            }
            
            /* =====================================================================
               Print performance and error.
               =================================================================== */
            if ( opts.lapack ) {
                printf("%5d   %7.2f (%7.2f)   %7.2f (%7.2f)",
                       (int) N, cpu_perf, cpu_time, mic_perf, mic_time );
            } else {
                printf("%5d     ---   (  ---  )   %7.2f (%7.2f)",
                       (int) N, mic_perf, mic_time );
            }
            if ( opts.check ) {
                printf("   %8.2e        %8.2e\n", result[0]*eps, result[1]*eps );
            } else {
                printf("     ---             ---\n");
            }
       
       	    fflush(0);	    
            TESTING_FREE_HOST( h_A );
            TESTING_FREE_HOST( tau );
            TESTING_FREE_HOST( diag );
            TESTING_FREE_HOST( offdiag );
            TESTING_FREE_HOST( h_R );
            TESTING_FREE_HOST( h_work );
            
            if ( opts.check ) {
                TESTING_FREE_HOST( h_Q );
                TESTING_FREE_HOST( work );
                #if defined(PRECISION_z) || defined(PRECISION_c)
                TESTING_FREE_HOST( rwork );
                #endif
            }
        }
    }

    magma_queue_destroy( queue );
    magma_finalize();
    return 0;
}
