| 12
 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
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 
 | /* ---------------------------------------------------------------------
*
*  -- PBLAS 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 pdswap_( Int * N,
              double * X, Int * IX, Int * JX, Int * DESCX, Int * INCX,
              double * Y, Int * IY, Int * JY, Int * DESCY, Int * INCY )
#else
void pdswap_( N, X, IX, JX, DESCX, INCX, Y, IY, JY, DESCY, INCY )
/*
*  .. Scalar Arguments ..
*/
   Int            * INCX, * INCY, * IX, * IY, * JX, * JY, * N;
/*
*  .. Array Arguments ..
*/
   Int            * DESCX, * DESCY;
   double         * X, * Y;
#endif
{
/*
*  Purpose
*  =======
*
*  PDSWAP  swaps two subvectors,
*
*     sub( Y ) := sub( X ) and sub( X ) := sub( Y )
*
*  where sub( X ) denotes X(IX,JX:JX+N-1) if INCX = M_X,
*                         X(IX:IX+N-1,JX) if INCX = 1 and INCX <> M_X,
*
*        sub( Y ) denotes Y(IY,JY:JY+N-1) if INCY = M_Y,
*                         Y(IY:IY+N-1,JY) if INCY = 1 and INCY <> M_Y.
*
*  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
*  =========
*
*  N       (global input) INTEGER
*          On entry,  N  specifies the  length of the  subvectors to  be
*          swapped. N must be at least zero.
*
*  X       (local input/local output) DOUBLE PRECISION array
*          On entry, X is an array of dimension (LLD_X, Kx), where LLD_X
*          is   at  least  MAX( 1, Lr( 1, IX ) )  when  INCX = M_X   and
*          MAX( 1, Lr( 1, IX+N-1 ) )  otherwise,  and,  Kx  is  at least
*          Lc( 1, JX+N-1 )  when  INCX = M_X  and Lc( 1, JX ) otherwise.
*          Before  entry,  this array  contains the local entries of the
*          matrix X. On exit, sub( X ) is overwritten with sub( Y ).
*
*  IX      (global input) INTEGER
*          On entry, IX  specifies X's global row index, which points to
*          the beginning of the submatrix sub( X ).
*
*  JX      (global input) INTEGER
*          On entry, JX  specifies X's global column index, which points
*          to the beginning of the submatrix sub( X ).
*
*  DESCX   (global and local input) INTEGER array
*          On entry, DESCX  is an integer array of dimension DLEN_. This
*          is the array descriptor for the matrix X.
*
*  INCX    (global input) INTEGER
*          On entry,  INCX   specifies  the  global  increment  for  the
*          elements of  X.  Only two values of  INCX   are  supported in
*          this version, namely 1 and M_X. INCX  must not be zero.
*
*  Y       (local input/local output) DOUBLE PRECISION array
*          On entry, Y is an array of dimension (LLD_Y, Ky), where LLD_Y
*          is   at  least  MAX( 1, Lr( 1, IY ) )  when  INCY = M_Y   and
*          MAX( 1, Lr( 1, IY+N-1 ) )  otherwise,  and,  Ky  is  at least
*          Lc( 1, JY+N-1 )  when  INCY = M_Y  and Lc( 1, JY ) otherwise.
*          Before  entry,  this array  contains the local entries of the
*          matrix Y. On exit, sub( Y ) is overwritten with sub( X ).
*
*  IY      (global input) INTEGER
*          On entry, IY  specifies Y's global row index, which points to
*          the beginning of the submatrix sub( Y ).
*
*  JY      (global input) INTEGER
*          On entry, JY  specifies Y's global column index, which points
*          to the beginning of the submatrix sub( Y ).
*
*  DESCY   (global and local input) INTEGER array
*          On entry, DESCY  is an integer array of dimension DLEN_. This
*          is the array descriptor for the matrix Y.
*
*  INCY    (global input) INTEGER
*          On entry,  INCY   specifies  the  global  increment  for  the
*          elements of  Y.  Only two values of  INCY   are  supported in
*          this version, namely 1 and M_Y. INCY  must not be zero.
*
*  -- Written on April 1, 1998 by
*     Antoine Petitet, University of Tennessee, Knoxville 37996, USA.
*
*  ---------------------------------------------------------------------
*/
/*
*  .. Local Scalars ..
*/
   char           Xscope, Yscope, * one, * top, tran, * zero;
   Int            OneBlock, OneDgrid, RRorCC, Square, Xcol, Xi, XisD, XisR,
                  Xinb1D, XinbD, XisRow, Xii, Xj, Xjj, Xld, Xlinc, Xm, XmyprocD,
                  XmyprocR, Xn, XnbD, XnpD, XnprocsD, XnprocsR, XprocD, XprocR,
                  Xroc, Xrow, Ycol, Yi, Yii, Yinb1D, YinbD, YisD, YisR, YisRow,
                  Yj, Yjj, Yld, Ylinc, Ym, YmyprocD, YmyprocR, Yn, YnbD, YnpD,
                  YnprocsD, YnprocsR, YprocD, YprocR, Yroc, Yrow, cdst, csrc,
                  ctxt, dst, gcdPQ, info, ione=1, k, l, lcmPQ, lcmb, mycol,
                  myrow, npcol, npq, nprow, p, q, rdst, rsrc, src, size;
   PBTYP_T        * type;
   PB_VM_T        VM;
/*
*  .. Local Arrays ..
*/
   Int            Xd[DLEN_], Yd[DLEN_];
   char           * buf = NULL;
/* ..
*  .. Executable Statements ..
*
*/
   PB_CargFtoC( *IX, *JX, DESCX, &Xi, &Xj, Xd );
   PB_CargFtoC( *IY, *JY, DESCY, &Yi, &Yj, Yd );
#ifndef NO_ARGCHK
/*
*  Test the input parameters
*/
   Cblacs_gridinfo( ( ctxt = Xd[CTXT_] ), &nprow, &npcol, &myrow, &mycol );
   if( !( info = ( ( nprow == -1 ) ? -( 501 + CTXT_ ) : 0 ) ) )
   {
      PB_Cchkvec( ctxt, "PDSWAP", "X", *N, 1, Xi, Xj, Xd, *INCX,  5, &info );
      PB_Cchkvec( ctxt, "PDSWAP", "Y", *N, 1, Yi, Yj, Yd, *INCY, 10, &info );
   }
   if( info ) { PB_Cabort( ctxt, "PDSWAP", info ); return; }
#endif
/*
*  Quick return if possible
*/
   if( *N == 0 ) return;
/*
*  Retrieve process grid information
*/
#ifdef NO_ARGCHK
   Cblacs_gridinfo( ( ctxt = Xd[CTXT_] ), &nprow, &npcol, &myrow, &mycol );
#endif
/*
*  Determine if sub( X ) is distributed or not
*/
   if( ( XisRow = ( *INCX == Xd[M_] ) ) != 0 )
      XisD = ( ( Xd[CSRC_] >= 0 ) && ( ( XnprocsD = npcol ) > 1 ) );
   else
      XisD = ( ( Xd[RSRC_] >= 0 ) && ( ( XnprocsD = nprow ) > 1 ) );
/*
*  Determine if sub( Y ) is distributed or not
*/
   if( ( YisRow = ( *INCY == Yd[M_] ) ) != 0 )
      YisD = ( ( Yd[CSRC_] >= 0 ) && ( ( YnprocsD = npcol ) > 1 ) );
   else
      YisD = ( ( Yd[RSRC_] >= 0 ) && ( ( YnprocsD = nprow ) > 1 ) );
/*
*  Are sub( X ) and sub( Y ) both row or column vectors ?
*/
   RRorCC = ( ( XisRow && YisRow ) || ( !( XisRow ) && !( YisRow ) ) );
/*
*  XisD && YisD <=> both vector operands are indeed distributed
*/
   if( XisD && YisD )
   {
/*
*  Retrieve sub( X )'s local information: Xii, Xjj, Xrow, Xcol ...
*/
      PB_Cinfog2l( Xi, Xj, Xd, nprow, npcol, myrow, mycol, &Xii, &Xjj,
                   &Xrow, &Xcol );
      if( XisRow )
      {
         XinbD  = Xd[INB_ ]; XnbD     = Xd[NB_ ]; Xld      = Xd[LLD_];
         Xlinc  = Xld;
         XprocD = Xcol;      XmyprocD = mycol;
         XprocR = Xrow;      XmyprocR = myrow;    XnprocsR = nprow;
         XisR   = ( ( Xrow == -1 ) || ( XnprocsR == 1 ) );
         Mfirstnb( Xinb1D, *N, Xj, XinbD, XnbD );
      }
      else
      {
         XinbD  = Xd[IMB_ ]; XnbD     = Xd[MB_ ]; Xld      = Xd[LLD_];
         Xlinc  = 1;
         XprocD = Xrow;      XmyprocD = myrow;
         XprocR = Xcol;      XmyprocR = mycol;    XnprocsR = npcol;
         XisR   = ( ( Xcol == -1 ) || ( XnprocsR == 1 ) );
         Mfirstnb( Xinb1D, *N, Xi, XinbD, XnbD );
      }
/*
*  Retrieve sub( Y )'s local information: Yii, Yjj, Yrow, Ycol ...
*/
      PB_Cinfog2l( Yi, Yj, Yd, nprow, npcol, myrow, mycol, &Yii, &Yjj,
                   &Yrow, &Ycol );
      if( YisRow )
      {
         YinbD  = Yd[INB_ ]; YnbD     = Yd[NB_ ]; Yld      = Yd[LLD_];
         Ylinc  = Yld;
         YprocD = Ycol;      YmyprocD = mycol;
         YprocR = Yrow;      YmyprocR = myrow;    YnprocsR = nprow;
         YisR   = ( ( Yrow == -1 ) || ( YnprocsR == 1 ) );
         Mfirstnb( Yinb1D, *N, Yj, YinbD, YnbD );
      }
      else
      {
         YinbD  = Yd[IMB_ ]; YnbD     = Yd[MB_ ]; Yld      = Yd[LLD_];
         Ylinc  = 1;
         YprocD = Yrow;      YmyprocD = myrow;
         YprocR = Ycol;      YmyprocR = mycol;    YnprocsR = npcol;
         YisR   = ( ( Ycol == -1 ) || ( YnprocsR == 1 ) );
         Mfirstnb( Yinb1D, *N, Yi, YinbD, YnbD );
      }
/*
*  Do sub( X ) and sub( Y ) span more than one process ?
*/
      OneDgrid = ( ( XnprocsD ==  1 ) && ( YnprocsD ==  1 ) );
      OneBlock = ( ( Xinb1D   >= *N ) && ( Yinb1D   >= *N ) );
/*
*  Are sub( X ) and sub( Y ) distributed in the same manner ?
*/
      Square   = ( ( Xinb1D   ==   Yinb1D ) && ( XnbD == YnbD ) &&
                   ( XnprocsD == YnprocsD ) );
      if( !( XisR ) )
      {
/*
*  sub( X ) is distributed but not replicated
*/
         if( YisR )
         {
/*
*  If sub( X ) is not replicated, but sub( Y ) is, a process row or column
*  YprocR need to be selected. It will contain the non-replicated vector to
*  swap sub( X ) with.
*/
            if( RRorCC )
            {
/*
*  sub( X ) and sub( Y ) are both row or column vectors
*/
               if( ( OneDgrid || OneBlock || Square ) && ( XprocD == YprocD ) )
               {
/*
*  sub( X ) and sub( Y ) start in the same process row or column XprocD=YprocD.
*  Enforce a purely local operation by choosing YprocR to be equal to XprocR.
*/
                  YprocR = XprocR;
               }
               else
               {
/*
*  Otherwise, communication has to occur, so choose the next process row or
*  column for YprocR to maximize the number of links, i.e reduce contention.
*/
                  YprocR = MModAdd1( XprocR, XnprocsR );
               }
            }
            else
            {
/*
*  sub( X ) and sub( Y ) are distributed in orthogonal directions, what is
*  chosen for YprocR does not really matter. Select the process origin.
*/
               YprocR = XprocD;
            }
         }
         else
         {
/*
*  Neither sub( X ) nor sub( Y ) are replicated. If I am not in process row or
*  column XprocR and not in process row or column YprocR, then quick return.
*/
            if( ( XmyprocR != XprocR ) && ( YmyprocR != YprocR ) )
               return;
         }
      }
      else
      {
/*
*  sub( X ) is distributed and replicated (so no quick return possible)
*/
         if( YisR )
         {
/*
*  sub( Y ) is distributed and replicated as well
*/
            if( RRorCC )
            {
/*
*  sub( X ) and sub( Y ) are both row or column vectors
*/
               if( ( OneDgrid || OneBlock || Square ) && ( XprocD == YprocD ) )
               {
/*
*  sub( X ) and sub( Y ) start in the same process row or column XprocD=YprocD.
*  Enforce a purely local operation by choosing XprocR and YprocR to be equal
*  to zero.
*/
                  XprocR = YprocR = 0;
               }
               else
               {
/*
*  Otherwise, communication has to occur, so select YprocR to be zero and the
*  next process row or column for XprocR in order to maximize the number of
*  used links, i.e reduce contention.
*/
                  YprocR = 0;
                  XprocR = MModAdd1( YprocR, YnprocsR );
               }
            }
            else
            {
/*
*  sub( X ) and sub( Y ) are distributed in orthogonal directions, select the
*  origin processes.
*/
               XprocR = YprocD;
               YprocR = XprocD;
            }
         }
         else
         {
/*
*  sub( Y ) is distributed, but not replicated
*/
            if( RRorCC )
            {
/*
*  sub( X ) and sub( Y ) are both row or column vectors
*/
               if( ( OneDgrid || OneBlock || Square ) && ( XprocD == YprocD ) )
               {
/*
*  sub( X ) and sub( Y ) start in the same process row or column XprocD=YprocD.
*  Enforce a purely local operation by choosing XprocR to be equal to YprocR.
*/
                  XprocR = YprocR;
               }
               else
               {
/*
*  Otherwise, communication has to occur, so choose the next process row or
*  column for XprocR to maximize the number of links, i.e reduce contention.
*/
                  XprocR = MModAdd1( YprocR, YnprocsR );
               }
            }
            else
            {
/*
*  sub( X ) and sub( Y ) are distributed in orthogonal directions, what is
*  chosen for XprocR does not really matter. Select the origin process.
*/
               XprocR = YprocD;
            }
         }
      }
/*
*  Even if sub( X ) and/or sub( Y ) are replicated, only two process row or
*  column are active, namely XprocR and YprocR. If any of those operands is
*  replicated, broadcast will occur (unless there is an easy way out).
*/
      type = PB_Cdtypeset(); size = type->size;
/*
*  A purely local operation occurs iff the operands start in the same process
*  and, if either the grid is mono-dimensional or there is a single local block
*  to be swapped or if both operands are aligned.
*/
      if( ( (    RRorCC   && ( XprocD == YprocD ) && ( XprocR == YprocR ) ) ||
            ( !( RRorCC ) && ( XprocD == YprocR ) && ( XprocR == YprocD ) ) ) &&
          ( OneDgrid || OneBlock || ( RRorCC && Square ) ) )
      {
         if( ( !XisR &&         ( XmyprocR == XprocR ) &&
               !YisR &&         ( YmyprocR == YprocR ) ) ||
             ( !XisR && YisR && ( YmyprocR == YprocR ) ) ||
             ( !YisR && XisR && ( XmyprocR == XprocR ) ) ||
             (  XisR && YisR                           ) )
         {
            XnpD = PB_Cnumroc( *N, 0, Xinb1D, XnbD, XmyprocD, XprocD,
                               XnprocsD );
            YnpD = PB_Cnumroc( *N, 0, Yinb1D, YnbD, YmyprocD, YprocD,
                               YnprocsD );
            if( ( XnpD > 0 ) && ( YnpD > 0 ) )
            {
               dswap_( &XnpD,
                       Mptr( ((char *) X), Xii, Xjj, Xld, size ), &Xlinc,
                       Mptr( ((char *) Y), Yii, Yjj, Yld, size ), &Ylinc );
            }
            if( RRorCC && XisR && YisR ) return;
         }
      }
      else if( ( RRorCC && OneDgrid ) || OneBlock || Square )
      {
/*
*  Otherwise, it may be possible to swap the distributed vectors in a single
*  message exchange iff the grid is mono-dimensional and the operands are
*  distributed in the same direction, or there is just one block to be exchanged
*  or if both operands are similarly distributed in their respective direction.
*/
         if( RRorCC && ( XprocR != YprocR ) )
         {
/*
*  Both operands are distributed in the same direction, but reside in different
*  process rows or columns.
*/
            if( XmyprocR == XprocR )
            {
               XnpD = PB_Cnumroc( *N, 0, Xinb1D, XnbD, XmyprocD, XprocD,
                                  XnprocsD );
               if( XnpD > 0 )
               {
                  dst = YprocD + MModSub( XmyprocD, XprocD, XnprocsD );
                  dst = MPosMod( dst, YnprocsD );
                  if( XisRow )
                  {
                     Cdgesd2d( ctxt, 1, XnpD, Mptr( ((char*) X), Xii, Xjj,
                               Xld, size ), Xld, YprocR, dst );
                     Cdgerv2d( ctxt, 1, XnpD, Mptr( ((char*) X), Xii, Xjj,
                               Xld, size ), Xld, YprocR, dst );
                  }
                  else
                  {
                     Cdgesd2d( ctxt, XnpD, 1, Mptr( ((char*) X), Xii, Xjj,
                               Xld, size ), Xld, dst, YprocR );
                     Cdgerv2d( ctxt, XnpD, 1, Mptr( ((char*) X), Xii, Xjj,
                               Xld, size ), Xld, dst, YprocR );
                  }
               }
            }
            if( YmyprocR == YprocR )
            {
               YnpD = PB_Cnumroc( *N, 0, Yinb1D, YnbD, YmyprocD, YprocD,
                                  YnprocsD );
               if( YnpD > 0 )
               {
                  dst = XprocD + MModSub( YmyprocD, YprocD, YnprocsD );
                  dst = MPosMod( dst, XnprocsD );
                  if( YisRow )
                  {
                     Cdgesd2d( ctxt, 1, YnpD, Mptr( ((char*) Y), Yii, Yjj,
                               Yld, size ), Yld, XprocR, dst );
                     Cdgerv2d( ctxt, 1, YnpD, Mptr( ((char*) Y), Yii, Yjj,
                               Yld, size ), Yld, XprocR, dst );
                  }
                  else
                  {
                     Cdgesd2d( ctxt, YnpD, 1, Mptr( ((char*) Y), Yii, Yjj,
                               Yld, size ), Yld, dst, XprocR );
                     Cdgerv2d( ctxt, YnpD, 1, Mptr( ((char*) Y), Yii, Yjj,
                               Yld, size ), Yld, dst, XprocR );
                  }
               }
            }
         }
         else
         {
/*
*  General case when just one message needs to be exchanged
*/
            if( XmyprocR == XprocR )
            {
/*
*  The processes owning a piece of sub( X ) send it to the corresponding
*  process owning s piece of sub ( Y ).
*/
               XnpD = PB_Cnumroc( *N, 0, Xinb1D, XnbD, XmyprocD, XprocD,
                                  XnprocsD );
               if( XnpD > 0 )
               {
                  dst = YprocD + MModSub( XmyprocD, XprocD, XnprocsD );
                  dst = MPosMod( dst, YnprocsD );
                  if( YisRow ) { rdst = YprocR; cdst = dst; }
                  else         { rdst = dst; cdst = YprocR; }
                  if( ( myrow == rdst ) && ( mycol == cdst ) )
                  {
                     dswap_( &XnpD,  Mptr( ((char *) X), Xii, Xjj, Xld,
                             size ), &Xlinc, Mptr( ((char *) Y), Yii, Yjj, Yld,
                             size ), &Ylinc );
                  }
                  else
                  {
                     if( XisRow )
                        Cdgesd2d( ctxt, 1, XnpD, Mptr( ((char *) X), Xii,
                                  Xjj, Xld, size ), Xld, rdst, cdst );
                     else
                        Cdgesd2d( ctxt, XnpD, 1, Mptr( ((char *) X), Xii,
                                  Xjj, Xld, size ), Xld, rdst, cdst );
                  }
               }
            }
            if( YmyprocR == YprocR )
            {
/*
*  The processes owning a piece of sub( Y ) receive the corresponding piece
*  of sub( X ) and send the piece of sub( Y ) they own to the same process.
*/
               YnpD = PB_Cnumroc( *N, 0, Yinb1D, YnbD, YmyprocD, YprocD,
                                  YnprocsD );
               if( YnpD > 0 )
               {
                  src = XprocD + MModSub( YmyprocD, YprocD, YnprocsD );
                  src = MPosMod( src, XnprocsD );
                  if( XisRow ) { rsrc = XprocR; csrc = src; }
                  else         { rsrc = src; csrc = XprocR; }
                  if( ( myrow != rsrc ) || ( mycol != csrc ) )
                  {
                     buf = PB_Cmalloc( YnpD * size );
                     if( XisRow )
                        Cdgerv2d( ctxt, 1, YnpD, buf,    1, rsrc, csrc );
                     else
                        Cdgerv2d( ctxt, YnpD, 1, buf, YnpD, rsrc, csrc );
                     if( YisRow )
                        Cdgesd2d( ctxt, 1, YnpD, Mptr( ((char *) Y), Yii,
                                  Yjj, Yld, size ), Yld, rsrc, csrc );
                     else
                        Cdgesd2d( ctxt, YnpD, 1, Mptr( ((char *) Y), Yii,
                                  Yjj, Yld, size ), Yld, rsrc, csrc );
                     dcopy_( &YnpD, buf, &ione, Mptr( ((char *) Y), Yii,
                             Yjj, Yld, size ), &Ylinc );
                     if( buf ) free( buf );
                  }
               }
            }
            if( XmyprocR == XprocR )
            {
/*
*  The processes owning a piece of sub( X ) receive the corresponding piece
*  of sub( Y ).
*/
               if( XnpD > 0 )
               {
                  if( ( myrow != rdst ) || ( mycol != cdst ) )
                  {
                     buf = PB_Cmalloc( XnpD * size );
                     if( YisRow )
                        Cdgerv2d( ctxt, 1, XnpD, buf,    1, rdst, cdst );
                     else
                        Cdgerv2d( ctxt, XnpD, 1, buf, XnpD, rdst, cdst );
                     dcopy_( &XnpD, buf, &ione, Mptr( ((char *) X), Xii,
                             Xjj, Xld, size ), &Xlinc );
                     if( buf ) free( buf );
                  }
               }
            }
         }
      }
      else if( ( XmyprocR == XprocR ) || ( YmyprocR == YprocR ) )
      {
/*
*  General case
*/
         tran   = ( RRorCC ? CNOTRAN : CTRAN );
         if( XisRow ) { Xscope = CCOLUMN; Xm = 1; rsrc = XprocR; }
         else         { Xscope = CROW;    Xn = 1; csrc = XprocR; }
         if( YisRow ) { Yscope = CCOLUMN; Ym = 1; rdst = YprocR; }
         else         { Yscope = CROW;    Yn = 1; cdst = YprocR; }
         lcmb   = PB_Clcm( XnprocsD * XnbD, YnprocsD * YnbD );
         one    = type->one; zero = type->zero;
         gcdPQ  = PB_Cgcd( XnprocsD, YnprocsD );
         lcmPQ  = ( XnprocsD / gcdPQ ) * YnprocsD;
         for( k = 0; k < gcdPQ; k++ )
         {
            p = 0; q = k;
            for( l = 0; l < lcmPQ; l++ )
            {
               Xroc = MModAdd( XprocD, p, XnprocsD );
               Yroc = MModAdd( YprocD, q, YnprocsD );
               if( ( XmyprocD == Xroc ) || ( YmyprocD == Yroc ) )
               {
                  XnpD = PB_Cnumroc( *N, 0, Xinb1D, XnbD, Xroc, XprocD,
                                     XnprocsD );
                  YnpD = PB_Cnumroc( *N, 0, Yinb1D, YnbD, Yroc, YprocD,
                                     YnprocsD );
                  PB_CVMinit( &VM, 0, XnpD, YnpD, Xinb1D, Yinb1D, XnbD, YnbD,
                              p, q, XnprocsD, YnprocsD, lcmb );
                  if( npq = PB_CVMnpq( &VM ) )
                  {
                     if( (    RRorCC   && ( Xroc ==   Yroc ) &&
                           ( XprocR == YprocR ) ) ||
                         ( !( RRorCC ) && ( Xroc == YprocR ) &&
                           ( XprocR == Yroc   ) ) )
                     {
/*
*  If I am at the intersection of the process cross, or simply common to the
*  process rows or columns owning sub( X ) and sub( Y )
*/
                        if( ( YmyprocD == Yroc ) && ( YmyprocR == YprocR ) )
                        {
                           PB_CVMswp( type, &VM, ROW, &Xscope, &tran, npq,
                                      Mptr( ((char *) X), Xii, Xjj, Xld, size ),
                                      Xlinc, Mptr( ((char *) Y), Yii, Yjj, Yld,
                                      size ), Ylinc );
                        }
                     }
                     else
                     {
/*
*  Perform the message exchange: pack the data I own, send it, receive the
*  remote data, and unpack it.
*/
                        if( ( XmyprocR == XprocR ) && ( XmyprocD == Xroc ) )
                        {
                           if( XisRow ) { Xn = npq; }
                           else         { Xm = npq; }
                           if( YisRow ) { Yn = npq; cdst = Yroc; }
                           else         { Ym = npq; rdst = Yroc; }
                           buf = PB_Cmalloc( npq * size );
                           PB_CVMpack( type, &VM, ROW, &Xscope, PACKING, NOTRAN,
                                       npq, 1, one, Mptr( ((char *) X), Xii,
                                       Xjj, Xld, size ), Xld, zero, buf, Xm );
                           Cdgesd2d( ctxt, Xm, Xn, buf, Xm, rdst, cdst );
                           Cdgerv2d( ctxt, Ym, Yn, buf, Ym, rdst, cdst );
                           PB_CVMpack( type, &VM, ROW, &Xscope, UNPACKING,
                                       &tran, npq, 1, zero, Mptr( ((char *) X),
                                       Xii, Xjj, Xld, size ), Xld, one, buf,
                                       Ym );
                           if( buf ) free ( buf );
                        }
                        if( ( YmyprocR == YprocR ) && ( YmyprocD == Yroc  ) )
                        {
                           if( XisRow ) { Xn = npq; csrc = Xroc; }
                           else         { Xm = npq; rsrc = Xroc; }
                           if( YisRow ) { Yn = npq; }
                           else         { Ym = npq; }
                           buf = PB_Cmalloc( npq * size );
                           PB_CVMpack( type, &VM, COLUMN, &Yscope, PACKING,
                                       NOTRAN, npq, 1, one, Mptr( ((char *) Y),
                                       Yii, Yjj, Yld, size ), Yld, zero, buf,
                                       Ym );
                           Cdgesd2d( ctxt, Ym, Yn, buf, Ym, rsrc, csrc );
                           Cdgerv2d( ctxt, Xm, Xn, buf, Xm, rsrc, csrc );
                           PB_CVMpack( type, &VM, COLUMN, &Yscope, UNPACKING,
                                       &tran, npq, 1, zero, Mptr( ((char *) Y),
                                       Yii, Yjj, Yld, size ), Yld, one, buf,
                                       Xm );
                           if( buf ) free ( buf );
                        }
                     }
                  }
               }
               p = MModAdd1( p, XnprocsD );
               q = MModAdd1( q, YnprocsD );
            }
         }
      }
      if( XisR )
      {
/*
*  Replicate sub( X ) when necessary
*/
         XnpD = PB_Cnumroc( *N, 0, Xinb1D, XnbD, XmyprocD, XprocD, XnprocsD );
         if( XnpD > 0 )
         {
            if( XisRow )
            {
               top = PB_Ctop( &ctxt, BCAST, COLUMN, TOP_GET );
               if( XmyprocR == XprocR )
                  Cdgebs2d( ctxt, COLUMN, top, 1, XnpD, Mptr( ((char *) X),
                            Xii, Xjj, Xld, size ), Xld );
               else
                  Cdgebr2d( ctxt, COLUMN, top, 1, XnpD, Mptr( ((char *) X),
                            Xii, Xjj, Xld, size ), Xld, XprocR, XmyprocD );
            }
            else
            {
               top = PB_Ctop( &ctxt, BCAST, ROW,    TOP_GET );
               if( XmyprocR == XprocR )
                  Cdgebs2d( ctxt, ROW,    top, XnpD, 1, Mptr( ((char *) X),
                            Xii, Xjj, Xld, size ), Xld );
               else
                  Cdgebr2d( ctxt, ROW,    top, XnpD, 1, Mptr( ((char *) X),
                            Xii, Xjj, Xld, size ), Xld, XmyprocD, XprocR );
            }
         }
      }
      if( YisR )
      {
/*
*  Replicate sub( Y ) when necessary
*/
         YnpD = PB_Cnumroc( *N, 0, Yinb1D, YnbD, YmyprocD, YprocD, YnprocsD );
         if( YnpD > 0 )
         {
            if( YisRow )
            {
               top = PB_Ctop( &ctxt, BCAST, COLUMN, TOP_GET );
               if( YmyprocR == YprocR )
                  Cdgebs2d( ctxt, COLUMN, top, 1, YnpD, Mptr( ((char *) Y),
                            Yii, Yjj, Yld, size ), Yld );
               else
                  Cdgebr2d( ctxt, COLUMN, top, 1, YnpD, Mptr( ((char *) Y),
                            Yii, Yjj, Yld, size ), Yld, YprocR, YmyprocD );
            }
            else
            {
               top = PB_Ctop( &ctxt, BCAST, ROW,    TOP_GET );
               if( YmyprocR == YprocR )
                  Cdgebs2d( ctxt, ROW,    top, YnpD, 1, Mptr( ((char *) Y),
                            Yii, Yjj, Yld, size ), Yld );
               else
                  Cdgebr2d( ctxt, ROW,    top, YnpD, 1, Mptr( ((char *) Y),
                            Yii, Yjj, Yld, size ), Yld, YmyprocD, YprocR );
            }
         }
      }
   }
   else if( !( XisD ) && YisD )
   {
/*
*  sub( X ) is not distributed and sub( Y ) is distributed.
*/
      PB_CpswapND( PB_Cdtypeset(), *N, ((char *) X), Xi, Xj, Xd, *INCX,
                   ((char *) Y), Yi, Yj, Yd, *INCY );
   }
   else if( XisD && !( YisD ) )
   {
/*
*  sub( X ) is distributed and sub( Y ) is not distributed.
*/
      PB_CpswapND( PB_Cdtypeset(), *N, ((char *) Y), Yi, Yj, Yd, *INCY,
                   ((char *) X), Xi, Xj, Xd, *INCX );
   }
   else
   {
/*
*  Neither sub( X ) nor sub( Y ) are distributed.
*/
      PB_CpswapNN( PB_Cdtypeset(), *N, ((char *) X), Xi, Xj, Xd, *INCX,
                   ((char *) Y), Yi, Yj, Yd, *INCY );
   }
/*
*  End of PDSWAP
*/
}
 |