1      SUBROUTINE PCUNGLQ( M, N, K, A, IA, JA, DESCA, TAU, WORK, LWORK,
2     $                    INFO )
3*
4*  -- ScaLAPACK routine (version 1.7) --
5*     University of Tennessee, Knoxville, Oak Ridge National Laboratory,
6*     and University of California, Berkeley.
7*     May 25, 2001
8*
9*     .. Scalar Arguments ..
10      INTEGER            IA, INFO, JA, K, LWORK, M, N
11*     ..
12*     .. Array Arguments ..
13      INTEGER            DESCA( * )
14      COMPLEX            A( * ), TAU( * ), WORK( * )
15*     ..
16*
17*  Purpose
18*  =======
19*
20*  PCUNGLQ generates an M-by-N complex distributed matrix Q denoting
21*  A(IA:IA+M-1,JA:JA+N-1) with orthonormal rows, which is defined as
22*  the first M rows of a product of K elementary reflectors of order N
23*
24*        Q  =  H(k)' . . . H(2)' H(1)'
25*
26*  as returned by PCGELQF.
27*
28*  Notes
29*  =====
30*
31*  Each global data object is described by an associated description
32*  vector.  This vector stores the information required to establish
33*  the mapping between an object element and its corresponding process
34*  and memory location.
35*
36*  Let A be a generic term for any 2D block cyclicly distributed array.
37*  Such a global array has an associated description vector DESCA.
38*  In the following comments, the character _ should be read as
39*  "of the global array".
40*
41*  NOTATION        STORED IN      EXPLANATION
42*  --------------- -------------- --------------------------------------
43*  DTYPE_A(global) DESCA( DTYPE_ )The descriptor type.  In this case,
44*                                 DTYPE_A = 1.
45*  CTXT_A (global) DESCA( CTXT_ ) The BLACS context handle, indicating
46*                                 the BLACS process grid A is distribu-
47*                                 ted over. The context itself is glo-
48*                                 bal, but the handle (the integer
49*                                 value) may vary.
50*  M_A    (global) DESCA( M_ )    The number of rows in the global
51*                                 array A.
52*  N_A    (global) DESCA( N_ )    The number of columns in the global
53*                                 array A.
54*  MB_A   (global) DESCA( MB_ )   The blocking factor used to distribute
55*                                 the rows of the array.
56*  NB_A   (global) DESCA( NB_ )   The blocking factor used to distribute
57*                                 the columns of the array.
58*  RSRC_A (global) DESCA( RSRC_ ) The process row over which the first
59*                                 row of the array A is distributed.
60*  CSRC_A (global) DESCA( CSRC_ ) The process column over which the
61*                                 first column of the array A is
62*                                 distributed.
63*  LLD_A  (local)  DESCA( LLD_ )  The leading dimension of the local
64*                                 array.  LLD_A >= MAX(1,LOCr(M_A)).
65*
66*  Let K be the number of rows or columns of a distributed matrix,
67*  and assume that its process grid has dimension p x q.
68*  LOCr( K ) denotes the number of elements of K that a process
69*  would receive if K were distributed over the p processes of its
70*  process column.
71*  Similarly, LOCc( K ) denotes the number of elements of K that a
72*  process would receive if K were distributed over the q processes of
73*  its process row.
74*  The values of LOCr() and LOCc() may be determined via a call to the
75*  ScaLAPACK tool function, NUMROC:
76*          LOCr( M ) = NUMROC( M, MB_A, MYROW, RSRC_A, NPROW ),
77*          LOCc( N ) = NUMROC( N, NB_A, MYCOL, CSRC_A, NPCOL ).
78*  An upper bound for these quantities may be computed by:
79*          LOCr( M ) <= ceil( ceil(M/MB_A)/NPROW )*MB_A
80*          LOCc( N ) <= ceil( ceil(N/NB_A)/NPCOL )*NB_A
81*
82*  Arguments
83*  =========
84*
85*  M       (global input) INTEGER
86*          The number of rows to be operated on i.e the number of rows
87*          of the distributed submatrix Q. M >= 0.
88*
89*  N       (global input) INTEGER
90*          The number of columns to be operated on i.e the number of
91*          columns of the distributed submatrix Q. N >= M >= 0.
92*
93*  K       (global input) INTEGER
94*          The number of elementary reflectors whose product defines the
95*          matrix Q. M >= K >= 0.
96*
97*  A       (local input/local output) COMPLEX pointer into the
98*          local memory to an array of dimension (LLD_A,LOCc(JA+N-1)).
99*          On entry, the i-th row must contain the vector which defines
100*          the elementary reflector H(i), IA <= i <= IA+K-1, as
101*          returned by PCGELQF in the K rows of its distributed matrix
102*          argument A(IA:IA+K-1,JA:*). On exit, this array contains the
103*          local pieces of the M-by-N distributed matrix Q.
104*
105*  IA      (global input) INTEGER
106*          The row index in the global array A indicating the first
107*          row of sub( A ).
108*
109*  JA      (global input) INTEGER
110*          The column index in the global array A indicating the
111*          first column of sub( A ).
112*
113*  DESCA   (global and local input) INTEGER array of dimension DLEN_.
114*          The array descriptor for the distributed matrix A.
115*
116*  TAU     (local input) COMPLEX, array, dimension LOCr(IA+K-1).
117*          This array contains the scalar factors TAU(i) of the
118*          elementary reflectors H(i) as returned by PCGELQF.
119*          TAU is tied to the distributed matrix A.
120*
121*  WORK    (local workspace/local output) COMPLEX array,
122*                                                      dimension (LWORK)
123*          On exit, WORK(1) returns the minimal and optimal LWORK.
124*
125*  LWORK   (local or global input) INTEGER
126*          The dimension of the array WORK.
127*          LWORK is local input and must be at least
128*          LWORK >= MB_A * ( MpA0 + NqA0 + MB_A ), where
129*
130*          IROFFA = MOD( IA-1, MB_A ), ICOFFA = MOD( JA-1, NB_A ),
131*          IAROW = INDXG2P( IA, MB_A, MYROW, RSRC_A, NPROW ),
132*          IACOL = INDXG2P( JA, NB_A, MYCOL, CSRC_A, NPCOL ),
133*          MpA0 = NUMROC( M+IROFFA, MB_A, MYROW, IAROW, NPROW ),
134*          NqA0 = NUMROC( N+ICOFFA, NB_A, MYCOL, IACOL, NPCOL ),
135*
136*          INDXG2P and NUMROC are ScaLAPACK tool functions;
137*          MYROW, MYCOL, NPROW and NPCOL can be determined by calling
138*          the subroutine BLACS_GRIDINFO.
139*
140*          If LWORK = -1, then LWORK is global input and a workspace
141*          query is assumed; the routine only calculates the minimum
142*          and optimal size for all work arrays. Each of these
143*          values is returned in the first entry of the corresponding
144*          work array, and no error message is issued by PXERBLA.
145*
146*
147*  INFO    (global output) INTEGER
148*          = 0:  successful exit
149*          < 0:  If the i-th argument is an array and the j-entry had
150*                an illegal value, then INFO = -(i*100+j), if the i-th
151*                argument is a scalar and had an illegal value, then
152*                INFO = -i.
153*
154*  =====================================================================
155*
156*     .. Parameters ..
157      INTEGER            BLOCK_CYCLIC_2D, CSRC_, CTXT_, DLEN_, DTYPE_,
158     $                   LLD_, MB_, M_, NB_, N_, RSRC_
159      PARAMETER          ( BLOCK_CYCLIC_2D = 1, DLEN_ = 9, DTYPE_ = 1,
160     $                     CTXT_ = 2, M_ = 3, N_ = 4, MB_ = 5, NB_ = 6,
161     $                     RSRC_ = 7, CSRC_ = 8, LLD_ = 9 )
162      COMPLEX            ZERO
163      PARAMETER          ( ZERO = ( 0.0E+0, 0.0E+0 ) )
164*     ..
165*     .. Local Scalars ..
166      LOGICAL            LQUERY
167      CHARACTER          COLBTOP, ROWBTOP
168      INTEGER            I, IACOL, IAROW, IB, ICTXT, IINFO, IL, IN, IPW,
169     $                   J, LWMIN, MPA0, MYCOL, MYROW, NPCOL, NPROW,
170     $                   NQA0
171*     ..
172*     .. Local Arrays ..
173      INTEGER            IDUM1( 2 ), IDUM2( 2 )
174*     ..
175*     .. External Subroutines ..
176      EXTERNAL           BLACS_GRIDINFO, CHK1MAT, PCHK1MAT, PCLARFB,
177     $                   PCLARFT, PCLASET, PCUNGL2, PB_TOPGET,
178     $                   PB_TOPSET, PXERBLA
179*     ..
180*     .. External Functions ..
181      INTEGER            ICEIL, INDXG2P, NUMROC
182      EXTERNAL           ICEIL, INDXG2P, NUMROC
183*     ..
184*     .. Intrinsic Functions ..
185      INTRINSIC          CMPLX, MAX, MIN, MOD, REAL
186*     ..
187*     .. Executable Statements ..
188*
189*     Get grid parameters
190*
191      ICTXT = DESCA( CTXT_ )
192      CALL BLACS_GRIDINFO( ICTXT, NPROW, NPCOL, MYROW, MYCOL )
193*
194*     Test the input parameters
195*
196      INFO = 0
197      IF( NPROW.EQ.-1 ) THEN
198         INFO = -(700+CTXT_)
199      ELSE
200         CALL CHK1MAT( M, 1, N, 2, IA, JA, DESCA, 7, INFO )
201         IF( INFO.EQ.0 ) THEN
202            IAROW = INDXG2P( IA, DESCA( MB_ ), MYROW, DESCA( RSRC_ ),
203     $                       NPROW )
204            IACOL = INDXG2P( JA, DESCA( NB_ ), MYCOL, DESCA( CSRC_ ),
205     $                       NPCOL )
206            MPA0 = NUMROC( M+MOD( IA-1, DESCA( MB_ ) ), DESCA( MB_ ),
207     $                     MYROW, IAROW, NPROW )
208            NQA0 = NUMROC( N+MOD( JA-1, DESCA( NB_ ) ), DESCA( NB_ ),
209     $                     MYCOL, IACOL, NPCOL )
210            LWMIN = DESCA( MB_ ) * ( MPA0 + NQA0 + DESCA( MB_ ) )
211*
212            WORK( 1 ) = CMPLX( REAL( LWMIN ) )
213            LQUERY = ( LWORK.EQ.-1 )
214            IF( N.LT.M ) THEN
215               INFO = -2
216            ELSE IF( K.LT.0 .OR. K.GT.M ) THEN
217               INFO = -3
218            ELSE IF( LWORK.LT.LWMIN .AND. .NOT.LQUERY ) THEN
219               INFO = -10
220            END IF
221         END IF
222         IDUM1( 1 ) = K
223         IDUM2( 1 ) = 3
224         IF( LWORK.EQ.-1 ) THEN
225            IDUM1( 2 ) = -1
226         ELSE
227            IDUM1( 2 ) = 1
228         END IF
229         IDUM2( 2 ) = 10
230         CALL PCHK1MAT( M, 1, N, 2, IA, JA, DESCA, 7, 2, IDUM1, IDUM2,
231     $                  INFO )
232      END IF
233*
234      IF( INFO.NE.0 ) THEN
235         CALL PXERBLA( ICTXT, 'PCUNGLQ', -INFO )
236         RETURN
237      ELSE IF( LQUERY ) THEN
238         RETURN
239      END IF
240*
241*     Quick return if possible
242*
243      IF( M.LE.0 )
244     $   RETURN
245*
246      IPW = DESCA( MB_ ) * DESCA( MB_ ) + 1
247      IN = MIN( ICEIL( IA, DESCA( MB_ ) ) * DESCA( MB_ ), IA+K-1 )
248      IL = MAX( ( (IA+K-2) / DESCA( MB_ ) ) * DESCA( MB_ ) + 1, IA )
249      CALL PB_TOPGET( ICTXT, 'Broadcast', 'Rowwise', ROWBTOP )
250      CALL PB_TOPGET( ICTXT, 'Broadcast', 'Columnwise', COLBTOP )
251      CALL PB_TOPSET( ICTXT, 'Broadcast', 'Rowwise', ' ' )
252      CALL PB_TOPSET( ICTXT, 'Broadcast', 'Columnwise', 'D-ring' )
253*
254      CALL PCLASET( 'All', IA+M-IL, IL-IA, ZERO, ZERO, A, IL, JA,
255     $              DESCA )
256*
257*     Use unblocked code for the last or only block.
258*
259      CALL PCUNGL2( IA+M-IL, N-IL+IA, IA+K-IL, A, IL, JA+IL-IA, DESCA,
260     $              TAU, WORK, LWORK, IINFO )
261*
262*     Is there at least one block of rows to loop over ?
263*
264      IF( IL.GT.IN+1 ) THEN
265*
266*        Use blocked code
267*
268         DO 10 I = IL-DESCA( MB_ ), IN+1, -DESCA( MB_ )
269            IB = MIN( DESCA( MB_ ), IA+M-I )
270            J = JA + I - IA
271*
272            IF( I+IB.LE.IA+M-1 ) THEN
273*
274*              Form the triangular factor of the block reflector
275*              H = H(i) H(i+1) . . . H(i+ib-1)
276*
277               CALL PCLARFT( 'Forward', 'Rowwise', N-I+IA, IB, A, I, J,
278     $                       DESCA, TAU, WORK, WORK( IPW ) )
279*
280*              Apply H' to A(i+ib:ia+m-1,j:ja+n-1) from the right
281*
282               CALL PCLARFB( 'Right', 'Conjugate transpose', 'Forward',
283     $                       'Rowwise', M-I-IB+IA, N-I+IA, IB, A, I, J,
284     $                       DESCA, WORK, A, I+IB, J, DESCA,
285     $                       WORK( IPW ) )
286            END IF
287*
288*           Apply H' to columns j:ja+n-1 of current block
289*
290            CALL PCUNGL2( IB, N-I+IA, IB, A, I, J, DESCA, TAU, WORK,
291     $                    LWORK, IINFO )
292*
293*           Set columns ia:i-1 of current block to zero
294*
295            CALL PCLASET( 'All', IB, I-IA, ZERO, ZERO, A, I, JA, DESCA )
296   10    CONTINUE
297*
298      END IF
299*
300*     Handle first block separately
301*
302      IF( IL.GT.IA ) THEN
303*
304         IB = IN - IA + 1
305*
306*        Form the triangular factor of the block reflector
307*        H = H(i) H(i+1) . . . H(i+ib-1)
308*
309         CALL PCLARFT( 'Forward', 'Rowwise', N, IB, A, IA, JA, DESCA,
310     $                 TAU, WORK, WORK( IPW ) )
311*
312*        Apply H' to A(ia+ib:ia+m-1,ja:ja+n-1) from the right
313*
314         CALL PCLARFB( 'Right', 'Conjugate transpose', 'Forward',
315     $                 'Rowwise', M-IB, N, IB, A, IA, JA, DESCA, WORK,
316     $                 A, IA+IB, JA, DESCA, WORK( IPW ) )
317*
318*        Apply H' to columns ja:ja+n-1 of current block
319*
320         CALL PCUNGL2( IB, N, IB, A, IA, JA, DESCA, TAU, WORK, LWORK,
321     $                 IINFO )
322*
323      END IF
324*
325      CALL PB_TOPSET( ICTXT, 'Broadcast', 'Rowwise', ROWBTOP )
326      CALL PB_TOPSET( ICTXT, 'Broadcast', 'Columnwise', COLBTOP )
327*
328      WORK( 1 ) = CMPLX( REAL( LWMIN ) )
329*
330      RETURN
331*
332*     End of PCUNGLQ
333*
334      END
335