001:       SUBROUTINE CHETRF( UPLO, N, A, LDA, IPIV, WORK, LWORK, INFO )
002: *
003: *  -- LAPACK routine (version 3.2) --
004: *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
005: *     November 2006
006: *
007: *     .. Scalar Arguments ..
008:       CHARACTER          UPLO
009:       INTEGER            INFO, LDA, LWORK, N
010: *     ..
011: *     .. Array Arguments ..
012:       INTEGER            IPIV( * )
013:       COMPLEX            A( LDA, * ), WORK( * )
014: *     ..
015: *
016: *  Purpose
017: *  =======
018: *
019: *  CHETRF computes the factorization of a complex Hermitian matrix A
020: *  using the Bunch-Kaufman diagonal pivoting method.  The form of the
021: *  factorization is
022: *
023: *     A = U*D*U**H  or  A = L*D*L**H
024: *
025: *  where U (or L) is a product of permutation and unit upper (lower)
026: *  triangular matrices, and D is Hermitian and block diagonal with 
027: *  1-by-1 and 2-by-2 diagonal blocks.
028: *
029: *  This is the blocked version of the algorithm, calling Level 3 BLAS.
030: *
031: *  Arguments
032: *  =========
033: *
034: *  UPLO    (input) CHARACTER*1
035: *          = 'U':  Upper triangle of A is stored;
036: *          = 'L':  Lower triangle of A is stored.
037: *
038: *  N       (input) INTEGER
039: *          The order of the matrix A.  N >= 0.
040: *
041: *  A       (input/output) COMPLEX array, dimension (LDA,N)
042: *          On entry, the Hermitian matrix A.  If UPLO = 'U', the leading
043: *          N-by-N upper triangular part of A contains the upper
044: *          triangular part of the matrix A, and the strictly lower
045: *          triangular part of A is not referenced.  If UPLO = 'L', the
046: *          leading N-by-N lower triangular part of A contains the lower
047: *          triangular part of the matrix A, and the strictly upper
048: *          triangular part of A is not referenced.
049: *
050: *          On exit, the block diagonal matrix D and the multipliers used
051: *          to obtain the factor U or L (see below for further details).
052: *
053: *  LDA     (input) INTEGER
054: *          The leading dimension of the array A.  LDA >= max(1,N).
055: *
056: *  IPIV    (output) INTEGER array, dimension (N)
057: *          Details of the interchanges and the block structure of D.
058: *          If IPIV(k) > 0, then rows and columns k and IPIV(k) were
059: *          interchanged and D(k,k) is a 1-by-1 diagonal block.
060: *          If UPLO = 'U' and IPIV(k) = IPIV(k-1) < 0, then rows and
061: *          columns k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k)
062: *          is a 2-by-2 diagonal block.  If UPLO = 'L' and IPIV(k) =
063: *          IPIV(k+1) < 0, then rows and columns k+1 and -IPIV(k) were
064: *          interchanged and D(k:k+1,k:k+1) is a 2-by-2 diagonal block.
065: *
066: *  WORK    (workspace/output) COMPLEX array, dimension (MAX(1,LWORK))
067: *          On exit, if INFO = 0, WORK(1) returns the optimal LWORK.
068: *
069: *  LWORK   (input) INTEGER
070: *          The length of WORK.  LWORK >=1.  For best performance
071: *          LWORK >= N*NB, where NB is the block size returned by ILAENV.
072: *
073: *  INFO    (output) INTEGER
074: *          = 0:  successful exit
075: *          < 0:  if INFO = -i, the i-th argument had an illegal value
076: *          > 0:  if INFO = i, D(i,i) is exactly zero.  The factorization
077: *                has been completed, but the block diagonal matrix D is
078: *                exactly singular, and division by zero will occur if it
079: *                is used to solve a system of equations.
080: *
081: *  Further Details
082: *  ===============
083: *
084: *  If UPLO = 'U', then A = U*D*U', where
085: *     U = P(n)*U(n)* ... *P(k)U(k)* ...,
086: *  i.e., U is a product of terms P(k)*U(k), where k decreases from n to
087: *  1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1
088: *  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as
089: *  defined by IPIV(k), and U(k) is a unit upper triangular matrix, such
090: *  that if the diagonal block D(k) is of order s (s = 1 or 2), then
091: *
092: *             (   I    v    0   )   k-s
093: *     U(k) =  (   0    I    0   )   s
094: *             (   0    0    I   )   n-k
095: *                k-s   s   n-k
096: *
097: *  If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k).
098: *  If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k),
099: *  and A(k,k), and v overwrites A(1:k-2,k-1:k).
100: *
101: *  If UPLO = 'L', then A = L*D*L', where
102: *     L = P(1)*L(1)* ... *P(k)*L(k)* ...,
103: *  i.e., L is a product of terms P(k)*L(k), where k increases from 1 to
104: *  n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1
105: *  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as
106: *  defined by IPIV(k), and L(k) is a unit lower triangular matrix, such
107: *  that if the diagonal block D(k) is of order s (s = 1 or 2), then
108: *
109: *             (   I    0     0   )  k-1
110: *     L(k) =  (   0    I     0   )  s
111: *             (   0    v     I   )  n-k-s+1
112: *                k-1   s  n-k-s+1
113: *
114: *  If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k).
115: *  If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k),
116: *  and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1).
117: *
118: *  =====================================================================
119: *
120: *     .. Local Scalars ..
121:       LOGICAL            LQUERY, UPPER
122:       INTEGER            IINFO, IWS, J, K, KB, LDWORK, LWKOPT, NB, NBMIN
123: *     ..
124: *     .. External Functions ..
125:       LOGICAL            LSAME
126:       INTEGER            ILAENV
127:       EXTERNAL           LSAME, ILAENV
128: *     ..
129: *     .. External Subroutines ..
130:       EXTERNAL           CHETF2, CLAHEF, XERBLA
131: *     ..
132: *     .. Intrinsic Functions ..
133:       INTRINSIC          MAX
134: *     ..
135: *     .. Executable Statements ..
136: *
137: *     Test the input parameters.
138: *
139:       INFO = 0
140:       UPPER = LSAME( UPLO, 'U' )
141:       LQUERY = ( LWORK.EQ.-1 )
142:       IF( .NOT.UPPER .AND. .NOT.LSAME( UPLO, 'L' ) ) THEN
143:          INFO = -1
144:       ELSE IF( N.LT.0 ) THEN
145:          INFO = -2
146:       ELSE IF( LDA.LT.MAX( 1, N ) ) THEN
147:          INFO = -4
148:       ELSE IF( LWORK.LT.1 .AND. .NOT.LQUERY ) THEN
149:          INFO = -7
150:       END IF
151: *
152:       IF( INFO.EQ.0 ) THEN
153: *
154: *        Determine the block size
155: *
156:          NB = ILAENV( 1, 'CHETRF', UPLO, N, -1, -1, -1 )
157:          LWKOPT = N*NB
158:          WORK( 1 ) = LWKOPT
159:       END IF
160: *
161:       IF( INFO.NE.0 ) THEN
162:          CALL XERBLA( 'CHETRF', -INFO )
163:          RETURN
164:       ELSE IF( LQUERY ) THEN
165:          RETURN
166:       END IF
167: *
168:       NBMIN = 2
169:       LDWORK = N
170:       IF( NB.GT.1 .AND. NB.LT.N ) THEN
171:          IWS = LDWORK*NB
172:          IF( LWORK.LT.IWS ) THEN
173:             NB = MAX( LWORK / LDWORK, 1 )
174:             NBMIN = MAX( 2, ILAENV( 2, 'CHETRF', UPLO, N, -1, -1, -1 ) )
175:          END IF
176:       ELSE
177:          IWS = 1
178:       END IF
179:       IF( NB.LT.NBMIN )
180:      $   NB = N
181: *
182:       IF( UPPER ) THEN
183: *
184: *        Factorize A as U*D*U' using the upper triangle of A
185: *
186: *        K is the main loop index, decreasing from N to 1 in steps of
187: *        KB, where KB is the number of columns factorized by CLAHEF;
188: *        KB is either NB or NB-1, or K for the last block
189: *
190:          K = N
191:    10    CONTINUE
192: *
193: *        If K < 1, exit from loop
194: *
195:          IF( K.LT.1 )
196:      $      GO TO 40
197: *
198:          IF( K.GT.NB ) THEN
199: *
200: *           Factorize columns k-kb+1:k of A and use blocked code to
201: *           update columns 1:k-kb
202: *
203:             CALL CLAHEF( UPLO, K, NB, KB, A, LDA, IPIV, WORK, N, IINFO )
204:          ELSE
205: *
206: *           Use unblocked code to factorize columns 1:k of A
207: *
208:             CALL CHETF2( UPLO, K, A, LDA, IPIV, IINFO )
209:             KB = K
210:          END IF
211: *
212: *        Set INFO on the first occurrence of a zero pivot
213: *
214:          IF( INFO.EQ.0 .AND. IINFO.GT.0 )
215:      $      INFO = IINFO
216: *
217: *        Decrease K and return to the start of the main loop
218: *
219:          K = K - KB
220:          GO TO 10
221: *
222:       ELSE
223: *
224: *        Factorize A as L*D*L' using the lower triangle of A
225: *
226: *        K is the main loop index, increasing from 1 to N in steps of
227: *        KB, where KB is the number of columns factorized by CLAHEF;
228: *        KB is either NB or NB-1, or N-K+1 for the last block
229: *
230:          K = 1
231:    20    CONTINUE
232: *
233: *        If K > N, exit from loop
234: *
235:          IF( K.GT.N )
236:      $      GO TO 40
237: *
238:          IF( K.LE.N-NB ) THEN
239: *
240: *           Factorize columns k:k+kb-1 of A and use blocked code to
241: *           update columns k+kb:n
242: *
243:             CALL CLAHEF( UPLO, N-K+1, NB, KB, A( K, K ), LDA, IPIV( K ),
244:      $                   WORK, N, IINFO )
245:          ELSE
246: *
247: *           Use unblocked code to factorize columns k:n of A
248: *
249:             CALL CHETF2( UPLO, N-K+1, A( K, K ), LDA, IPIV( K ), IINFO )
250:             KB = N - K + 1
251:          END IF
252: *
253: *        Set INFO on the first occurrence of a zero pivot
254: *
255:          IF( INFO.EQ.0 .AND. IINFO.GT.0 )
256:      $      INFO = IINFO + K - 1
257: *
258: *        Adjust IPIV
259: *
260:          DO 30 J = K, K + KB - 1
261:             IF( IPIV( J ).GT.0 ) THEN
262:                IPIV( J ) = IPIV( J ) + K - 1
263:             ELSE
264:                IPIV( J ) = IPIV( J ) - K + 1
265:             END IF
266:    30    CONTINUE
267: *
268: *        Increase K and return to the start of the main loop
269: *
270:          K = K + KB
271:          GO TO 20
272: *
273:       END IF
274: *
275:    40 CONTINUE
276:       WORK( 1 ) = LWKOPT
277:       RETURN
278: *
279: *     End of CHETRF
280: *
281:       END
282: