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 sorcsd
30 * Author: Intel Corporation
31 *****************************************************************************/
32 
33 #include "lapacke_utils.h"
34 
LAPACKE_sorcsd(int matrix_layout,char jobu1,char jobu2,char jobv1t,char jobv2t,char trans,char signs,lapack_int m,lapack_int p,lapack_int q,float * x11,lapack_int ldx11,float * x12,lapack_int ldx12,float * x21,lapack_int ldx21,float * x22,lapack_int ldx22,float * theta,float * u1,lapack_int ldu1,float * u2,lapack_int ldu2,float * v1t,lapack_int ldv1t,float * v2t,lapack_int ldv2t)35 lapack_int LAPACKE_sorcsd( int matrix_layout, char jobu1, char jobu2,
36                            char jobv1t, char jobv2t, char trans, char signs,
37                            lapack_int m, lapack_int p, lapack_int q, float* x11,
38                            lapack_int ldx11, float* x12, lapack_int ldx12,
39                            float* x21, lapack_int ldx21, float* x22,
40                            lapack_int ldx22, float* theta, float* u1,
41                            lapack_int ldu1, float* u2, lapack_int ldu2,
42                            float* v1t, lapack_int ldv1t, float* v2t,
43                            lapack_int ldv2t )
44 {
45     lapack_int info = 0;
46     lapack_int lwork = -1;
47     lapack_int* iwork = NULL;
48     float* work = NULL;
49     float work_query;
50     int lapack_layout;
51     if( matrix_layout != LAPACK_COL_MAJOR && matrix_layout != LAPACK_ROW_MAJOR ) {
52         LAPACKE_xerbla( "LAPACKE_sorcsd", -1 );
53         return -1;
54     }
55     if( LAPACKE_lsame( trans, 'n' ) && matrix_layout == LAPACK_COL_MAJOR ) {
56         lapack_layout = LAPACK_COL_MAJOR;
57     } else {
58         lapack_layout = LAPACK_ROW_MAJOR;
59     }
60 #ifndef LAPACK_DISABLE_NAN_CHECK
61     if( LAPACKE_get_nancheck() ) {
62         /* Optionally check input matrices for NaNs */
63         if( LAPACKE_sge_nancheck( lapack_layout, p, q, x11, ldx11 ) ) {
64             return -11;
65         }
66         if( LAPACKE_sge_nancheck( lapack_layout, p, m-q, x12, ldx12 ) ) {
67             return -13;
68         }
69         if( LAPACKE_sge_nancheck( lapack_layout, m-p, q, x21, ldx21 ) ) {
70             return -15;
71         }
72         if( LAPACKE_sge_nancheck( lapack_layout, m-p, m-q, x22, ldx22 ) ) {
73             return -17;
74         }
75     }
76 #endif
77     /* Allocate memory for working array(s) */
78     iwork = (lapack_int*)LAPACKE_malloc( sizeof(lapack_int) * MAX(1,m-MIN(MIN(p,m-p),MIN(q,m-q))) );
79     if( iwork == NULL ) {
80         info = LAPACK_WORK_MEMORY_ERROR;
81         goto exit_level_0;
82     }
83     /* Query optimal working array(s) size */
84     info = LAPACKE_sorcsd_work( matrix_layout, jobu1, jobu2, jobv1t, jobv2t,
85                                 trans, signs, m, p, q, x11, ldx11, x12, ldx12,
86                                 x21, ldx21, x22, ldx22, theta, u1, ldu1, u2,
87                                 ldu2, v1t, ldv1t, v2t, ldv2t, &work_query,
88                                 lwork, iwork );
89     if( info != 0 ) {
90         goto exit_level_1;
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_1;
98     }
99     /* Call middle-level interface */
100     info = LAPACKE_sorcsd_work( matrix_layout, jobu1, jobu2, jobv1t, jobv2t,
101                                 trans, signs, m, p, q, x11, ldx11, x12, ldx12,
102                                 x21, ldx21, x22, ldx22, theta, u1, ldu1, u2,
103                                 ldu2, v1t, ldv1t, v2t, ldv2t, work, lwork,
104                                 iwork );
105     /* Release memory and exit */
106     LAPACKE_free( work );
107 exit_level_1:
108     LAPACKE_free( iwork );
109 exit_level_0:
110     if( info == LAPACK_WORK_MEMORY_ERROR ) {
111         LAPACKE_xerbla( "LAPACKE_sorcsd", info );
112     }
113     return info;
114 }
115