forked from xiaoyeli/superlu_dist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpzgstrf.c
2008 lines (1783 loc) · 73.9 KB
/
pzgstrf.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 Performs LU factorization in parallel
*
* <pre>
* -- Distributed SuperLU routine (version 6.1) --
* Lawrence Berkeley National Lab, Univ. of California Berkeley.
* October 1, 2014
*
* Modified:
* September 1, 1999
* Feburary 7, 2001 use MPI_Isend/MPI_Irecv
* October 15, 2008 latency-reducing panel factorization
* July 12, 2011 static scheduling and arbitrary look-ahead
* March 13, 2013 change NTAGS to MPI_TAG_UB value
* September 24, 2015 replace xLAMCH by xMACH, using C99 standard.
* December 31, 2015 rename xMACH to xMACH_DIST.
* September 30, 2017 optimization for Intel Knights Landing (KNL) node .
* June 1, 2018 add parallel AWPM pivoting; add back arrive_at_ublock()
* February 8, 2019 version 6.1.1
*
* Sketch of the algorithm
*
* =======================
*
* The following relations hold:
* * A_kk = L_kk * U_kk
* * L_ik = Aik * U_kk^(-1)
* * U_kj = L_kk^(-1) * A_kj
*
* ----------------------------------
* | | |
* ----|-----------------------------
* | | \ U_kk| |
* | | \ | U_kj |
* | |L_kk \ | || |
* ----|-------|---------||----------
* | | | \/ |
* | | | |
* | | | |
* | | | |
* | | L_ik ==> A_ij |
* | | | |
* | | | |
* | | | |
* ----------------------------------
*
* Handle the first block of columns separately.
* * Factor diagonal and subdiagonal blocks and test for exact
* singularity. ( pzgstrf2(0), one column at a time )
* * Compute block row of U
* * Update trailing matrix
*
* Loop over the remaining blocks of columns.
* mycol = MYCOL( iam, grid );
* myrow = MYROW( iam, grid );
* N = nsupers;
* For (k = 1; k < N; ++k) {
* krow = PROW( k, grid );
* kcol = PCOL( k, grid );
* Pkk = PNUM( krow, kcol, grid );
*
* * Factor diagonal and subdiagonal blocks and test for exact
* singularity.
* if ( mycol == kcol ) {
* pzgstrf2(k), one column at a time
* }
*
* * Parallel triangular solve
* if ( iam == Pkk ) multicast L_k,k to this process row;
* if ( myrow == krow && mycol != kcol ) {
* Recv L_k,k from process Pkk;
* for (j = k+1; j < N; ++j)
* if ( PCOL( j, grid ) == mycol && A_k,j != 0 )
* U_k,j = L_k,k \ A_k,j;
* }
*
* * Parallel rank-k update
* if ( myrow == krow ) multicast U_k,k+1:N to this process column;
* if ( mycol == kcol ) multicast L_k+1:N,k to this process row;
* if ( myrow != krow ) {
* Pkj = PNUM( krow, mycol, grid );
* Recv U_k,k+1:N from process Pkj;
* }
* if ( mycol != kcol ) {
* Pik = PNUM( myrow, kcol, grid );
* Recv L_k+1:N,k from process Pik;
* }
* for (j = k+1; k < N; ++k) {
* for (i = k+1; i < N; ++i)
* if ( myrow == PROW( i, grid ) && mycol == PCOL( j, grid )
* && L_i,k != 0 && U_k,j != 0 )
* A_i,j = A_i,j - L_i,k * U_k,j;
* }
* }
*
* </pre>
*/
#include <math.h>
/*#include "mkl.h"*/
#include "superlu_zdefs.h"
#ifdef GPU_ACC
#include "cublas_utils.h"
/*#include "cublas_zgemm.h"*/
// #define NUM_CUDA_STREAMS 16
// #define NUM_CUDA_STREAMS 16
#endif
/* Various defininations */
/*
Name : SUPERNODE_PROFILE
Purpose : For SuperNode Level profiling of various measurements such as gigaflop/sec
obtained,bandwidth achieved:
Overhead : Low
*/
// #define SUPERNODE_PROFILE
/*
Name : BAELINE
Purpose : baseline to compare performance against
Overhead : NA : this won't be used for running experiments
*/
// #define BASELINE
/*
Name : PHI_FRAMEWORK
Purpose : To simulate and test algorithm used for offloading Phi
Overhead : NA : this won't be used for running experiments
*/
#define PHI_FRAMEWORK
#if 0
#define CACHELINE 64 /* bytes, Xeon Phi KNL */
#else
#define CACHELINE 0 /* not worry about false sharing of different threads */
#endif
//#define GEMM_PADLEN 1
#define GEMM_PADLEN 8
#define PZGSTRF2 pzgstrf2_trsm
#define PZGSTRS2 pzgstrs2_omp
extern void PZGSTRF2 (superlu_dist_options_t *, int_t, int_t, double,
Glu_persist_t *, gridinfo_t *, LocalLU_t *,
MPI_Request *, int, SuperLUStat_t *, int *);
#ifdef _CRAY
extern void PZGSTRS2 (int_t, int_t, Glu_persist_t *, gridinfo_t *,
LocalLU_t *, SuperLUStat_t *, _fcd, _fcd, _fcd);
#else
extern void PZGSTRS2 (int_t, int_t, Glu_persist_t *, gridinfo_t *,
LocalLU_t *, SuperLUStat_t *);
#endif
#ifdef ISORT
extern void isort (int_t N, int_t * ARRAY1, int_t * ARRAY2);
extern void isort1 (int_t N, int_t * ARRAY);
#else
int
superlu_sort_perm (const void *arg1, const void *arg2)
{
const int_t *val1 = (const int_t *) arg1;
const int_t *val2 = (const int_t *) arg2;
return (*val2 < *val1);
}
#endif
/************************************************************************/
#include "zscatter.c"
/************************************************************************/
/*! \brief
*
* <pre>
* Purpose
* =======
*
* PZGSTRF performs the LU factorization in parallel.
*
* Arguments
* =========
*
* options (input) superlu_dist_options_t*
* The structure defines the input parameters to control
* how the LU decomposition will be performed.
* The following field should be defined:
* o ReplaceTinyPivot (yes_no_t)
* Specifies whether to replace the tiny diagonals by
* sqrt(epsilon)*norm(A) during LU factorization.
*
* m (input) int
* Number of rows in the matrix.
*
* n (input) int
* Number of columns in the matrix.
*
* anorm (input) double
* The norm of the original matrix A, or the scaled A if
* equilibration was done.
*
* LUstruct (input/output) LUstruct_t*
* The data structures to store the distributed L and U factors.
* The following fields should be defined:
*
* o Glu_persist (input) Glu_persist_t*
* Global data structure (xsup, supno) replicated on all processes,
* describing the supernode partition in the factored matrices
* L and U:
* xsup[s] is the leading column of the s-th supernode,
* supno[i] is the supernode number to which column i belongs.
*
* o Llu (input/output) LocalLU_t*
* The distributed data structures to store L and U factors.
* See superlu_ddefs.h for the definition of 'LocalLU_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'.
*
* stat (output) SuperLUStat_t*
* Record the statistics on runtime and floating-point operation count.
* 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
* > 0: if info = i, U(i,i) is exactly zero. The factorization has
* been completed, but the factor U is exactly singular,
* and division by zero will occur if it is used to solve a
* system of equations.
* </pre>
*/
int_t
pzgstrf(superlu_dist_options_t * options, int m, int n, double anorm,
LUstruct_t * LUstruct, gridinfo_t * grid, SuperLUStat_t * stat, int *info)
{
#ifdef _CRAY
_fcd ftcs = _cptofcd ("N", strlen ("N"));
_fcd ftcs1 = _cptofcd ("L", strlen ("L"));
_fcd ftcs2 = _cptofcd ("N", strlen ("N"));
_fcd ftcs3 = _cptofcd ("U", strlen ("U"));
#endif
doublecomplex zero = {0.0, 0.0};
doublecomplex alpha = {1.0, 0.0}, beta = {0.0, 0.0};
int_t *xsup;
int_t *lsub, *lsub1, *usub, *Usub_buf;
int_t **Lsub_buf_2, **Usub_buf_2;
doublecomplex **Lval_buf_2, **Uval_buf_2; /* pointers to starts of bufs */
doublecomplex *lusup, *lusup1, *uval, *Uval_buf; /* pointer to current buf */
int_t fnz, i, ib, ijb, ilst, it, iukp, jb, jj, klst, knsupc,
lb, lib, ldv, ljb, lptr, lptr0, lptrj, luptr, luptr0, luptrj,
nlb, nub, nsupc, rel, rukp, il, iu;
int_t Pc, Pr;
int iam, kcol, krow, yourcol, mycol, myrow, pi, pj;
int j, k, lk, nsupers; /* k - current panel to work on */
int k0; /* counter of the next supernode to be factored */
int kk, kk0, kk1, kk2, jj0; /* panels in the look-ahead window */
int iukp0, rukp0, flag0, flag1;
int nsupr, nbrow, segsize;
int msg0, msg2;
int_t **Ufstnz_br_ptr, **Lrowind_bc_ptr;
doublecomplex **Unzval_br_ptr, **Lnzval_bc_ptr;
int_t *index;
doublecomplex *nzval;
doublecomplex *ucol;
int *indirect, *indirect2;
int_t *tempi;
doublecomplex *tempu, *tempv, *tempr;
/* doublecomplex *tempv2d, *tempU2d; Sherry */
int iinfo;
int *ToRecv, *ToSendD, **ToSendR;
Glu_persist_t *Glu_persist = LUstruct->Glu_persist;
LocalLU_t *Llu = LUstruct->Llu;
superlu_scope_t *scp;
float s_eps;
double thresh;
/*int full;*/
int ldt, ldu, lead_zero, ncols, ncb, nrb, p, pr, pc, nblocks;
int_t *etree_supno_l, *etree_supno, *blocks, *blockr, *Ublock, *Urows,
*Lblock, *Lrows, *perm_u, *sf_block, *sf_block_l, *nnodes_l,
*nnodes_u, *edag_supno_l, *recvbuf, **edag_supno;
float edag_supno_l_bytes;
#ifdef ISORT
int_t *iperm_u;
#endif
int *msgcnt; /* Count the size of the message xfer'd in each buffer:
* 0 : transferred in Lsub_buf[]
* 1 : transferred in Lval_buf[]
* 2 : transferred in Usub_buf[]
* 3 : transferred in Uval_buf[]
*/
int **msgcnts, **msgcntsU; /* counts in the look-ahead window */
int *factored; /* factored[j] == 0 : L col panel j is factorized. */
int *factoredU; /* factoredU[i] == 1 : U row panel i is factorized. */
int nnodes, *sendcnts, *sdispls, *recvcnts, *rdispls, *srows, *rrows;
etree_node *head, *tail, *ptr;
int *num_child;
int num_look_aheads, look_id;
int *look_ahead; /* global look_ahead table */
int_t *perm_c_supno, *iperm_c_supno;
/* perm_c_supno[k] = j means at the k-th step of elimination,
* the j-th supernode is chosen. */
MPI_Request *recv_req, **recv_reqs, **send_reqs, **send_reqs_u,
**recv_reqs_u;
MPI_Request *send_req, *U_diag_blk_send_req = NULL;
MPI_Status status;
void *attr_val;
int flag;
/* The following variables are used to pad GEMM dimensions so that
each is a multiple of vector length (8 doubles for KNL) */
int gemm_m_pad = GEMM_PADLEN, gemm_k_pad = GEMM_PADLEN,
gemm_n_pad = GEMM_PADLEN;
int gemm_padding = 0;
int iword = sizeof (int_t);
int dword = sizeof (doublecomplex);
/* For measuring load imbalence in omp threads */
double omp_load_imblc = 0.0;
double *omp_loop_time;
double schur_flop_timer = 0.0;
double pdgstrf2_timer = 0.0;
double pdgstrs2_timer = 0.0;
double lookaheadupdatetimer = 0.0;
double InitTimer = 0.0; /* including compute schedule, malloc */
double tt_start, tt_end;
/* #if !defined( GPU_ACC ) */
/* Counters for memory operations and timings */
double scatter_mem_op_counter = 0.0;
double scatter_mem_op_timer = 0.0;
double scatterL_mem_op_counter = 0.0;
double scatterL_mem_op_timer = 0.0;
double scatterU_mem_op_counter = 0.0;
double scatterU_mem_op_timer = 0.0;
/* Counters for flops/gather/scatter and timings */
double GatherLTimer = 0.0;
double LookAheadRowSepMOP = 0.0;
double GatherUTimer = 0.0;
double GatherMOP = 0.0;
double LookAheadGEMMTimer = 0.0;
double LookAheadGEMMFlOp = 0.0;
double LookAheadScatterTimer = 0.0;
double LookAheadScatterMOP = 0.0;
double RemainGEMMTimer = 0.0;
double RemainGEMM_flops = 0.0;
double RemainScatterTimer = 0.0;
double NetSchurUpTimer = 0.0;
double schur_flop_counter = 0.0;
/* #endif */
#if ( PRNTlevel>= 1)
/* count GEMM max dimensions */
int gemm_max_m = 0, gemm_max_n = 0, gemm_max_k = 0;
#endif
#if ( DEBUGlevel>=2 )
int_t num_copy = 0, num_update = 0;
#endif
#if ( PRNTlevel==3 )
int zero_msg = 0, total_msg = 0;
#endif
#if ( PROFlevel>=1 )
double t1, t2;
float msg_vol = 0, msg_cnt = 0;
double comm_wait_time = 0.0;
/* Record GEMM dimensions and times */
FILE *fopen(), *fgemm;
int gemm_count = 0;
typedef struct {
int m, n, k;
double microseconds;
} gemm_profile;
gemm_profile *gemm_stats;
#endif
/* Test the input parameters. */
*info = 0;
if (m < 0)
*info = -2;
else if (n < 0)
*info = -3;
if (*info) {
pxerr_dist ("pzgstrf", grid, -*info);
return (-1);
}
/* Quick return if possible. */
if (m == 0 || n == 0) return 0;
double tt1 = SuperLU_timer_ ();
/*
* Initialization.
*/
iam = grid->iam;
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;
s_eps = smach_dist("Epsilon");
thresh = s_eps * anorm;
MPI_Comm_get_attr (MPI_COMM_WORLD, MPI_TAG_UB, &attr_val, &flag);
if (!flag) {
fprintf (stderr, "Could not get TAG_UB\n");
return (-1);
}
int tag_ub = *(int *) attr_val;
#if ( PRNTlevel>=1 )
if (!iam) {
printf ("MPI tag upper bound = %d\n", tag_ub); fflush(stdout);
}
#endif
#if ( DEBUGlevel>=1 )
if (s_eps == 0.0)
printf (" ***** warning s_eps = %e *****\n", s_eps);
CHECK_MALLOC (iam, "Enter pdgstrf()");
#endif
#if (PROFlevel >= 1 )
gemm_stats = (gemm_profile *) SUPERLU_MALLOC(nsupers * sizeof(gemm_profile));
if (iam == 0) fgemm = fopen("dgemm_mnk.dat", "w");
int *prof_sendR = intCalloc_dist(nsupers);
#endif
stat->ops[FACT] = 0.0;
stat->current_buffer = 0.0;
stat->peak_buffer = 0.0;
stat->gpu_buffer = 0.0;
/* make sure the range of look-ahead window [0, MAX_LOOKAHEADS-1] */
num_look_aheads = SUPERLU_MAX(0, SUPERLU_MIN(options->num_lookaheads, MAX_LOOKAHEADS - 1));
if (Pr * Pc > 1) {
if (!(U_diag_blk_send_req =
(MPI_Request *) SUPERLU_MALLOC (Pr * sizeof (MPI_Request))))
ABORT ("Malloc fails for U_diag_blk_send_req[].");
/* flag no outstanding Isend */
U_diag_blk_send_req[myrow] = MPI_REQUEST_NULL; /* used 0 before */
/* allocating buffers for look-ahead */
i = Llu->bufmax[0];
if (i != 0) {
if ( !(Llu->Lsub_buf_2[0] = intMalloc_dist ((num_look_aheads + 1) * ((size_t) i))) )
ABORT ("Malloc fails for Lsub_buf.");
tempi = Llu->Lsub_buf_2[0];
for (jj = 0; jj < num_look_aheads; jj++)
Llu->Lsub_buf_2[jj+1] = tempi + i*(jj+1); /* vectorize */
//Llu->Lsub_buf_2[jj + 1] = Llu->Lsub_buf_2[jj] + i;
}
i = Llu->bufmax[1];
if (i != 0) {
if (!(Llu->Lval_buf_2[0] = doublecomplexMalloc_dist ((num_look_aheads + 1) * ((size_t) i))))
ABORT ("Malloc fails for Lval_buf[].");
tempr = Llu->Lval_buf_2[0];
for (jj = 0; jj < num_look_aheads; jj++)
Llu->Lval_buf_2[jj+1] = tempr + i*(jj+1); /* vectorize */
//Llu->Lval_buf_2[jj + 1] = Llu->Lval_buf_2[jj] + i;
}
i = Llu->bufmax[2];
if (i != 0) {
if (!(Llu->Usub_buf_2[0] = intMalloc_dist ((num_look_aheads + 1) * i)))
ABORT ("Malloc fails for Usub_buf_2[].");
tempi = Llu->Usub_buf_2[0];
for (jj = 0; jj < num_look_aheads; jj++)
Llu->Usub_buf_2[jj+1] = tempi + i*(jj+1); /* vectorize */
//Llu->Usub_buf_2[jj + 1] = Llu->Usub_buf_2[jj] + i;
}
i = Llu->bufmax[3];
if (i != 0) {
if (!(Llu->Uval_buf_2[0] = doublecomplexMalloc_dist ((num_look_aheads + 1) * i)))
ABORT ("Malloc fails for Uval_buf_2[].");
tempr = Llu->Uval_buf_2[0];
for (jj = 0; jj < num_look_aheads; jj++)
Llu->Uval_buf_2[jj+1] = tempr + i*(jj+1); /* vectorize */
//Llu->Uval_buf_2[jj + 1] = Llu->Uval_buf_2[jj] + i;
}
}
log_memory( (Llu->bufmax[0] + Llu->bufmax[2]) * (num_look_aheads + 1)
* iword +
(Llu->bufmax[1] + Llu->bufmax[3]) * (num_look_aheads + 1)
* dword, stat );
/* creating pointers to the look-ahead buffers */
if (! (Lsub_buf_2 = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (int_t *))))
ABORT ("Malloc fails for Lsub_buf_2[].");
if (! (Lval_buf_2 = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (doublecomplex *))))
ABORT ("Malloc fails for Lval_buf_2[].");
if (! (Usub_buf_2 = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (int_t *))))
ABORT ("Malloc fails for Uval_buf_2[].");
if (! (Uval_buf_2 = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (doublecomplex *))))
ABORT ("Malloc fails for buf_2[].");
for (i = 0; i <= num_look_aheads; i++) {
Lval_buf_2[i] = Llu->Lval_buf_2[i];
Lsub_buf_2[i] = Llu->Lsub_buf_2[i];
Uval_buf_2[i] = Llu->Uval_buf_2[i];
Usub_buf_2[i] = Llu->Usub_buf_2[i];
}
if (!(msgcnts = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (int *))))
ABORT ("Malloc fails for msgcnts[].");
if (!(msgcntsU = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (int *))))
ABORT ("Malloc fails for msgcntsU[].");
for (i = 0; i <= num_look_aheads; i++) {
if (!(msgcnts[i] = SUPERLU_MALLOC (4 * sizeof (int))))
ABORT ("Malloc fails for msgcnts[].");
if (!(msgcntsU[i] = SUPERLU_MALLOC (4 * sizeof (int))))
ABORT ("Malloc fails for msgcntsU[].");
}
if (! (recv_reqs_u = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (MPI_Request *))))
ABORT ("Malloc fails for recv_reqs_u[].");
if (! (send_reqs_u = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (MPI_Request *))))
ABORT ("Malloc fails for send_reqs_u[].");
if (! (send_reqs = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (MPI_Request *))))
ABORT ("Malloc fails for send_reqs_u[].");
if (! (recv_reqs = SUPERLU_MALLOC ((1 + num_look_aheads) * sizeof (MPI_Request *))))
ABORT ("Malloc fails for recv_reqs[].");
for (i = 0; i <= num_look_aheads; i++) {
if (!(recv_reqs_u[i] = (MPI_Request *) SUPERLU_MALLOC (2 * sizeof (MPI_Request))))
ABORT ("Malloc fails for recv_req_u[i].");
if (!(send_reqs_u[i] = (MPI_Request *) SUPERLU_MALLOC (2 * Pr * sizeof (MPI_Request))))
ABORT ("Malloc fails for send_req_u[i].");
if (!(send_reqs[i] = (MPI_Request *) SUPERLU_MALLOC (2 * Pc * sizeof (MPI_Request))))
ABORT ("Malloc fails for send_reqs[i].");
if (!(recv_reqs[i] = (MPI_Request *) SUPERLU_MALLOC (4 * sizeof (MPI_Request))))
ABORT ("Malloc fails for recv_req[].");
send_reqs[i][0] = send_reqs[i][1] = MPI_REQUEST_NULL;
recv_reqs[i][0] = recv_reqs[i][1] = MPI_REQUEST_NULL;
}
if (!(factored = SUPERLU_MALLOC (nsupers * sizeof (int_t))))
ABORT ("Malloc fails for factored[].");
if (!(factoredU = SUPERLU_MALLOC (nsupers * sizeof (int_t))))
ABORT ("Malloc fails for factoredU[].");
for (i = 0; i < nsupers; i++) factored[i] = factoredU[i] = -1;
log_memory(2 * nsupers * iword, stat);
int num_threads = 1;
#ifdef _OPENMP
#pragma omp parallel default(shared)
#pragma omp master
{
//if (omp_get_thread_num () == 0)
num_threads = omp_get_num_threads ();
}
#endif
#if 0
omp_loop_time = (double *) _mm_malloc (sizeof (double) * num_threads,64);
#else
omp_loop_time = (double *) doubleMalloc_dist(num_threads);
#endif
#if ( PRNTlevel>=1 )
if(!iam) {
printf(".. Starting with %d OpenMP threads \n", num_threads );
fflush(stdout);
}
#endif
nblocks = 0;
ncb = nsupers / Pc; /* number of column blocks, horizontal */
nrb = nsupers / Pr; /* number of row blocks, vertical */
/* in order to have dynamic scheduling */
int *full_u_cols;
int *blk_ldu;
#if 0
full_u_cols = (int_t *) _mm_malloc (sizeof (int_t) * ncb,64);
blk_ldu = (int_t *) _mm_malloc (sizeof (int_t) * ncb,64);
#else
full_u_cols = SUPERLU_MALLOC(ncb * sizeof(int));
blk_ldu = SUPERLU_MALLOC(ncb * sizeof(int));
#endif
log_memory(2 * ncb * iword, stat);
#if 0 /* Sherry: not used? */
/* This bunch is used for static scheduling */
pair *full_col_count = (pair *) _mm_malloc (sizeof (pair) * ncb,64);
int_t *count_cols, *sum_cols, *partition;
count_cols = (int_t *) _mm_malloc (sizeof (int_t) * num_threads,64);
sum_cols = (int_t *) _mm_malloc (sizeof (int_t) * num_threads,64);
partition = (int_t *) _mm_malloc (sizeof (int_t) * num_threads * ncb,64);
int_t ldp = ncb;
#endif
/* ##################################################################
* Compute a good static schedule based on the factorization task graph.
* ################################################################## */
perm_c_supno = SUPERLU_MALLOC (2 * nsupers * sizeof (int_t));
iperm_c_supno = perm_c_supno + nsupers;
static_schedule(options, m, n, LUstruct, grid, stat,
perm_c_supno, iperm_c_supno, info);
#if ( DEBUGlevel >= 2 )
PrintInt10("schedule:perm_c_supno", nsupers, perm_c_supno);
/* Turn off static schedule */
printf("[%d] .. Turn off static schedule for debugging ..\n", iam);
for (i = 0; i < nsupers; ++i) perm_c_supno[i] = iperm_c_supno[i] = i;
#endif
/* ################################################################## */
/* constructing look-ahead table to indicate the last dependency */
int *look_ahead_l; /* Sherry: add comment on look_ahead_l[] */
stat->num_look_aheads = num_look_aheads;
look_ahead_l = SUPERLU_MALLOC (nsupers * sizeof (int));
look_ahead = SUPERLU_MALLOC (nsupers * sizeof (int));
for (lb = 0; lb < nsupers; lb++) look_ahead_l[lb] = -1; /* vectorized */
log_memory(3 * nsupers * iword, stat);
/* Sherry: omp parallel?
not worth doing, due to concurrent write to look_ahead_l[jb] */
for (lb = 0; lb < nrb; ++lb) { /* go through U-factor */
ib = lb * Pr + myrow;
index = Llu->Ufstnz_br_ptr[lb];
if (index) { /* Not an empty row */
k = BR_HEADER;
for (j = 0; j < index[0]; ++j) {
jb = index[k]; /* global block number */
if (jb != ib)
look_ahead_l[jb] =
SUPERLU_MAX (iperm_c_supno[ib], look_ahead_l[jb]);
k += UB_DESCRIPTOR + SuperSize (index[k]);
}
}
}
if (myrow < nsupers % grid->nprow) { /* leftover block rows */
ib = nrb * Pr + myrow;
index = Llu->Ufstnz_br_ptr[nrb];
if (index) { /* Not an empty row */
k = BR_HEADER;
for (j = 0; j < index[0]; ++j) {
jb = index[k];
if (jb != ib)
look_ahead_l[jb] =
SUPERLU_MAX (iperm_c_supno[ib], look_ahead_l[jb]);
k += UB_DESCRIPTOR + SuperSize (index[k]);
}
}
}
if (options->SymPattern == NO) {
/* Sherry: omp parallel?
not worth doing, due to concurrent write to look_ahead_l[jb] */
for (lb = 0; lb < ncb; lb++) { /* go through L-factor */
ib = lb * Pc + mycol;
index = Llu->Lrowind_bc_ptr[lb];
if (index) {
k = BC_HEADER;
for (j = 0; j < index[0]; j++) {
jb = index[k];
if (jb != ib)
look_ahead_l[jb] =
SUPERLU_MAX (iperm_c_supno[ib], look_ahead_l[jb]);
k += LB_DESCRIPTOR + index[k + 1];
}
}
}
if (mycol < nsupers % grid->npcol) { /* leftover block columns */
ib = ncb * Pc + mycol;
index = Llu->Lrowind_bc_ptr[ncb];
if (index) {
k = BC_HEADER;
for (j = 0; j < index[0]; j++) {
jb = index[k];
if (jb != ib)
look_ahead_l[jb] =
SUPERLU_MAX (iperm_c_supno[ib], look_ahead_l[jb]);
k += LB_DESCRIPTOR + index[k + 1];
}
}
}
}
MPI_Allreduce (look_ahead_l, look_ahead, nsupers, MPI_INT, MPI_MAX, grid->comm);
SUPERLU_FREE (look_ahead_l);
#ifdef ISORT
iperm_u = SUPERLU_MALLOC (nsupers * sizeof (int_t));
perm_u = SUPERLU_MALLOC (nsupers * sizeof (int_t));
#else
perm_u = SUPERLU_MALLOC (2 * nsupers * sizeof (int_t));
#endif
log_memory(nsupers * iword, stat);
k = sp_ienv_dist (3); /* max supernode size */
#if 0
if ( !(Llu->ujrow = doubleMalloc_dist(k*(k+1)/2)) )
ABORT("Malloc fails for ujrow[].");
#else
/* Instead of half storage, we'll do full storage */
if (!(Llu->ujrow = doublecomplexCalloc_dist (k * k)))
ABORT ("Malloc fails for ujrow[].");
#endif
log_memory(k * k * iword, stat);
#if ( PRNTlevel>=1 )
if (!iam) {
printf (".. thresh = s_eps %e * anorm %e = %e\n", s_eps, anorm,
thresh);
printf
(".. Buffer size: Lsub %ld\tLval %ld\tUsub %ld\tUval %ld\tLDA %ld\n",
(long int) Llu->bufmax[0], (long int) Llu->bufmax[1],
(long int) Llu->bufmax[2], (long int) Llu->bufmax[3],
(long int) Llu->bufmax[4]);
fflush(stdout);
}
#endif
Lrowind_bc_ptr = Llu->Lrowind_bc_ptr;
Lnzval_bc_ptr = Llu->Lnzval_bc_ptr;
Ufstnz_br_ptr = Llu->Ufstnz_br_ptr;
Unzval_br_ptr = Llu->Unzval_br_ptr;
ToRecv = Llu->ToRecv;
ToSendD = Llu->ToSendD;
ToSendR = Llu->ToSendR;
ldt = sp_ienv_dist (3); /* Size of maximum supernode */
k = CEILING (nsupers, Pr); /* Number of local block rows */
/* Following code is for finding maximum row dimension of all L panels */
int local_max_row_size = 0;
int max_row_size;
#if 0
#if defined _OPENMP // Sherry: parallel reduction -- seems slower?
#pragma omp parallel for reduction(max :local_max_row_size) private(lk,lsub)
#endif
#endif
for (int i = mycol; i < nsupers; i += Pc) { /* grab my local columns */
//int tpc = PCOL (i, grid);
lk = LBj (i, grid);
lsub = Lrowind_bc_ptr[lk];
if (lsub != NULL) {
if (lsub[1] > local_max_row_size) local_max_row_size = lsub[1];
}
}
/* Max row size is global reduction within a row */
MPI_Allreduce (&local_max_row_size, &max_row_size, 1, MPI_INT, MPI_MAX,
(grid->rscp.comm));
/* Buffer size is max of look-ahead window */
/* int_t buffer_size =
SUPERLU_MAX (max_row_size * num_threads * ldt,
get_max_buffer_size ()); */
#ifdef GPU_ACC
int cublas_nb = get_cublas_nb();
int nstreams = get_num_cuda_streams ();
int buffer_size = SUPERLU_MAX(max_row_size*nstreams*cublas_nb,get_max_buffer_size());
/* array holding last column blk for each partition,
used in SchCompUdt--CUDA.c */
#if 0
int *stream_end_col = (int_t *) _mm_malloc (sizeof (int_t) * nstreams,64);
#else
int *stream_end_col = SUPERLU_MALLOC( nstreams * sizeof(int) );
#endif
#else /* not to use GPU */
int Threads_per_process = get_thread_per_process();
int buffer_size = SUPERLU_MAX(max_row_size*Threads_per_process*ldt,get_max_buffer_size());
#endif /* end ifdef GPU_ACC */
int_t max_ncols = 0;
#if 0
/* symmetric assumption -- using L's supernode to estimate. */
/* Note that in following expression 8 can be anything
as long as its not too big */
int bigu_size = 8 * sp_ienv_dist (3) * (max_row_size);
#else
int_t bigu_size = estimate_bigu_size( nsupers, Ufstnz_br_ptr, Glu_persist,
grid, perm_u, &max_ncols );
#endif
/* +16 to avoid cache line false sharing */
// int_t bigv_size = SUPERLU_MAX(max_row_size * (bigu_size / ldt),
int_t bigv_size = SUPERLU_MAX(max_row_size * max_ncols,
(ldt*ldt + CACHELINE / dword) * num_threads);
/* bigU and bigV are either on CPU or on GPU, not both. */
doublecomplex* bigU; /* for storing entire U(k,:) panel, prepare for GEMM.
bigU has the same size either on CPU or on CPU. */
doublecomplex* bigV; /* for storing GEMM output matrix, i.e. update matrix.
bigV is large to hold the aggregate GEMM output.*/
bigU = NULL;
bigV = NULL;
#if ( PRNTlevel>=1 )
if(!iam) {
printf("\t.. GEMM buffer size: max_row_size X max_ncols = %d x " IFMT "\n",
max_row_size, max_ncols);
printf(".. BIG U size " IFMT "\t BIG V size " IFMT "\n", bigu_size, bigv_size);
fflush(stdout);
}
#endif
#ifdef GPU_ACC
if ( checkCuda(cudaHostAlloc((void**)&bigU, bigu_size * sizeof(doublecomplex), cudaHostAllocDefault)) )
ABORT("Malloc fails for zgemm buffer U ");
bigv_size = buffer_size;
#if ( PRNTlevel>=1 )
if (!iam) printf("[%d] .. BIG V bigv_size %d, using buffer_size %d (on GPU)\n", iam, bigv_size, buffer_size);
#endif
if ( checkCuda(cudaHostAlloc((void**)&bigV, bigv_size * sizeof(doublecomplex) ,cudaHostAllocDefault)) )
ABORT("Malloc fails for zgemm buffer V");
DisplayHeader();
#if ( PRNTlevel>=1 )
printf(" Starting with %d Cuda Streams \n",nstreams );
#endif
cublasHandle_t *handle;
handle = (cublasHandle_t *) SUPERLU_MALLOC(sizeof(cublasHandle_t)*nstreams);
for(int i = 0; i < nstreams; i++) handle[i] = create_handle();
// creating streams
cudaStream_t *streams;
streams = (cudaStream_t *) SUPERLU_MALLOC(sizeof(cudaStream_t)*nstreams);
for (int i = 0; i < nstreams; ++i)
checkCuda( cudaStreamCreate(&streams[i]) );
// allocating data in device
doublecomplex *dA, *dB, *dC;
cudaError_t cudaStat;
#if 0
// cudaStat = cudaMalloc( (void**)&dA, m*k*sizeof(double));
// HOw much should be the size of dA?
// for time being just making it
// cudaStat = cudaMalloc( (void**)&dA, ((max_row_size*sp_ienv_dist(3)))* sizeof(double));
#endif
cudaStat = cudaMalloc( (void**)&dA, max_row_size*sp_ienv_dist(3)* sizeof(doublecomplex));
if (cudaStat!= cudaSuccess) {
fprintf(stderr, "!!!! Error in allocating A in the device %ld \n",m*k*sizeof(doublecomplex) );
return 1;
}
// size of B should be max_supernode_size*buffer
cudaStat = cudaMalloc((void**)&dB, bigu_size * sizeof(doublecomplex));
if (cudaStat!= cudaSuccess) {
fprintf(stderr, "!!!! Error in allocating B in the device %ld \n",n*k*sizeof(doublecomplex));
return 1;
}
cudaStat = cudaMalloc((void**)&dC, buffer_size* sizeof(doublecomplex) );
if (cudaStat!= cudaSuccess) {
fprintf(stderr, "!!!! Error in allocating C in the device \n" );
return 1;
}
stat->gpu_buffer += ( max_row_size * sp_ienv_dist(3)
+ bigu_size + buffer_size ) * dword;
#else /* not CUDA */
// for GEMM padding 0
j = bigu_size / ldt;
bigu_size += (gemm_k_pad * (j + ldt + gemm_n_pad));
bigv_size += (gemm_m_pad * (j + max_row_size + gemm_n_pad));
//#ifdef __INTEL_COMPILER
// bigU = _mm_malloc(bigu_size * sizeof(doublecomplex), 1<<12); // align at 4K page
// bigV = _mm_malloc(bigv_size * sizeof(doublecomplex), 1<<12);
//#else
if ( !(bigU = doublecomplexMalloc_dist(bigu_size)) )
ABORT ("Malloc fails for zgemm U buffer");
//Maximum size of bigU= sqrt(buffsize) ?
// int bigv_size = 8 * ldt * ldt * num_threads;
if ( !(bigV = doublecomplexMalloc_dist(bigv_size)) )
ABORT ("Malloc failed for zgemm V buffer");
//#endif
#endif /* end ifdef GPU_ACC */
log_memory((bigv_size + bigu_size) * dword, stat);
// mlock(bigU,(bigu_size) * sizeof (double));
#if ( PRNTlevel>=1 )
if(!iam) {
printf (" Max row size is %d \n", max_row_size);
printf (" Threads per process %d \n", num_threads);
fflush(stdout);
}
#endif
#if 0 /* Sherry */
if (!(tempv2d = doublecomplexCalloc_dist (2 * ((size_t) ldt) * ldt)))
ABORT ("Calloc fails for tempv2d[].");
tempU2d = tempv2d + ldt * ldt;
#endif
/* Sherry: (ldt + 16), avoid cache line false sharing.
KNL cacheline size = 64 bytes = 16 int */
iinfo = ldt + CACHELINE / sizeof(int);
if (!(indirect = SUPERLU_MALLOC (iinfo * num_threads * sizeof(int))))
ABORT ("Malloc fails for indirect[].");
if (!(indirect2 = SUPERLU_MALLOC (iinfo * num_threads * sizeof(int))))
ABORT ("Malloc fails for indirect[].");
log_memory(2 * ldt*ldt * dword + 2 * iinfo * num_threads * iword, stat);
int_t *lookAheadFullRow,*lookAheadStRow,*lookAhead_lptr,*lookAhead_ib,
*RemainStRow,*Remain_lptr,*Remain_ib;
lookAheadFullRow = intMalloc_dist( (num_look_aheads+1) );
lookAheadStRow = intMalloc_dist( (num_look_aheads+1) );
lookAhead_lptr = intMalloc_dist( (num_look_aheads+1) );
lookAhead_ib = intMalloc_dist( (num_look_aheads+1) );
int_t mrb = (nsupers + Pr - 1) / Pr;
int_t mcb = (nsupers + Pc - 1) / Pc;
RemainStRow = intMalloc_dist(mrb);
#if 0
Remain_lptr = (int *) _mm_malloc(sizeof(int)*mrb,1);
#else
Remain_lptr = intMalloc_dist(mrb);
#endif
// mlock(Remain_lptr, sizeof(int)*mrb );
Remain_ib = intMalloc_dist(mrb);
Remain_info_t *Remain_info;
#if 0
Remain_info = (Remain_info_t *) _mm_malloc(mrb*sizeof(Remain_info_t),64);
#else
Remain_info = (Remain_info_t *) SUPERLU_MALLOC(mrb*sizeof(Remain_info_t));
#endif
doublecomplex *lookAhead_L_buff, *Remain_L_buff; /* Stores entire L-panel */
Ublock_info_t *Ublock_info;
ldt = sp_ienv_dist (3); /* max supernode size */
/* The following is quite loose */
lookAhead_L_buff = doublecomplexMalloc_dist(ldt*ldt* (num_look_aheads+1) );
#if 0
Remain_L_buff = (doublecomplex *) _mm_malloc( sizeof(doublecomplex)*(Llu->bufmax[1]),64);
Ublock_info = (Ublock_info_t *) _mm_malloc(mcb*sizeof(Ublock_info_t),64);
int * Ublock_info_iukp = (int *) _mm_malloc(mcb*sizeof(int),64);
int * Ublock_info_rukp = (int *) _mm_malloc(mcb*sizeof(int),64);
int * Ublock_info_jb = (int *) _mm_malloc(mcb*sizeof(int),64);
#else
j = gemm_m_pad * (ldt + max_row_size + gemm_k_pad);
Remain_L_buff = doublecomplexMalloc_dist(Llu->bufmax[1] + j); /* This is loose */
Ublock_info = (Ublock_info_t *) SUPERLU_MALLOC(mcb*sizeof(Ublock_info_t));
int *Ublock_info_iukp = (int *) SUPERLU_MALLOC(mcb*sizeof(int));
int *Ublock_info_rukp = (int *) SUPERLU_MALLOC(mcb*sizeof(int));
int *Ublock_info_jb = (int *) SUPERLU_MALLOC(mcb*sizeof(int));
#endif
long long alloc_mem = 3 * mrb * iword + mrb * sizeof(Remain_info_t)
+ ldt * ldt * (num_look_aheads+1) * dword
+ Llu->bufmax[1] * dword ;
log_memory(alloc_mem, stat);
InitTimer = SuperLU_timer_() - tt1;
double pxgstrfTimer = SuperLU_timer_();