1 /*****************************************************************************
2   Copyright (c) 2014, Intel Corp.
3   All rights reserved.
4 
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions are met:
7 
8     * Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10     * Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13     * Neither the name of Intel Corporation nor the names of its contributors
14       may be used to endorse or promote products derived from this software
15       without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27   THE POSSIBILITY OF SUCH DAMAGE.
28 *****************************************************************************
29 * Contents: Native high-level C interface to LAPACK function sggevx
30 * Author: Intel Corporation
31 *****************************************************************************/
32 
33 #include "lapacke_utils.h"
34 
LAPACKE_sggevx(int matrix_layout,char balanc,char jobvl,char jobvr,char sense,lapack_int n,float * a,lapack_int lda,float * b,lapack_int ldb,float * alphar,float * alphai,float * beta,float * vl,lapack_int ldvl,float * vr,lapack_int ldvr,lapack_int * ilo,lapack_int * ihi,float * lscale,float * rscale,float * abnrm,float * bbnrm,float * rconde,float * rcondv)35 lapack_int LAPACKE_sggevx( int matrix_layout, char balanc, char jobvl,
36                            char jobvr, char sense, lapack_int n, float* a,
37                            lapack_int lda, float* b, lapack_int ldb,
38                            float* alphar, float* alphai, float* beta, float* vl,
39                            lapack_int ldvl, float* vr, lapack_int ldvr,
40                            lapack_int* ilo, lapack_int* ihi, float* lscale,
41                            float* rscale, float* abnrm, float* bbnrm,
42                            float* rconde, float* rcondv )
43 {
44     lapack_int info = 0;
45     lapack_int lwork = -1;
46     lapack_logical* bwork = NULL;
47     lapack_int* iwork = NULL;
48     float* work = NULL;
49     float work_query;
50     if( matrix_layout != LAPACK_COL_MAJOR && matrix_layout != LAPACK_ROW_MAJOR ) {
51         LAPACKE_xerbla( "LAPACKE_sggevx", -1 );
52         return -1;
53     }
54 #ifndef LAPACK_DISABLE_NAN_CHECK
55     if( LAPACKE_get_nancheck() ) {
56         /* Optionally check input matrices for NaNs */
57         if( LAPACKE_sge_nancheck( matrix_layout, n, n, a, lda ) ) {
58             return -7;
59         }
60         if( LAPACKE_sge_nancheck( matrix_layout, n, n, b, ldb ) ) {
61             return -9;
62         }
63     }
64 #endif
65     /* Allocate memory for working array(s) */
66     if( LAPACKE_lsame( sense, 'b' ) || LAPACKE_lsame( sense, 'e' ) ||
67         LAPACKE_lsame( sense, 'v' ) ) {
68         bwork = (lapack_logical*)
69             LAPACKE_malloc( sizeof(lapack_logical) * MAX(1,n) );
70         if( bwork == NULL ) {
71             info = LAPACK_WORK_MEMORY_ERROR;
72             goto exit_level_0;
73         }
74     }
75     if( LAPACKE_lsame( sense, 'b' ) || LAPACKE_lsame( sense, 'n' ) ||
76         LAPACKE_lsame( sense, 'v' ) ) {
77         iwork = (lapack_int*)LAPACKE_malloc( sizeof(lapack_int) * MAX(1,n+6) );
78         if( iwork == NULL ) {
79             info = LAPACK_WORK_MEMORY_ERROR;
80             goto exit_level_1;
81         }
82     }
83     /* Query optimal working array(s) size */
84     info = LAPACKE_sggevx_work( matrix_layout, balanc, jobvl, jobvr, sense, n, a,
85                                 lda, b, ldb, alphar, alphai, beta, vl, ldvl, vr,
86                                 ldvr, ilo, ihi, lscale, rscale, abnrm, bbnrm,
87                                 rconde, rcondv, &work_query, lwork, iwork,
88                                 bwork );
89     if( info != 0 ) {
90         goto exit_level_2;
91     }
92     lwork = (lapack_int)work_query;
93     /* Allocate memory for work arrays */
94     work = (float*)LAPACKE_malloc( sizeof(float) * lwork );
95     if( work == NULL ) {
96         info = LAPACK_WORK_MEMORY_ERROR;
97         goto exit_level_2;
98     }
99     /* Call middle-level interface */
100     info = LAPACKE_sggevx_work( matrix_layout, balanc, jobvl, jobvr, sense, n, a,
101                                 lda, b, ldb, alphar, alphai, beta, vl, ldvl, vr,
102                                 ldvr, ilo, ihi, lscale, rscale, abnrm, bbnrm,
103                                 rconde, rcondv, work, lwork, iwork, bwork );
104     /* Release memory and exit */
105     LAPACKE_free( work );
106 exit_level_2:
107     if( LAPACKE_lsame( sense, 'b' ) || LAPACKE_lsame( sense, 'n' ) ||
108         LAPACKE_lsame( sense, 'v' ) ) {
109         LAPACKE_free( iwork );
110     }
111 exit_level_1:
112     if( LAPACKE_lsame( sense, 'b' ) || LAPACKE_lsame( sense, 'e' ) ||
113         LAPACKE_lsame( sense, 'v' ) ) {
114         LAPACKE_free( bwork );
115     }
116 exit_level_0:
117     if( info == LAPACK_WORK_MEMORY_ERROR ) {
118         LAPACKE_xerbla( "LAPACKE_sggevx", info );
119     }
120     return info;
121 }
122