spotrs_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 * spotrs_1 is the test program for the C interface to LAPACK
00036 * routine spotrs
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_spotrs( char *uplo, lapack_int *n, lapack_int *nrhs,
00055                                  lapack_int *lda, lapack_int *ldb );
00056 static void init_a( lapack_int size, float *a );
00057 static void init_b( lapack_int size, float *b );
00058 static int compare_spotrs( float *b, float *b_i, lapack_int info,
00059                            lapack_int info_i, lapack_int ldb, lapack_int nrhs );
00060 
00061 int main(void)
00062 {
00063     /* Local scalars */
00064     char uplo, uplo_i;
00065     lapack_int n, n_i;
00066     lapack_int nrhs, nrhs_i;
00067     lapack_int lda, lda_i;
00068     lapack_int lda_r;
00069     lapack_int ldb, ldb_i;
00070     lapack_int ldb_r;
00071     lapack_int info, info_i;
00072     lapack_int i;
00073     int failed;
00074 
00075     /* Local arrays */
00076     float *a = NULL, *a_i = NULL;
00077     float *b = NULL, *b_i = NULL;
00078     float *b_save = NULL;
00079     float *a_r = NULL;
00080     float *b_r = NULL;
00081 
00082     /* Iniitialize the scalar parameters */
00083     init_scalars_spotrs( &uplo, &n, &nrhs, &lda, &ldb );
00084     lda_r = n+2;
00085     ldb_r = nrhs+2;
00086     uplo_i = uplo;
00087     n_i = n;
00088     nrhs_i = nrhs;
00089     lda_i = lda;
00090     ldb_i = ldb;
00091 
00092     /* Allocate memory for the LAPACK routine arrays */
00093     a = (float *)LAPACKE_malloc( lda*n * sizeof(float) );
00094     b = (float *)LAPACKE_malloc( ldb*nrhs * sizeof(float) );
00095 
00096     /* Allocate memory for the C interface function arrays */
00097     a_i = (float *)LAPACKE_malloc( lda*n * sizeof(float) );
00098     b_i = (float *)LAPACKE_malloc( ldb*nrhs * sizeof(float) );
00099 
00100     /* Allocate memory for the backup arrays */
00101     b_save = (float *)LAPACKE_malloc( ldb*nrhs * sizeof(float) );
00102 
00103     /* Allocate memory for the row-major arrays */
00104     a_r = (float *)LAPACKE_malloc( n*(n+2) * sizeof(float) );
00105     b_r = (float *)LAPACKE_malloc( n*(nrhs+2) * sizeof(float) );
00106 
00107     /* Initialize input arrays */
00108     init_a( lda*n, a );
00109     init_b( ldb*nrhs, b );
00110 
00111     /* Backup the ouptut arrays */
00112     for( i = 0; i < ldb*nrhs; i++ ) {
00113         b_save[i] = b[i];
00114     }
00115 
00116     /* Call the LAPACK routine */
00117     spotrs_( &uplo, &n, &nrhs, a, &lda, b, &ldb, &info );
00118 
00119     /* Initialize input data, call the column-major middle-level
00120      * interface to LAPACK routine and check the results */
00121     for( i = 0; i < lda*n; i++ ) {
00122         a_i[i] = a[i];
00123     }
00124     for( i = 0; i < ldb*nrhs; i++ ) {
00125         b_i[i] = b_save[i];
00126     }
00127     info_i = LAPACKE_spotrs_work( LAPACK_COL_MAJOR, uplo_i, n_i, nrhs_i, a_i,
00128                                   lda_i, b_i, ldb_i );
00129 
00130     failed = compare_spotrs( b, b_i, info, info_i, ldb, nrhs );
00131     if( failed == 0 ) {
00132         printf( "PASSED: column-major middle-level interface to spotrs\n" );
00133     } else {
00134         printf( "FAILED: column-major middle-level interface to spotrs\n" );
00135     }
00136 
00137     /* Initialize input data, call the column-major high-level
00138      * interface to LAPACK routine and check the results */
00139     for( i = 0; i < lda*n; i++ ) {
00140         a_i[i] = a[i];
00141     }
00142     for( i = 0; i < ldb*nrhs; i++ ) {
00143         b_i[i] = b_save[i];
00144     }
00145     info_i = LAPACKE_spotrs( LAPACK_COL_MAJOR, uplo_i, n_i, nrhs_i, a_i, lda_i,
00146                              b_i, ldb_i );
00147 
00148     failed = compare_spotrs( b, b_i, info, info_i, ldb, nrhs );
00149     if( failed == 0 ) {
00150         printf( "PASSED: column-major high-level interface to spotrs\n" );
00151     } else {
00152         printf( "FAILED: column-major high-level interface to spotrs\n" );
00153     }
00154 
00155     /* Initialize input data, call the row-major middle-level
00156      * interface to LAPACK routine and check the results */
00157     for( i = 0; i < lda*n; i++ ) {
00158         a_i[i] = a[i];
00159     }
00160     for( i = 0; i < ldb*nrhs; i++ ) {
00161         b_i[i] = b_save[i];
00162     }
00163 
00164     LAPACKE_sge_trans( LAPACK_COL_MAJOR, n, n, a_i, lda, a_r, n+2 );
00165     LAPACKE_sge_trans( LAPACK_COL_MAJOR, n, nrhs, b_i, ldb, b_r, nrhs+2 );
00166     info_i = LAPACKE_spotrs_work( LAPACK_ROW_MAJOR, uplo_i, n_i, nrhs_i, a_r,
00167                                   lda_r, b_r, ldb_r );
00168 
00169     LAPACKE_sge_trans( LAPACK_ROW_MAJOR, n, nrhs, b_r, nrhs+2, b_i, ldb );
00170 
00171     failed = compare_spotrs( b, b_i, info, info_i, ldb, nrhs );
00172     if( failed == 0 ) {
00173         printf( "PASSED: row-major middle-level interface to spotrs\n" );
00174     } else {
00175         printf( "FAILED: row-major middle-level interface to spotrs\n" );
00176     }
00177 
00178     /* Initialize input data, call the row-major high-level
00179      * interface to LAPACK routine and check the results */
00180     for( i = 0; i < lda*n; i++ ) {
00181         a_i[i] = a[i];
00182     }
00183     for( i = 0; i < ldb*nrhs; i++ ) {
00184         b_i[i] = b_save[i];
00185     }
00186 
00187     /* Init row_major arrays */
00188     LAPACKE_sge_trans( LAPACK_COL_MAJOR, n, n, a_i, lda, a_r, n+2 );
00189     LAPACKE_sge_trans( LAPACK_COL_MAJOR, n, nrhs, b_i, ldb, b_r, nrhs+2 );
00190     info_i = LAPACKE_spotrs( LAPACK_ROW_MAJOR, uplo_i, n_i, nrhs_i, a_r, lda_r,
00191                              b_r, ldb_r );
00192 
00193     LAPACKE_sge_trans( LAPACK_ROW_MAJOR, n, nrhs, b_r, nrhs+2, b_i, ldb );
00194 
00195     failed = compare_spotrs( b, b_i, info, info_i, ldb, nrhs );
00196     if( failed == 0 ) {
00197         printf( "PASSED: row-major high-level interface to spotrs\n" );
00198     } else {
00199         printf( "FAILED: row-major high-level interface to spotrs\n" );
00200     }
00201 
00202     /* Release memory */
00203     if( a != NULL ) {
00204         LAPACKE_free( a );
00205     }
00206     if( a_i != NULL ) {
00207         LAPACKE_free( a_i );
00208     }
00209     if( a_r != NULL ) {
00210         LAPACKE_free( a_r );
00211     }
00212     if( b != NULL ) {
00213         LAPACKE_free( b );
00214     }
00215     if( b_i != NULL ) {
00216         LAPACKE_free( b_i );
00217     }
00218     if( b_r != NULL ) {
00219         LAPACKE_free( b_r );
00220     }
00221     if( b_save != NULL ) {
00222         LAPACKE_free( b_save );
00223     }
00224 
00225     return 0;
00226 }
00227 
00228 /* Auxiliary function: spotrs scalar parameters initialization */
00229 static void init_scalars_spotrs( char *uplo, lapack_int *n, lapack_int *nrhs,
00230                                  lapack_int *lda, lapack_int *ldb )
00231 {
00232     *uplo = 'L';
00233     *n = 4;
00234     *nrhs = 2;
00235     *lda = 8;
00236     *ldb = 8;
00237 
00238     return;
00239 }
00240 
00241 /* Auxiliary functions: spotrs array parameters initialization */
00242 static void init_a( lapack_int size, float *a ) {
00243     lapack_int i;
00244     for( i = 0; i < size; i++ ) {
00245         a[i] = 0;
00246     }
00247     a[0] = 2.039607763e+000;  /* a[0,0] */
00248     a[8] = 0.000000000e+000;  /* a[0,1] */
00249     a[16] = 0.000000000e+000;  /* a[0,2] */
00250     a[24] = 0.000000000e+000;  /* a[0,3] */
00251     a[1] = -1.529705763e+000;  /* a[1,0] */
00252     a[9] = 1.640122056e+000;  /* a[1,1] */
00253     a[17] = 0.000000000e+000;  /* a[1,2] */
00254     a[25] = 0.000000000e+000;  /* a[1,3] */
00255     a[2] = 2.745625973e-001;  /* a[2,0] */
00256     a[10] = -2.499813884e-001;  /* a[2,1] */
00257     a[18] = 7.887488008e-001;  /* a[2,2] */
00258     a[26] = 0.000000000e+000;  /* a[2,3] */
00259     a[3] = -4.902903363e-002;  /* a[3,0] */
00260     a[11] = 6.737302542e-001;  /* a[3,1] */
00261     a[19] = 6.616575122e-001;  /* a[3,2] */
00262     a[27] = 5.346896052e-001;  /* a[3,3] */
00263 }
00264 static void init_b( lapack_int size, float *b ) {
00265     lapack_int i;
00266     for( i = 0; i < size; i++ ) {
00267         b[i] = 0;
00268     }
00269     b[0] = 8.699999809e+000;  /* b[0,0] */
00270     b[8] = 8.300000191e+000;  /* b[0,1] */
00271     b[1] = -1.335000038e+001;  /* b[1,0] */
00272     b[9] = 2.130000114e+000;  /* b[1,1] */
00273     b[2] = 1.889999986e+000;  /* b[2,0] */
00274     b[10] = 1.610000014e+000;  /* b[2,1] */
00275     b[3] = -4.139999866e+000;  /* b[3,0] */
00276     b[11] = 5.000000000e+000;  /* b[3,1] */
00277 }
00278 
00279 /* Auxiliary function: C interface to spotrs results check */
00280 /* Return value: 0 - test is passed, non-zero - test is failed */
00281 static int compare_spotrs( float *b, float *b_i, lapack_int info,
00282                            lapack_int info_i, lapack_int ldb, lapack_int nrhs )
00283 {
00284     lapack_int i;
00285     int failed = 0;
00286     for( i = 0; i < ldb*nrhs; i++ ) {
00287         failed += compare_floats(b[i],b_i[i]);
00288     }
00289     failed += (info == info_i) ? 0 : 1;
00290     if( info != 0 || info_i != 0 ) {
00291         printf( "info=%d, info_i=%d\n",(int)info,(int)info_i );
00292     }
00293 
00294     return failed;
00295 }


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