-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdgstrs_Bglobal_Bsend.c
1017 lines (922 loc) · 29.4 KB
/
pdgstrs_Bglobal_Bsend.c
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
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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! \file
Copyright (c) 2003, The Regents of the University of California, through
Lawrence Berkeley National Laboratory (subject to receipt of any required
approvals from U.S. Dept. of Energy)
All rights reserved.
The source code is distributed under BSD license, see the file License.txt
at the top-level directory.
*/
/*! @file
* \brief Solves a system of distributed linear equations
*
* <pre>
* -- Distributed SuperLU routine (version 1.0) --
* Lawrence Berkeley National Lab, Univ. of California Berkeley.
* September 1, 1999
*
* Modified:
* Feburary 7, 2001 use MPI_Isend/MPI_Irecv
* October 2, 2001 use MPI_Isend/MPI_Irecv with MPI_Test
* </pre>
*/
#include "superlu_ddefs.h"
/*#define ISEND_IRECV*/
/* Parry's change
Use MPI_Bsend with a large buffer attached in the main program */
#define BSEND 1
/*
* Function prototypes
*/
#ifdef _CRAY
fortran void STRSM(_fcd, _fcd, _fcd, _fcd, int*, int*, double*,
double*, int*, double*, int*);
fortran void SGEMM(_fcd, _fcd, int*, int*, int*, double*, double*,
int*, double*, int*, double*, double*, int*);
_fcd ftcs1;
_fcd ftcs2;
_fcd ftcs3;
#endif
/*! \brief
*
* <pre>
* Purpose
* =======
*
* pdgstrs_Bglobal solves a system of distributed linear equations
* A*X = B with a general N-by-N matrix A using the LU factorization
* computed by pdgstrf.
*
* Arguments
* =========
*
* n (input) int (global)
* The order of the system of linear equations.
*
* LUstruct (input) LUstruct_t*
* The distributed data structures storing L and U factors.
* The L and U factors are obtained from pdgstrf for
* the possibly scaled and permuted matrix A.
* See superlu_ddefs.h for the definition of 'LUstruct_t'.
*
* grid (input) gridinfo_t*
* The 2D process mesh. It contains the MPI communicator, the number
* of process rows (NPROW), the number of process columns (NPCOL),
* and my process rank. It is an input argument to all the
* parallel routines.
* Grid can be initialized by subroutine SUPERLU_GRIDINIT.
* See superlu_ddefs.h for the definition of 'gridinfo_t'.
*
* B (input/output) double*
* On entry, the right-hand side matrix of the possibly equilibrated
* and row permuted system.
* On exit, the solution matrix of the possibly equilibrated
* and row permuted system if info = 0;
*
* NOTE: Currently, the N-by-NRHS matrix B must reside on all
* processes when calling this routine.
*
* ldb (input) int (global)
* Leading dimension of matrix B.
*
* nrhs (input) int (global)
* Number of right-hand sides.
*
* stat (output) SuperLUStat_t*
* Record the statistics about the triangular solves.
* See util.h for the definition of 'SuperLUStat_t'.
*
* info (output) int*
* = 0: successful exit
* < 0: if info = -i, the i-th argument had an illegal value
* </pre>
*/
void
pdgstrs_Bglobal(int_t n, LUstruct_t *LUstruct, gridinfo_t *grid, double *B,
int_t ldb, int nrhs, SuperLUStat_t *stat, int *info)
{
Glu_persist_t *Glu_persist = LUstruct->Glu_persist;
LocalLU_t *Llu = LUstruct->Llu;
double alpha = 1.0;
double *lsum; /* Local running sum of the updates to B-components */
double *x; /* X component at step k. */
double *lusup, *dest;
double *recvbuf, *tempv;
double *rtemp; /* Result of full matrix-vector multiply. */
int_t **Ufstnz_br_ptr = Llu->Ufstnz_br_ptr;
int_t *Urbs, *Urbs1; /* Number of row blocks in each block column of U. */
Ucb_indptr_t **Ucb_indptr;/* Vertical linked list pointing to Uindex[] */
int_t **Ucb_valptr; /* Vertical linked list pointing to Unzval[] */
int_t iam, kcol, krow, mycol, myrow;
int_t i, ii, il, j, jj, k, lb, ljb, lk, lptr, luptr;
int_t nb, nlb, nub, nsupers;
int_t *xsup, *lsub, *usub;
int_t *ilsum; /* Starting position of each supernode in lsum (LOCAL)*/
int_t Pc, Pr;
int knsupc, nsupr;
int ldalsum; /* Number of lsum entries locally owned. */
int maxrecvsz, p, pi;
int_t **Lrowind_bc_ptr;
double **Lnzval_bc_ptr;
MPI_Status status;
#if defined(ISEND_IRECV) || defined(BSEND)
MPI_Request *send_req, recv_req;
int test_flag;
#endif
/*-- Counts used for L-solve --*/
int_t *fmod; /* Modification count for L-solve. */
int_t **fsendx_plist = Llu->fsendx_plist;
int_t nfrecvx = Llu->nfrecvx; /* Number of X components to be recv'd. */
int_t *frecv; /* Count of modifications to be recv'd from
processes in this row. */
int_t nfrecvmod = 0; /* Count of total modifications to be recv'd. */
int_t nleaf = 0, nroot = 0;
/*-- Counts used for U-solve --*/
int_t *bmod; /* Modification count for L-solve. */
int_t **bsendx_plist = Llu->bsendx_plist;
int_t nbrecvx = Llu->nbrecvx; /* Number of X components to be recv'd. */
int_t *brecv; /* Count of modifications to be recv'd from
processes in this row. */
int_t nbrecvmod = 0; /* Count of total modifications to be recv'd. */
double t;
#if ( DEBUGlevel>=2 )
int_t Ublocks = 0;
#endif
/*-- Function prototypes --*/
extern void gather_diag_to_all(int_t, int_t, double [], Glu_persist_t *,
LocalLU_t *, gridinfo_t *, int_t, int_t [],
int_t [], double [], int_t, double []);
t = SuperLU_timer_();
/* Test input parameters. */
*info = 0;
if ( n < 0 ) *info = -1;
else if ( nrhs < 0 ) *info = -9;
if ( *info ) {
pxerbla("PDGSTRS_BGLOBAL", grid, -*info);
return;
}
/*
* Initialization.
*/
iam = grid->iam;
#ifdef BSEND
if(!iam) {
printf("Using MPI_Bsend in triangular solve\n");
fflush(stdout);
}
#endif
Pc = grid->npcol;
Pr = grid->nprow;
myrow = MYROW( iam, grid );
mycol = MYCOL( iam, grid );
nsupers = Glu_persist->supno[n-1] + 1;
xsup = Glu_persist->xsup;
Lrowind_bc_ptr = Llu->Lrowind_bc_ptr;
Lnzval_bc_ptr = Llu->Lnzval_bc_ptr;
nlb = CEILING( nsupers, Pr ); /* Number of local block rows. */
stat->ops[SOLVE] = 0.0;
#if ( DEBUGlevel>=1 )
CHECK_MALLOC(iam, "Enter pdgstrs_Bglobal()");
#endif
/* Save the count to be altered so it can be used by
subsequent call to PDGSTRS_BGLOBAL. */
if ( !(fmod = intMalloc_dist(nlb)) )
ABORT("Calloc fails for fmod[].");
for (i = 0; i < nlb; ++i) fmod[i] = Llu->fmod[i];
if ( !(frecv = intMalloc_dist(nlb)) )
ABORT("Malloc fails for frecv[].");
Llu->frecv = frecv;
#if defined(ISEND_IRECV) || defined(BSEND)
if ( !(send_req = (MPI_Request*) SUPERLU_MALLOC(Pr*sizeof(MPI_Request))) )
ABORT("Malloc fails for send_req[].");
for (i = 0; i < Pr; ++i) send_req[i] = MPI_REQUEST_NULL;
#endif
#ifdef _CRAY
ftcs1 = _cptofcd("L", strlen("L"));
ftcs2 = _cptofcd("N", strlen("N"));
ftcs3 = _cptofcd("U", strlen("U"));
#endif
/* Obtain ilsum[] and ldalsum for process column 0. */
ilsum = Llu->ilsum;
ldalsum = Llu->ldalsum;
/* Allocate working storage. */
knsupc = sp_ienv_dist(3);
maxrecvsz = knsupc * nrhs + SUPERLU_MAX( XK_H, LSUM_H );
if ( !(lsum = doubleCalloc_dist(((size_t)ldalsum) * nrhs + nlb * LSUM_H)))
ABORT("Calloc fails for lsum[].");
if ( !(x = doubleMalloc_dist(ldalsum * nrhs + nlb * XK_H)) )
ABORT("Malloc fails for x[].");
if ( !(recvbuf = doubleMalloc_dist(maxrecvsz)) )
ABORT("Malloc fails for recvbuf[].");
if ( !(rtemp = doubleMalloc_dist(maxrecvsz)) )
ABORT("Malloc fails for rtemp[].");
/*---------------------------------------------------
* Forward solve Ly = b.
*---------------------------------------------------*/
/*
* Copy B into X on the diagonal processes.
*/
ii = 0;
for (k = 0; k < nsupers; ++k) {
knsupc = SuperSize( k );
krow = PROW( k, grid );
if ( myrow == krow ) {
lk = LBi( k, grid ); /* Local block number. */
il = LSUM_BLK( lk );
lsum[il - LSUM_H] = k; /* Block number prepended in the header. */
kcol = PCOL( k, grid );
if ( mycol == kcol ) { /* Diagonal process. */
jj = X_BLK( lk );
x[jj - XK_H] = k; /* Block number prepended in the header. */
RHS_ITERATE(j)
for (i = 0; i < knsupc; ++i) /* X is stored in blocks. */
x[i + jj + j*knsupc] = B[i + ii + j*ldb];
}
}
ii += knsupc;
}
/*
* Compute frecv[] and nfrecvmod counts on the diagonal processes.
*/
{
superlu_scope_t *scp = &grid->rscp;
for (k = 0; k < nsupers; ++k) {
krow = PROW( k, grid );
if ( myrow == krow ) {
lk = LBi( k, grid ); /* Local block number. */
kcol = PCOL( k, grid ); /* Root process in this row scope. */
if ( mycol != kcol && fmod[lk] )
i = 1; /* Contribution from non-diagonal process. */
else i = 0;
MPI_Reduce( &i, &frecv[lk], 1, mpi_int_t,
MPI_SUM, kcol, scp->comm );
if ( mycol == kcol ) { /* Diagonal process. */
nfrecvmod += frecv[lk];
if ( !frecv[lk] && !fmod[lk] ) ++nleaf;
#if ( DEBUGlevel>=2 )
printf("(%2d) frecv[%4d] %2d\n", iam, k, frecv[lk]);
assert( frecv[lk] < Pc );
#endif
}
}
}
}
/* ---------------------------------------------------------
Solve the leaf nodes first by all the diagonal processes.
--------------------------------------------------------- */
#if ( DEBUGlevel>=1 )
printf("(%2d) nleaf %4d\n", iam, nleaf);
#endif
for (k = 0; k < nsupers && nleaf; ++k) {
krow = PROW( k, grid );
kcol = PCOL( k, grid );
if ( myrow == krow && mycol == kcol ) { /* Diagonal process */
knsupc = SuperSize( k );
lk = LBi( k, grid );
if ( frecv[lk]==0 && fmod[lk]==0 ) {
fmod[lk] = -1; /* Do not solve X[k] in the future. */
ii = X_BLK( lk );
lk = LBj( k, grid ); /* Local block number, column-wise. */
lsub = Lrowind_bc_ptr[lk];
lusup = Lnzval_bc_ptr[lk];
nsupr = lsub[1];
#ifdef _CRAY
STRSM(ftcs1, ftcs1, ftcs2, ftcs3, &knsupc, &nrhs, &alpha,
lusup, &nsupr, &x[ii], &knsupc);
#else
dtrsm_("L", "L", "N", "U", &knsupc, &nrhs, &alpha,
lusup, &nsupr, &x[ii], &knsupc);
#endif
stat->ops[SOLVE] += knsupc * (knsupc - 1) * nrhs;
--nleaf;
#if ( DEBUGlevel>=2 )
printf("(%2d) Solve X[%2d]\n", iam, k);
#endif
/*
* Send Xk to process column Pc[k].
*/
for (p = 0; p < Pr; ++p)
if ( fsendx_plist[lk][p] != EMPTY ) {
pi = PNUM( p, kcol, grid );
#ifdef ISEND_IRECV
#if 1
MPI_Test( &send_req[p], &test_flag, &status );
#else
if ( send_req[p] != MPI_REQUEST_NULL )
MPI_Wait( &send_req[p], &status );
#endif
MPI_Isend( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm, &send_req[p]);
#else
#ifdef BSEND
MPI_Bsend( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm );
#else
MPI_Send( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm );
#endif
#endif
#if ( DEBUGlevel>=2 )
printf("(%2d) Sent X[%2.0f] to P %2d\n",
iam, x[ii-XK_H], pi);
#endif
}
/*
* Perform local block modifications: lsum[i] -= L_i,k * X[k]
*/
nb = lsub[0] - 1;
lptr = BC_HEADER + LB_DESCRIPTOR + knsupc;
luptr = knsupc; /* Skip diagonal block L(k,k). */
dlsum_fmod(lsum, x, &x[ii], rtemp, nrhs, knsupc, k,
fmod, nb, lptr, luptr, xsup, grid, Llu,
send_req,stat);
#ifdef ISEND_IRECV
/* Wait for previous Isends to complete. */
for (p = 0; p < Pr; ++p) {
if ( fsendx_plist[lk][p] != EMPTY )
/*MPI_Wait( &send_req[p], &status );*/
MPI_Test( &send_req[p], &test_flag, &status );
}
#endif
}
} /* if diagonal process ... */
} /* for k ... */
/* -----------------------------------------------------------
Compute the internal nodes asynchronously by all processes.
----------------------------------------------------------- */
#if ( DEBUGlevel>=1 )
printf("(%2d) nfrecvx %4d, nfrecvmod %4d, nleaf %4d\n",
iam, nfrecvx, nfrecvmod, nleaf);
#endif
while ( nfrecvx || nfrecvmod ) { /* While not finished. */
/* Receive a message. */
#ifdef ISEND_IRECV
/* -MPI- FATAL: Remote protocol queue full */
MPI_Irecv( recvbuf, maxrecvsz, MPI_DOUBLE, MPI_ANY_SOURCE,
MPI_ANY_TAG, grid->comm, &recv_req );
MPI_Wait( &recv_req, &status );
#else
MPI_Recv( recvbuf, maxrecvsz, MPI_DOUBLE, MPI_ANY_SOURCE,
MPI_ANY_TAG, grid->comm, &status );
#endif
k = *recvbuf;
#if ( DEBUGlevel>=2 )
printf("(%2d) Recv'd block %d, tag %2d\n", iam, k, status.MPI_TAG);
#endif
switch ( status.MPI_TAG ) {
case Xk:
--nfrecvx;
lk = LBj( k, grid ); /* Local block number, column-wise. */
lsub = Lrowind_bc_ptr[lk];
lusup = Lnzval_bc_ptr[lk];
if ( lsub ) {
nb = lsub[0];
lptr = BC_HEADER;
luptr = 0;
knsupc = SuperSize( k );
/*
* Perform local block modifications: lsum[i] -= L_i,k * X[k]
*/
dlsum_fmod(lsum, x, &recvbuf[XK_H], rtemp, nrhs, knsupc, k,
fmod, nb, lptr, luptr, xsup, grid, Llu,
send_req, stat);
} /* if lsub */
break;
case LSUM:
--nfrecvmod;
lk = LBi( k, grid ); /* Local block number, row-wise. */
ii = X_BLK( lk );
knsupc = SuperSize( k );
tempv = &recvbuf[LSUM_H];
RHS_ITERATE(j)
for (i = 0; i < knsupc; ++i)
x[i + ii + j*knsupc] += tempv[i + j*knsupc];
if ( (--frecv[lk])==0 && fmod[lk]==0 ) {
fmod[lk] = -1; /* Do not solve X[k] in the future. */
lk = LBj( k, grid ); /* Local block number, column-wise. */
lsub = Lrowind_bc_ptr[lk];
lusup = Lnzval_bc_ptr[lk];
nsupr = lsub[1];
#ifdef _CRAY
STRSM(ftcs1, ftcs1, ftcs2, ftcs3, &knsupc, &nrhs, &alpha,
lusup, &nsupr, &x[ii], &knsupc);
#else
dtrsm_("L", "L", "N", "U", &knsupc, &nrhs, &alpha,
lusup, &nsupr, &x[ii], &knsupc);
#endif
stat->ops[SOLVE] += knsupc * (knsupc - 1) * nrhs;
#if ( DEBUGlevel>=2 )
printf("(%2d) Solve X[%2d]\n", iam, k);
#endif
/*
* Send Xk to process column Pc[k].
*/
kcol = PCOL( k, grid );
for (p = 0; p < Pr; ++p)
if ( fsendx_plist[lk][p] != EMPTY ) {
pi = PNUM( p, kcol, grid );
#ifdef ISEND_IRECV
#if 1
MPI_Test( &send_req[p], &test_flag, &status );
#else
if ( send_req[p] != MPI_REQUEST_NULL )
MPI_Wait( &send_req[p], &status );
#endif
MPI_Isend( &x[ii-XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm,
&send_req[p]);
#else
#ifdef BSEND
MPI_Bsend( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm );
#else
MPI_Send( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm );
#endif
#endif
#if ( DEBUGlevel>=2 )
printf("(%2d) Sent X[%2.0f] to P %2d\n",
iam, x[ii-XK_H], pi);
#endif
}
/*
* Perform local block modifications.
*/
nb = lsub[0] - 1;
lptr = BC_HEADER + LB_DESCRIPTOR + knsupc;
luptr = knsupc; /* Skip diagonal block L(k,k). */
dlsum_fmod(lsum, x, &x[ii], rtemp, nrhs, knsupc, k,
fmod, nb, lptr, luptr, xsup, grid, Llu,
send_req, stat);
#ifdef ISEND_IRECV
/* Wait for the previous Isends to complete. */
for (p = 0; p < Pr; ++p) {
if ( fsendx_plist[lk][p] != EMPTY )
MPI_Test( &send_req[p], &test_flag, &status );
}
#endif
} /* if */
break;
#if ( DEBUGlevel>=1 )
default:
printf("(%2d) Recv'd wrong message tag %4d\n", status.MPI_TAG);
break;
#endif
} /* switch */
} /* while not finished ... */
#if ( PRNTlevel>=2 )
t = SuperLU_timer_() - t;
if ( !iam ) printf(".. L-solve time\t%8.2f\n", t);
t = SuperLU_timer_();
#endif
#if ( PRNTlevel==2 )
if ( !iam ) printf("\n.. After L-solve: y =\n");
for (i = 0, k = 0; k < nsupers; ++k) {
krow = PROW( k, grid );
kcol = PCOL( k, grid );
if ( myrow == krow && mycol == kcol ) { /* Diagonal process */
knsupc = SuperSize( k );
lk = LBi( k, grid );
ii = X_BLK( lk );
for (j = 0; j < knsupc; ++j)
printf("\t(%d)\t%4d\t%.10f\n", iam, xsup[k]+j, x[ii+j]);
}
MPI_Barrier( grid->comm );
}
#endif
SUPERLU_FREE(fmod);
SUPERLU_FREE(frecv);
SUPERLU_FREE(rtemp);
/* MPI_Barrier( grid->comm ); Drain messages in the forward solve. */
/*---------------------------------------------------
* Back solve Ux = y.
*
* The Y components from the forward solve is already
* on the diagonal processes.
*---------------------------------------------------*/
/* Save the count to be altered so it can be used by
subsequent call to PDGSTRS_BGLOBAL. */
if ( !(bmod = intMalloc_dist(nlb)) )
ABORT("Calloc fails for bmod[].");
for (i = 0; i < nlb; ++i) bmod[i] = Llu->bmod[i];
if ( !(brecv = intMalloc_dist(nlb)) )
ABORT("Malloc fails for brecv[].");
Llu->brecv = brecv;
/*
* Compute brecv[] and nbrecvmod counts on the diagonal processes.
*/
{
superlu_scope_t *scp = &grid->rscp;
for (k = 0; k < nsupers; ++k) {
krow = PROW( k, grid );
if ( myrow == krow ) {
lk = LBi( k, grid ); /* Local block number. */
kcol = PCOL( k, grid ); /* Root process in this row scope. */
if ( mycol != kcol && bmod[lk] )
i = 1; /* Contribution from non-diagonal process. */
else i = 0;
MPI_Reduce( &i, &brecv[lk], 1, mpi_int_t,
MPI_SUM, kcol, scp->comm );
if ( mycol == kcol ) { /* Diagonal process. */
nbrecvmod += brecv[lk];
if ( !brecv[lk] && !bmod[lk] ) ++nroot;
#if ( DEBUGlevel>=2 )
printf("(%2d) brecv[%4d] %2d\n", iam, k, brecv[lk]);
assert( brecv[lk] < Pc );
#endif
}
}
}
}
/* Re-initialize lsum to zero. Each block header is already in place. */
for (k = 0; k < nsupers; ++k) {
krow = PROW( k, grid );
if ( myrow == krow ) {
knsupc = SuperSize( k );
lk = LBi( k, grid );
il = LSUM_BLK( lk );
dest = &lsum[il];
RHS_ITERATE(j)
for (i = 0; i < knsupc; ++i) dest[i + j*knsupc] = 0.0;
}
}
/* Set up additional pointers for the index and value arrays of U.
nlb is the number of local block rows. */
nub = CEILING( nsupers, Pc ); /* Number of local block columns. */
if ( !(Urbs = (int_t *) intCalloc_dist(2*nub)) )
ABORT("Malloc fails for Urbs[]"); /* Record number of nonzero
blocks in a block column. */
Urbs1 = Urbs + nub;
if ( !(Ucb_indptr = SUPERLU_MALLOC(nub * sizeof(Ucb_indptr_t *))) )
ABORT("Malloc fails for Ucb_indptr[]");
if ( !(Ucb_valptr = SUPERLU_MALLOC(nub * sizeof(int_t *))) )
ABORT("Malloc fails for Ucb_valptr[]");
/* Count number of row blocks in a block column.
One pass of the skeleton graph of U. */
for (lk = 0; lk < nlb; ++lk) {
usub = Ufstnz_br_ptr[lk];
if ( usub ) { /* Not an empty block row. */
/* usub[0] -- number of column blocks in this block row. */
#if ( DEBUGlevel>=2 )
Ublocks += usub[0];
#endif
i = BR_HEADER; /* Pointer in index array. */
for (lb = 0; lb < usub[0]; ++lb) { /* For all column blocks. */
k = usub[i]; /* Global block number */
++Urbs[LBj(k,grid)];
i += UB_DESCRIPTOR + SuperSize( k );
}
}
}
/* Set up the vertical linked lists for the row blocks.
One pass of the skeleton graph of U. */
for (lb = 0; lb < nub; ++lb)
if ( Urbs[lb] ) { /* Not an empty block column. */
if ( !(Ucb_indptr[lb]
= SUPERLU_MALLOC(Urbs[lb] * sizeof(Ucb_indptr_t))) )
ABORT("Malloc fails for Ucb_indptr[lb][]");
if ( !(Ucb_valptr[lb] = (int_t *) intMalloc_dist(Urbs[lb])) )
ABORT("Malloc fails for Ucb_valptr[lb][]");
}
for (lk = 0; lk < nlb; ++lk) { /* For each block row. */
usub = Ufstnz_br_ptr[lk];
if ( usub ) { /* Not an empty block row. */
i = BR_HEADER; /* Pointer in index array. */
j = 0; /* Pointer in nzval array. */
for (lb = 0; lb < usub[0]; ++lb) { /* For all column blocks. */
k = usub[i]; /* Global block number, column-wise. */
ljb = LBj( k, grid ); /* Local block number, column-wise. */
Ucb_indptr[ljb][Urbs1[ljb]].lbnum = lk;
Ucb_indptr[ljb][Urbs1[ljb]].indpos = i;
Ucb_valptr[ljb][Urbs1[ljb]] = j;
++Urbs1[ljb];
j += usub[i+1];
i += UB_DESCRIPTOR + SuperSize( k );
}
}
}
#if ( DEBUGlevel>=2 )
for (p = 0; p < Pr*Pc; ++p) {
if (iam == p) {
printf("(%2d) .. Ublocks %d\n", iam, Ublocks);
for (lb = 0; lb < nub; ++lb) {
printf("(%2d) Local col %2d: # row blocks %2d\n",
iam, lb, Urbs[lb]);
if ( Urbs[lb] ) {
for (i = 0; i < Urbs[lb]; ++i)
printf("(%2d) .. row blk %2d:\
lbnum %d, indpos %d, valpos %d\n",
iam, i,
Ucb_indptr[lb][i].lbnum,
Ucb_indptr[lb][i].indpos,
Ucb_valptr[lb][i]);
}
}
}
MPI_Barrier( grid->comm );
}
for (p = 0; p < Pr*Pc; ++p) {
if ( iam == p ) {
printf("\n(%d) bsendx_plist[][]", iam);
for (lb = 0; lb < nub; ++lb) {
printf("\n(%d) .. local col %2d: ", iam, lb);
for (i = 0; i < Pr; ++i)
printf("%4d", bsendx_plist[lb][i]);
}
printf("\n");
}
MPI_Barrier( grid->comm );
}
#endif /* DEBUGlevel */
#if ( PRNTlevel>=3 )
t = SuperLU_timer_() - t;
if ( !iam) printf(".. Setup U-solve time\t%8.2f\n", t);
t = SuperLU_timer_();
#endif
/*
* Solve the roots first by all the diagonal processes.
*/
#if ( DEBUGlevel>=1 )
printf("(%2d) nroot %4d\n", iam, nroot);
#endif
for (k = nsupers-1; k >= 0 && nroot; --k) {
krow = PROW( k, grid );
kcol = PCOL( k, grid );
if ( myrow == krow && mycol == kcol ) { /* Diagonal process. */
knsupc = SuperSize( k );
lk = LBi( k, grid ); /* Local block number, row-wise. */
if ( brecv[lk]==0 && bmod[lk]==0 ) {
bmod[lk] = -1; /* Do not solve X[k] in the future. */
ii = X_BLK( lk );
lk = LBj( k, grid ); /* Local block number, column-wise */
lsub = Lrowind_bc_ptr[lk];
lusup = Lnzval_bc_ptr[lk];
nsupr = lsub[1];
#ifdef _CRAY
STRSM(ftcs1, ftcs3, ftcs2, ftcs2, &knsupc, &nrhs, &alpha,
lusup, &nsupr, &x[ii], &knsupc);
#else
dtrsm_("L", "U", "N", "N", &knsupc, &nrhs, &alpha,
lusup, &nsupr, &x[ii], &knsupc);
#endif
stat->ops[SOLVE] += knsupc * (knsupc + 1) * nrhs;
--nroot;
#if ( DEBUGlevel>=2 )
printf("(%2d) Solve X[%2d]\n", iam, k);
#endif
/*
* Send Xk to process column Pc[k].
*/
for (p = 0; p < Pr; ++p)
if ( bsendx_plist[lk][p] != EMPTY ) {
pi = PNUM( p, kcol, grid );
#ifdef ISEND_IRECV
#if 1
MPI_Test( &send_req[p], &test_flag, &status );
#else
if ( send_req[p] != MPI_REQUEST_NULL )
MPI_Wait( &send_req[p], &status );
#endif
MPI_Isend( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm, &send_req[p]);
#else
#ifdef BSEND
MPI_Bsend( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm );
#else
MPI_Send( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm );
#endif
#endif
#if ( DEBUGlevel>=2 )
printf("(%2d) Sent X[%2.0f] to P %2d\n",
iam, x[ii-XK_H], pi);
#endif
}
/*
* Perform local block modifications: lsum[i] -= U_i,k * X[k]
*/
if ( Urbs[lk] )
dlsum_bmod(lsum, x, &x[ii], nrhs, k, bmod, Urbs,
Ucb_indptr, Ucb_valptr, xsup, grid, Llu,
send_req, stat);
#ifdef ISEND_IRECV
/* Wait for the previous Isends to complete. */
for (p = 0; p < Pr; ++p) {
if ( bsendx_plist[lk][p] != EMPTY )
/*MPI_Wait( &send_req[p], &status );*/
MPI_Test( &send_req[p], &test_flag, &status );
}
#endif
} /* if root ... */
} /* if diagonal process ... */
} /* for k ... */
/*
* Compute the internal nodes asynchronously by all processes.
*/
while ( nbrecvx || nbrecvmod ) { /* While not finished. */
/* Receive a message. */
MPI_Recv( recvbuf, maxrecvsz, MPI_DOUBLE, MPI_ANY_SOURCE,
MPI_ANY_TAG, grid->comm, &status );
k = *recvbuf;
#if ( DEBUGlevel>=2 )
printf("(%2d) Recv'd block %d, tag %2d\n", iam, k, status.MPI_TAG);
#endif
switch ( status.MPI_TAG ) {
case Xk:
--nbrecvx;
lk = LBj( k, grid ); /* Local block number, column-wise. */
/*
* Perform local block modifications:
* lsum[i] -= U_i,k * X[k]
*/
dlsum_bmod(lsum, x, &recvbuf[XK_H], nrhs, k, bmod, Urbs,
Ucb_indptr, Ucb_valptr, xsup, grid, Llu,
send_req, stat);
break;
case LSUM:
--nbrecvmod;
lk = LBi( k, grid ); /* Local block number, row-wise. */
ii = X_BLK( lk );
knsupc = SuperSize( k );
tempv = &recvbuf[LSUM_H];
RHS_ITERATE(j)
for (i = 0; i < knsupc; ++i)
x[i + ii + j*knsupc] += tempv[i + j*knsupc];
if ( (--brecv[lk])==0 && bmod[lk]==0 ) {
bmod[lk] = -1; /* Do not solve X[k] in the future. */
lk = LBj( k, grid ); /* Local block number, column-wise. */
lsub = Lrowind_bc_ptr[lk];
lusup = Lnzval_bc_ptr[lk];
nsupr = lsub[1];
#ifdef _CRAY
STRSM(ftcs1, ftcs3, ftcs2, ftcs2, &knsupc, &nrhs, &alpha,
lusup, &nsupr, &x[ii], &knsupc);
#else
dtrsm_("L", "U", "N", "N", &knsupc, &nrhs, &alpha,
lusup, &nsupr, &x[ii], &knsupc);
#endif
stat->ops[SOLVE] += knsupc * (knsupc + 1) * nrhs;
#if ( DEBUGlevel>=2 )
printf("(%2d) Solve X[%2d]\n", iam, k);
#endif
/*
* Send Xk to process column Pc[k].
*/
kcol = PCOL( k, grid );
for (p = 0; p < Pr; ++p)
if ( bsendx_plist[lk][p] != EMPTY ) {
pi = PNUM( p, kcol, grid );
#ifdef ISEND_IRECV
#if 1
MPI_Test( &send_req[p], &test_flag, &status );
#else
if ( send_req[p] != MPI_REQUEST_NULL )
MPI_Wait( &send_req[p], &status );
#endif
MPI_Isend( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm,
&send_req[p] );
#else
#ifdef BSEND
MPI_Bsend( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm );
#else
MPI_Send( &x[ii - XK_H], knsupc * nrhs + XK_H,
MPI_DOUBLE, pi, Xk, grid->comm );
#endif
#endif
#if ( DEBUGlevel>=2 )
printf("(%2d) Sent X[%2.0f] to P %2d\n",
iam, x[ii - XK_H], pi);
#endif
}
/*
* Perform local block modifications:
* lsum[i] -= U_i,k * X[k]
*/
if ( Urbs[lk] )
dlsum_bmod(lsum, x, &x[ii], nrhs, k, bmod, Urbs,
Ucb_indptr, Ucb_valptr, xsup, grid, Llu,
send_req, stat);
#ifdef ISEND_IRECV
/* Wait for the previous Isends to complete. */
for (p = 0; p < Pr; ++p) {
if ( bsendx_plist[lk][p] != EMPTY )
/*MPI_Wait( &send_req[p], &status );*/
MPI_Test( &send_req[p], &test_flag, &status );
}
#endif
} /* if becomes solvable */
break;
#if ( DEBUGlevel>=1 )
default:
printf("(%2d) Recv'd wrong message tag %4d\n", status.MPI_TAG);
break;
#endif
} /* switch */
} /* while not finished ... */
#if ( PRNTlevel>=3 )
t = SuperLU_timer_() - t;
if ( !iam ) printf(".. U-solve time\t%8.2f\n", t);
#endif
/* Copy the solution X into B (on all processes). */
{
int_t num_diag_procs, *diag_procs, *diag_len;
double *work;
get_diag_procs(n, Glu_persist, grid, &num_diag_procs,
&diag_procs, &diag_len);
jj = diag_len[0];
for (j = 1; j < num_diag_procs; ++j) jj = SUPERLU_MAX(jj, diag_len[j]);
if ( !(work = doubleMalloc_dist(jj*nrhs)) )
ABORT("Malloc fails for work[]");
gather_diag_to_all(n, nrhs, x, Glu_persist, Llu,
grid, num_diag_procs, diag_procs, diag_len,
B, ldb, work);
SUPERLU_FREE(diag_procs);
SUPERLU_FREE(diag_len);
SUPERLU_FREE(work);
}
/* Deallocate storage. */
SUPERLU_FREE(lsum);
SUPERLU_FREE(x);
SUPERLU_FREE(recvbuf);
for (i = 0; i < nub; ++i)
if ( Urbs[i] ) {
SUPERLU_FREE(Ucb_indptr[i]);
SUPERLU_FREE(Ucb_valptr[i]);
}
SUPERLU_FREE(Ucb_indptr);
SUPERLU_FREE(Ucb_valptr);
SUPERLU_FREE(Urbs);
SUPERLU_FREE(bmod);
SUPERLU_FREE(brecv);
#ifdef ISEND_IRECV
for (p = 0; p < Pr; ++p) {
if ( send_req[p] != MPI_REQUEST_NULL )
MPI_Wait( &send_req[p], &status );
}
SUPERLU_FREE(send_req);
#endif
stat->utime[SOLVE] = SuperLU_timer_() - t;
#if ( DEBUGlevel>=1 )
CHECK_MALLOC(iam, "Exit pdgstrs_Bglobal()");
#endif
/* Chao debug */
MPI_Barrier( grid->comm ); /* Drain messages in the forward solve. */
} /* PDGSTRS_BGLOBAL */
/*! \brief
*
* <pre>
* Gather the components of x vector on the diagonal processes
* onto all processes, and combine them into the global vector y.
* </pre>
*/
static void
gather_diag_to_all(int_t n, int_t nrhs, double x[],
Glu_persist_t *Glu_persist, LocalLU_t *Llu,
gridinfo_t *grid, int_t num_diag_procs,
int_t diag_procs[], int_t diag_len[],
double y[], int_t ldy, double work[])
{
int_t i, ii, j, k, lk, lwork, nsupers, p;
int_t *ilsum, *xsup;
int iam, knsupc, pkk;
double *x_col, *y_col;
iam = grid->iam;
nsupers = Glu_persist->supno[n-1] + 1;
xsup = Glu_persist->xsup;
ilsum = Llu->ilsum;
for (p = 0; p < num_diag_procs; ++p) {
pkk = diag_procs[p];
if ( iam == pkk ) {
/* Copy x vector into a buffer. */
lwork = 0;
for (k = p; k < nsupers; k += num_diag_procs) {
knsupc = SuperSize( k );
lk = LBi( k, grid );
ii = X_BLK( lk ); /*ilsum[lk] + (lk+1)*XK_H;*/
x_col = &x[ii];
for (j = 0; j < nrhs; ++j) {
for (i = 0; i < knsupc; ++i) work[i+lwork] = x_col[i];
lwork += knsupc;
x_col += knsupc;
}
}
MPI_Bcast( work, lwork, MPI_DOUBLE, pkk, grid->comm );