001:       SUBROUTINE SGEQP3( M, N, A, LDA, JPVT, TAU, 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:       INTEGER            INFO, LDA, LWORK, M, N
009: *     ..
010: *     .. Array Arguments ..
011:       INTEGER            JPVT( * )
012:       REAL               A( LDA, * ), TAU( * ), WORK( * )
013: *     ..
014: *
015: *  Purpose
016: *  =======
017: *
018: *  SGEQP3 computes a QR factorization with column pivoting of a
019: *  matrix A:  A*P = Q*R  using Level 3 BLAS.
020: *
021: *  Arguments
022: *  =========
023: *
024: *  M       (input) INTEGER
025: *          The number of rows of the matrix A. M >= 0.
026: *
027: *  N       (input) INTEGER
028: *          The number of columns of the matrix A.  N >= 0.
029: *
030: *  A       (input/output) REAL array, dimension (LDA,N)
031: *          On entry, the M-by-N matrix A.
032: *          On exit, the upper triangle of the array contains the
033: *          min(M,N)-by-N upper trapezoidal matrix R; the elements below
034: *          the diagonal, together with the array TAU, represent the
035: *          orthogonal matrix Q as a product of min(M,N) elementary
036: *          reflectors.
037: *
038: *  LDA     (input) INTEGER
039: *          The leading dimension of the array A. LDA >= max(1,M).
040: *
041: *  JPVT    (input/output) INTEGER array, dimension (N)
042: *          On entry, if JPVT(J).ne.0, the J-th column of A is permuted
043: *          to the front of A*P (a leading column); if JPVT(J)=0,
044: *          the J-th column of A is a free column.
045: *          On exit, if JPVT(J)=K, then the J-th column of A*P was the
046: *          the K-th column of A.
047: *
048: *  TAU     (output) REAL array, dimension (min(M,N))
049: *          The scalar factors of the elementary reflectors.
050: *
051: *  WORK    (workspace/output) REAL array, dimension (MAX(1,LWORK))
052: *          On exit, if INFO=0, WORK(1) returns the optimal LWORK.
053: *
054: *  LWORK   (input) INTEGER
055: *          The dimension of the array WORK. LWORK >= 3*N+1.
056: *          For optimal performance LWORK >= 2*N+( N+1 )*NB, where NB
057: *          is the optimal blocksize.
058: *
059: *          If LWORK = -1, then a workspace query is assumed; the routine
060: *          only calculates the optimal size of the WORK array, returns
061: *          this value as the first entry of the WORK array, and no error
062: *          message related to LWORK is issued by XERBLA.
063: *
064: *  INFO    (output) INTEGER
065: *          = 0: successful exit.
066: *          < 0: if INFO = -i, the i-th argument had an illegal value.
067: *
068: *  Further Details
069: *  ===============
070: *
071: *  The matrix Q is represented as a product of elementary reflectors
072: *
073: *     Q = H(1) H(2) . . . H(k), where k = min(m,n).
074: *
075: *  Each H(i) has the form
076: *
077: *     H(i) = I - tau * v * v'
078: *
079: *  where tau is a real/complex scalar, and v is a real/complex vector
080: *  with v(1:i-1) = 0 and v(i) = 1; v(i+1:m) is stored on exit in
081: *  A(i+1:m,i), and tau in TAU(i).
082: *
083: *  Based on contributions by
084: *    G. Quintana-Orti, Depto. de Informatica, Universidad Jaime I, Spain
085: *    X. Sun, Computer Science Dept., Duke University, USA
086: *
087: *  =====================================================================
088: *
089: *     .. Parameters ..
090:       INTEGER            INB, INBMIN, IXOVER
091:       PARAMETER          ( INB = 1, INBMIN = 2, IXOVER = 3 )
092: *     ..
093: *     .. Local Scalars ..
094:       LOGICAL            LQUERY
095:       INTEGER            FJB, IWS, J, JB, LWKOPT, MINMN, MINWS, NA, NB,
096:      $                   NBMIN, NFXD, NX, SM, SMINMN, SN, TOPBMN
097: *     ..
098: *     .. External Subroutines ..
099:       EXTERNAL           SGEQRF, SLAQP2, SLAQPS, SORMQR, SSWAP, XERBLA
100: *     ..
101: *     .. External Functions ..
102:       INTEGER            ILAENV
103:       REAL               SNRM2
104:       EXTERNAL           ILAENV, SNRM2
105: *     ..
106: *     .. Intrinsic Functions ..
107:       INTRINSIC          INT, MAX, MIN
108: *     ..
109: *     .. Executable Statements ..
110: *
111:       INFO = 0
112:       LQUERY = ( LWORK.EQ.-1 )
113:       IF( M.LT.0 ) THEN
114:          INFO = -1
115:       ELSE IF( N.LT.0 ) THEN
116:          INFO = -2
117:       ELSE IF( LDA.LT.MAX( 1, M ) ) THEN
118:          INFO = -4
119:       END IF
120: *
121:       IF( INFO.EQ.0 ) THEN
122:          MINMN = MIN( M, N )
123:          IF( MINMN.EQ.0 ) THEN
124:             IWS = 1
125:             LWKOPT = 1
126:          ELSE
127:             IWS = 3*N + 1
128:             NB = ILAENV( INB, 'SGEQRF', ' ', M, N, -1, -1 )
129:             LWKOPT = 2*N + ( N + 1 )*NB
130:          END IF
131:          WORK( 1 ) = LWKOPT
132: *
133:          IF( ( LWORK.LT.IWS ) .AND. .NOT.LQUERY ) THEN
134:             INFO = -8
135:          END IF
136:       END IF
137: *
138:       IF( INFO.NE.0 ) THEN
139:          CALL XERBLA( 'SGEQP3', -INFO )
140:          RETURN
141:       ELSE IF( LQUERY ) THEN
142:          RETURN
143:       END IF
144: *
145: *     Quick return if possible.
146: *
147:       IF( MINMN.EQ.0 ) THEN
148:          RETURN
149:       END IF
150: *
151: *     Move initial columns up front.
152: *
153:       NFXD = 1
154:       DO 10 J = 1, N
155:          IF( JPVT( J ).NE.0 ) THEN
156:             IF( J.NE.NFXD ) THEN
157:                CALL SSWAP( M, A( 1, J ), 1, A( 1, NFXD ), 1 )
158:                JPVT( J ) = JPVT( NFXD )
159:                JPVT( NFXD ) = J
160:             ELSE
161:                JPVT( J ) = J
162:             END IF
163:             NFXD = NFXD + 1
164:          ELSE
165:             JPVT( J ) = J
166:          END IF
167:    10 CONTINUE
168:       NFXD = NFXD - 1
169: *
170: *     Factorize fixed columns
171: *     =======================
172: *
173: *     Compute the QR factorization of fixed columns and update
174: *     remaining columns.
175: *
176:       IF( NFXD.GT.0 ) THEN
177:          NA = MIN( M, NFXD )
178: *CC      CALL SGEQR2( M, NA, A, LDA, TAU, WORK, INFO )
179:          CALL SGEQRF( M, NA, A, LDA, TAU, WORK, LWORK, INFO )
180:          IWS = MAX( IWS, INT( WORK( 1 ) ) )
181:          IF( NA.LT.N ) THEN
182: *CC         CALL SORM2R( 'Left', 'Transpose', M, N-NA, NA, A, LDA,
183: *CC  $                   TAU, A( 1, NA+1 ), LDA, WORK, INFO )
184:             CALL SORMQR( 'Left', 'Transpose', M, N-NA, NA, A, LDA, TAU,
185:      $                   A( 1, NA+1 ), LDA, WORK, LWORK, INFO )
186:             IWS = MAX( IWS, INT( WORK( 1 ) ) )
187:          END IF
188:       END IF
189: *
190: *     Factorize free columns
191: *     ======================
192: *
193:       IF( NFXD.LT.MINMN ) THEN
194: *
195:          SM = M - NFXD
196:          SN = N - NFXD
197:          SMINMN = MINMN - NFXD
198: *
199: *        Determine the block size.
200: *
201:          NB = ILAENV( INB, 'SGEQRF', ' ', SM, SN, -1, -1 )
202:          NBMIN = 2
203:          NX = 0
204: *
205:          IF( ( NB.GT.1 ) .AND. ( NB.LT.SMINMN ) ) THEN
206: *
207: *           Determine when to cross over from blocked to unblocked code.
208: *
209:             NX = MAX( 0, ILAENV( IXOVER, 'SGEQRF', ' ', SM, SN, -1,
210:      $           -1 ) )
211: *
212: *
213:             IF( NX.LT.SMINMN ) THEN
214: *
215: *              Determine if workspace is large enough for blocked code.
216: *
217:                MINWS = 2*SN + ( SN+1 )*NB
218:                IWS = MAX( IWS, MINWS )
219:                IF( LWORK.LT.MINWS ) THEN
220: *
221: *                 Not enough workspace to use optimal NB: Reduce NB and
222: *                 determine the minimum value of NB.
223: *
224:                   NB = ( LWORK-2*SN ) / ( SN+1 )
225:                   NBMIN = MAX( 2, ILAENV( INBMIN, 'SGEQRF', ' ', SM, SN,
226:      $                    -1, -1 ) )
227: *
228: *
229:                END IF
230:             END IF
231:          END IF
232: *
233: *        Initialize partial column norms. The first N elements of work
234: *        store the exact column norms.
235: *
236:          DO 20 J = NFXD + 1, N
237:             WORK( J ) = SNRM2( SM, A( NFXD+1, J ), 1 )
238:             WORK( N+J ) = WORK( J )
239:    20    CONTINUE
240: *
241:          IF( ( NB.GE.NBMIN ) .AND. ( NB.LT.SMINMN ) .AND.
242:      $       ( NX.LT.SMINMN ) ) THEN
243: *
244: *           Use blocked code initially.
245: *
246:             J = NFXD + 1
247: *
248: *           Compute factorization: while loop.
249: *
250: *
251:             TOPBMN = MINMN - NX
252:    30       CONTINUE
253:             IF( J.LE.TOPBMN ) THEN
254:                JB = MIN( NB, TOPBMN-J+1 )
255: *
256: *              Factorize JB columns among columns J:N.
257: *
258:                CALL SLAQPS( M, N-J+1, J-1, JB, FJB, A( 1, J ), LDA,
259:      $                      JPVT( J ), TAU( J ), WORK( J ), WORK( N+J ),
260:      $                      WORK( 2*N+1 ), WORK( 2*N+JB+1 ), N-J+1 )
261: *
262:                J = J + FJB
263:                GO TO 30
264:             END IF
265:          ELSE
266:             J = NFXD + 1
267:          END IF
268: *
269: *        Use unblocked code to factor the last or only block.
270: *
271: *
272:          IF( J.LE.MINMN )
273:      $      CALL SLAQP2( M, N-J+1, J-1, A( 1, J ), LDA, JPVT( J ),
274:      $                   TAU( J ), WORK( J ), WORK( N+J ),
275:      $                   WORK( 2*N+1 ) )
276: *
277:       END IF
278: *
279:       WORK( 1 ) = IWS
280:       RETURN
281: *
282: *     End of SGEQP3
283: *
284:       END
285: