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