forked from HLRA-JHPCN/HACApK-MAGMA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
m_HACApK_base.f90
executable file
·1489 lines (1394 loc) · 52.6 KB
/
m_HACApK_base.f90
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
!=====================================================================*
! *
! Software Name : HACApK *
! Version : 1.1.0 *
! *
! License *
! This file is part of HACApK. *
! HACApK is a free software, you can use it under the terms *
! of The MIT License (MIT). See LICENSE file and User's guide *
! for more details. *
! *
! ppOpen-HPC project: *
! Open Source Infrastructure for Development and Execution of *
! Large-Scale Scientific Applications on Post-Peta-Scale *
! Supercomputers with Automatic Tuning (AT). *
! *
! Sponsorship: *
! Japan Science and Technology Agency (JST), Basic Research *
! Programs: CREST, Development of System Software Technologies *
! for post-Peta Scale High Performance Computing. *
! *
! Copyright (c) 2015 <Akihiro Ida and Takeshi Iwashita> *
! *
!=====================================================================*
!C**************************************************************************
!C This file includes basic routines for H-matrices
!C created by Akihiro Ida at Kyoto University on May 2012
!C added functions related to ACA+ to HACApK1.0.0 on Nov. 2016
!C corrected the allocation for st_ctl%lthr on Nov. 2016
!C last modified by Akihiro Ida on Jan. 2017
!C**************************************************************************
module m_HACApK_base
#if defined(ISO_C_BINDING)
use iso_c_binding
#endif
use m_HACApK_calc_entry_ij
implicit real*8(a-h,o-z)
implicit integer*4(i-n)
integer omp_get_thread_num, omp_get_num_threads
external omp_get_thread_num, omp_get_num_threads
!*** type :: st_HACApK_cluster
type :: st_HACApK_cluster
integer*4 ndim
integer*4 nstrt,nsize,ndpth,nnson,nmbr
integer*4 ndscd ! number of descendants
real*8,pointer :: bmin(:)=>null() ! Bounding box
real*8,pointer :: bmax(:)=>null()
real*8 :: zwdth
type(st_HACApK_cluster),pointer :: pc_sons(:)=>null()
end type st_HACApK_cluster
!*** type :: st_HACApK_leafmtx
type :: st_HACApK_leafmtx
integer*4 ltmtx ! kind of the matrix; 1:rk 2:full
integer*4 kt
integer*4 nstrtl,ndl;
integer*4 nstrtt,ndt;
integer*8 a1size !!!
real*8,pointer :: a1(:,:)=>null(),a2(:,:)=>null()
end type st_HACApK_leafmtx
!*** type :: st_HACApK_leafmtxp
type :: st_HACApK_leafmtxp
#if defined(ISO_C_BINDING)
integer(c_int) nd ! number of unknowns
integer(c_int) nlf ! number of sub-matrices
integer(c_int) nlfkt ! number of low-rank sub matrices
integer(c_int) ktmax
integer(c_int) st_lf_stride !!!
#else
integer*4 nd ! number of unknowns
integer*4 nlf ! number of sub-matrices
integer*4 nlfkt ! number of low-rank sub matrices
integer*4 ktmax
integer*4 st_lf_stride !!!
#endif
#if defined(HAVE_MAGMA) | defined(HAVE_MAGMA_BATCH)
#if defined(ISO_C_BINDING)
integer(c_int) m
integer(c_int) n
integer(c_int) gn
integer(c_int) max_block
integer(c_double) gflops
type(c_ptr) :: mtx1_gpu
type(c_ptr) :: mtx2_gpu
type(c_ptr) :: zu_gpu
type(c_ptr) :: zau_gpu
type(c_ptr) :: zau_pin
type(c_ptr) :: zbu_gpu
#else
integer*4 m
integer*4 n
integer*4 gn
integer*4 max_block
real*8 gflops
real*8,pointer :: mtx1_gpu
real*8,pointer :: mtx2_gpu
real*8,pointer :: zu_gpu
real*8,pointer :: zau_gpu
real*8,pointer :: zau_pin
real*8,pointer :: zbu_gpu
#endif
!
#if defined(ISO_C_BINDING)
integer(c_int) num_batch
integer(c_int) total_size_y
integer(c_int) transA
type(c_ptr) :: d_A_array
type(c_ptr) :: d_X_array
type(c_ptr) :: d_Y_array
type(c_ptr) :: d_M
type(c_ptr) :: d_N
type(c_ptr) :: d_lda
type(c_ptr) :: d_inc
#else
integer*4 num_batch
integer*4 total_size_y
integer*4 transA
real*8, pointer :: d_A_array
real*8, pointer :: d_X_array
real*8, pointer :: d_Y_array
integer*4,pointer :: d_M
integer*4,pointer :: d_N
integer*4,pointer :: d_lda
integer*4,pointer :: d_inc
#endif
!
#if defined(ISO_C_BINDING)
type(c_ptr) :: batch_order
type(c_ptr) :: h_A_array
type(c_ptr) :: h_X_array
type(c_ptr) :: h_Y_array
type(c_ptr) :: h_type
type(c_ptr) :: h_I
type(c_ptr) :: h_J
type(c_ptr) :: h_M
type(c_ptr) :: h_N
type(c_ptr) :: h_lda
type(c_ptr) :: max_M
type(c_ptr) :: max_N
type(c_ptr) :: batch_size
#else
integer*4,pointer :: batch_order
real*8, pointer :: h_A_array
real*8, pointer :: h_X_array
real*8, pointer :: h_Y_array
integer*4,pointer :: h_type
integer*4,pointer :: h_I
integer*4,pointer :: h_J
integer*4,pointer :: h_M
integer*4,pointer :: h_N
integer*4,pointer :: h_lda
integer*4,pointer :: max_M
integer*4,pointer :: max_N
integer*4,pointer :: batch_size
#endif
! streamed GEMV
integer*4 num_streamed
integer*4 num_streamed_t
#if defined(ISO_C_BINDING)
type(c_ptr) :: h_A_array_streamed
type(c_ptr) :: h_X_array_streamed
type(c_ptr) :: h_Y_array_streamed
type(c_ptr) :: h_M_streamed
type(c_ptr) :: h_N_streamed
type(c_ptr) :: h_lda_streamed
#else
real*8, pointer :: h_A_array_streamed
real*8, pointer :: h_X_array_streamed
real*8, pointer :: h_Y_array_streamed
integer*4,pointer :: h_M_streamed
integer*4,pointer :: h_N_streamed
integer*4,pointer :: h_lda_streamed
#endif
!
! multi-GPU support
#if defined(ISO_C_BINDING)
type(c_ptr) :: d_A_mgpu
type(c_ptr) :: d_X_mgpu
type(c_ptr) :: d_Y_mgpu
type(c_ptr) :: d_M_mgpu
type(c_ptr) :: d_N_mgpu
type(c_ptr) :: d_lda_mgpu
type(c_ptr) :: d_inc_mgpu
type(c_ptr) :: zu_mgpu
type(c_ptr) :: zau_mgpu
type(c_ptr) :: zbu_mgpu
type(c_ptr) :: h_A_mgpu
type(c_ptr) :: h_X_mgpu
type(c_ptr) :: h_Y_mgpu
type(c_ptr) :: h_type_mgpu
type(c_ptr) :: h_I_mgpu
type(c_ptr) :: h_J_mgpu
type(c_ptr) :: h_M_mgpu
type(c_ptr) :: h_N_mgpu
type(c_ptr) :: h_lda_mgpu
type(c_ptr) :: max_M_mgpu
type(c_ptr) :: max_N_mgpu
type(c_ptr) :: nlf_mgpu
type(c_ptr) :: num_batch_mgpu
type(c_ptr) :: total_size_y_mgpu
type(c_ptr) :: iwork
#else
real*8, pointer :: d_A_mgpu
real*8, pointer :: d_X_mgpu
real*8, pointer :: d_Y_mgpu
integer*4,pointer :: d_M_mgpu
integer*4,pointer :: d_N_mgpu
integer*4,pointer :: d_lda_mgpu
integer*4,pointer :: d_inc_mgpu
real*8, pointer :: zu_mgpu
real*8, pointer :: zau_mgpu
real*8, pointer :: zbu_mgpu
real*8, pointer :: h_A_mgpu
real*8, pointer :: h_X_mgpu
real*8, pointer :: h_Y_mgpu
integer*4,pointer :: h_type_mgpu
integer*4,pointer :: h_I_mgpu
integer*4,pointer :: h_J_mgpu
integer*4,pointer :: h_M_mgpu
integer*4,pointer :: h_N_mgpu
integer*4,pointer :: h_lda_mgpu
integer*4,pointer :: max_M_mgpu
integer*4,pointer :: max_N_mgpu
integer*4,pointer :: nlf_mgpu
integer*4,pointer :: num_batch_mgpu
integer*4,pointer :: total_size_y_mgpu
integer*4,pointer :: iwork
#endif
!integer mpi_comm
integer mpi_rank
#endif
type(st_HACApK_leafmtx),pointer :: st_lf(:)=>null()
end type st_HACApK_leafmtxp
!*** type :: st_HACApK_lcontrol
type :: st_HACApK_lcontrol
integer :: lf_umpi
integer*4 lpmd_offset ! for C-interface
integer*4 lod_offset ! for C-interface
integer*4 lsp_offset ! for C-interface
integer*4 lnp_offset ! for C-interface
integer*4 lthr_offset ! for C-interface
real*8, pointer :: param(:) =>null()
integer*4,pointer :: lpmd(:) =>null()
integer*4,pointer :: lod(:) =>null()
integer*4,pointer :: lsp(:) =>null()
integer*4,pointer :: lnp(:) =>null()
integer*4,pointer :: lthr(:) =>null()
end type st_HACApK_lcontrol
interface
real*8 function HACApK_unrm_d(nd,za)
implicit real*8(a-h,o-z)
real*8 :: za(:)
end function
subroutine HACApK_adotsub_dsm(zr,zaa,zu,ndl,ndt,mdl)
implicit real*8(a-h,o-z)
real*8 :: zaa(:,:)
real*8 :: zu(:),zr(:)
end subroutine
subroutine HACApK_maxabsvalloc_d(za,zz,il,nd)
implicit real*8(a-h,o-z)
real*8 :: za(:)
endsubroutine
subroutine HACApK_maxabsvallocm_d(za,zz,il,nd,lmask)
implicit real*8(a-h,o-z)
real*8 :: za(:)
integer lmask(:)
endsubroutine
subroutine HACApK_minabsvalloc_d(za,zz,il,nd)
implicit real*8(a-h,o-z)
real*8 za(:)
end subroutine
integer function HACApK_med3(nl,nr,nlr2)
endfunction
end interface
contains
!***HACApK_init
integer function HACApK_init(nd,st_ctl,st_bemv,icomma)
implicit real*8(a-h,o-z)
include 'mpif.h'
type(st_HACApK_calc_entry) :: st_bemv
type(st_HACApK_lcontrol) :: st_ctl
integer,optional :: icomma
integer grank
character*320 logfile
allocate(st_ctl%param(100))
st_ctl%param(1:100)=0.0
st_ctl%param(1) =1; ! Print : 0:Only Error 1:STD 2:Dubug
st_ctl%param(9) =1; ! 1:load balancer
st_ctl%param(10)=1; ! 1:fulfill the matrix 0: not fulfill
st_ctl%param(11)=0; ! 1:check accuracy of H-matrix 0: not check
st_ctl%param(12)=0; ! 1:write down the H-matrix to file 0: not write
st_ctl%param(21)=15; ! cluster : leaf size 15
st_ctl%param(22)=1.0; ! cluster : max leaf size 1.0*nffc
st_ctl%param(51)=2.0; ! H-matrix : dicision param of distance 2.0
st_ctl%param(60)=2 ! 1:ACA 2:ACA+
st_ctl%param(61)=1 ! ACA norm 1:MREM 2:test 3:norm
st_ctl%param(62)=7 ! ACA : predictive average of k
st_ctl%param(63)=1000; ! ACA : k-max of R_k-matrix 30
st_ctl%param(64)=1; ! ACA : minimun kt
st_ctl%param(72)=1.0e-3; ! ACA_EPS
st_ctl%param(83)=500; ! solver : maximum iterative number
st_ctl%param(85)=1; ! solver : 1:BiCGSTAB 2:GCR(m)
st_ctl%param(87)=8; ! solver : number of iteration for reset
st_ctl%param(99)=1 ! Measure the time of Ax; iterative number
ierr=0; lrtrn=0
if(present(icomma))then
icomm=icomma; st_ctl%lf_umpi=1
else
icomm=MPI_COMM_WORLD; lf_umpi=0
call MPI_Init ( ierr )
if( ierr .ne. 0 ) then
print*, 'HACApK_init; Error: MPI_Init failed !!!'
else
print*, 'HACApK_init; Error: MPI_Inited !!!'
endif
endif
call MPI_Comm_size ( icomm, nrank, ierr )
if(ierr.ne.0) then
print*, 'Error: MPI_Comm_size failed !!!'
endif
call MPI_Comm_rank ( icomm, irank, ierr )
if(ierr.ne.0) then
print*, 'Error: MPI_Comm_rank failed !!!'
endif
! st_ctl%param(63)=1; ! ACA : k-max of R_k-matrix 30
! if(irank.eq.0) then
! WRITE(*,*)
! WRITE(*,*) 'k-max set to be',st_ctl%param(63)
! WRITE(*,*)
! endif
!
! st_ctl%param(21)=30; ! cluster : leaf size 15
! if(irank.eq.0) then
! WRITE(*,*) 'leaf size set to be',st_ctl%param(21)
! endif
allocate(st_ctl%lpmd(30)); st_ctl%lpmd(:)=0
st_ctl%lpmd(1)=icomm; st_ctl%lpmd(2)=nrank; st_ctl%lpmd(3)=irank; st_ctl%lpmd(4)=20
!
call MPI_Comm_rank ( MPI_COMM_WORLD, grank, ierr )
st_ctl%lpmd(30)=grank
!
#ifdef HAVE_PaRSEC
!st_ctl%param(21)=ceiling(dsqrt(20.0*dble(nd))); ! cluster : leaf size 15
st_ctl%param(21)=200;
if( grank .eq. 0 ) then
print*, ' '
print*, '!!! cluster leaf size is increased to',st_ctl%param(21)
print*, ' '
endif
#endif
! nthr = omp_get_num_threads()
nthr = 1
if(nthr>0) st_ctl%lpmd(20)=nthr
allocate(st_ctl%lod(nd),st_ctl%lthr(nthr+1),st_ctl%lnp(nrank),st_ctl%lsp(nrank),stat = ierr)
call MPI_Barrier( icomm, ierr )
if(st_ctl%param(1)>0)then
write(logfile,'(a,i4.4,a)') 'log',irank,'.txt'
open(st_ctl%lpmd(4),file=logfile)
endif
st_bemv%nd=nd
if(st_bemv%lp61==1) then; endif
if(ierr/=0)then; goto 9999; endif
9999 continue
HACApK_init=lrtrn
endfunction
!***HACApK_finalize
integer function HACApK_finalize(st_ctl)
implicit real*8(a-h,o-z)
include 'mpif.h'
type(st_HACApK_lcontrol) :: st_ctl
if(st_ctl%param(1)>0) close(st_ctl%lpmd(4))
if(st_ctl%lf_umpi==0) call MPI_Finalize (ierr)
lrtrn=HACApK_free_lcontrol(st_ctl)
HACApK_finalize=lrtrn
endfunction
!***HACApK_chk_st_ctl
subroutine HACApK_chk_st_ctl(st_ctl)
type(st_HACApK_lcontrol) :: st_ctl
if(st_ctl%param(21)<2) then
st_ctl%param(21)=3
print*,'Warning: Invalid param(21)=',st_ctl%param(21)
print*,'It is changed to param(21)=3.'
endif
end subroutine HACApK_chk_st_ctl
!***HACApK_generate_frame_leafmtx
subroutine HACApK_generate_frame_leafmtx(st_leafmtxp,st_bemv,st_ctl,gmid,lnmtx,nofc,nffc,ndim)
include 'mpif.h'
type(st_HACApK_cluster) :: st_clt
type(st_HACApK_lcontrol) :: st_ctl
type(st_HACApK_calc_entry) :: st_bemv
type(st_HACApK_leafmtxp) :: st_leafmtxp
type(st_HACApK_leafmtx),dimension(:), allocatable :: st_leafmtx
real*8 :: gmid(nofc,ndim)
integer*8 :: mem8
integer*4 :: lnmtx(3)
integer, dimension(:), allocatable :: lhp, lnp
real*8,pointer :: param(:)
integer*4,pointer :: lpmd(:),lod(:),lthr(:),lodfc(:)
1000 format(5(a,i12)/)
2000 format(5(a,e15.7)/)
param => st_ctl%param(:)
lpmd => st_ctl%lpmd(:); lod => st_ctl%lod(:); lthr(0:) => st_ctl%lthr
mpinr=lpmd(3); mpilog=lpmd(4); nrank=lpmd(2); icomm=lpmd(1); nthr=lpmd(20)
nd=nofc*nffc
allocate(lodfc(nofc),stat=ierr)
if(ierr/=0)then
print*,'HACApK_generate_frame_leafmtx: ierr=',ierr
endif
do il=1,nofc
lodfc(il)=il
enddo
!!!!!!!!!!!!!!!!!! start clustering !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
nsrt=1; ndf=nofc; nclst=0; ndpth=0; ndscd=0
call HACApK_generate_cbitree(st_clt,gmid,param,lpmd,lodfc,ndpth,ndscd,nsrt,ndf,nofc,ndim,nclst)
if(st_ctl%param(1)>0 .and. st_ctl%lpmd(30)==0) write(*,1000) 'No. of cluster=',nclst
if(st_ctl%param(1)>0) write(mpilog,1000) 'No. of cluster=',nclst
call HACApK_bndbox(st_clt,gmid,lodfc,nofc)
!!!!!!!!!!!!!!!!!! end clustering !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!! start construction of H-matrix !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ndpth=0; lnmtx(1:3)=0
call HACApK_count_lntmx(st_clt,st_clt,param,lpmd,lnmtx,nofc,nffc)
if(st_ctl%param(1)>0 .and. st_ctl%lpmd(30)==0) print*,'No. of nsmtx',lnmtx(1:3)
if(st_ctl%param(1)>0 .and. st_ctl%lpmd(30)==0) print*,' 1:Rk-matrix 2: dense-mat 3:H-matrix'
st_leafmtxp%nlfkt=lnmtx(1)
nlf=lnmtx(1)+lnmtx(2)
allocate(st_leafmtx(nlf))
st_leafmtxp%nlf=nlf; nlf=0
call HACApK_generate_leafmtx(st_leafmtx,st_clt,st_clt,param,lpmd,lnmtx,nofc,nffc,nlf)
if(st_ctl%param(1)>1 .and. st_ctl%lpmd(30)==0) print*,'HACApK_generate_frame_leafmtx; HACApK_generate_leafmtx end'
call HACApK_qsort_row_leafmtx(st_leafmtx,1,nlf)
if(st_ctl%param(1)>1 .and. st_ctl%lpmd(30)==0) print*,'HACApK_generate_frame_leafmtx; HACApK_qsort_row_leafmtx end'
ilp=1; ips=1
do ip=1,nlf
il=st_leafmtx(ip)%nstrtl
if(il<ilp)then ; print *,'Error!; HACApK_generate_frame_leafmtx row_sort';
elseif(il>ilp)then;
call HACApK_qsort_col_leafmtx(st_leafmtx,ips,ip-1)
ilp=il; ips=ip
endif
enddo
call HACApK_free_st_clt(st_clt)
do il=1,nofc
do ig=1,nffc
is=ig+(il-1)*nffc
lod(is)=lodfc(il)
enddo
enddo
!!!!!!!!!!!!!!!!!! start MPI load balance !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
allocate(lhp(nrank+1), lnp(nrank), stat = ierr)
if(ierr.ne.0) then
print*, 'Memory allocation #11 failed !!!'
goto 9999
endif
mdim = nd / nrank
mdim1 = mdim + 1
mrank = nd - mdim * nrank
lbrns=param(9)
if(nrank==2) lbrns=0
if(lbrns==0)then
do jrank=1,mrank; lnp(jrank) = mdim1; enddo
do jrank=mrank+1,nrank; lnp(jrank) = mdim; enddo
lhp(1)=0; lhp(nrank+1)=nd
do jrank=2,nrank
lhp(jrank)=lhp(jrank-1)+lnp(jrank-1)
enddo
else
ktp=param(62)
call HACApK_setcutrow(lhp,mem8,lpmd,st_leafmtx,st_ctl,nd,st_leafmtxp%nlf,ktp)
do jrank=1,nrank
lnp(jrank)=lhp(jrank+1)-lhp(jrank)
enddo
endif
lpmd(6)=lhp(mpinr+1)+1; lpmd(7)=lhp(mpinr+2); lpmd(5)=lpmd(7)-lpmd(6)+1
call MPI_Barrier( icomm, ierr )
if(st_ctl%param(1)>1 .and. mpinr==0) print*,'nlf=',st_leafmtxp%nlf
! do il=1,mmm
! if(mpinr==0) write(*,1000) 'il=',il,' nstrtl=',st_leafmtx(il)%nstrtl,' nstrtt=',st_leafmtx(il)%nstrtt
! enddo
! stop
lpmd(11)=0; lpmd(12)=st_leafmtxp%nlf
do ip=1,st_leafmtxp%nlf
if(st_leafmtx(ip)%nstrtl>=lpmd(6) .and. st_leafmtx(ip)%nstrtl<=lpmd(7) .and. lpmd(11)==0) lpmd(11)=ip
if(st_leafmtx(ip)%nstrtl>lpmd(7) .and. lpmd(11)/=0) then
lpmd(12)=ip-1; exit
endif
enddo
! write(*,1000) 'irank=',mpinr,' ndlf_s=',lpmd(11),', ndlf_e=',lpmd(12)
st_leafmtxp%nlf=lpmd(12)-lpmd(11)+1
allocate(st_leafmtxp%st_lf(st_leafmtxp%nlf));
st_leafmtxp%st_lf(1:st_leafmtxp%nlf)=st_leafmtx(lpmd(11):lpmd(12))
lnmtx(:)=0; mem8=0; ktp=param(62)
do ip=lpmd(11),lpmd(12)
ltmtx=st_leafmtx(ip)%ltmtx; ndl=st_leafmtx(ip)%ndl; ndt=st_leafmtx(ip)%ndt; ns=ndl*ndt
if(ltmtx==1)then
lnmtx(1)=lnmtx(1)+1; mem8=mem8+(ndt+ndl)*ktp
else
lnmtx(2)=lnmtx(2)+1; mem8=mem8+ns
endif
enddo
deallocate(st_leafmtx)
call HACApK_setcutthread(lthr,st_leafmtxp,st_ctl,mem8,nthr,ktp)
9999 continue
! stop
end subroutine HACApK_generate_frame_leafmtx
!***HACApK_setcutrow
subroutine HACApK_setcutrow(lhp,ncpc1,lpmd,st_leafmtx,st_ctl,nd,nlf,kt)
type(st_HACApK_leafmtx) :: st_leafmtx(*)
type(st_HACApK_lcontrol) :: st_ctl
integer :: lhp(*),lpmd(*)
integer*8 :: ncpc,ncpc1
1000 format(5(a,i12)/)
2000 format(5(a,e15.7)/)
mpinr=lpmd(3); mpilog=lpmd(4); nrank=lpmd(2); icomm=lpmd(1)
call MPI_Barrier( icomm, ierr )
ncpc=0
do il=1,nlf
ltmtx=st_leafmtx(il)%ltmtx
ndl=st_leafmtx(il)%ndl; ndt=st_leafmtx(il)%ndt
if(ltmtx==1)then
ncpc=ncpc+(ndl+ndt)*kt
else
ncpc=ncpc+ndl*ndt
endif
enddo
ncpc1=ncpc/nrank
if(st_ctl%param(1)>0 .and. mpinr==0) write(6,1000) 'ncpc=',ncpc,' ncpc/nrank=',ncpc1
lhp(1)=0; lhp(nrank+1)=nd
ncpc=0; ip=1
do il=1,nlf
ltmtx=st_leafmtx(il)%ltmtx; nstrtl=st_leafmtx(il)%nstrtl
ndl=st_leafmtx(il)%ndl; ndt=st_leafmtx(il)%ndt
if(ltmtx==1)then
ncpc=ncpc+(ndl+ndt)*kt
else
ncpc=ncpc+ndl*ndt
endif
if(ncpc>ncpc1*ip .and. nstrtl-1>lhp(ip))then
lhp(ip+1)=nstrtl-1
! if(mpinr==0) write(6,1000) 'ip=',ip,' lhp(ip+1)=',lhp(ip+1)
ip=ip+1
if(ip==nrank) exit
endif
enddo
if(st_ctl%param(1)>1 .and. mpinr==0) print*,'HACApK_generate_frame_leafmtx; HACApK_setcutrow end'
end subroutine HACApK_setcutrow
!***HACApK_setcutthread
subroutine HACApK_setcutthread(lthr,st_leafmtxp,st_ctl,mem8,nthr,ktp)
type(st_HACApK_leafmtxp) :: st_leafmtxp
type(st_HACApK_lcontrol) :: st_ctl
integer :: lthr(0:*)
integer*8 :: mem8,nth1_mem,imem
nlf=st_leafmtxp%nlf
nth1_mem=mem8/nthr
if(st_ctl%param(1)>1) print*,'HACApK_setcutthread; nlf=',nlf,' mem8=',mem8,' nthr=',nthr
lthr(0)=1; lthr(nthr)=nlf+1
imem=0; ith=1; kt=ktp
do il=1,nlf
ltmtx=st_leafmtxp%st_lf(il)%ltmtx
ndl=st_leafmtxp%st_lf(il)%ndl; ndt=st_leafmtxp%st_lf(il)%ndt
if(ltmtx==1)then
if(ktp==0) kt=st_leafmtxp%st_lf(il)%kt
imem=imem+(ndl+ndt)*kt
else
imem=imem+ndl*ndt
endif
if(imem>nth1_mem*ith)then
lthr(ith)=il
ith=ith+1
if(ith==nthr) exit
endif
enddo
if(st_ctl%param(1)>1) print*,'HACApK_setcutthread; lthr=',lthr(0:nthr)
end subroutine HACApK_setcutthread
!***HACApK_accuracy_leafmtx_body
subroutine HACApK_accuracy_leafmtx_body(zhnrm,zanrm,st_leafmtxp,st_bemv,lodl,lodt,lpmd,nofc,nffc)
type(st_HACApK_leafmtxp) :: st_leafmtxp
type(st_HACApK_calc_entry) :: st_bemv
integer*4 :: lodl(nofc*nffc),lodt(nofc*nffc),lpmd(*)
1000 format(5(a,i12)/)
2000 format(5(a,1pe15.7)/)
do ip=1,st_leafmtxp%nlf
ndl=st_leafmtxp%st_lf(ip)%ndl; ndt=st_leafmtxp%st_lf(ip)%ndt; ns=ndl*ndt
nstrtl=st_leafmtxp%st_lf(ip)%nstrtl; nstrtt=st_leafmtxp%st_lf(ip)%nstrtt
if(st_leafmtxp%st_lf(ip)%ltmtx==1)then
kt=st_leafmtxp%st_lf(ip)%kt
do il=1,ndl; ill=il+nstrtl-1
do it=1,ndt; itt=it+nstrtt-1
zz=HACApK_entry_ij(lodl(ill),lodt(itt),st_bemv)
zanrm=zanrm+zz*zz
do ik=1,kt
zz=zz-st_leafmtxp%st_lf(ip)%a2(il,ik)*st_leafmtxp%st_lf(ip)%a1(it,ik)
enddo
zhnrm=zhnrm+zz*zz
enddo
enddo
elseif(st_leafmtxp%st_lf(ip)%ltmtx==2)then
do il=1,ndl; ill=il+nstrtl-1
do it=1,ndt; itt=it+nstrtt-1
zanrm=zanrm+st_leafmtxp%st_lf(ip)%a1(it,il)*st_leafmtxp%st_lf(ip)%a1(it,il)
enddo
enddo
endif
enddo
end subroutine HACApK_accuracy_leafmtx_body
!***HACApK_accuracy_leafmtx
subroutine HACApK_accuracy_leafmtx(st_leafmtxp,st_bemv,st_ctl,lodl,lodt,lpmd,nofc,nffc)
include 'mpif.h'
type(st_HACApK_leafmtxp) :: st_leafmtxp
type(st_HACApK_calc_entry) :: st_bemv
type(st_HACApK_lcontrol) :: st_ctl
integer*4 :: lodl(nofc*nffc),lodt(nofc*nffc),lpmd(*)
1000 format(5(a,i12)/)
2000 format(5(a,1pe15.8)/)
mpinr=lpmd(3); mpilog=lpmd(4); nrank=lpmd(2); icomm=lpmd(1)
ndnr_s=lpmd(6); ndnr_e=lpmd(7); ndnr=lpmd(5)
if(st_ctl%param(1)>1 .and. mpinr==0) print*,'sub HACApK_accuracy_leafmtx start'
zhnrm=0.0d0; zanrm=0.0d0; ktmax=0
call HACApK_accuracy_leafmtx_body(zhnrm1,zanrm1,st_leafmtxp,st_bemv,lodl,lodt,lpmd,nofc,nffc)
call MPI_reduce( zanrm1, zanrm, 1, MPI_DOUBLE_PRECISION, MPI_SUM,0, icomm, ierr );
call MPI_reduce( zhnrm1, zhnrm, 1, MPI_DOUBLE_PRECISION, MPI_SUM,0, icomm, ierr );
zanrm=dsqrt(zanrm); zhnrm=dsqrt(zhnrm)
if(st_ctl%param(1)>0 .and. mpinr==0) print*,'sub HACApK_accuracy_leafmtx; zanrm=',zanrm
if(st_ctl%param(1)>0 .and. mpinr==0) print*,'sub HACApK_accuracy_leafmtx; zhnrm=',zhnrm
if(st_ctl%param(1)>0 .and. mpinr==0) print*,'sub HACApK_accuracy_leafmtx; zhnrm/zanrm=',zhnrm/zanrm
if(st_ctl%param(1)>0) write(mpilog,2000) 'sub HACApK_accuracy_leafmtx; zanrm1=',dsqrt(zanrm1)
if(st_ctl%param(1)>0) write(mpilog,2000) 'sub HACApK_accuracy_leafmtx; zhnrm1=',dsqrt(zhnrm1)
if(st_ctl%param(1)>0) write(mpilog,2000) 'sub HACApK_accuracy_leafmtx; zhnrm1/zanrm1=',dsqrt(zhnrm1)/dsqrt(zanrm1)
if(mpinr==0 .and. st_ctl%param(1)>0) write(mpilog,2000) 'sub HACApK_accuracy_leafmtx; zanrm=',zanrm
if(mpinr==0 .and. st_ctl%param(1)>0) write(mpilog,2000) 'sub HACApK_accuracy_leafmtx; zhnrm=',zhnrm
if(mpinr==0 .and. st_ctl%param(1)>0) write(mpilog,2000) 'sub HACApK_accuracy_leafmtx; zhnrm/zanrm=',zhnrm/zanrm
end subroutine HACApK_accuracy_leafmtx
!***HACApK_calc_vec
! ld==0: row direction, ld==1: column direction
subroutine HACApK_calc_vec(zaa, zab, ndp, k, ip, vec, nstrtl, nstrtt,lod, st_bemv, lmsk, ld)
type(st_HACApK_calc_entry) :: st_bemv
real*8,target :: zab(:,:)
real*8 :: vec(:),zaa(:,:)
real*8,pointer :: zz(:)
integer*4 :: lmsk(:),lod(:)
do ii=1,ndp
if(lmsk(ii)==0) then
if(ld==0)then
ill=ip+nstrtl-1; itt=ii+nstrtt-1
else
ill=ii+nstrtl-1; itt=ip+nstrtt-1
endif
vec(ii)=HACApK_entry_ij(lod(ill),lod(itt),st_bemv)
endif
enddo
if(k==0) return
zz => zab(ip,1:k)
call HACApK_adotsub_dsm(vec,zaa,zz,ndp,k,ndp)
where(lmsk==1) vec=0.0d0
endsubroutine
!***HACApK_aca
integer function HACApK_aca(zaa,zab,param,ndl,ndt,nstrtl,nstrtt,lod,st_bemv,kmax,eps,znrmmat,pACA_EPS)
type(st_HACApK_calc_entry) :: st_bemv
real*8 :: param(:)
real*8,target :: zaa(ndl,kmax),zab(ndt,kmax)
real*8,pointer :: prow(:),pcol(:)
integer*4 :: lod(:)
integer*4,dimension(:), allocatable :: lrow_msk,lcol_msk
1000 format(5(a,i12)/)
2000 format(5(a,1pe15.8)/)
! write(6,1000) 'nstrtl=',nstrtl,' nstrtt=',nstrtt,' ndl=',ndl,' ndt=',ndt, 'kmax=',kmax
krank=min(ndl,ndt)
znrm=znrmmat*sqrt(real(ndl)*real(ndt))
if(param(61)==1) ACA_EPS=pACA_EPS
if(param(61)==2 .or. param(61)==3) ACA_EPS=pACA_EPS*znrm
allocate(lrow_msk(ndl),lcol_msk(ndt)); lrow_msk(:)=0; lcol_msk(:)=0; nrow_done=0; ncol_done=0
HACApK_aca=0; k=1; lstop_aca=0
kstop=min(kmax,krank)
if(nstrtl>nstrtt)then; ist=1
else; ist=ndl
endif
do
! print*,'k=',k,' zeps=',zeps,' ist=',ist
pcol => zaa(:,k); prow => zab(:,k)
call HACApK_calc_vec(zab, zaa, ndt, k-1, ist, prow, nstrtl, nstrtt,lod, st_bemv, lcol_msk,0)
! print*, 'prow=',prow
call HACApK_maxabsvallocm_d(prow,row_maxval,jst,ndt,lcol_msk)
! print*,'jst=',jst,' row_maxval=',row_maxval
! write(6,1000) 'ist=',ist,' jst=',jst
zdltinv=1.0d0/prow(jst); prow(:)=prow(:)*zdltinv
call HACApK_calc_vec(zaa, zab, ndl, k-1, jst, pcol, nstrtl, nstrtt,lod, st_bemv, lrow_msk,1)
lrow_msk(ist)=1
call HACApK_maxabsvallocm_d(pcol,col_maxval,istn,ndl,lrow_msk)
! print*, 'pcol=',pcol
! print *,'zeps=',zeps
! print*,'istn=',istn,' col_maxval=',col_maxval
lcol_msk(jst)=1
ist=istn; nrow_done=nrow_done+1; ncol_done=ncol_done+1
if(abs(row_maxval)<ACA_EPS .and. abs(col_maxval)<ACA_EPS .and. k>=param(64)) then
print *, 'ACA_EPS=',ACA_EPS
print *, 'abs(row_maxval)=',abs(row_maxval)
print *, 'abs(col_maxval)=',abs(col_maxval)
print *, 'stop HACApK_aca 3';
!!! stop
goto 9999
endif
zeps=HACApK_unrm_d(ndl,pcol)*HACApK_unrm_d(ndt,prow)
! zcolm=HACApK_unrm_d(ndl,pcol); zrowm=HACApK_unrm_d(ndt,prow)
! zeps=max(zcolm,zrowm,zcolm*zrowm)
if(k==1 .and. param(61)==1) znrm=zeps
zeps=zeps/znrm
! print*,'pcol'; print*,pcol
! print*,'prow'; print*,prow
! print*,'lcol_msk',lcol_msk
! print*,'lrow_msk',lrow_msk
! write(6,2000) 'zeps=',zeps
if(zeps<eps .or. k==kstop) lstop_aca = 1
if(lstop_aca==1 .and. k>=param(64)) then
! print *,'k=',k, 'param(64)=',param(64)
! print *,'zeps=',zeps
! print *,'eps=',eps
! print*,'???????????'
exit
endif
k=k+1
enddo
9999 continue
deallocate(lrow_msk,lcol_msk)
HACApK_aca=k
! print*,'HACApK_aca=',aca
if(zeps>eps .and. k<krank)then
print *,'k=',k
print *,'zeps=',zeps
print *,'eps=',eps
write(6,1000) 'nstrtl=',nstrtl,' nstrtt=',nstrtt,' ndl=',ndl,' ndt=',ndt
print*,'znrm=',znrm
stop
endif
! stop
endfunction
!***HACApK_acaplus
integer function HACApK_acaplus(zaa,zab,param,ndl,ndt,nstrtl,nstrtt,lod,st_bemv,kmax,eps,znrmmat,pACA_EPS)
type(st_HACApK_calc_entry) :: st_bemv
real*8 :: param(:)
real*8,target :: zaa(ndl,kmax),zab(ndt,kmax)
integer*4 :: lod(:)
integer*4,dimension(:), allocatable :: lrow_msk,lcol_msk
real*8,dimension(:), allocatable :: pa_ref, pb_ref
real*8,pointer :: prow(:),pcol(:)
1000 format(5(a,i12)/)
2000 format(5(a,1pe15.8)/)
za_ACA_EPS=1.0e-30
! write(6,1000) 'nstrtl=',nstrtl,' nstrtt=',nstrtt,' ndl=',ndl,' ndt=',ndt
znrm=znrmmat*sqrt(real(ndl)*real(ndt))
if(param(61)==2 .or. param(61)==1) ACA_EPS=pACA_EPS
if(param(61)==3) ACA_EPS=pACA_EPS*znrm
HACApK_acaplus=0; ntries = max(ndl,ndt)+1; ntries_row = 6; ntries_col = 6;
allocate(lrow_msk(ndl),lcol_msk(ndt))
k = 0; lrow_msk(:)=0; lcol_msk(:)=0
j_ref=1; allocate(pa_ref(ndl)) ! arbitrary j_ref
call HACApK_calc_vec(zaa, zab, ndl, k, j_ref, pa_ref, nstrtl, nstrtt,lod, st_bemv, lrow_msk, 1)
!!! print*,'pa_ref=',pa_ref
colnorm = HACApK_unrm_d(ndl,pa_ref)
call HACApK_minabsvalloc_d(pa_ref,rownorm,i_ref,ndl) ! determine i_ref:=argmin ||pa_ref(1:ndl)||
!!! print*,'i_ref=',i_ref
allocate(pb_ref(ndt))
call HACApK_calc_vec(zab, zaa, ndt, k, i_ref, pb_ref, nstrtl, nstrtt,lod, st_bemv, lcol_msk,0)
!!! print*,'pb_ref=',pb_ref
rownorm=HACApK_unrm_d(ndt,pb_ref)
apxnorm = 0.0; lstop_aca = 0;
do while((k<kmax) .and. (ntries_row>0 .or. ntries_col>0) .and. (ntries>0))
ntries=ntries-1
pcol => zaa(:,k+1); prow => zab(:,k+1)
col_maxval = 0.0; call HACApK_maxabsvalloc_d(pa_ref,col_maxval,i,ndl)
row_maxval = 0.0; call HACApK_maxabsvalloc_d(pb_ref,row_maxval,j,ndt)
!!! write(6,1000) 'i=',i,' i_ref=',i_ref,' j=',j,' j_ref=',j_ref
if(row_maxval>col_maxval)then
if(j/=j_ref)then; call HACApK_calc_vec(zaa, zab, ndl, k, j, pcol, nstrtl, nstrtt,lod, st_bemv, lrow_msk, 1)
else; pcol(:)=pa_ref(:)
endif
call HACApK_maxabsvalloc_d(pcol,col_maxval,i,ndl)
if(col_maxval < ACA_EPS .and. k>=param(64))then; lstop_aca = 1;
! print*,'2***************lstop_aca==1***********************2'
else
call HACApK_calc_vec(zab, zaa, ndt, k, i, prow, nstrtl, nstrtt,lod, st_bemv, lcol_msk,0)
if(abs(pcol(i))>1.0e-20) then
zinvmax=1.0/pcol(i)
else
k=max(k-1,0); exit
endif
! if(isnan(zinvmax))then
! print*,'1.0/pcol(i)=NaN',' k=',k
! exit
! stop
! endif
pcol(:)=pcol(:)*zinvmax
endif
else
if(i/=i_ref)then; call HACApK_calc_vec(zab, zaa, ndt, k, i, prow, nstrtl, nstrtt,lod, st_bemv, lcol_msk,0)
else; prow(:)=pb_ref(:)
endif
call HACApK_maxabsvalloc_d(prow,row_maxval,j,ndt)
if(row_maxval < ACA_EPS .and. k>=param(64))then; lstop_aca = 1
! print*,'3***************lstop_aca==1***********************3'
else
call HACApK_calc_vec(zaa, zab, ndl, k, j, pcol, nstrtl, nstrtt,lod, st_bemv, lrow_msk, 1)
if(abs(prow(j))>1.0e-20) then
zinvmax=1.0/prow(j)
else
k=max(k-1,0); exit
endif
! if(isnan(zinvmax))then
! print*,'1.0/prow(j)=NaN',' k=',k
! exit
! stop
! endif
prow(:)=prow(:)*zinvmax
endif
endif
lrow_msk(i) = 1; lcol_msk(j) = 1
!!! write(6,1000) 'i=',i,' i_ref=',i_ref,' j=',j,' j_ref=',j_ref
if(i/=i_ref)then
zinvmax = -pcol(i_ref)
pb_ref(:)=pb_ref(:)+prow(:)*zinvmax
rownorm = HACApK_unrm_d(ndt,pb_ref)
endif
if(i==i_ref .or. rownorm<ACA_EPS)then
if(i==i_ref) ntries_row=ntries_row+1
if(ntries_row>0)then
rownorm = 0.0; i=i_ref
! print*,'lrow_msk',lrow_msk
do while(i/=mod((i_ref+ndl-2),ndl)+1 .and. rownorm<za_ACA_EPS .and. ntries_row>0)
! print*,'i=',i,' ii=',mod((i_ref+ndl-2),ndl)+1
if(lrow_msk(i)==0)then
! write(6,1000) 'i=',i
call HACApK_calc_vec(zab, zaa, ndt, k+1, i, pb_ref, nstrtl, nstrtt,lod, st_bemv, lcol_msk,0)
rownorm = HACApK_unrm_d(ndt,pb_ref)
if(rownorm<ACA_EPS) lrow_msk(i) = 1
ntries_row=ntries_row-1
else
rownorm = 0.0;
endif
i=mod(i,ndl)+1
enddo
i_ref=mod((i+ndl-2),ndl)+1
endif
endif
!!! print*,'i_ref=',i_ref
if(j/=j_ref)then
zinvmax = -prow(j_ref)
pa_ref(:)=pa_ref(:)+pcol(:)*zinvmax
colnorm = HACApK_unrm_d(ndl,pa_ref)
endif
if(j==j_ref .or. colnorm<ACA_EPS)then
if(j==j_ref) ntries_col=ntries_col+1
if(ntries_col>0)then
colnorm = 0.0; j=j_ref
! print*,'lcol_msk',lcol_msk
do while(j/=mod((j_ref+ndt-2),ndt)+1 .and. colnorm<za_ACA_EPS .and. ntries_col>0)
if(lcol_msk(j)==0)then
call HACApK_calc_vec(zaa, zab, ndl, k+1, j, pa_ref, nstrtl, nstrtt,lod, st_bemv, lrow_msk, 1)
colnorm = HACApK_unrm_d(ndl,pa_ref)
if(colnorm<ACA_EPS) lcol_msk(j)=1
ntries_col=ntries_col-1
else
colnorm = 0.0
endif
j=mod(j,ndt)+1
enddo
j_ref=mod((j+ndt-2),ndt)+1
endif
endif
! write(6,2000) 'colnorm=',colnorm,' rownorm=',rownorm
if(colnorm<ACA_EPS .and. rownorm<ACA_EPS .and. k>=param(64))then
lstop_aca=1; k=k+1;
! print*,'1***************lstop_aca==1***********************1'
endif
if(lstop_aca==0)then
blknorm = (HACApK_unrm_d(ndl,pcol)*HACApK_unrm_d(ndt,prow))
if(k == 0)then
if(param(61)==1)then
apxnorm = blknorm
elseif(param(61)==2 .or. param(61)==3)then
apxnorm =znrm
else
print*,'ERROR!:: invalid param(61)=',param(61)
stop
endif
else
if( blknorm < apxnorm * eps &
.and. rownorm < apxnorm * eps &
.and. colnorm < apxnorm * eps &
.and. k>=param(64)) lstop_aca = 1;
endif
endif
if(.false.)then
print*,'pcol'; print*,pcol
print*,'prow'; print*,prow
endif
if(lstop_aca==1 .and. k>=param(64)) exit
k=k+1
enddo
! if(k==kmax .or. ntries_row==0 .or. ntries_col==0 .or. ntries==0)then
! k=k-1
! endif
if(k<param(64))then
print*, 'colnorm=',colnorm,' rownorm=',rownorm,'ACA_EPS=',ACA_EPS
print*, 'col_maxval=',col_maxval,' row_maxval=',row_maxval
print*, 'ntries_row=',ntries_row,' ntries_col=',ntries_col,' ntries=',ntries
print*, 'k=',k
! k=k-1; if(k<1) stop
! stop
endif
deallocate(lrow_msk,lcol_msk,pa_ref,pb_ref)
HACApK_acaplus=k
!!! print*,'HACApK_acaplus=',HACApK_acaplus
!!! write(6,2000) 'blknorm=',blknorm/apxnorm,' colnorm=',colnorm/apxnorm,' rownorm=',rownorm/apxnorm
!!! if(nstrtt== 113) stop
endfunction
!***HACApK_fill_leafmtx_hyp
subroutine HACApK_fill_leafmtx_hyp(st_lf,st_bemv,param,znrmmat,lpmd,lnmtx,lodl,lodt,nd,nlf,lnps,lnpe,lthr)
! type(st_HACApK_leafmtxp) :: st_leafmtxp
type(st_HACApK_leafmtx) :: st_lf(:)
type(st_HACApK_calc_entry) :: st_bemv
real*8 ::param(:)
integer*4 :: lodl(nd),lodt(nd),lpmd(:),lnmtx(:),lthr(0:)
real*8, allocatable :: zab(:,:),zaa(:,:)
!real*8, dimension(:,:), pointer :: aa
1000 format(5(a,i12)/)
eps=param(71); ACA_EPS=param(72)*eps; kparam=param(63)
! ith = omp_get_thread_num()
! nthr = omp_get_num_threads()
ith = 0
nthr = 1
ith1 = ith+1
nths=lthr(ith); nthe=lthr(ith1)-1
ierr=0
do ip=nths,nthe
ndl =st_lf(ip)%ndl ; ndt =st_lf(ip)%ndt ; ns=ndl*ndt
nstrtl=st_lf(ip)%nstrtl; nstrtt=st_lf(ip)%nstrtt; ltmtx=st_lf(ip)%ltmtx
if(ltmtx==1)then
allocate(zab(ndt,kparam),zaa(ndl,kparam),stat=ierr)
if(ierr.ne.0) then