ScaLAPACK 2.1  2.1
ScaLAPACK: Scalable Linear Algebra PACKage
PB_CInOutV2.c
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------
2 *
3 * -- PBLAS auxiliary routine (version 2.0) --
4 * University of Tennessee, Knoxville, Oak Ridge National Laboratory,
5 * and University of California, Berkeley.
6 * April 1, 1998
7 *
8 * ---------------------------------------------------------------------
9 */
10 /*
11 * Include files
12 */
13 #include "../pblas.h"
14 #include "../PBpblas.h"
15 #include "../PBtools.h"
16 #include "../PBblacs.h"
17 #include "../PBblas.h"
18 
19 #ifdef __STDC__
20 void PB_CInOutV2( PBTYP_T * TYPE, char * CONJUG, char * ROWCOL, int M,
21  int N, int KA, int * DESCA, int K, char * Y, int IY,
22  int JY, int * DESCY, char * YROC, char * * YAPTR,
23  int * DYA, int * YAFREE, int * YASUM, int * YAPBY )
24 #else
25 void PB_CInOutV2( TYPE, CONJUG, ROWCOL, M, N, KA, DESCA, K, Y, IY, JY,
26  DESCY, YROC, YAPTR, DYA, YAFREE, YASUM, YAPBY )
27 /*
28 * .. Scalar Arguments ..
29 */
30  char * CONJUG, * ROWCOL, * YROC;
31  int * YAPBY, * YAFREE, IY, JY, K, KA, M, N, * YASUM;
32  PBTYP_T * TYPE;
33 /*
34 * .. Array Arguments ..
35 */
36  int * DESCA, * DESCY, * DYA;
37  char * Y, * * YAPTR;
38 #endif
39 {
40 /*
41 * Purpose
42 * =======
43 *
44 * PB_CInOutV2 returns a pointer to an array that contains a one-dimen-
45 * sional input/output subvector which is replicated over the rows or
46 * columns of a submatrix described by DESCA. A subvector is specified
47 * on input to this routine that is reused whenever possible. On return,
48 * the subvector is specified by a pointer to some data, a descriptor
49 * array describing its layout, a logical value indicating if this local
50 * piece of data has been dynamically allocated by this function, a lo-
51 * gical value specifying if sum reduction should occur, and finally a
52 * logical value specifying if it is necessary to copy back the alloca-
53 * ted data to the original data. This routine is specifically designed
54 * for traditional Level 2 like PBLAS operations using an input/output
55 * vector such as PxTRSV.
56 *
57 * Notes
58 * =====
59 *
60 * A description vector is associated with each 2D block-cyclicly dis-
61 * tributed matrix. This vector stores the information required to
62 * establish the mapping between a matrix entry and its corresponding
63 * process and memory location.
64 *
65 * In the following comments, the character _ should be read as
66 * "of the distributed matrix". Let A be a generic term for any 2D
67 * block cyclicly distributed matrix. Its description vector is DESC_A:
68 *
69 * NOTATION STORED IN EXPLANATION
70 * ---------------- --------------- ------------------------------------
71 * DTYPE_A (global) DESCA[ DTYPE_ ] The descriptor type.
72 * CTXT_A (global) DESCA[ CTXT_ ] The BLACS context handle, indicating
73 * the NPROW x NPCOL BLACS process grid
74 * A is distributed over. The context
75 * itself is global, but the handle
76 * (the integer value) may vary.
77 * M_A (global) DESCA[ M_ ] The number of rows in the distribu-
78 * ted matrix A, M_A >= 0.
79 * N_A (global) DESCA[ N_ ] The number of columns in the distri-
80 * buted matrix A, N_A >= 0.
81 * IMB_A (global) DESCA[ IMB_ ] The number of rows of the upper left
82 * block of the matrix A, IMB_A > 0.
83 * INB_A (global) DESCA[ INB_ ] The number of columns of the upper
84 * left block of the matrix A,
85 * INB_A > 0.
86 * MB_A (global) DESCA[ MB_ ] The blocking factor used to distri-
87 * bute the last M_A-IMB_A rows of A,
88 * MB_A > 0.
89 * NB_A (global) DESCA[ NB_ ] The blocking factor used to distri-
90 * bute the last N_A-INB_A columns of
91 * A, NB_A > 0.
92 * RSRC_A (global) DESCA[ RSRC_ ] The process row over which the first
93 * row of the matrix A is distributed,
94 * NPROW > RSRC_A >= 0.
95 * CSRC_A (global) DESCA[ CSRC_ ] The process column over which the
96 * first column of A is distributed.
97 * NPCOL > CSRC_A >= 0.
98 * LLD_A (local) DESCA[ LLD_ ] The leading dimension of the local
99 * array storing the local blocks of
100 * the distributed matrix A,
101 * IF( Lc( 1, N_A ) > 0 )
102 * LLD_A >= MAX( 1, Lr( 1, M_A ) )
103 * ELSE
104 * LLD_A >= 1.
105 *
106 * Let K be the number of rows of a matrix A starting at the global in-
107 * dex IA,i.e, A( IA:IA+K-1, : ). Lr( IA, K ) denotes the number of rows
108 * that the process of row coordinate MYROW ( 0 <= MYROW < NPROW ) would
109 * receive if these K rows were distributed over NPROW processes. If K
110 * is the number of columns of a matrix A starting at the global index
111 * JA, i.e, A( :, JA:JA+K-1, : ), Lc( JA, K ) denotes the number of co-
112 * lumns that the process MYCOL ( 0 <= MYCOL < NPCOL ) would receive if
113 * these K columns were distributed over NPCOL processes.
114 *
115 * The values of Lr() and Lc() may be determined via a call to the func-
116 * tion PB_Cnumroc:
117 * Lr( IA, K ) = PB_Cnumroc( K, IA, IMB_A, MB_A, MYROW, RSRC_A, NPROW )
118 * Lc( JA, K ) = PB_Cnumroc( K, JA, INB_A, NB_A, MYCOL, CSRC_A, NPCOL )
119 *
120 * Arguments
121 * =========
122 *
123 * TYPE (local input) pointer to a PBTYP_T structure
124 * On entry, TYPE is a pointer to a structure of type PBTYP_T,
125 * that contains type information (See pblas.h).
126 *
127 * CONJUG (global input) pointer to CHAR
128 * On entry, CONJUG specifies if this routine should return
129 * the conjugate subvector as follows:
130 * = 'N' or 'n': The initial subvector is returned,
131 * = 'Z' or 'z': The conjugate subvector is returned.
132 *
133 * ROWCOL (global input) pointer to CHAR
134 * On entry, ROWCOL specifies if this routine should return a
135 * row or column subvector replicated over the underlying subma-
136 * trix as follows:
137 * = 'R' or 'r': A row subvector is returned,
138 * = 'C' or 'c': A column subvector is returned.
139 *
140 * M (global input) INTEGER
141 * On entry, M specifies the number of rows of the underlying
142 * submatrix described by DESCA. M must be at least zero.
143 *
144 * N (global input) INTEGER
145 * On entry, N specifies the number of columns of the underlying
146 * submatrix described by DESCA. N must be at least zero.
147 *
148 * KA (global input) INTEGER
149 * On entry, KA specifies a global row index when ROWCOL is 'R'
150 * or 'r' and a global column index otherwise. This index deter-
151 * mines a process row or column in which the output subvector
152 * contains a copy of the input subvector.
153 *
154 * DESCA (global and local input/output) INTEGER array
155 * On entry, DESCA is an integer array of dimension DLEN_. This
156 * is the array descriptor for the matrix A. EXCEPTIONALLY, THIS
157 * INTERNAL ROUTINE MAY MODIFY DESCA IN ORDER TO MINIMIZE THE
158 * AMOUNT OF DATA TO BE MOVED FOR THE VECTOR Y. SEE PxGEMV FOR
159 * AN EXAMPLE.
160 *
161 * K (global input) INTEGER
162 * On entry, K specifies the length of the non-distributed di-
163 * mension of the subvector sub( Y ). K must be at least zero.
164 *
165 * Y (local input) pointer to CHAR
166 * On entry, Y is an array of dimension (LLD_Y, Ky), where LLD_Y
167 * is at least MAX( 1, Lr( K, IY ) ) when YROC is 'R' or 'r'
168 * and MAX( 1, Lr( 1, IY+Ly-1 ) ) otherwise, and, Ky is at
169 * least Lc( 1, JY+Ly-1 ) when YROC is 'R' or 'r' and
170 * Lc( K, JY ) otherwise. Ly is N when ROWCOL is 'R' or 'r' and
171 * M otherwise. Before entry, this array contains the local
172 * entries of the matrix Y.
173 *
174 * IY (global input) INTEGER
175 * On entry, IY specifies Y's global row index, which points to
176 * the beginning of the submatrix sub( Y ).
177 *
178 * JY (global input) INTEGER
179 * On entry, JY specifies Y's global column index, which points
180 * to the beginning of the submatrix sub( Y ).
181 *
182 * DESCY (global and local input) INTEGER array
183 * On entry, DESCY is an integer array of dimension DLEN_. This
184 * is the array descriptor for the matrix Y.
185 *
186 * YROC (global input) pointer to CHAR
187 * On entry, YROC specifies the orientation of the subvector
188 * sub( Y ). When YROC is 'R' or 'r', sub( Y ) is a row vector,
189 * and a column vector otherwise.
190 *
191 * YAPTR (local output) pointer to pointer to CHAR
192 * On exit, * YAPTR is an array containing the same data as the
193 * subvector sub( Y ) which is replicated over the rows or co-
194 * lumns of the underlying matrix as specified by ROWCOL and
195 * DESCA.
196 *
197 * DYA (global and local output) INTEGER array
198 * On exit, DYA is a descriptor array of dimension DLEN_ descri-
199 * bing the data layout of the data pointed to by * YAPTR.
200 *
201 * YAFREE (local output) INTEGER
202 * On exit, YAFREE specifies if it was possible to reuse the
203 * subvector sub( Y ), i.e., if some dynamic memory was alloca-
204 * ted for the data pointed to by * YAPTR or not. When YAFREE is
205 * zero, no dynamic memory was allocated. Otherwise, some dyna-
206 * mic memory was allocated by this function that one MUST re-
207 * lease as soon as possible.
208 *
209 * YASUM (global output) INTEGER
210 * On exit, YASUM specifies if a global sum reduction should be
211 * performed to obtain the correct sub( Y ). When YASUM is zero,
212 * no reduction is to be performed, otherwise reduction should
213 * occur.
214 *
215 * YAPBY (global output) INTEGER
216 * On exit, YAPBY specifies if the data pointed to by * YAPTR
217 * must be move back onto sub( Y ) to obtain the correct result.
218 * When YAPBY is zero, no supplementary data movement is neces-
219 * sary, otherwise a data redistribution should occur.
220 *
221 * -- Written on April 1, 1998 by
222 * Antoine Petitet, University of Tennessee, Knoxville 37996, USA.
223 *
224 * ---------------------------------------------------------------------
225 */
226 /*
227 * .. Local Scalars ..
228 */
229  int Acol, Acoldst, Aimb, Ainb, AisD, AisR, Amb, Amp, Anb, Anq,
230  Arow, Arowdst, Ycol, Yii, Yimb, Yimb1, Yinb, Yinb1, YisD,
231  YisR, YisRow, Yjj, Yld, Ymb, Ymp, Ynb, Ynq, Yrow, ctxt,
232  izero=0, nprow, myrow, npcol, mycol;
233 /* ..
234 * .. Executable Statements ..
235 *
236 */
237 /*
238 * Initialize the output parameters to a default value
239 */
240  *YAFREE = 0;
241  *YASUM = 0;
242  *YAPBY = 0;
243  *YAPTR = NULL;
244 /*
245 * Quick return if possible
246 */
247  if( ( M <= 0 ) || ( N <= 0 ) || ( K <= 0 ) )
248  {
249  if( Mupcase( ROWCOL[0] ) == CROW )
250  {
251  PB_Cdescset( DYA, K, N, 1, DESCA[INB_], 1, DESCA[NB_], DESCA[RSRC_],
252  DESCA[CSRC_], DESCA[CTXT_], 1 );
253  }
254  else
255  {
256  PB_Cdescset( DYA, M, K, DESCA[IMB_], 1, DESCA[MB_], 1, DESCA[RSRC_],
257  DESCA[CSRC_], DESCA[CTXT_], DESCA[LLD_] );
258  }
259  return;
260  }
261 /*
262 * Retrieve process grid information
263 */
264  Cblacs_gridinfo( ( ctxt = DESCY[CTXT_] ), &nprow, &npcol, &myrow, &mycol );
265 /*
266 * Retrieve sub( Y )'s local information: Yii, Yjj, Yrow, Ycol
267 */
268  Minfog2l( IY, JY, DESCY, nprow, npcol, myrow, mycol, Yii, Yjj, Yrow, Ycol );
269 /*
270 * Is sub( Y ) distributed or not, replicated or not ?
271 */
272  if( ( YisRow = ( Mupcase( YROC[0] ) == CROW ) ) != 0 )
273  {
274  YisD = ( ( Ycol >= 0 ) && ( npcol > 1 ) );
275  YisR = ( ( Yrow == -1 ) || ( nprow == 1 ) );
276  }
277  else
278  {
279  YisD = ( ( Yrow >= 0 ) && ( nprow > 1 ) );
280  YisR = ( ( Ycol == -1 ) || ( npcol == 1 ) );
281  }
282 
283  Aimb = DESCA[ IMB_ ]; Ainb = DESCA[ INB_ ];
284  Amb = DESCA[ MB_ ]; Anb = DESCA[ NB_ ];
285  Arow = DESCA[ RSRC_ ]; Acol = DESCA[ CSRC_ ];
286 
287  if( Mupcase( ROWCOL[0] ) == CROW )
288  {
289 /*
290 * Want a row vector
291 */
292  AisR = ( ( Arow < 0 ) || ( nprow == 1 ) );
293 /*
294 * Figure out in which process row sub( Y ) or a copy of it should be found
295 */
296  Arowdst = PB_Cindxg2p( KA, Aimb, Amb, Arow, Arow, nprow );
297 
298  if( YisRow && ( Mupcase( CONJUG[0] ) == CNOCONJG ) )
299  {
300 /*
301 * It is possible to reuse sub( Y ) iff sub( Y ) is already a row vector and
302 * the data does not need to be conjugated.
303 */
304  AisD = ( ( Acol >= 0 ) && ( npcol > 1 ) );
305 
306  Yinb = DESCY[INB_]; Ynb = DESCY[NB_];
307  Yinb1 = PB_Cfirstnb( N, JY, Yinb, Ynb );
308 /*
309 * sub( Y ) is aligned with A (reuse condition) iff both operands are not
310 * distributed, or both of them are distributed and start in the same process
311 * column and either N is smaller than the first blocksize of sub( Y ) and A,
312 * or their column blocking factors match.
313 */
314  if( ( !AisD && !YisD ) ||
315  ( ( AisD && YisD ) &&
316  ( ( Acol == Ycol ) &&
317  ( ( ( Ainb >= N ) && ( Yinb1 >= N ) ) ||
318  ( ( Ainb == Yinb1 ) && ( Anb == Ynb ) ) ) ) ) )
319  {
320  Ynq = PB_Cnumroc( N, 0, Yinb1, Ynb, mycol, Ycol, npcol );
321  Ymp = ( YisR ? K : ( ( myrow == Yrow ) ? K : 0 ) );
322  Yld = MAX( 1, K );
323 
324  if( YisR )
325  {
326 /*
327 * If sub( Y ) is replicated, there is no need to move sub( Y ) after the
328 * operation (*YAPBY = 0), and it can be reused where needed and zeroed out
329 * elsewhere.
330 */
331  *YASUM = ( AisR ? 0 : ( nprow > 1 ) );
332  *YAPBY = 0;
333  Yld = DESCY[ LLD_ ];
334  if( Ynq > 0 )
335  {
336  *YAPTR = Mptr( Y, Yii, Yjj, Yld, TYPE->size );
337  if( !AisR && ( myrow != Arowdst ) )
338  TYPE->Ftzpad( C2F_CHAR( ALL ), C2F_CHAR( NOCONJG ), &K,
339  &Ynq, &izero, TYPE->zero, TYPE->zero, *YAPTR,
340  &Yld );
341  }
342  }
343  else
344  {
345 /*
346 * sub( Y ) is not replicated, the descriptor of A may need to be modified ...
347 */
348  if( AisR )
349  {
350 /*
351 * If A is replicated, use only the copy in the process row where sub( Y )
352 * resides -> modify DESCA !!!
353 */
354  *YASUM = 0;
355  *YAPBY = 0;
356  Yld = DESCY[ LLD_ ];
357  DESCA[ IMB_ ] = M;
358  DESCA[ RSRC_ ] = Yrow;
359  if( ( Ynq > 0 ) && ( Ymp > 0 ) )
360  *YAPTR = Mptr( Y, Yii, Yjj, Yld, TYPE->size );
361  }
362  else
363  {
364  if( PB_Cspan( M, 0, Aimb, Amb, Arow, nprow ) )
365  {
366 /*
367 * Otherwise, A is not replicated, let assume in addition that it spans more
368 * than one process row.
369 */
370  *YASUM = ( nprow > 1 );
371  *YAPBY = 0;
372 
373  if( myrow == Yrow )
374  {
375 /*
376 * If sub( Y ) is not in the desired process row, send it there and zero it.
377 * Otherwise, reuse it.
378 */
379  Yld = DESCY[ LLD_ ];
380  if( Ynq > 0 )
381  {
382  *YAPTR = Mptr( Y, Yii, Yjj, Yld, TYPE->size );
383  if( Yrow != Arowdst )
384  {
385  TYPE->Cgesd2d( ctxt, K, Ynq, *YAPTR, Yld, Arowdst,
386  mycol );
387  TYPE->Ftzpad( C2F_CHAR( ALL ),
388  C2F_CHAR( NOCONJG ), &K, &Ynq,
389  &izero, TYPE->zero, TYPE->zero,
390  *YAPTR, &Yld );
391  }
392  }
393  }
394  else
395  {
396 /*
397 * Allocate space in the other process rows and initialize to zero. If sub( Y )
398 * was not in the desired process row, receive it.
399 */
400  Yld = MAX( 1, K );
401  if( Ynq > 0 )
402  {
403  *YAPTR = PB_Cmalloc( K * Ynq * TYPE->size );
404  *YAFREE = 1;
405  if( ( Yrow != Arowdst ) && ( myrow == Arowdst ) )
406  TYPE->Cgerv2d( ctxt, K, Ynq, *YAPTR, Yld, Yrow,
407  mycol );
408  else
409  TYPE->Ftzpad( C2F_CHAR( ALL ),
410  C2F_CHAR( NOCONJG ), &K, &Ynq,
411  &izero, TYPE->zero, TYPE->zero,
412  *YAPTR, &Yld );
413  }
414  }
415  }
416  else
417  {
418 /*
419 * A spans only one process row
420 */
421  if( Yrow == Arow )
422  {
423 /*
424 * If A and sub( Y ) resides in the same process row, things are easy.
425 */
426  *YASUM = 0;
427  *YAPBY = 0;
428  Yld = DESCY[ LLD_ ];
429  if( ( myrow == Yrow ) && ( Ynq > 0 ) )
430  *YAPTR = Mptr( Y, Yii, Yjj, Yld, TYPE->size );
431  }
432  else
433  {
434 /*
435 * Otherwise, sub( Y ) resides in another process row, thus allocate zero-data
436 * in process row where a copy of sub( Y ) is desired, and receive it. Set
437 * *YAPBY to 1, so that this data will be added (moved) after the local
438 * operation has been performed.
439 */
440  *YASUM = 0;
441  *YAPBY = 1;
442  if( Ynq > 0 )
443  {
444  if( myrow == Yrow )
445  {
446  Yld = DESCY[ LLD_ ];
447  TYPE->Cgesd2d( ctxt, K, Ynq, Mptr( Y, Yii, Yjj,
448  Yld, TYPE->size ), Yld, Arowdst,
449  mycol );
450  }
451  else if( myrow == Arowdst )
452  {
453  Yld = MAX( 1, K );
454  *YAPTR = PB_Cmalloc( K*Ynq*TYPE->size );
455  *YAFREE = 1;
456  TYPE->Cgerv2d( ctxt, K, Ynq, *YAPTR, Yld, Yrow,
457  mycol );
458  }
459  }
460  Yrow = Arowdst;
461  }
462  }
463  }
464  }
465 /*
466 * Describe the resulting operand. Note that when reduction should occur, Yrow
467 * contains the destination row. Assuming every process row needs the result,
468 * Yrow is then -1.
469 */
470  PB_Cdescset( DYA, K, N, K, Yinb1, 1, Ynb, Yrow, Ycol, ctxt, Yld );
471  return;
472  }
473  }
474 /*
475 * sub( Y ) cannot be reused, force YAPBY to 1 for the later update of sub( Y ).
476 */
477  *YAPBY = 1;
478  Anq = PB_Cnumroc( N, 0, Ainb, Anb, mycol, Acol, npcol );
479  Yld = MAX( 1, K );
480 
481  if( YisR )
482  {
483 /*
484 * If sub( Y ) is replicated, allocate space in every process row owning some
485 * columns of A and initialize it to zero only where needed. There may be some
486 * wasted space (suppose A was residing in just one row), however, it is hoped
487 * that moving back this data to sub( Y ) will then be cheaper ...
488 */
489  *YASUM = ( AisR ? 0 : ( nprow > 1 ) );
490  if( Anq > 0 )
491  {
492  *YAPTR = PB_Cmalloc( K * Anq * TYPE->size );
493  *YAFREE = 1;
494  if( ( Arowdst >= 0 ) && ( myrow != Arowdst ) )
495  TYPE->Ftzpad( C2F_CHAR( ALL ), C2F_CHAR( NOCONJG ), &K, &Anq,
496  &izero, TYPE->zero, TYPE->zero, *YAPTR, &Yld );
497  }
498  }
499  else
500  {
501 /*
502 * sub( Y ) resides in only one process row
503 */
504  if( AisR )
505  {
506 /*
507 * If A is replicated, then modify sub( A ) so that only one process row will
508 * compute the result before moving it back to sub( Y ).
509 */
510  *YASUM = 0;
511  DESCA[ IMB_ ] = M;
512  if( YisRow )
513  {
514 /*
515 * Choose a different process row than Yrow for better performance (more links)
516 * in the later move-back phase.
517 */
518  DESCA[RSRC_] = MModSub1( Yrow, nprow );
519  }
520  else
521  {
522  DESCA[RSRC_] = 0;
523  }
524  if( ( myrow == ( Arowdst = DESCA[RSRC_] ) ) && ( Anq > 0 ) )
525  {
526  *YAPTR = PB_Cmalloc( K * Anq * TYPE->size );
527  *YAFREE = 1;
528  }
529  }
530  else
531  {
532  if( PB_Cspan( M, 0, Aimb, Amb, Arow, nprow ) )
533  {
534 /*
535 * If A is not replicated, and spans more than just one process row, then
536 * allocate space in every process row and zero it where needed.
537 */
538  *YASUM = ( nprow > 1 );
539  if( Anq > 0 )
540  {
541  *YAPTR = PB_Cmalloc( K * Anq * TYPE->size );
542  *YAFREE = 1;
543  if( myrow != Arowdst )
544  TYPE->Ftzpad( C2F_CHAR( ALL ), C2F_CHAR( NOCONJG ), &K,
545  &Anq, &izero, TYPE->zero, TYPE->zero, *YAPTR,
546  &Yld );
547  }
548  }
549  else
550  {
551 /*
552 * If A is not replicated, and spans only one process row, then allocate space
553 * within that process row.
554 */
555  *YASUM = 0;
556  if( ( myrow == Arowdst ) && ( Anq > 0 ) )
557  {
558  *YAPTR = PB_Cmalloc( K * Anq * TYPE->size );
559  *YAFREE = 1;
560  }
561  }
562  }
563  }
564 /*
565 * Describe the resulting operand. Note that when reduction should occur,
566 * Arowdst contains the destination row. Assuming every process row needs the
567 * result, Arowdst is then -1.
568 */
569  PB_Cdescset( DYA, K, N, K, Ainb, 1, Anb, Arowdst, Acol, ctxt, Yld );
570 /*
571 * Move sub( Y ) in the desired processes and with the correct layout
572 */
573  if( YisRow )
574  {
575  PB_Cpaxpby( TYPE, CONJUG, K, N, TYPE->one, Y, IY, JY, DESCY, ROW,
576  TYPE->zero, *YAPTR, 0, 0, DYA, ROW );
577  }
578  else
579  {
580  PB_Cpaxpby( TYPE, CONJUG, N, K, TYPE->one, Y, IY, JY, DESCY, COLUMN,
581  TYPE->zero, *YAPTR, 0, 0, DYA, ROW );
582  }
583  }
584  else
585  {
586 /*
587 * Want a column vector with original data in col KA
588 */
589  AisR = ( ( Acol < 0 ) || ( npcol == 1 ) );
590 /*
591 * Figure out in which process column sub( Y ) or a copy of it should be found.
592 */
593  Acoldst = PB_Cindxg2p( KA, Ainb, Anb, Acol, Acol, npcol );
594 
595  if( !( YisRow ) && ( Mupcase( CONJUG[0] ) == CNOCONJG ) )
596  {
597 /*
598 * It is possible to reuse sub( Y ) iff sub( Y ) is already a column vector and
599 * the data does not need to be conjugated.
600 */
601  AisD = ( ( Arow >= 0 ) && ( nprow > 1 ) );
602 
603  Yimb = DESCY[IMB_]; Ymb = DESCY[MB_];
604  Yimb1 = PB_Cfirstnb( M, IY, Yimb, Ymb );
605 /*
606 * sub( Y ) is aligned with A (reuse condition) iff both operands are not
607 * distributed, or both of them are distributed and start in the same process
608 * row and either M is smaller than the first blocksize of sub( Y ) and A, or
609 * their row blocking factors match.
610 */
611  if( ( !AisD && !YisD ) ||
612  ( ( AisD && YisD ) &&
613  ( ( Arow == Yrow ) &&
614  ( ( ( Aimb >= M ) && ( Yimb1 >= M ) ) ||
615  ( ( Aimb == Yimb1 ) && ( Amb == Ymb ) ) ) ) ) )
616  {
617  Ymp = PB_Cnumroc( M, 0, Yimb1, Ymb, myrow, Yrow, nprow );
618  Ynq = ( YisR ? K : ( ( mycol == Ycol ) ? K : 0 ) );
619  Yld = MAX( 1, Ymp );
620 
621  if( YisR )
622  {
623 /*
624 * If sub( Y ) is replicated, there is no need to move sub( Y ) after the
625 * operation (*YAPBY = 0), and it can be reused where needed and zeroed out
626 * elsewhere.
627 */
628  *YASUM = ( AisR ? 0 : ( npcol > 1 ) );
629  *YAPBY = 0;
630  Yld = DESCY[ LLD_ ];
631  if( Ymp > 0 )
632  {
633  *YAPTR = Mptr( Y, Yii, Yjj, Yld, TYPE->size );
634  if( !AisR && ( mycol != Acoldst ) )
635  TYPE->Ftzpad( C2F_CHAR( ALL ), C2F_CHAR( NOCONJG ), &Ymp,
636  &K, &izero, TYPE->zero, TYPE->zero, *YAPTR,
637  &Yld );
638  }
639  }
640  else
641  {
642 /*
643 * sub( Y ) is not replicated, the descriptor of A may need to be modified ...
644 */
645  if( AisR )
646  {
647 /*
648 * If A is replicated, use only the copy in the process column where sub( Y )
649 * resides -> modify DESCA !!!
650 */
651  *YASUM = 0;
652  *YAPBY = 0;
653  Yld = DESCY[ LLD_ ];
654  DESCA[ INB_ ] = N;
655  DESCA[ CSRC_ ] = Ycol;
656  if( ( Ymp > 0 ) && ( Ynq > 0 ) )
657  *YAPTR = Mptr( Y, Yii, Yjj, Yld, TYPE->size );
658  }
659  else
660  {
661  if( PB_Cspan( N, 0, Ainb, Anb, Acol, npcol ) )
662  {
663 /*
664 * Otherwise, A is not replicated, let assume in addition that it spans more
665 * than one process column.
666 */
667  *YASUM = ( npcol > 1 );
668  *YAPBY = 0;
669 
670  if( mycol == Ycol )
671  {
672 /*
673 * If sub( Y ) is not in the desired process column, send it there and zero it.
674 * Otherwise, reuse it.
675 */
676  Yld = DESCY[ LLD_ ];
677  if( Ymp > 0 )
678  {
679  *YAPTR = Mptr( Y, Yii, Yjj, Yld, TYPE->size );
680  if( Ycol != Acoldst )
681  {
682  TYPE->Cgesd2d( ctxt, Ymp, K, *YAPTR, Yld, myrow,
683  Acoldst );
684  TYPE->Ftzpad( C2F_CHAR( ALL ),
685  C2F_CHAR( NOCONJG ), &Ymp, &K,
686  &izero, TYPE->zero, TYPE->zero,
687  *YAPTR, &Yld );
688  }
689  }
690  }
691  else
692  {
693 /*
694 * Allocate space in the other process columns and initialize to zero. If
695 * sub( Y ) was not in the desired process column, receive it.
696 */
697  Yld = MAX( 1, Ymp );
698  if( Ymp > 0 )
699  {
700  *YAPTR = PB_Cmalloc( Ymp * K * TYPE->size );
701  *YAFREE = 1;
702  if( ( Ycol != Acoldst ) && ( mycol == Acoldst ) )
703  TYPE->Cgerv2d( ctxt, Ymp, K, *YAPTR, Yld, myrow,
704  Ycol );
705  else
706  TYPE->Ftzpad( C2F_CHAR( ALL ),
707  C2F_CHAR( NOCONJG ), &Ymp, &K,
708  &izero, TYPE->zero, TYPE->zero,
709  *YAPTR, &Yld );
710  }
711  }
712  }
713  else
714  {
715 /*
716 * A spans only one process column
717 */
718  if( Ycol == Acol )
719  {
720 /*
721 * If A and sub( Y ) resides in the same process column, things are easy.
722 */
723  *YASUM = 0;
724  *YAPBY = 0;
725  Yld = DESCY[ LLD_ ];
726  if( ( mycol == Ycol ) && ( Ymp > 0 ) )
727  *YAPTR = Mptr( Y, Yii, Yjj, Yld, TYPE->size );
728  }
729  else
730  {
731 /*
732 * Otherwise, sub( Y ) resides in another process column, thus allocate
733 * zero-data in process column where a copy of sub( Y ) is desired, and receive
734 * it. Set *YAPBY to 1, so that this data will be added (moved) after the local
735 * operation has been performed.
736 */
737  *YASUM = 0;
738  *YAPBY = 1;
739  if( Ymp > 0 )
740  {
741  if( mycol == Ycol )
742  {
743  Yld = DESCY[ LLD_ ];
744  TYPE->Cgesd2d( ctxt, Ymp, K, Mptr( Y, Yii, Yjj,
745  Yld, TYPE->size ), Yld, myrow,
746  Acoldst );
747  }
748  else if( mycol == Acoldst )
749  {
750  Yld = MAX( 1, Ymp ) ;
751  *YAPTR = PB_Cmalloc( Ymp * K * TYPE->size );
752  *YAFREE = 1;
753  TYPE->Cgerv2d( ctxt, Ymp, K, *YAPTR, Yld, myrow,
754  Ycol );
755  }
756  }
757  Ycol = Acoldst;
758  }
759  }
760  }
761  }
762 /*
763 * Describe the resulting operand. Note that when reduction should occur, Ycol
764 * contains the destination column. Assuming every process column needs the
765 * result, Ycol is then -1.
766 */
767  PB_Cdescset( DYA, M, K, Yimb1, K, Ymb, 1, Yrow, Ycol, ctxt, Yld );
768  return;
769  }
770  }
771 /*
772 * sub( Y ) cannot be reused, force YAPBY to 1 for the later update of sub( Y ).
773 */
774  *YAPBY = 1;
775  Amp = PB_Cnumroc( M, 0, Aimb, Amb, myrow, Arow, nprow );
776  Yld = MAX( 1, Amp );
777 
778  if( YisR )
779  {
780 /*
781 * If sub( Y ) is replicated, allocate space in every process column owning some
782 * columns of A and initialize it to zero only where needed. There may be some
783 * wasted space (suppose A was residing in just one column), however, it is
784 * hoped that moving back this data to sub( Y ) will then be cheaper ...
785 */
786  *YASUM = ( AisR ? 0 : ( npcol > 1 ) );
787  if( Amp > 0 )
788  {
789  *YAPTR = PB_Cmalloc( Amp * K * TYPE->size );
790  *YAFREE = 1;
791  if( ( Acoldst >= 0 ) && ( mycol != Acoldst ) )
792  TYPE->Ftzpad( C2F_CHAR( ALL ), C2F_CHAR( NOCONJG ), &Amp, &K,
793  &izero, TYPE->zero, TYPE->zero, *YAPTR, &Yld );
794  }
795  }
796  else
797  {
798 /*
799 * sub( Y ) resides in only one process column
800 */
801  if( AisR )
802  {
803 /*
804 * If A is replicated, then modify sub( A ) so that only one process column will
805 * compute the result before moving it back to sub( Y ).
806 */
807  *YASUM = 0;
808  DESCA[ INB_ ] = N;
809  if( YisRow )
810  {
811  DESCA[ CSRC_ ] = 0;
812  }
813  else
814  {
815 /*
816 * Choose a different process column than Ycol for better performance (more
817 * links) in the later move-back phase.
818 */
819  DESCA[ CSRC_ ] = MModSub1( Ycol, npcol );
820  }
821  if( ( mycol == ( Acoldst = DESCA[CSRC_] ) ) && ( Amp > 0 ) )
822  {
823  *YAPTR = PB_Cmalloc( Amp * K * TYPE->size );
824  *YAFREE = 1;
825  }
826  }
827  else
828  {
829  if( PB_Cspan( N, 0, Ainb, Anb, Acol, npcol ) )
830  {
831 /*
832 * If A is not replicated, and spans more than just one process column, then
833 * allocate space in every process column and zero it where needed.
834 */
835  *YASUM = ( npcol > 1 );
836  if( Amp > 0 )
837  {
838  *YAPTR = PB_Cmalloc( Amp * K * TYPE->size );
839  *YAFREE = 1;
840  if( mycol != Acoldst )
841  TYPE->Ftzpad( C2F_CHAR( ALL ), C2F_CHAR( NOCONJG ), &Amp,
842  &K, &izero, TYPE->zero, TYPE->zero, *YAPTR,
843  &Yld );
844  }
845  }
846  else
847  {
848 /*
849 * If A is not replicated, and spans only one process column, then allocate
850 * space within that process column.
851 */
852  *YASUM = 0;
853  if( ( mycol == Acoldst ) && ( Amp > 0 ) )
854  {
855  *YAPTR = PB_Cmalloc( Amp * K * TYPE->size );
856  *YAFREE = 1;
857  }
858  }
859  }
860  }
861 /*
862 * Describe the resulting operand. Note that when reduction should occur,
863 * Acoldst contains the destination column. Assuming every process column needs
864 * the result, Acoldst is then -1.
865 */
866  PB_Cdescset( DYA, M, K, Aimb, K, Amb, 1, Arow, Acoldst, ctxt, Yld );
867 /*
868 * Move sub( Y ) in the desired processes and with the correct layout
869 */
870  if( YisRow )
871  {
872  PB_Cpaxpby( TYPE, CONJUG, K, M, TYPE->one, Y, IY, JY, DESCY, ROW,
873  TYPE->zero, *YAPTR, 0, 0, DYA, COLUMN );
874  }
875  else
876  {
877  PB_Cpaxpby( TYPE, CONJUG, M, K, TYPE->one, Y, IY, JY, DESCY, COLUMN,
878  TYPE->zero, *YAPTR, 0, 0, DYA, COLUMN );
879  }
880  }
881 /*
882 * End of PB_CInOutV2
883 */
884 }
TYPE
#define TYPE
Definition: clamov.c:7
ROW
#define ROW
Definition: PBblacs.h:46
MB_
#define MB_
Definition: PBtools.h:43
PB_Cpaxpby
void PB_Cpaxpby()
NB_
#define NB_
Definition: PBtools.h:44
COLUMN
#define COLUMN
Definition: PBblacs.h:45
CSRC_
#define CSRC_
Definition: PBtools.h:46
NOCONJG
#define NOCONJG
Definition: PBblas.h:45
PB_Cfirstnb
int PB_Cfirstnb()
LLD_
#define LLD_
Definition: PBtools.h:47
CNOCONJG
#define CNOCONJG
Definition: PBblas.h:19
CROW
#define CROW
Definition: PBblacs.h:21
IMB_
#define IMB_
Definition: PBtools.h:41
PB_CInOutV2
void PB_CInOutV2(PBTYP_T *TYPE, char *CONJUG, char *ROWCOL, int M, int N, int KA, int *DESCA, int K, char *Y, int IY, int JY, int *DESCY, char *YROC, char **YAPTR, int *DYA, int *YAFREE, int *YASUM, int *YAPBY)
Definition: PB_CInOutV2.c:25
PB_Cdescset
void PB_Cdescset()
Minfog2l
#define Minfog2l(i_, j_, desc_, nr_, nc_, r_, c_, ii_, jj_, pr_, pc_)
Definition: PBtools.h:428
PB_Cindxg2p
int PB_Cindxg2p()
RSRC_
#define RSRC_
Definition: PBtools.h:45
PB_Cnumroc
int PB_Cnumroc()
PB_Cmalloc
char * PB_Cmalloc()
ALL
#define ALL
Definition: PBblas.h:50
INB_
#define INB_
Definition: PBtools.h:42
C2F_CHAR
#define C2F_CHAR(a)
Definition: pblas.h:121
PB_Cspan
int PB_Cspan()
MModSub1
#define MModSub1(I, d)
Definition: PBtools.h:105
MAX
#define MAX(a_, b_)
Definition: PBtools.h:77
Cblacs_gridinfo
void Cblacs_gridinfo()
PBTYP_T
Definition: pblas.h:325
Mupcase
#define Mupcase(C)
Definition: PBtools.h:83
Mptr
#define Mptr(a_, i_, j_, lda_, siz_)
Definition: PBtools.h:132
CTXT_
#define CTXT_
Definition: PBtools.h:38