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 middle-level C interface to LAPACK function ztgsyl
30 * Author: Intel Corporation
31 *****************************************************************************/
32 
33 #include "lapacke_utils.h"
34 
LAPACKE_ztgsyl_work(int matrix_layout,char trans,lapack_int ijob,lapack_int m,lapack_int n,const lapack_complex_double * a,lapack_int lda,const lapack_complex_double * b,lapack_int ldb,lapack_complex_double * c,lapack_int ldc,const lapack_complex_double * d,lapack_int ldd,const lapack_complex_double * e,lapack_int lde,lapack_complex_double * f,lapack_int ldf,double * scale,double * dif,lapack_complex_double * work,lapack_int lwork,lapack_int * iwork)35 lapack_int LAPACKE_ztgsyl_work( int matrix_layout, char trans, lapack_int ijob,
36                                 lapack_int m, lapack_int n,
37                                 const lapack_complex_double* a, lapack_int lda,
38                                 const lapack_complex_double* b, lapack_int ldb,
39                                 lapack_complex_double* c, lapack_int ldc,
40                                 const lapack_complex_double* d, lapack_int ldd,
41                                 const lapack_complex_double* e, lapack_int lde,
42                                 lapack_complex_double* f, lapack_int ldf,
43                                 double* scale, double* dif,
44                                 lapack_complex_double* work, lapack_int lwork,
45                                 lapack_int* iwork )
46 {
47     lapack_int info = 0;
48     if( matrix_layout == LAPACK_COL_MAJOR ) {
49         /* Call LAPACK function and adjust info */
50         LAPACK_ztgsyl( &trans, &ijob, &m, &n, a, &lda, b, &ldb, c, &ldc, d,
51                        &ldd, e, &lde, f, &ldf, scale, dif, work, &lwork, iwork,
52                        &info );
53         if( info < 0 ) {
54             info = info - 1;
55         }
56     } else if( matrix_layout == LAPACK_ROW_MAJOR ) {
57         lapack_int lda_t = MAX(1,m);
58         lapack_int ldb_t = MAX(1,n);
59         lapack_int ldc_t = MAX(1,m);
60         lapack_int ldd_t = MAX(1,m);
61         lapack_int lde_t = MAX(1,n);
62         lapack_int ldf_t = MAX(1,m);
63         lapack_complex_double* a_t = NULL;
64         lapack_complex_double* b_t = NULL;
65         lapack_complex_double* c_t = NULL;
66         lapack_complex_double* d_t = NULL;
67         lapack_complex_double* e_t = NULL;
68         lapack_complex_double* f_t = NULL;
69         /* Check leading dimension(s) */
70         if( lda < m ) {
71             info = -7;
72             LAPACKE_xerbla( "LAPACKE_ztgsyl_work", info );
73             return info;
74         }
75         if( ldb < n ) {
76             info = -9;
77             LAPACKE_xerbla( "LAPACKE_ztgsyl_work", info );
78             return info;
79         }
80         if( ldc < n ) {
81             info = -11;
82             LAPACKE_xerbla( "LAPACKE_ztgsyl_work", info );
83             return info;
84         }
85         if( ldd < m ) {
86             info = -13;
87             LAPACKE_xerbla( "LAPACKE_ztgsyl_work", info );
88             return info;
89         }
90         if( lde < n ) {
91             info = -15;
92             LAPACKE_xerbla( "LAPACKE_ztgsyl_work", info );
93             return info;
94         }
95         if( ldf < n ) {
96             info = -17;
97             LAPACKE_xerbla( "LAPACKE_ztgsyl_work", info );
98             return info;
99         }
100         /* Query optimal working array(s) size if requested */
101         if( lwork == -1 ) {
102             LAPACK_ztgsyl( &trans, &ijob, &m, &n, a, &lda_t, b, &ldb_t, c,
103                            &ldc_t, d, &ldd_t, e, &lde_t, f, &ldf_t, scale, dif,
104                            work, &lwork, iwork, &info );
105             return (info < 0) ? (info - 1) : info;
106         }
107         /* Allocate memory for temporary array(s) */
108         a_t = (lapack_complex_double*)
109             LAPACKE_malloc( sizeof(lapack_complex_double) * lda_t * MAX(1,m) );
110         if( a_t == NULL ) {
111             info = LAPACK_TRANSPOSE_MEMORY_ERROR;
112             goto exit_level_0;
113         }
114         b_t = (lapack_complex_double*)
115             LAPACKE_malloc( sizeof(lapack_complex_double) * ldb_t * MAX(1,n) );
116         if( b_t == NULL ) {
117             info = LAPACK_TRANSPOSE_MEMORY_ERROR;
118             goto exit_level_1;
119         }
120         c_t = (lapack_complex_double*)
121             LAPACKE_malloc( sizeof(lapack_complex_double) * ldc_t * MAX(1,n) );
122         if( c_t == NULL ) {
123             info = LAPACK_TRANSPOSE_MEMORY_ERROR;
124             goto exit_level_2;
125         }
126         d_t = (lapack_complex_double*)
127             LAPACKE_malloc( sizeof(lapack_complex_double) * ldd_t * MAX(1,m) );
128         if( d_t == NULL ) {
129             info = LAPACK_TRANSPOSE_MEMORY_ERROR;
130             goto exit_level_3;
131         }
132         e_t = (lapack_complex_double*)
133             LAPACKE_malloc( sizeof(lapack_complex_double) * lde_t * MAX(1,n) );
134         if( e_t == NULL ) {
135             info = LAPACK_TRANSPOSE_MEMORY_ERROR;
136             goto exit_level_4;
137         }
138         f_t = (lapack_complex_double*)
139             LAPACKE_malloc( sizeof(lapack_complex_double) * ldf_t * MAX(1,n) );
140         if( f_t == NULL ) {
141             info = LAPACK_TRANSPOSE_MEMORY_ERROR;
142             goto exit_level_5;
143         }
144         /* Transpose input matrices */
145         LAPACKE_zge_trans( matrix_layout, m, m, a, lda, a_t, lda_t );
146         LAPACKE_zge_trans( matrix_layout, n, n, b, ldb, b_t, ldb_t );
147         LAPACKE_zge_trans( matrix_layout, m, n, c, ldc, c_t, ldc_t );
148         LAPACKE_zge_trans( matrix_layout, m, m, d, ldd, d_t, ldd_t );
149         LAPACKE_zge_trans( matrix_layout, n, n, e, lde, e_t, lde_t );
150         LAPACKE_zge_trans( matrix_layout, m, n, f, ldf, f_t, ldf_t );
151         /* Call LAPACK function and adjust info */
152         LAPACK_ztgsyl( &trans, &ijob, &m, &n, a_t, &lda_t, b_t, &ldb_t, c_t,
153                        &ldc_t, d_t, &ldd_t, e_t, &lde_t, f_t, &ldf_t, scale,
154                        dif, work, &lwork, iwork, &info );
155         if( info < 0 ) {
156             info = info - 1;
157         }
158         /* Transpose output matrices */
159         LAPACKE_zge_trans( LAPACK_COL_MAJOR, m, n, c_t, ldc_t, c, ldc );
160         LAPACKE_zge_trans( LAPACK_COL_MAJOR, m, n, f_t, ldf_t, f, ldf );
161         /* Release memory and exit */
162         LAPACKE_free( f_t );
163 exit_level_5:
164         LAPACKE_free( e_t );
165 exit_level_4:
166         LAPACKE_free( d_t );
167 exit_level_3:
168         LAPACKE_free( c_t );
169 exit_level_2:
170         LAPACKE_free( b_t );
171 exit_level_1:
172         LAPACKE_free( a_t );
173 exit_level_0:
174         if( info == LAPACK_TRANSPOSE_MEMORY_ERROR ) {
175             LAPACKE_xerbla( "LAPACKE_ztgsyl_work", info );
176         }
177     } else {
178         info = -1;
179         LAPACKE_xerbla( "LAPACKE_ztgsyl_work", info );
180     }
181     return info;
182 }
183