sgebal_1.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002   Copyright (c) 2010, Intel Corp.
00003   All rights reserved.
00004 
00005   Redistribution and use in source and binary forms, with or without
00006   modification, are permitted provided that the following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice,
00009       this list of conditions and the following disclaimer.
00010     * Redistributions in binary form must reproduce the above copyright
00011       notice, this list of conditions and the following disclaimer in the
00012       documentation and/or other materials provided with the distribution.
00013     * Neither the name of Intel Corporation nor the names of its contributors
00014       may be used to endorse or promote products derived from this software
00015       without specific prior written permission.
00016 
00017   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00027   THE POSSIBILITY OF SUCH DAMAGE.
00028 *****************************************************************************/
00029 /*  Contents: test routine for C interface to LAPACK
00030 *   Author: Intel Corporation
00031 *   Created in March, 2010
00032 *
00033 * Purpose
00034 *
00035 * sgebal_1 is the test program for the C interface to LAPACK
00036 * routine sgebal
00037 * The program doesn't require an input, the input data is hardcoded in the
00038 * test program.
00039 * The program tests the C interface in the four combinations:
00040 *   1) column-major layout, middle-level interface
00041 *   2) column-major layout, high-level interface
00042 *   3) row-major layout, middle-level interface
00043 *   4) row-major layout, high-level interface
00044 * The output of the C interface function is compared to those obtained from
00045 * the corresponiding LAPACK routine with the same input data, and the
00046 * comparison diagnostics is then printed on the standard output having PASSED
00047 * keyword if the test is passed, and FAILED keyword if the test isn't passed.
00048 *****************************************************************************/
00049 #include <stdio.h>
00050 #include "lapacke.h"
00051 #include "lapacke_utils.h"
00052 #include "test_utils.h"
00053 
00054 static void init_scalars_sgebal( char *job, lapack_int *n, lapack_int *lda );
00055 static void init_a( lapack_int size, float *a );
00056 static void init_scale( lapack_int size, float *scale );
00057 static int compare_sgebal( float *a, float *a_i, lapack_int ilo,
00058                            lapack_int ilo_i, lapack_int ihi, lapack_int ihi_i,
00059                            float *scale, float *scale_i, lapack_int info,
00060                            lapack_int info_i, char job, lapack_int lda,
00061                            lapack_int n );
00062 
00063 int main(void)
00064 {
00065     /* Local scalars */
00066     char job, job_i;
00067     lapack_int n, n_i;
00068     lapack_int lda, lda_i;
00069     lapack_int lda_r;
00070     lapack_int ilo, ilo_i;
00071     lapack_int ihi, ihi_i;
00072     lapack_int info, info_i;
00073     lapack_int i;
00074     int failed;
00075 
00076     /* Local arrays */
00077     float *a = NULL, *a_i = NULL;
00078     float *scale = NULL, *scale_i = NULL;
00079     float *a_save = NULL;
00080     float *scale_save = NULL;
00081     float *a_r = NULL;
00082 
00083     /* Iniitialize the scalar parameters */
00084     init_scalars_sgebal( &job, &n, &lda );
00085     lda_r = n+2;
00086     job_i = job;
00087     n_i = n;
00088     lda_i = lda;
00089 
00090     /* Allocate memory for the LAPACK routine arrays */
00091     a = (float *)LAPACKE_malloc( lda*n * sizeof(float) );
00092     scale = (float *)LAPACKE_malloc( n * sizeof(float) );
00093 
00094     /* Allocate memory for the C interface function arrays */
00095     a_i = (float *)LAPACKE_malloc( lda*n * sizeof(float) );
00096     scale_i = (float *)LAPACKE_malloc( n * sizeof(float) );
00097 
00098     /* Allocate memory for the backup arrays */
00099     a_save = (float *)LAPACKE_malloc( lda*n * sizeof(float) );
00100     scale_save = (float *)LAPACKE_malloc( n * sizeof(float) );
00101 
00102     /* Allocate memory for the row-major arrays */
00103     a_r = (float *)LAPACKE_malloc( n*(n+2) * sizeof(float) );
00104 
00105     /* Initialize input arrays */
00106     init_a( lda*n, a );
00107     init_scale( n, scale );
00108 
00109     /* Backup the ouptut arrays */
00110     for( i = 0; i < lda*n; i++ ) {
00111         a_save[i] = a[i];
00112     }
00113     for( i = 0; i < n; i++ ) {
00114         scale_save[i] = scale[i];
00115     }
00116 
00117     /* Call the LAPACK routine */
00118     sgebal_( &job, &n, a, &lda, &ilo, &ihi, scale, &info );
00119 
00120     /* Initialize input data, call the column-major middle-level
00121      * interface to LAPACK routine and check the results */
00122     for( i = 0; i < lda*n; i++ ) {
00123         a_i[i] = a_save[i];
00124     }
00125     for( i = 0; i < n; i++ ) {
00126         scale_i[i] = scale_save[i];
00127     }
00128     info_i = LAPACKE_sgebal_work( LAPACK_COL_MAJOR, job_i, n_i, a_i, lda_i,
00129                                   &ilo_i, &ihi_i, scale_i );
00130 
00131     failed = compare_sgebal( a, a_i, ilo, ilo_i, ihi, ihi_i, scale, scale_i,
00132                              info, info_i, job, lda, n );
00133     if( failed == 0 ) {
00134         printf( "PASSED: column-major middle-level interface to sgebal\n" );
00135     } else {
00136         printf( "FAILED: column-major middle-level interface to sgebal\n" );
00137     }
00138 
00139     /* Initialize input data, call the column-major high-level
00140      * interface to LAPACK routine and check the results */
00141     for( i = 0; i < lda*n; i++ ) {
00142         a_i[i] = a_save[i];
00143     }
00144     for( i = 0; i < n; i++ ) {
00145         scale_i[i] = scale_save[i];
00146     }
00147     info_i = LAPACKE_sgebal( LAPACK_COL_MAJOR, job_i, n_i, a_i, lda_i, &ilo_i,
00148                              &ihi_i, scale_i );
00149 
00150     failed = compare_sgebal( a, a_i, ilo, ilo_i, ihi, ihi_i, scale, scale_i,
00151                              info, info_i, job, lda, n );
00152     if( failed == 0 ) {
00153         printf( "PASSED: column-major high-level interface to sgebal\n" );
00154     } else {
00155         printf( "FAILED: column-major high-level interface to sgebal\n" );
00156     }
00157 
00158     /* Initialize input data, call the row-major middle-level
00159      * interface to LAPACK routine and check the results */
00160     for( i = 0; i < lda*n; i++ ) {
00161         a_i[i] = a_save[i];
00162     }
00163     for( i = 0; i < n; i++ ) {
00164         scale_i[i] = scale_save[i];
00165     }
00166 
00167     if( LAPACKE_lsame( job, 'b' ) || LAPACKE_lsame( job, 'p' ) ||
00168         LAPACKE_lsame( job, 's' ) ) {
00169         LAPACKE_sge_trans( LAPACK_COL_MAJOR, n, n, a_i, lda, a_r, n+2 );
00170     }
00171     info_i = LAPACKE_sgebal_work( LAPACK_ROW_MAJOR, job_i, n_i, a_r, lda_r,
00172                                   &ilo_i, &ihi_i, scale_i );
00173 
00174     if( LAPACKE_lsame( job, 'b' ) || LAPACKE_lsame( job, 'p' ) ||
00175         LAPACKE_lsame( job, 's' ) ) {
00176         LAPACKE_sge_trans( LAPACK_ROW_MAJOR, n, n, a_r, n+2, a_i, lda );
00177     }
00178 
00179     failed = compare_sgebal( a, a_i, ilo, ilo_i, ihi, ihi_i, scale, scale_i,
00180                              info, info_i, job, lda, n );
00181     if( failed == 0 ) {
00182         printf( "PASSED: row-major middle-level interface to sgebal\n" );
00183     } else {
00184         printf( "FAILED: row-major middle-level interface to sgebal\n" );
00185     }
00186 
00187     /* Initialize input data, call the row-major high-level
00188      * interface to LAPACK routine and check the results */
00189     for( i = 0; i < lda*n; i++ ) {
00190         a_i[i] = a_save[i];
00191     }
00192     for( i = 0; i < n; i++ ) {
00193         scale_i[i] = scale_save[i];
00194     }
00195 
00196     /* Init row_major arrays */
00197     if( LAPACKE_lsame( job, 'b' ) || LAPACKE_lsame( job, 'p' ) ||
00198         LAPACKE_lsame( job, 's' ) ) {
00199         LAPACKE_sge_trans( LAPACK_COL_MAJOR, n, n, a_i, lda, a_r, n+2 );
00200     }
00201     info_i = LAPACKE_sgebal( LAPACK_ROW_MAJOR, job_i, n_i, a_r, lda_r, &ilo_i,
00202                              &ihi_i, scale_i );
00203 
00204     if( LAPACKE_lsame( job, 'b' ) || LAPACKE_lsame( job, 'p' ) ||
00205         LAPACKE_lsame( job, 's' ) ) {
00206         LAPACKE_sge_trans( LAPACK_ROW_MAJOR, n, n, a_r, n+2, a_i, lda );
00207     }
00208 
00209     failed = compare_sgebal( a, a_i, ilo, ilo_i, ihi, ihi_i, scale, scale_i,
00210                              info, info_i, job, lda, n );
00211     if( failed == 0 ) {
00212         printf( "PASSED: row-major high-level interface to sgebal\n" );
00213     } else {
00214         printf( "FAILED: row-major high-level interface to sgebal\n" );
00215     }
00216 
00217     /* Release memory */
00218     if( a != NULL ) {
00219         LAPACKE_free( a );
00220     }
00221     if( a_i != NULL ) {
00222         LAPACKE_free( a_i );
00223     }
00224     if( a_r != NULL ) {
00225         LAPACKE_free( a_r );
00226     }
00227     if( a_save != NULL ) {
00228         LAPACKE_free( a_save );
00229     }
00230     if( scale != NULL ) {
00231         LAPACKE_free( scale );
00232     }
00233     if( scale_i != NULL ) {
00234         LAPACKE_free( scale_i );
00235     }
00236     if( scale_save != NULL ) {
00237         LAPACKE_free( scale_save );
00238     }
00239 
00240     return 0;
00241 }
00242 
00243 /* Auxiliary function: sgebal scalar parameters initialization */
00244 static void init_scalars_sgebal( char *job, lapack_int *n, lapack_int *lda )
00245 {
00246     *job = 'B';
00247     *n = 4;
00248     *lda = 8;
00249 
00250     return;
00251 }
00252 
00253 /* Auxiliary functions: sgebal array parameters initialization */
00254 static void init_a( lapack_int size, float *a ) {
00255     lapack_int i;
00256     for( i = 0; i < size; i++ ) {
00257         a[i] = 0;
00258     }
00259     a[0] = 5.139999866e+000;  /* a[0,0] */
00260     a[8] = 9.100000262e-001;  /* a[0,1] */
00261     a[16] = 0.000000000e+000;  /* a[0,2] */
00262     a[24] = -3.279999924e+001;  /* a[0,3] */
00263     a[1] = 9.100000262e-001;  /* a[1,0] */
00264     a[9] = 2.000000030e-001;  /* a[1,1] */
00265     a[17] = 0.000000000e+000;  /* a[1,2] */
00266     a[25] = 3.450000000e+001;  /* a[1,3] */
00267     a[2] = 1.899999976e+000;  /* a[2,0] */
00268     a[10] = 8.000000119e-001;  /* a[2,1] */
00269     a[18] = -4.000000060e-001;  /* a[2,2] */
00270     a[26] = -3.000000000e+000;  /* a[2,3] */
00271     a[3] = -3.300000131e-001;  /* a[3,0] */
00272     a[11] = 3.499999940e-001;  /* a[3,1] */
00273     a[19] = 0.000000000e+000;  /* a[3,2] */
00274     a[27] = 6.600000262e-001;  /* a[3,3] */
00275 }
00276 static void init_scale( lapack_int size, float *scale ) {
00277     lapack_int i;
00278     for( i = 0; i < size; i++ ) {
00279         scale[i] = 0;
00280     }
00281 }
00282 
00283 /* Auxiliary function: C interface to sgebal results check */
00284 /* Return value: 0 - test is passed, non-zero - test is failed */
00285 static int compare_sgebal( float *a, float *a_i, lapack_int ilo,
00286                            lapack_int ilo_i, lapack_int ihi, lapack_int ihi_i,
00287                            float *scale, float *scale_i, lapack_int info,
00288                            lapack_int info_i, char job, lapack_int lda,
00289                            lapack_int n )
00290 {
00291     lapack_int i;
00292     int failed = 0;
00293     if( LAPACKE_lsame( job, 'b' ) || LAPACKE_lsame( job, 'p' ) ||
00294         LAPACKE_lsame( job, 's' ) ) {
00295         for( i = 0; i < lda*n; i++ ) {
00296             failed += compare_floats(a[i],a_i[i]);
00297         }
00298     }
00299     failed += (ilo == ilo_i) ? 0 : 1;
00300     failed += (ihi == ihi_i) ? 0 : 1;
00301     for( i = 0; i < n; i++ ) {
00302         failed += compare_floats(scale[i],scale_i[i]);
00303     }
00304     failed += (info == info_i) ? 0 : 1;
00305     if( info != 0 || info_i != 0 ) {
00306         printf( "info=%d, info_i=%d\n",(int)info,(int)info_i );
00307     }
00308 
00309     return failed;
00310 }


swiftnav
Author(s):
autogenerated on Sat Jun 8 2019 18:56:06