File: PB_CGatherV.c

package info (click to toggle)
scalapack 1.8.0-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 32,240 kB
  • ctags: 29,143
  • sloc: fortran: 288,069; ansic: 64,035; makefile: 1,911
file content (741 lines) | stat: -rw-r--r-- 27,493 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
/* ---------------------------------------------------------------------
*
*  -- PBLAS auxiliary routine (version 2.0) --
*     University of Tennessee, Knoxville, Oak Ridge National Laboratory,
*     and University of California, Berkeley.
*     April 1, 1998
*
*  ---------------------------------------------------------------------
*/
/*
*  Include files
*/
#include "../pblas.h"
#include "../PBpblas.h"
#include "../PBtools.h"
#include "../PBblacs.h"
#include "../PBblas.h"

#ifdef __STDC__
void PB_CGatherV( PBTYP_T * TYPE, char * ALLOC, char * DIRECA, int M, int N,
                  char * A, int IA, int JA, int * DESCA, char * AROC,
                  char * * B, int * DESCB, int * BFREE )
#else
void PB_CGatherV( TYPE, ALLOC, DIRECA, M, N, A, IA, JA, DESCA, AROC, B,
                  DESCB, BFREE )
/*
*  .. Scalar Arguments ..
*/
   char           * ALLOC, * AROC, * DIRECA;
   int            * BFREE, IA, JA, M, N;
   PBTYP_T        * TYPE;
/*
*  .. Array Arguments ..
*/
   int            * DESCA, * DESCB;
   char           * A, * * B;
#endif
{
/*
*  Purpose
*  =======
*
*  PB_CGatherV aggregates a submatrix sub( A ) = A(IA:IA+M-1, JA:JA+N-1)
*  into a one-dimensional multivector B. The submatrix sub( A ) is  spe-
*  cified on input to  this routine that is reused whenever possible. On
*  return, the one-dimensional multivector is specified by a pointer  to
*  some data, a descriptor array describing its layout and a logical va-
*  lue indicating if this local piece of data has been dynamically allo-
*  cated by this function.
*
*  Notes
*  =====
*
*  A description  vector  is associated with each 2D block-cyclicly dis-
*  tributed matrix.  This  vector  stores  the  information  required to
*  establish the  mapping  between a  matrix entry and its corresponding
*  process and memory location.
*
*  In  the  following  comments,   the character _  should  be  read  as
*  "of  the  distributed  matrix".  Let  A  be a generic term for any 2D
*  block cyclicly distributed matrix.  Its description vector is DESC_A:
*
*  NOTATION         STORED IN       EXPLANATION
*  ---------------- --------------- ------------------------------------
*  DTYPE_A (global) DESCA[ DTYPE_ ] The descriptor type.
*  CTXT_A  (global) DESCA[ CTXT_  ] The BLACS context handle, indicating
*                                   the NPROW x NPCOL BLACS process grid
*                                   A  is  distributed over. The context
*                                   itself  is  global,  but  the handle
*                                   (the integer value) may vary.
*  M_A     (global) DESCA[ M_     ] The  number of rows in the distribu-
*                                   ted matrix A, M_A >= 0.
*  N_A     (global) DESCA[ N_     ] The number of columns in the distri-
*                                   buted matrix A, N_A >= 0.
*  IMB_A   (global) DESCA[ IMB_   ] The number of rows of the upper left
*                                   block of the matrix A, IMB_A > 0.
*  INB_A   (global) DESCA[ INB_   ] The  number  of columns of the upper
*                                   left   block   of   the  matrix   A,
*                                   INB_A > 0.
*  MB_A    (global) DESCA[ MB_    ] The blocking factor used to  distri-
*                                   bute the last  M_A-IMB_A  rows of A,
*                                   MB_A > 0.
*  NB_A    (global) DESCA[ NB_    ] The blocking factor used to  distri-
*                                   bute the last  N_A-INB_A  columns of
*                                   A, NB_A > 0.
*  RSRC_A  (global) DESCA[ RSRC_  ] The process row over which the first
*                                   row of the matrix  A is distributed,
*                                   NPROW > RSRC_A >= 0.
*  CSRC_A  (global) DESCA[ CSRC_  ] The  process column  over  which the
*                                   first column of  A  is  distributed.
*                                   NPCOL > CSRC_A >= 0.
*  LLD_A   (local)  DESCA[ LLD_   ] The  leading dimension  of the local
*                                   array  storing  the  local blocks of
*                                   the distributed matrix A,
*                                   IF( Lc( 1, N_A ) > 0 )
*                                      LLD_A >= MAX( 1, Lr( 1, M_A ) )
*                                   ELSE
*                                      LLD_A >= 1.
*
*  Let K be the number of  rows of a matrix A starting at the global in-
*  dex IA,i.e, A( IA:IA+K-1, : ). Lr( IA, K ) denotes the number of rows
*  that the process of row coordinate MYROW ( 0 <= MYROW < NPROW ) would
*  receive if these K rows were distributed over NPROW processes.  If  K
*  is the number of columns of a matrix  A  starting at the global index
*  JA, i.e, A( :, JA:JA+K-1, : ), Lc( JA, K ) denotes the number  of co-
*  lumns that the process MYCOL ( 0 <= MYCOL < NPCOL ) would  receive if
*  these K columns were distributed over NPCOL processes.
*
*  The values of Lr() and Lc() may be determined via a call to the func-
*  tion PB_Cnumroc:
*  Lr( IA, K ) = PB_Cnumroc( K, IA, IMB_A, MB_A, MYROW, RSRC_A, NPROW )
*  Lc( JA, K ) = PB_Cnumroc( K, JA, INB_A, NB_A, MYCOL, CSRC_A, NPCOL )
*
*  Arguments
*  =========
*
*  TYPE    (local input) pointer to a PBTYP_T structure
*          On entry,  TYPE  is a pointer to a structure of type PBTYP_T,
*          that contains type information (See pblas.h).
*
*  ALLOC   (global input) pointer to CHAR
*          On entry, ALLOC specifies if data  should  be  allocated even
*          when unnecessary as follows:
*             ALLOC = 'A' or 'a'    data allocation is enforced,
*             ALLOC = 'R' or 'r'    data is reused when possible.
*
*  DIRECA  (global input) pointer to CHAR
*          On entry,  DIRECA  specifies  the direction in which the rows
*          or columns of sub( A ) should be aggregated as follows:
*             DIRECA = 'F' or 'f'   forward  or increasing,
*             DIRECA = 'B' or 'b'   backward or decreasing.
*
*  M       (global input) INTEGER
*          On entry,  M  specifies the number of rows of  the  submatrix
*          sub( A ). M must be at least zero.
*
*  N       (global input) INTEGER
*          On entry, N specifies the number of columns of the  submatrix
*          sub( A ). N must be at least zero.
*
*  A       (local input) pointer to CHAR
*          On entry, A is an array of dimension (LLD_A, Ka), where LLD_A
*          is   DESCA[LLD_], i.e. at least  MAX( 1, Lr( M, IA ) ),  and,
*          Ka is at least Lc( N, JA ). Before entry, this array contains
*          the local entries of the matrix A.
*
*  IA      (global input) INTEGER
*          On entry, IA  specifies A's global row index, which points to
*          the beginning of the submatrix sub( A ).
*
*  JA      (global input) INTEGER
*          On entry, JA  specifies A's global column index, which points
*          to the beginning of the submatrix sub( A ).
*
*  DESCA   (global and local input) INTEGER array
*          On entry, DESCA  is an integer array of dimension DLEN_. This
*          is the array descriptor for the matrix A.
*
*  AROC    (global input) pointer to CHAR
*          On entry,  AROC  specifies  the  orientation of the submatrix
*          sub( A ). When AROC is 'R' or 'r',  sub( A ) is a row matrix,
*          and a column matrix otherwise.
*
*  B       (local output) pointer to pointer to CHAR
*          On exit, * B is an array containing the aggregated  submatrix
*          sub( A ).
*
*  DESCB   (global and local output) INTEGER array
*          On exit, DESCB is a descriptor array of dimension DLEN_  des-
*          cribing the data layout of the data pointed to by * B.
*
*  BFREE   (local output) INTEGER
*          On exit,  BFREE  specifies  if  it has been possible to reuse
*          the submatrix sub( A ), i.e., if some dynamic  memory was al-
*          located for the data pointed to by *B or not. When  BFREE  is
*          zero, no dynamic memory was allocated.  Otherwise, some dyna-
*          mic  memory  was allocated by this function that one MUST re-
*          lease as soon as possible.
*
*  -- Written on April 1, 1998 by
*     Antoine Petitet, University of Tennessee, Knoxville 37996, USA.
*
*  ---------------------------------------------------------------------
*/
/*
*  .. Local Scalars ..
*/
   char           * one, * zero;
   int            Afwd, AggRow, AiiD, AiiR, Ainb1D, Ainb1R, Ald, AmyprocD,
                  AmyprocR, AnR, AnbD, AnbR, AnnxtL, AnnxtR, AnpD, AnpR, AnpreR,
                  AnprocsR, ArocR, AsrcD, AsrcR, Bld, Bsrc_, ctxt, k, kb, kblks,
                  kn, ktmp, mycol, mydist, mydistnb, myrow, nlen, npcol, nprow,
                  offset, size, srcdist;
   MMADD_T        add;
   MMSHFT_T       shft;
/*
*  .. Local Arrays ..
*/
   char           * Aptr = NULL, * Bptr = NULL;
/* ..
*  .. Executable Statements ..
*
*/
/*
*  Initialize the output parameters to a default value
*/
   *BFREE = 0;
   *B     = NULL;
/*
*  Quick return if possible
*/
   if( ( M <= 0 ) || ( N <= 0 ) )
   {
      PB_Cdescset( DESCB, M, N, DESCA[IMB_], DESCA[INB_], DESCA[MB_],
                   DESCA[NB_], DESCA[RSRC_], DESCA[CSRC_], DESCA[CTXT_], 1 );
      return;
   }
/*
*  Retrieve process grid information
*/
   Cblacs_gridinfo( ( ctxt = DESCA[CTXT_] ), &nprow, &npcol, &myrow, &mycol );

   if( ( AggRow = ( Mupcase( AROC[0] ) == CROW ) ) != 0 )
   {
/*
*  Accumulate rows of sub( A )
*/
      AnbR = DESCA[MB_]; AnbD = DESCA[NB_]; Ald = DESCA[LLD_];
      PB_Cinfog2l( IA, JA, DESCA, nprow, npcol, myrow, mycol, &AiiR, &AiiD,
                   &AsrcR, &AsrcD );
      Ainb1D = PB_Cfirstnb( N, JA, DESCA[INB_], AnbD );
      AnpD   = PB_Cnumroc( N, 0, Ainb1D, AnbD, mycol, AsrcD, npcol );
/*
*  If sub( A ) is either replicated or spans only one process row, no data needs
*  to be exchanged by the processes, the operation is purely local.
*/
      if( !( PB_Cspan( M, IA, DESCA[IMB_], AnbR, AsrcR, nprow ) ) )
      {
         if( Mupcase( ALLOC[0] ) == CREUSE )
         {
/*
*  sub( A ) can be reused
*/
            if( ( ( myrow == AsrcR ) || ( AsrcR < 0 ) ) && ( AnpD > 0 ) )
            {
/*
*  If I own some entries of sub( A ), set *B
*/
               Bld = Ald;
               *B  = Mptr( A, AiiR, AiiD, Ald, TYPE->size );
            }
            else { Bld = 1; }
         }
         else
         {
/*
*  sub( A ) cannot be reused, make a copy of it.
*/
            if( ( ( myrow == AsrcR ) || ( AsrcR < 0 ) ) && ( AnpD > 0 ) )
            {
/*
*  If I own some entries of sub( A ), allocate space for the copy, and copy the
*  data.
*/
               Bld = M;
               if( AnpD > 0 )
               {
                  size   = TYPE->size;
                  *B     = PB_Cmalloc( AnpD * M * size );
                  *BFREE = 1;
                  TYPE->Fmmadd( &M, &AnpD, TYPE->one, Mptr( A, AiiR, AiiD, Ald,
                                size ), &Ald, TYPE->zero, *B, &Bld );
               }
            }
            else { Bld = 1; }
         }
/*
*  Describe the resulting operand
*/
         PB_Cdescset( DESCB, M, N, M, Ainb1D, AnbR, AnbD, AsrcR, AsrcD, ctxt,
                      Bld );
         return;
      }

      AnR      = M;     Bsrc_ = RSRC_;
      AmyprocR = myrow; AmyprocD = mycol; AnprocsR = nprow;
      Ainb1R   = PB_Cfirstnb( M, IA, DESCA[IMB_], AnbR );
      AnpR     = PB_Cnumroc( M, 0, Ainb1R, AnbR, myrow, AsrcR, nprow );
   }
   else
   {
/*
*  Accumulate columns of sub( A )
*/
      AnbD = DESCA[MB_ ]; AnbR = DESCA[NB_ ]; Ald = DESCA[LLD_];
      PB_Cinfog2l( IA, JA, DESCA, nprow, npcol, myrow, mycol, &AiiD, &AiiR,
                   &AsrcD, &AsrcR );
      Ainb1D = PB_Cfirstnb( M, IA, DESCA[IMB_], AnbD );
      AnpD   = PB_Cnumroc( M, 0, Ainb1D, AnbD, myrow, AsrcD, nprow );
/*
*  If sub( A ) is either replicated or spans only one process column, no data
*  needs to be exchanged by the processes, the operation is purely local.
*/
      if( !( PB_Cspan( N, JA, DESCA[INB_], AnbR, AsrcR, npcol ) ) )
      {
         if( Mupcase( ALLOC[0] ) == CREUSE )
         {
/*
*  sub( A ) can be reused
*/
            Bld = Ald;
            if( ( ( mycol == AsrcR ) || ( AsrcR < 0 ) ) && ( AnpD > 0 ) )
/*
*  If I own some entries of sub( A ), set *B
*/
               *B = Mptr( A, AiiD, AiiR, Ald, TYPE->size );
         }
         else
         {
/*
*  sub( A ) cannot be reused, make a copy of it.
*/
            Bld = MAX( 1, AnpD );
            if( ( ( mycol == AsrcR ) || ( AsrcR < 0 ) ) && ( AnpD > 0 ) )
            {
/*
*  If I own some entries of sub( A ), allocate space for the copy, and copy the
*  data.
*/
               if( AnpD > 0 )
               {
                  size   = TYPE->size;
                  *B     = PB_Cmalloc( AnpD * N * size );
                  *BFREE = 1;
                  TYPE->Fmmadd( &AnpD, &N, TYPE->one, Mptr( A, AiiD, AiiR, Ald,
                                size ), &Ald, TYPE->zero, *B, &Bld );
               }
            }
         }
/*
*  Describe the resulting operand
*/
         PB_Cdescset( DESCB, M, N, Ainb1D, N, AnbD, AnbR, AsrcD, AsrcR, ctxt,
                      Bld );
         return;
      }

      AnR      = N;     Bsrc_    = CSRC_;
      AmyprocR = mycol; AmyprocD = myrow; AnprocsR = npcol;
      Ainb1R   = PB_Cfirstnb( N, JA, DESCA[INB_], AnbR );
      AnpR     = PB_Cnumroc( N, 0, Ainb1R, AnbR, mycol, AsrcR, npcol );
   }
/*
*  sub( A ) is not replicated and spans more than one process row or column.
*  Forward row (resp. column) accumulation will leave the resulting operand in
*  the process(es) where the global row IA+M-1 (resp. global column JA+N-1)
*  resides.
*/
   if( ( Afwd = ( Mupcase( DIRECA[0] ) == CFORWARD ) ) != 0 )
   {
      if( ( AnpD > 0 ) && ( AnpR > 0 ) )
      {
/*
*  Compute how may rows or columns are before me -> AnpreR
*/
         AnpreR = PB_Cnpreroc( AnR, 0, Ainb1R, AnbR, AmyprocR, AsrcR,
                               AnprocsR );

         if( AnpreR == 0 )
         {
/*
*  If zero rows or columns are before me, I must be the source, so send my piece
*  to the process after me in the grid.
*/
            if( AggRow )
            {
               TYPE->Cgesd2d( ctxt, AnpR, AnpD, Mptr( A, AiiR, AiiD, Ald,
                              TYPE->size ), Ald, MModAdd1( AmyprocR, AnprocsR ),
                              AmyprocD );
            }
            else
            {
               TYPE->Cgesd2d( ctxt, AnpD, AnpR, Mptr( A, AiiD, AiiR,
                              Ald, TYPE->size ), Ald, AmyprocD,
                              MModAdd1( AmyprocR, AnprocsR ) );
            }
         }
         else if( AnpreR > 0 )
         {
/*
*  Otherwise, allocate some space for the rows or columns I have and the ones
*  globally preceeding the ones I have, that I am about to receive.
*/
            size     = TYPE->size; one = TYPE->one; zero = TYPE->zero;
            add      = TYPE->Fmmadd;
            *B       = Bptr = PB_Cmalloc( ( AnpreR + AnpR ) * AnpD * size );
            nlen     = AnpreR;
            mydistnb = MModSub( AmyprocR, AsrcR, AnprocsR ) * AnbR;
            kblks    = ( ( ( ktmp = AnR - Ainb1R - 1 ) >= 0 ) ?
                          ( ( ktmp / AnbR ) + 1 ) / AnprocsR : 0 );
            offset   = kblks * AnbR;
            kn       = Ainb1R + mydistnb - AnbR;
            kn       = MIN( kn, AnpreR ) +
                       ( MAX( 1, kblks ) - 1 ) * mydistnb;
            if( AggRow )
            {
               shft = TYPE->Frshft;
               Aptr = Mptr( A, AiiR, AiiD, Ald, size );
               Bld  = AnpreR + AnpR;
/*
*  Receive the rows globally preceeding the ones I have
*/
               TYPE->Cgerv2d( ctxt, AnpreR, AnpD, *B, Bld, MModSub1( AmyprocR,
                              AnprocsR ), AmyprocD );
/*
*  Sort the received buffer and insert at the correct place the rows of sub( A )
*  I own (from bottom to top).
*/
               if( ( ( AnpR - 1 ) / AnbR ) == kblks )
               {
                  kb      = AnpR - offset;
                  add( &kb, &AnpD, one, Mptr( Aptr, offset, 0, Ald, size ),
                       &Ald, zero, Mptr( Bptr, nlen+offset, 0, Bld, size ),
                       &Bld );
               }

               for( k = kblks; k >= 1; k-- )
               {
                  kb      = nlen - kn;
                  shft( &kb, &AnpD, &offset, Mptr( Bptr, kn, 0, Bld, size ),
                        &Bld );
                  offset -= AnbR;
                  add( &AnbR, &AnpD, one, Mptr( Aptr, offset, 0, Ald, size ),
                       &Ald, zero, Mptr( Bptr, kn+offset, 0, Bld, size ),
                       &Bld );
                  kn     -= mydistnb;
                  nlen   -= kb;
               }

               if( AnpreR + AnpR != AnR )
               {
/*
*  If I am not the last process, i.e I am not supposed to own all of the AnR
*  rows by the end of the operation, then send the sorted buffer to the next
*  process and release the dynamically allocated buffer.
*/
                  TYPE->Cgesd2d( ctxt, AnpreR+AnpR, AnpD, *B, Bld,
                                 MModAdd1( AmyprocR, AnprocsR ), AmyprocD );
                  if( *B ) free( *B );
               }
            }
            else
            {
               shft = TYPE->Fcshft;
               Aptr = Mptr( A, AiiD, AiiR, Ald, size );
               Bld  = MAX( 1, AnpD );
/*
*  Receive the columns globally preceeding the ones I have
*/
               TYPE->Cgerv2d( ctxt, AnpD, AnpreR, *B, Bld, AmyprocD,
                              MModSub1( AmyprocR, AnprocsR ) );
/*
*  Sort the received buffer and insert at the correct place the columns of
*  sub( A ) I own (from right to left).
*/
               if( ( ( AnpR - 1 ) / AnbR ) == kblks )
               {
                  kb      = AnpR - offset;
                  add( &AnpD, &kb, one, Mptr( Aptr, 0, offset, Ald, size ),
                       &Ald, zero, Mptr( Bptr, 0, nlen+offset, Bld, size ),
                       &Bld );
               }

               for( k = kblks; k >= 1; k-- )
               {
                  kb      = nlen - kn;
                  shft( &AnpD, &kb, &offset, Mptr( Bptr, 0, kn, Bld, size ),
                        &Bld );
                  offset -= AnbR;
                  add( &AnpD, &AnbR, one, Mptr( Aptr, 0, offset, Ald, size ),
                       &Ald, zero, Mptr( Bptr, 0, kn + offset, Bld, size ),
                       &Bld );
                  kn     -= mydistnb;
                  nlen   -= kb;
               }

               if( AnpreR + AnpR != AnR )
               {
/*
*  If I am not the last process, i.e I am not supposed to own all of the AnR
*  columns by the end of the operation, then send the sorted buffer to the next
*  process and release the dynamically allocated buffer.
*/
                  TYPE->Cgesd2d( ctxt, AnpD, AnpreR+AnpR, *B, Bld, AmyprocD,
                                 MModAdd1( AmyprocR, AnprocsR ) );
                  if( *B ) free( *B );
               }
            }
         }
      }
   }
   else
   {
/*
*  Backward accumulation, compute the process row or column coordinate ArocR,
*  that is going to have the resulting operand.
*/
      ArocR = PB_Cindxg2p( AnR-1, Ainb1R, AnbR, AsrcR, AsrcR, AnprocsR );

      if( ( AnpD > 0 ) && ( AnpR > 0 ) )
      {
/*
*  Compute how may rows or columns are after me -> AnnxtR
*/
         AnnxtR = PB_Cnnxtroc( AnR, 0, Ainb1R, AnbR, AmyprocR, AsrcR,
                               AnprocsR );
         AnnxtL = PB_Cnnxtroc( AnR, 0, Ainb1R, AnbR, ArocR,    AsrcR,
                               AnprocsR );

         if( ( AnnxtR = MModSub( AnnxtR, AnnxtL, AnR ) ) == 0 )
         {
/*
*  If zero rows or columns are after me, I must be the source, so send my piece
*  to the process before me in the grid.
*/
            if( AggRow )
            {
               TYPE->Cgesd2d( ctxt, AnpR, AnpD, Mptr( A, AiiR, AiiD, Ald,
                              TYPE->size ), Ald, MModSub1( AmyprocR, AnprocsR ),
                              AmyprocD );
            }
            else
            {
               TYPE->Cgesd2d( ctxt, AnpD, AnpR, Mptr( A, AiiD, AiiR, Ald,
                              TYPE->size ), Ald, AmyprocD, MModSub1( AmyprocR,
                              AnprocsR ) );
            }
         }
         else if( AnnxtR > 0 )
         {
/*
*  Otherwise, allocate some space for the rows or columns I have and the ones
*  globally following the ones I have, that I am about to receive.
*/
            size     = TYPE->size; one = TYPE->one; zero = TYPE->zero;
            add      = TYPE->Fmmadd;
            *B       = Bptr = PB_Cmalloc( ( AnnxtR + AnpR ) * AnpD * size );
            kblks    = ( ( ( ktmp = AnR - Ainb1R - 1 ) >= 0 ) ?
                         ( ( ktmp / AnbR ) + 1 ) / AnprocsR : 0 );
            mydist   = MModSub( ArocR, AmyprocR, AnprocsR );
            mydistnb = mydist * AnbR;
            srcdist  = MModSub( ArocR, AsrcR,    AnprocsR );

            if( AggRow )
            {
               shft = TYPE->Frshft;
               Aptr = Mptr( A, AiiR, AiiD, Ald, size );
               Bld  = AnnxtR + AnpR;
/*
*  Receive the rows globally following the ones I have
*/
               TYPE->Cgerv2d( ctxt, AnnxtR, AnpD, Mptr( *B, AnpR, 0, Bld,
                              size ), Bld, MModAdd1( AmyprocR, AnprocsR ),
                              AmyprocD );
/*
*  Sort the received buffer and insert at the correct place the rows of sub( A )
*  I own (from top to bottom).
*/
               if( mydist > srcdist )
               {
                  offset  = -AnpR;
                  kb      = Ainb1R + srcdist*AnbR;
               }
               else if( mydist == srcdist )
               {
                  add( &Ainb1R, &AnpD, one, Aptr, &Ald, zero, Bptr, &Bld );
                  Aptr    = Mptr( Aptr, Ainb1R, 0, Ald, size );
                  Bptr    = Mptr( Bptr, Ainb1R, 0, Ald, size );
                  offset  = Ainb1R - AnpR;
                  kb      = mydistnb;
               }
               else
               {
                  add( &AnbR, &AnpD, one, Aptr, &Ald, zero, Bptr, &Bld );
                  Aptr    = Mptr( Aptr, AnbR, 0, Ald, size );
                  Bptr    = Mptr( Bptr, AnbR, 0, Ald, size );
                  offset  = AnbR - AnpR;
                  kb      = mydistnb;
               }

               for( k = kblks; k >= 1; k-- )
               {
                  shft( &kb, &AnpD, &offset, Bptr, &Bld );
                  Bptr    = Mptr( Bptr, kb, 0, Bld, size );
                  add( &AnbR, &AnpD, one, Aptr, &Ald, zero, Bptr, &Bld );
                  Aptr    = Mptr( Aptr, AnbR, 0, Ald, size );
                  Bptr    = Mptr( Bptr, AnbR, 0, Ald, size );
                  offset += AnbR;
                  kb      = mydistnb;
               }

               if( AnnxtR + AnpR != AnR )
               {
/*
*  If I am not the last process, i.e I am not supposed to own all of the AnR
*  rows by the end of the operation, then send the sorted buffer to the previous
*  process and release the dynamically allocated buffer.
*/
                  TYPE->Cgesd2d( ctxt, AnnxtR+AnpR, AnpD, *B, Bld,
                                 MModSub1( AmyprocR, AnprocsR ), AmyprocD );
                  if( *B ) free( *B );
               }
            }
            else
            {
               shft = TYPE->Fcshft;
               Aptr = Mptr( A, AiiD, AiiR, Ald, size );
               Bld  = MAX( 1, AnpD );
/*
*  Receive the columns globally following the ones I have
*/
               TYPE->Cgerv2d( ctxt, AnpD, AnnxtR, Mptr( *B, 0, AnpR, Bld,
                              size ), Bld, AmyprocD, MModAdd1( AmyprocR,
                              AnprocsR ) );
/*
*  Sort the received buffer and insert at the correct place the columns of
*  sub( A ) I own (from left to right).
*/
               if( mydist > srcdist )
               {
                  offset  = -AnpR;
                  kb      = Ainb1R + srcdist*AnbR;
               }
               else if( mydist == srcdist )
               {
                  add( &AnpD, &Ainb1R, one, Aptr, &Ald, zero, Bptr, &Bld );
                  Aptr    = Mptr( Aptr, 0, Ainb1R, Ald, size );
                  Bptr    = Mptr( Bptr, 0, Ainb1R, Bld, size );
                  offset  = Ainb1R - AnpR;
                  kb      = mydistnb;
               }
               else
               {
                  add( &AnpD, &AnbR, one, Aptr, &Ald, zero, Bptr, &Bld );
                  Aptr    = Mptr( Aptr, 0, AnbR, Ald, size );
                  Bptr    = Mptr( Bptr, 0, AnbR, Bld, size );
                  offset  = AnbR - AnpR;
                  kb      = mydistnb;
               }

               for( k = kblks; k >= 1; k-- )
               {
                  shft( &AnpD, &kb, &offset, Bptr, &Bld );
                  Bptr    = Mptr( Bptr, 0, kb, Bld, size );
                  add( &AnpD, &AnbR, one, Aptr, &Ald, zero, Bptr, &Bld );
                  Aptr    = Mptr( Aptr, 0, AnbR, Ald, size );
                  Bptr    = Mptr( Bptr, 0, AnbR, Bld, size );
                  offset += AnbR;
                  kb      = mydistnb;
               }

               if( AnnxtR + AnpR != AnR )
               {
/*
*  If I am not the last process, i.e I am not supposed to own all of the AnR
*  columns by the end of the operation, then send the sorted buffer to the
*  previous process and release the dynamically allocated buffer.
*/
                  TYPE->Cgesd2d( ctxt, AnpD, AnnxtR+AnpR, *B, Bld, AmyprocD,
                                 MModSub1( AmyprocR, AnprocsR ) );
                  if( *B ) free( *B );
               }
            }
         }
      }
   }
/*
*  Describe the resulting operand
*/
   if( AggRow )
   {
      PB_Cdescset( DESCB, M, N, M, Ainb1D, AnbR, AnbD, AsrcR, AsrcD, ctxt, M );
   }
   else
   {
      PB_Cdescset( DESCB, M, N, Ainb1D, N, AnbD, AnbR, AsrcD, AsrcR, ctxt,
                   MAX( 1, AnpD ) );
   }
/*
*  Compute globally in which process row or column the resulting operand is
*  residing and set *BFREE accordingly.
*/
   if( Afwd )
   {
      if( AnR + AnbR > Ainb1R + ( AnprocsR - 1 ) * AnbR )
      {
/*
*  If sub( A ) is spanning all process rows or columns of the grid, the result
*  must be in the process row or column preceeding the one owning IA or JA,
*  don't you think ?
*/
         DESCB[Bsrc_] = MModSub1( AsrcR, AnprocsR );
      }
      else
      {
/*
*  Otherwise, the result is in the process row or column where the row IA+M-1
*  or column JA+N-1 of sub( A ) resides.
*/
         DESCB[Bsrc_] = PB_Cindxg2p( AnR-1, Ainb1R, AnbR, AsrcR, AsrcR,
                                     AnprocsR );
      }
      if( ( AnpD > 0 ) && ( AnpR > 0 ) && ( AmyprocR == DESCB[Bsrc_] ) )
         *BFREE = 1;
   }
   else
   {
      if( AnR + AnbR > Ainb1R + ( AnprocsR - 1 ) * AnbR )
      {
/*
*  If sub( A ) is spanning all process rows or columns of the grid, the result
*  must be in the process row or column following the one owning IA+M-1 or
*  JA+N-1, don't you think ?
*/
         DESCB[Bsrc_] = MModAdd1( ArocR, AnprocsR );
      }
      else
      {
/*
*  Otherwise, the result is in the process row or column where the row IA or
*  column JA of sub( A ) resides.
*/
         DESCB[Bsrc_] = AsrcR;
      }
      if( ( AnpD > 0 ) && ( AnpR > 0 ) && ( AmyprocR == DESCB[Bsrc_] ) )
         *BFREE = 1;
   }
/*
*  End of PB_CGatherV
*/
}