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 sgges
30 * Author: Intel Corporation
31 *****************************************************************************/
32 
33 #include "lapacke_utils.h"
34 
LAPACKE_sgges(int matrix_layout,char jobvsl,char jobvsr,char sort,LAPACK_S_SELECT3 selctg,lapack_int n,float * a,lapack_int lda,float * b,lapack_int ldb,lapack_int * sdim,float * alphar,float * alphai,float * beta,float * vsl,lapack_int ldvsl,float * vsr,lapack_int ldvsr)35 lapack_int LAPACKE_sgges( int matrix_layout, char jobvsl, char jobvsr, char sort,
36                           LAPACK_S_SELECT3 selctg, lapack_int n, float* a,
37                           lapack_int lda, float* b, lapack_int ldb,
38                           lapack_int* sdim, float* alphar, float* alphai,
39                           float* beta, float* vsl, lapack_int ldvsl, float* vsr,
40                           lapack_int ldvsr )
41 {
42     lapack_int info = 0;
43     lapack_int lwork = -1;
44     lapack_logical* bwork = NULL;
45     float* work = NULL;
46     float work_query;
47     if( matrix_layout != LAPACK_COL_MAJOR && matrix_layout != LAPACK_ROW_MAJOR ) {
48         LAPACKE_xerbla( "LAPACKE_sgges", -1 );
49         return -1;
50     }
51 #ifndef LAPACK_DISABLE_NAN_CHECK
52     if( LAPACKE_get_nancheck() ) {
53         /* Optionally check input matrices for NaNs */
54         if( LAPACKE_sge_nancheck( matrix_layout, n, n, a, lda ) ) {
55             return -7;
56         }
57         if( LAPACKE_sge_nancheck( matrix_layout, n, n, b, ldb ) ) {
58             return -9;
59         }
60     }
61 #endif
62     /* Allocate memory for working array(s) */
63     if( LAPACKE_lsame( sort, 's' ) ) {
64         bwork = (lapack_logical*)
65             LAPACKE_malloc( sizeof(lapack_logical) * MAX(1,n) );
66         if( bwork == NULL ) {
67             info = LAPACK_WORK_MEMORY_ERROR;
68             goto exit_level_0;
69         }
70     }
71     /* Query optimal working array(s) size */
72     info = LAPACKE_sgges_work( matrix_layout, jobvsl, jobvsr, sort, selctg, n, a,
73                                lda, b, ldb, sdim, alphar, alphai, beta, vsl,
74                                ldvsl, vsr, ldvsr, &work_query, lwork, bwork );
75     if( info != 0 ) {
76         goto exit_level_1;
77     }
78     lwork = (lapack_int)work_query;
79     /* Allocate memory for work arrays */
80     work = (float*)LAPACKE_malloc( sizeof(float) * lwork );
81     if( work == NULL ) {
82         info = LAPACK_WORK_MEMORY_ERROR;
83         goto exit_level_1;
84     }
85     /* Call middle-level interface */
86     info = LAPACKE_sgges_work( matrix_layout, jobvsl, jobvsr, sort, selctg, n, a,
87                                lda, b, ldb, sdim, alphar, alphai, beta, vsl,
88                                ldvsl, vsr, ldvsr, work, lwork, bwork );
89     /* Release memory and exit */
90     LAPACKE_free( work );
91 exit_level_1:
92     if( LAPACKE_lsame( sort, 's' ) ) {
93         LAPACKE_free( bwork );
94     }
95 exit_level_0:
96     if( info == LAPACK_WORK_MEMORY_ERROR ) {
97         LAPACKE_xerbla( "LAPACKE_sgges", info );
98     }
99     return info;
100 }
101