-
Notifications
You must be signed in to change notification settings - Fork 15
/
amica17.f90
3903 lines (3446 loc) · 155 KB
/
amica17.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
program main
!#define MKL
use funmod2
#ifndef MKL
use mpi
#endif
implicit none
intrinsic count
intrinsic len
intrinsic product
intrinsic cmplx
include 'amica17_header.f90'
#ifdef MKL
include 'mpif.h'
include 'mkl_vml.f90'
#endif
!-------------- INITIALIZE MPI ----------------
!--- init mpi and get myrank
call MPI_INIT(ierr)
call MPI_COMM_RANK(MPI_COMM_WORLD,myrank,ierr)
call MPI_COMM_SIZE(MPI_COMM_WORLD,tot_procs,ierr)
!--- get number of procs on this host
call MPI_GET_PROCESSOR_NAME(host_name,name_len,ierr)
print *, myrank+1, 'processor name = ', trim(host_name); call flush(6)
!--- turn hostname into a number (hash)
host_num = 0
do i = 1,name_len
host_num = host_num + iachar(host_name(i:i)) * 31**(name_len-i)
end do
host_num = abs(host_num)
print *, myrank+1, 'host_num = ', host_num; call flush(6)
!call MPI_COMM_SPLIT(MPI_COMM_WORLD,host_num,0,node_comm,ierr)
call MPI_COMM_SPLIT(MPI_COMM_WORLD,host_num,0,node_comm,ierr)
call MPI_COMM_RANK(node_comm,node_rank,ierr)
call MPI_COMM_SIZE(node_comm,node_procs,ierr)
print *, 'This is MPI process', myrank+1, 'of', tot_procs, &
'; I am process', node_rank+1, 'of', node_procs, 'on node: ', trim(host_name)
call flush(6)
call MPI_BARRIER(MPI_COMM_WORLD,ierr)
!--- set up MPI communicators
!call MPI_COMM_SPLIT(MPI_COMM_WORLD,node_rank,0,seg_comm,ierr)
!call MPI_COMM_SPLIT(MPI_COMM_WORLD,myrank,0,seg_comm,ierr)
seg_comm = MPI_COMM_WORLD
!--- make unused processes wait
!if (node_rank > 0) then
! call MPI_BCAST(tot_procs,1,MPI_INTEGER,0,MPI_COMM_WORLD,ierr)
! call MPI_FINALIZE(ierr)
! stop
!end if
call MPI_COMM_RANK(seg_comm,seg_rank,ierr)
call MPI_COMM_SIZE(seg_comm,seg_nodes,ierr)
print *, myrank+1, ' : node root process', seg_rank+1, 'of', seg_nodes; call flush(6)
call MPI_BARRIER(seg_comm,ierr)
!--------------- GET ARGS -----------------
if (myrank == 0) then
print "(a)", 'Processing arguments ...'
call flush(6)
call get_cmd_args ! process input arguments from command line and file
call date_and_time(date,time) ! tag output with (hopefully) unique month/day/hr/min/sec of execution
call system_clock(c0)
if (outdirparam == '') then
call system('mkdir '//outdir//date(5:8)//'_'//time(1:6))
open(unit=20,file=outdir//date(5:8)//'_'//time(1:6)//'/'//printoutfile,status='replace')
else
call system('mkdir '//trim(outdirparam)) ! Make the output directory specified in the parameter file
open(unit=20,file=trim(outdirparam)//'/'//printoutfile,status='replace')
end if
print *, 'output directory = ', trim(outdirparam); call flush(6)
end if
!--------------- INITIALIZE OPENMP ----------------
call MPI_BCAST(max_thrds,1,MPI_INTEGER,0,seg_comm,ierr)
!if ((node_procs == 1) .and. (seg_nodes == 1)) then
num_thrds = max_thrds
!else
! num_thrds = min(max_thrds,node_procs)
!end if
print *, myrank+1, ': setting num_thrds to ', num_thrds, ' ...'; call flush(6)
call omp_set_num_threads(num_thrds)
!$OMP PARALLEL DEFAULT(SHARED) &
!$OMP & PRIVATE(thrdnum) SHARED(num_thrds_used)
thrdnum = omp_get_thread_num()
if (thrdnum == 0) then
num_thrds_used = omp_get_num_threads()
end if
!$OMP END PARALLEL
num_thrds = num_thrds_used
print *, myrank+1, ': using', num_thrds, 'threads.'
call flush(6)
call MPI_BARRIER(seg_comm,ierr)
!--- gather num thrds used by all procs on root
if (myrank == 0) then
allocate(node_thrds(seg_nodes),stat=ierr); call tststat(ierr)
end if
call MPI_GATHER(num_thrds,1,MPI_INTEGER,node_thrds,1,MPI_INTEGER,0,seg_comm,ierr)
if (myrank == 0) then
print *, myrank+1, ': node_thrds = ', node_thrds
end if
!---------------- GET RECL MULTIPLIER ----------------
if (myrank == 0) then
call bytes_in_rec(nbyte)
print *, myrank+1, ': REAL nbyte = ', nbyte; call flush(6)
end if
!--------------- BROADCAST VARIABLES ----------------
call MPI_BCAST(num_files,1,MPI_INTEGER,0,seg_comm,ierr)
if (seg_rank > 0) then
allocate(infile(num_files))
allocate(num_samples(num_files))
allocate(field_dim(num_files))
end if
do k = 1,num_files
call MPI_BCAST(infile(k),500,MPI_CHARACTER,0,seg_comm,ierr)
end do
call MPI_BCAST(num_samples,num_files,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(data_dim,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(field_dim,num_files,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(filter_length,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(do_opt_block,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(do_sphere,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(do_mean,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_sphere,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_mean,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(update_A,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(update_c,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(update_mu,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(update_alpha,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(update_gm,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(update_beta,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(dorho,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_A,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_c,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_mu,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_alpha,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_gm,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_beta,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(load_rho,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(outdirparam,500,MPI_CHARACTER,0,seg_comm,ierr)
call MPI_BCAST(indirparam,500,MPI_CHARACTER,0,seg_comm,ierr)
call MPI_BCAST(write_LLt,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(write_nd,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(block_size,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(blk_min,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(blk_max,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(blk_step,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(do_reject,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(maxrej,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(rejstart,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(rejint,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(rejsig,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(invsigmax,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(load_rej,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(chpdfstart,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(chpdfint,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(numchpdf,1,MPI_INTEGER,0,seg_comm,ierr)
!call MPI_BCAST(decwindow,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(do_newton,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(newt_start,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(newt_ramp,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(newtrate,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(dft_length,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(doscaling,1,MPI_LOGICAL,0,seg_comm,ierr)
call MPI_BCAST(scalestep,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(writestep,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(max_iter,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(num_models,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(nbyte,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(pdftype,1,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(lrate,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(lrate0,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(rholrate0,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(minrho,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(maxrho,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(rho0,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(minlrate,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(lratefact,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(rholratefact,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
!--- set up the random number generator for this node
call system_clock(c1)
call random_seed(PUT = c1 * (myrank+1) * (seed+myrank+1))
!call DRANDINITIALIZE(1,1,(myrank+1)*(c1/tot_procs + 1),lseed,state,lstate,info)
!-------------------- GET THE DATA ------------------------
!--- calculate file/offsets for the segment nodes
allocate(seg_list(seg_nodes,2,3)) ! start/stop, file/sample/coord
if (myrank == 0) then
print *, 'getting segment list ...'; call flush(6)
call get_seg_list
end if
call MPI_BCAST(seg_list,seg_nodes*2*3,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(all_blks,1,MPI_INTEGER,0,seg_comm,ierr)
numgoodsum = all_blks
!-------------- load the data and build samplist for this node -------------
call get_data
print *, myrank+1, ': data = ', dataseg(1)%data(1:2,1); call flush(6)
!print *, myrank+1, ': numsegs = ', numsegs; call flush(6)
allocate(blk_size(numsegs),stat=ierr); call tststat(ierr)
do seg = 1,numsegs
fieldsize = dataseg(seg)%lastdim
!print *, myrank+1, 'seg ', seg, ': fieldsize = ', fieldsize; call flush(6)
if (fieldsize < num_thrds) then
print *, 'Error: minimum segment size too small'; call flush(6)
quittmp = 1
exit
else
quittmp = 0
end if
blk_size(seg) = min(fieldsize,block_size)
end do
call MPI_ALLREDUCE(quittmp,quit,1,MPI_INTEGER,MPI_MAX,seg_comm,ierr)
if (quit == 1) then
stop
end if
!---------------------------- get the mean --------------------------------
nx = data_dim
allocate( work(5*nx*nx),stat=ierr); call tststat(ierr)
allocate(mean(nx))
mean = dble(0.0)
if (load_mean) then
if (seg_rank == 0) then
print *, myrank+1, ': reading mean from: ', trim(adjustl(indirparam))//'/'//'mean'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'mean',access='direct',recl=2*nbyte*nx)
read(15,rec=1) mean
close(15)
!print *, 'mean = ', mean(1:min(5,nw)); call flush(6)
end if
elseif (do_mean) then
if (myrank == 0) then
print *, 'getting the mean ...'; call flush(6)
end if
allocate(meantmp(nx))
meantmp = dble(0.0)
call DSCAL(nx,dble(0.0),meantmp,1)
cnt1 = 0
do seg = 1,numsegs
fnum = dataseg(seg)%filenum
fieldsize = dataseg(seg)%lastdim
ldim = dataseg(seg)%lastdim
call DAXPY(nx,dble(1.0),sum(dataseg(seg)%data(:,1:ldim),2),1,meantmp,1)
cnt1 = cnt1 + fieldsize
end do
call DSCAL(nx,dble(0.0),mean,1)
cnt = 0
call MPI_REDUCE(meantmp,mean,nx,MPI_DOUBLE_PRECISION,MPI_SUM,0,seg_comm,ierr)
call MPI_REDUCE(cnt1,cnt,1,MPI_INTEGER,MPI_SUM,0,seg_comm,ierr)
if (myrank == 0) then
call DSCAL(nx,dble(1.0)/dble(cnt),mean,1)
print *, ' mean = ', mean(1:3); call flush(6)
end if
end if
call MPI_BCAST(mean,nx,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
!--- subtract the mean
if (myrank == 0) then
print *, 'subtracting the mean ...'; call flush(6)
end if
do seg = 1,numsegs
fnum = dataseg(seg)%filenum
fieldsize = dataseg(seg)%lastdim
num_blocks = fieldsize / blk_size(seg)
lastblocksize = mod(fieldsize,blk_size(seg))
!print *, 'num_blocks = ', num_blocks, ' lastblocksize = ', lastblocksize, ' fieldsize = ', fieldsize; call flush(6);
do k = 1,num_blocks
bstrt = (k-1)*blk_size(seg) + 1
bstp = bstrt + blk_size(seg) - 1
call DAXPY(nx*blk_size(seg),dble(-1.0),spread(mean,2,blk_size(seg)),1,dataseg(seg)%data(:,bstrt:bstp),1)
end do
if (lastblocksize .ne. 0) then
bstrt = num_blocks*blk_size(seg) + 1
bstp = fieldsize
call DAXPY(nx*lastblocksize,dble(-1.0),spread(mean,2,lastblocksize),1,dataseg(seg)%data(:,bstrt:bstp),1)
end if
end do
!------------------------ sphere the data -------------------------------
allocate(S(nx,nx)); S = dble(0.0)
allocate(Stmp(nx,nx)); Stmp = dble(0.0)
!--- get the covariance matrix
if (myrank == 0) then
print *, 'getting the covariance matrix ...'; call flush(6)
end if
call DSCAL(nx*nx,dble(0.0),Stmp,1)
cnt1 = 0
do seg = 1,numsegs
fnum = dataseg(seg)%filenum
fieldsize = dataseg(seg)%lastdim
ldim = dataseg(seg)%lastdim
!print *, 'seg ', seg, ' fnum = ', fnum, ' fieldsize = ', fieldsize, ' ldim = ', ldim; call flush(6)
num_blocks = fieldsize / blk_size(seg)
lastblocksize = mod(fieldsize,blk_size(seg))
!print *, 'seg ', seg, ' num_blocks = ', num_blocks, ' lastblocksize = ', lastblocksize; call flush(6)
do k = 1,num_blocks
bstrt = (k-1)*blk_size(seg) + 1
bstp = bstrt + blk_size(seg) - 1
call DSYRK('L','N',nx,blk_size(seg),dble(1.0),dataseg(seg)%data(:,bstrt:bstp),nx,dble(1.0),Stmp,nx)
end do
if (lastblocksize .ne. 0) then
bstrt = num_blocks*blk_size(seg) + 1
bstp = ldim
call DSYRK('L','N',nx,lastblocksize,dble(1.0),dataseg(seg)%data(:,bstrt:bstp),nx,dble(1.0),Stmp,nx)
end if
cnt1 = cnt1 + fieldsize
end do
call DSCAL(nx*nx,dble(0.0),S,1)
cnt = 0
call MPI_REDUCE(Stmp,S,nx*nx,MPI_DOUBLE_PRECISION,MPI_SUM,0,seg_comm,ierr)
call MPI_REDUCE(cnt1,cnt,1,MPI_INTEGER,MPI_SUM,0,seg_comm,ierr)
if (myrank == 0) then
print *, 'cnt = ', cnt; call flush(6)
call DSCAL(nx*nx,dble(1.0)/dble(cnt),S,1)
end if
call MPI_BCAST(S,nx*nx,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
allocate(eigs(nx))
allocate(eigv(nx))
!lwork = 10*nx*nx
!print *, 'doing eig nx = ', nx, ' lwork = ', lwork; call flush(6)
!call DCOPY(nx*nx,S,1,Stmp,1)
!call DSYEV('V','L',nx,Stmp,nx,eigs,work,lwork,info)
!if (info == 0) then
! print *, 'minimum eigenvalues = ', eigs(1:min(nx/2,3))
! write(20,*) 'minimum eigenvalues = ', eigs(1:min(nx/2,3))
! print *, 'maximum eigenvalues = ', eigs(nx:(nx-min(nx/2,3)+1):-1)
! write(20,*) 'maximum eigenvalues = ', eigs(nx:(nx-min(nx/2,3)+1):-1)
! call flush(6)
!else
! print "(a)", 'Error doing eigenvalue decomposition!!!'
! write(20,"(a)") 'Error doing eigenvalue decomposition!!!'
! call flush(6)
!end if
!numeigs = min(pcakeep,count(eigs > mineig))
!print *, 'num eigs kept = ', numeigs; call flush(6)
!write(20,*) 'num eigs kept = ', numeigs; call flush(6)
if (load_sphere) then
if (seg_rank == 0) then
print *, myrank+1, ': reading S from: ', trim(adjustl(indirparam))//'/'//'S'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'S',access='direct',recl=2*nbyte*nx*nx)
read(15,rec=1) S
close(15)
!print *, 'initial S = ', S(1:min(5,nx*nx)); call flush(6)
call DCOPY(nx*nx,S,1,Stmp,1)
lwork = 5*nx*nx
allocate( wr(nx),stat=ierr); call tststat(ierr)
call DGEQRF(nx,nx,Stmp,nx,wr,work,lwork,info)
if (info < 0) then
print *, 'error getting QR!!!'
end if
sldet = dble(0.0)
do i = 1,numeigs
sldet = sldet + log(Stmp(i,i))
end do
deallocate(wr)
end if
else
!--- get the sphering matrix
if (myrank == 0) then
print *, 'getting the sphering matrix ...'; call flush(6)
end if
if (do_sphere) then
if (seg_rank == 0) then
lwork = 10*nx*nx
!print *, 'doing eig nx = ', nx, ' lwork = ', lwork; call flush(6)
call DCOPY(nx*nx,S,1,Stmp,1)
call DSYEV('V','L',nx,Stmp,nx,eigs,work,lwork,info)
if (info == 0) then
print *, 'minimum eigenvalues = ', eigs(1:min(nx/2,3))
write(20,*) 'minimum eigenvalues = ', eigs(1:min(nx/2,3))
print *, 'maximum eigenvalues = ', eigs(nx:(nx-min(nx/2,3)+1):-1)
write(20,*) 'maximum eigenvalues = ', eigs(nx:(nx-min(nx/2,3)+1):-1)
call flush(6)
else
print "(a)", 'Error doing eigenvalue decomposition!!!'
write(20,"(a)") 'Error doing eigenvalue decomposition!!!'
call flush(6)
end if
numeigs = min(pcakeep,count(eigs > mineig))
print *, 'num eigs kept = ', numeigs; call flush(6)
write(20,*) 'num eigs kept = ', numeigs; call flush(6)
allocate(Stmp2(nx,nx)); Stmp2 = dble(0.0)
!call DSCAL(nx*nx,dble(0.0),Stmp2,1)
!---reverse the order of eigenvectors
do i = 1,nx
eigv(i) = eigs(nx-i+1)
do j = 1,nx
Stmp2(i,j) = Stmp(j,nx-i+1)
end do
!call DCOPY(nx,Stmp(:,nx-i+1),1,Stmp2(:,i),1)
end do
call DCOPY(nx*nx,Stmp2,1,Stmp,1)
sldet = dble(0.0)
do i = 1,numeigs
do j = 1,nx
Stmp2(i,j) = Stmp2(i,j) / sqrt(eigv(i))
!if (isNaN(Stmp2(i,j))) then
! print *, 'NaN! i,j = ', i, ',', j, ' eigv = ', eigv(i); call flush(6)
!end if
end do
sldet = sldet - dble(0.5)*log(eigv(i))
!call DSCAL(nx,dble(1.0)/sqrt(eigs(nx-i+1)),Stmp(:,i),1)
end do
if (numeigs == nx) then
call DSCAL(nx*nx,dble(0.0),S,1)
if (do_approx_sphere) then
call DGEMM('T','N',nx,nx,nx,dble(1.0),Stmp,nx,Stmp2,nx,dble(1.0),S,nx)
else
call DCOPY(nx*nx,Stmp2,1,S,1)
endif
else
if (do_approx_sphere) then
allocate(Stmp3(numeigs,numeigs)); Stmp3 = dble(0.0);
call DCOPY(nx*nx,Stmp,1,S,1)
call DGESVD( 'O', 'A', numeigs, numeigs, S, nx, eigs, S, nx, Stmp, nx, work, lwork, info )
call DSCAL(numeigs*numeigs,dble(0.0),Stmp3,1)
call DGEMM('T','T',numeigs,numeigs,numeigs,dble(1.0),Stmp,nx,S,nx,dble(1.0),Stmp3,numeigs)
call DSCAL(nx*nx,dble(0.0),S,1)
call DGEMM('N','N',numeigs,nx,numeigs,dble(1.0),Stmp3,numeigs,Stmp2,nx,dble(1.0),S,nx)
else
call DCOPY(nx*nx,Stmp2,1,S,1)
end if
end if
end if
else
!--- just normalize by the channel variances (don't sphere)
call DCOPY(nx*nx,S,1,Stmp,1)
call DSCAL(nx*nx,dble(0.0),S,1)
sldet = dble(0.0)
do i = 1,nx
if (Stmp(i,i) .gt. dble(0.0)) then
S(i,i) = dble(1.0) / sqrt(Stmp(i,i))
sldet = sldet + dble(0.5)*log(S(i,i))
end if
end do
numeigs = nx
end if
end if
if (myrank == 0) then
print *, 'sphering the data ...'
call flush(6)
end if
call MPI_BCAST(S,nx*nx,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(sldet,1,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(numeigs,1,MPI_INTEGER,0,seg_comm,ierr)
allocate(xtmp(nx,maxval(blk_size))); xtmp = dble(0.0);
do seg = 1,numsegs
fnum = dataseg(seg)%filenum
fieldsize = dataseg(seg)%lastdim
num_blocks = fieldsize / blk_size(seg)
lastblocksize = mod(fieldsize,blk_size(seg))
do k = 1,num_blocks
bstrt = (k-1)*blk_size(seg) + 1
bstp = bstrt + blk_size(seg) - 1
call DSCAL(nx*blk_size(seg),dble(0.0),xtmp(:,1:blk_size(seg)),1)
call DGEMM('N','N',nx,blk_size(seg),nx,dble(1.0),S,nx,dataseg(seg)%data(:,bstrt:bstp),nx,dble(1.0),xtmp(:,1:blk_size(seg)),nx)
call DCOPY(nx*blk_size(seg),xtmp(:,1:blk_size(seg)),1,dataseg(seg)%data(:,bstrt:bstp),1)
end do
if (lastblocksize .ne. 0) then
bstrt = num_blocks * blk_size(seg) + 1
bstp = bstrt + lastblocksize - 1
call DSCAL(nx*lastblocksize,dble(0.0),xtmp(:,1:lastblocksize),1)
call DGEMM('N','N',nx,lastblocksize,nx,dble(1.0),S,nx,dataseg(seg)%data(:,bstrt:bstp),nx,dble(1.0),xtmp(:,1:lastblocksize),nx)
call DCOPY(nx*lastblocksize,xtmp(:,1:lastblocksize),1,dataseg(seg)%data(:,bstrt:bstp),1)
end if
end do
deallocate(xtmp)
nw = numeigs
if (seg_rank == 0) then
! get the pseudoinverse of S
call DCOPY(nx*nx,S,1,Stmp2,1)
allocate(Spinv(nx,numeigs)); Spinv = dble(0.0)
allocate(sUtmp(numeigs,numeigs))
allocate(sVtmp(numeigs,nx))
print *, 'numeigs = ', numeigs; call flush(6)
call DGESVD( 'A', 'S', numeigs, nx, Stmp2, nx, eigs, sUtmp, numeigs, sVtmp, numeigs, work, lwork, info )
do i = 1,numeigs
sVtmp(i,:) = sVtmp(i,:) / eigs(i)
end do
call DGEMM('T','T',nx,numeigs,numeigs,dble(1.0),sVtmp,numeigs,sUtmp,numeigs,dble(0.0),Spinv,nx)
end if
if (seg_rank == 0 .and. print_debug) then
print *, 'S = '; call flush(6)
call matout(S(1:2,1:2),2,2)
print *, 'Sphered data = '; call flush(6)
call matout(dataseg(1)%data(1:2,1:2),2,2)
end if
call flush(6)
call MPI_BARRIER(seg_comm,ierr)
!-------------------- ALLOCATE VARIABLES ---------------------
if (num_comps .eq. -1) then
num_comps = nw * num_models
end if
print *, myrank + 1, ': Allocating variables ...'; call flush(6)
allocate( Dtemp (num_models),stat=ierr); call tststat(ierr); Dtemp = dble(0.0)
allocate( Dsum (num_models),stat=ierr); call tststat(ierr); Dsum = dble(0.0)
if (myrank == 0) then
allocate( LL (max(1,max_iter)),stat=ierr); call tststat(ierr); LL = dble(0.0)
end if
allocate( pdtype(nw,num_models),stat=ierr); call tststat(ierr)
allocate( m2(nw,num_models),stat=ierr); call tststat(ierr); m2 = dble(0.0)
allocate( m4(nw,num_models),stat=ierr); call tststat(ierr); m4 = dble(0.0)
allocate( m2sum(nw,num_models),stat=ierr); call tststat(ierr); m2sum = dble(0.0)
allocate( m4sum(nw,num_models),stat=ierr); call tststat(ierr); m4sum = dble(0.0)
allocate( nsub(num_models),stat=ierr); call tststat(ierr);
pdtype = pdftype
if (pdftype == 1) then
do_choose_pdfs = .true.
numchpdf = 0
end if
allocate( comp_list (nw,num_models),stat=ierr); call tststat(ierr)
allocate( comp_used (num_comps),stat=ierr); call tststat(ierr)
allocate( c (nw,num_models),stat=ierr); call tststat(ierr); c = dble(0.0)
allocate( dc_numer_tmp_t (nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dc_numer_tmp_t = dble(0.0)
allocate( dc_numer_tmp (nw,num_models),stat=ierr); call tststat(ierr); dc_numer_tmp = dble(0.0)
allocate( dc_numer (nw,num_models),stat=ierr); call tststat(ierr); dc_numer = dble(0.0)
allocate( dc_denom_tmp_t (nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dc_denom_tmp_t = dble(0.0)
allocate( dc_denom_tmp (nw,num_models),stat=ierr); call tststat(ierr); dc_denom_tmp = dble(0.0)
allocate( dc_denom (nw,num_models),stat=ierr); call tststat(ierr); dc_denom = dble(0.0)
allocate( cx (nx,num_models),stat=ierr); call tststat(ierr); cx = dble(0.0)
allocate( A (nw,num_comps),stat=ierr); call tststat(ierr); A = dble(0.0)
allocate( Ax (nx,num_comps),stat=ierr); call tststat(ierr); Ax = dble(0.0)
allocate( W (nw,nw,num_models),stat=ierr); call tststat(ierr); W = dble(0.0)
allocate( wc (nw,num_models),stat=ierr); call tststat(ierr); wc = dble(0.0)
allocate( LLtmp_t(num_thrds),stat=ierr); call tststat(ierr); LLtmp_t = dble(0.0)
if (do_newton) then
allocate( dbaralpha_numer_tmp_t (num_mix,nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dbaralpha_numer_tmp_t = dble(0.0)
allocate( dbaralpha_numer_tmp (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dbaralpha_numer_tmp = dble(0.0)
allocate( dbaralpha_numer (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dbaralpha_numer = dble(0.0)
allocate( dbaralpha_denom_tmp_t (num_mix,nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dbaralpha_denom_tmp_t = dble(0.0)
allocate( dbaralpha_denom_tmp (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dbaralpha_denom_tmp = dble(0.0)
allocate( dbaralpha_denom (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dbaralpha_denom = dble(0.0)
allocate( dlambda_numer_tmp_t (num_mix,nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dlambda_numer_tmp_t = dble(0.0)
allocate( dlambda_numer_tmp (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dlambda_numer_tmp = dble(0.0)
allocate( dlambda_numer (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dlambda_numer = dble(0.0)
allocate( dlambda_denom_tmp_t (num_mix,nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dlambda_denom_tmp_t = dble(0.0)
allocate( dlambda_denom_tmp (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dlambda_denom_tmp = dble(0.0)
allocate( dlambda_denom (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dlambda_denom = dble(0.0)
allocate( dkappa_numer_tmp_t (num_mix,nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dkappa_numer_tmp_t = dble(0.0)
allocate( dkappa_numer_tmp (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dkappa_numer_tmp = dble(0.0)
allocate( dkappa_numer (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dkappa_numer = dble(0.0)
allocate( dkappa_denom_tmp_t (num_mix,nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dkappa_denom_tmp_t = dble(0.0)
allocate( dkappa_denom_tmp (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dkappa_denom_tmp = dble(0.0)
allocate( dkappa_denom (num_mix,nw,num_models),stat=ierr); call tststat(ierr); dkappa_denom = dble(0.0)
allocate( dsigma2_numer_tmp_t (nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dsigma2_numer_tmp_t = dble(0.0)
allocate( dsigma2_numer_tmp (nw,num_models),stat=ierr); call tststat(ierr); dsigma2_numer_tmp = dble(0.0)
allocate( dsigma2_numer (nw,num_models),stat=ierr); call tststat(ierr); dsigma2_numer = dble(0.0)
allocate( dsigma2_denom_tmp_t (nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dsigma2_denom_tmp_t = dble(0.0)
allocate( dsigma2_denom_tmp (nw,num_models),stat=ierr); call tststat(ierr); dsigma2_denom_tmp = dble(0.0)
allocate( dsigma2_denom (nw,num_models),stat=ierr); call tststat(ierr); dsigma2_denom = dble(0.0)
end if
allocate( Wtmp(nw,nw),stat=ierr); call tststat(ierr); Wtmp = dble(0.0)
allocate( Wtmp2(nw,nw,num_thrds),stat=ierr); call tststat(ierr); Wtmp2 = dble(0.0)
allocate( dAk (nw,num_comps),stat=ierr); call tststat(ierr); dAk = dble(0.0)
allocate( dA (nw,nw,num_models),stat=ierr); call tststat(ierr); dA = dble(0.0)
allocate( dWtmp_t (nw,nw,num_models,num_thrds),stat=ierr); call tststat(ierr); dWtmp_t = dble(0.0)
allocate( dWtmp (nw,nw,num_models),stat=ierr); call tststat(ierr); dWtmp = dble(0.0)
allocate( wr(nw),stat=ierr); call tststat(ierr); wr = dble(0.0)
allocate( nd(max(1,max_iter),num_comps),stat=ierr); call tststat(ierr); nd = dble(0.0)
allocate( zeta (num_comps),stat=ierr); call tststat(ierr); zeta = dble(0.0)
allocate( baralpha (num_mix,nw,num_models),stat=ierr); call tststat(ierr); baralpha = dble(0.0)
allocate( kappa (nw,num_models),stat=ierr); call tststat(ierr); kappa = dble(0.0)
allocate( lambda (nw,num_models),stat=ierr); call tststat(ierr); lambda = dble(0.0)
allocate( sigma2 (nw,num_models),stat=ierr); call tststat(ierr); sigma2 = dble(0.0)
allocate( gm (num_models),stat=ierr); call tststat(ierr); gm = dble(0.0)
!if (update_gm) then
allocate( dgm_numer_tmp_t (num_models,num_thrds),stat=ierr); call tststat(ierr); dgm_numer_tmp_t = dble(0.0)
allocate( dgm_numer_tmp (num_models),stat=ierr); call tststat(ierr); dgm_numer_tmp = dble(0.0)
allocate( dgm_numer (num_models),stat=ierr); call tststat(ierr); dgm_numer = dble(0.0)
!end if
allocate( alpha (num_mix, num_comps),stat=ierr); call tststat(ierr); alpha = dble(0.0)
if (update_alpha) then
allocate( dalpha_numer (num_mix, num_comps),stat=ierr); call tststat(ierr); dalpha_numer = dble(0.0)
allocate( dalpha_numer_tmp_t (num_mix, num_comps, num_thrds),stat=ierr); call tststat(ierr); dalpha_numer_tmp_t = dble(0.0)
allocate( dalpha_numer_tmp (num_mix, num_comps),stat=ierr); call tststat(ierr); dalpha_numer_tmp = dble(0.0)
allocate( dalpha_denom (num_mix, num_comps),stat=ierr); call tststat(ierr); dalpha_denom = dble(0.0)
allocate( dalpha_denom_tmp_t (num_mix, num_comps, num_thrds),stat=ierr); call tststat(ierr); dalpha_denom_tmp_t = dble(0.0)
allocate( dalpha_denom_tmp (num_mix, num_comps),stat=ierr); call tststat(ierr); dalpha_denom_tmp = dble(0.0)
end if
allocate( mu (num_mix, num_comps),stat=ierr); call tststat(ierr); mu = dble(0.0)
allocate( mutmp (num_mix, num_comps),stat=ierr); call tststat(ierr); mutmp = dble(0.0)
if (update_mu) then
allocate( dmu_numer (num_mix, num_comps),stat=ierr); call tststat(ierr); dmu_numer = dble(0.0)
allocate( dmu_numer_tmp_t (num_mix, num_comps, num_thrds),stat=ierr); call tststat(ierr); dmu_numer_tmp_t = dble(0.0)
allocate( dmu_numer_tmp (num_mix, num_comps),stat=ierr); call tststat(ierr); dmu_numer_tmp = dble(0.0)
allocate( dmu_denom (num_mix, num_comps),stat=ierr); call tststat(ierr); dmu_denom = dble(0.0)
allocate( dmu_denom_tmp_t (num_mix, num_comps, num_thrds),stat=ierr); call tststat(ierr); dmu_denom_tmp_t = dble(0.0)
allocate( dmu_denom_tmp (num_mix, num_comps),stat=ierr); call tststat(ierr); dmu_denom_tmp = dble(0.0)
end if
allocate( sbeta (num_mix, num_comps),stat=ierr); call tststat(ierr); sbeta = dble(0.0);
allocate( sbetatmp (num_mix, num_comps),stat=ierr); call tststat(ierr); sbetatmp = dble(0.0);
if (update_beta) then
allocate( dbeta_numer (num_mix, num_comps),stat=ierr); call tststat(ierr); dbeta_numer = dble(0.0);
allocate( dbeta_denom (num_mix, num_comps),stat=ierr); call tststat(ierr); dbeta_denom = dble(0.0);
allocate( dbeta_numer_tmp_t (num_mix, num_comps, num_thrds),stat=ierr); call tststat(ierr); dbeta_numer_tmp_t = dble(0.0);
allocate( dbeta_numer_tmp (num_mix, num_comps),stat=ierr); call tststat(ierr); dbeta_numer_tmp = dble(0.0);
allocate( dbeta_denom_tmp_t (num_mix, num_comps, num_thrds),stat=ierr); call tststat(ierr); dbeta_denom_tmp_t = dble(0.0);
allocate( dbeta_denom_tmp (num_mix, num_comps),stat=ierr); call tststat(ierr); dbeta_denom_tmp = dble(0.0);
end if
allocate( rho (num_mix, num_comps),stat=ierr); call tststat(ierr); rho = dble(0.0)
if (dorho) then
allocate( rhotmp (num_mix, num_comps),stat=ierr); call tststat(ierr); rhotmp = dble(0.0)
allocate( drho_numer (num_mix, num_comps),stat=ierr); call tststat(ierr); drho_numer = dble(0.0)
allocate( drho_numer_tmp_t (num_mix, num_comps, num_thrds),stat=ierr); call tststat(ierr); drho_numer_tmp_t = dble(0.0)
allocate( drho_numer_tmp (num_mix, num_comps),stat=ierr); call tststat(ierr); drho_numer_tmp = dble(0.0)
allocate( drho_denom (num_mix, num_comps),stat=ierr); call tststat(ierr); drho_denom = dble(0.0)
allocate( drho_denom_tmp_t (num_mix, num_comps, num_thrds),stat=ierr); call tststat(ierr); drho_denom_tmp_t = dble(0.0)
allocate( drho_denom_tmp (num_mix, num_comps),stat=ierr); call tststat(ierr); drho_denom_tmp = dble(0.0)
end if
allocate(ipivnw(nw)); ipivnw = dble(0.0)
allocate(ipivnx(nx)); ipivnx = dble(0.0)
allocate(tmpvec3(nw)); tmpvec3 = dble(0.0)
allocate(tmpvec4(nw)); tmpvec4 = dble(0.0)
allocate(xtmp(100,100)); xtmp = dble(0.0)
call random_number(xtmp)
!call DRANDUNIFORM(100*100,dble(0.0),dble(1.0),state,xtmp,info)
deallocate(xtmp)
!------------------- INITIALIZE VARIABLES ----------------------
print *, myrank+1, ': Initializing variables ...'; call flush(6);
if (seg_rank == 0) then
if (load_gm) then
print *, myrank+1, ': reading gm init from: ', trim(adjustl(indirparam))//'/'//'gm'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'gm',access='direct',recl=2*nbyte*num_models)
read(15,rec=1) gm
close(15)
else
gm = dble(1.0) / dble(num_models)
end if
if (load_alpha) then
print *, myrank+1, ': reading alpha init from: ', trim(adjustl(indirparam))//'/'//'alpha'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'alpha',access='direct',recl=2*nbyte*num_mix*num_comps)
read(15,rec=1) alpha
close(15)
else
alpha(1:num_mix,:) = dble(1.0) / dble(num_mix)
end if
if (load_mu) then
print *, myrank+1, ': reading mu init from: ', trim(adjustl(indirparam))//'/'//'mu'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'mu',access='direct',recl=2*nbyte*num_mix*num_comps)
read(15,rec=1) mu
close(15)
else
do k = 1,num_comps
do j = 1,num_mix
mu(j,k) = dble(j) - dble(1.0) - dble(num_mix-1)/dble(2.0)
end do
end do
if (.not. fix_init) then
call random_number(mutmp)
!call DRANDUNIFORM(num_mix*num_comps,dble(0.0),dble(1.0),state,mutmp,info)
mu(1:num_mix,:) = mu(1:num_mix,:) + dble(0.05)*(dble(1.0) - dble(2.0)*mutmp)
end if
end if
if (load_beta) then
print *, myrank+1, ': reading sbeta init from: ', trim(adjustl(indirparam))//'/'//'sbeta'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'sbeta',access='direct',recl=2*nbyte*num_mix*num_comps)
read(15,rec=1) sbeta
close(15)
else
if (fix_init) then
sbeta(1:num_mix,:) = dble(1.0)
else
call random_number(sbetatmp)
!call DRANDUNIFORM(num_mix*num_comps,dble(0.0),dble(1.0),state,sbetatmp,info)
sbeta(1:num_mix,:) = dble(1.0) + dble(0.1)*(dble(0.5) - sbetatmp)
end if
end if
if (load_rho) then
print *, myrank+1, ': reading rho init from: ', trim(adjustl(indirparam))//'/'//'rho'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'rho',access='direct',recl=2*nbyte*num_mix*num_comps)
read(15,rec=1) rho
close(15)
else
rho(1:num_mix,:) = rho0
end if
if (load_c) then
print *, myrank+1, ': reading c init from: ', trim(adjustl(indirparam))//'/'//'c'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'c',access='direct',recl=2*nbyte*nw*num_models)
read(15,rec=1) c
close(15)
else
c = dble(0.0)
end if
if (load_A) then
print *, myrank+1, ': reading A init from: ', trim(adjustl(indirparam))//'/'//'A'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'A',access='direct',recl=2*nbyte*nw*num_comps)
read(15,rec=1) A
close(15)
do h = 1,num_models
do i = 1,nw
comp_list(i,h) = (h-1) * nw + i
end do
end do
else
!print *, myrank+1, ': Ainit is identity'; call flush(6)
do h = 1,num_models
if (fix_init) then
A(:,(h-1)*nw+1:h*nw) = dble(0.0)
do i = 1,nw
A(i,(h-1)*nw+i) = dble(1.0)
end do
else
call random_number(Wtmp)
!CALL DRANDUNIFORM(nw*nw,dble(0.0),dble(1.0),state,Wtmp,info)
A(:,(h-1)*nw+1:h*nw) = dble(0.01)*(dble(0.5)-Wtmp)
do i = 1,nw
A(i,(h-1)*nw+i) = dble(1.0)
!wnrm = DNRM2(nw,W(i,:,h),1)
Anrmk = sqrt(sum(A(:,(h-1)*nw+i)*A(:,(h-1)*nw+i)))
A(:,(h-1)*nw+i) = A(:,(h-1)*nw+i) / Anrmk
comp_list(i,h) = (h-1) * nw + i
end do
end if
end do
end if
call get_unmixing_matrices !print *, myrank+1, ': Ainit is identity'; call flush(6)
if (load_comp_list) then
print *, myrank+1, ': reading comp_list init from: ', trim(adjustl(indirparam))//'comp_list'; call flush(6)
open(unit=15,file=trim(adjustl(indirparam))//'/'//'c',access='direct',recl=2*nbyte*nw*num_models)
read(15,rec=1) comp_list
close(15)
comp_used = .false.
do h = 1,num_models
do i = 1,nw
comp_used(comp_list(i,h)) = .true.
end do
end do
else
do h = 1,num_models
do i = 1,nw
comp_list(i,h) = (h-1) * nw + i
end do
end do
comp_used = .true.
end if
if (print_debug) then
print *, 'data ='; call flush(6)
call matout(dataseg(1)%data(1:2,1:6),2,6)
print *, 'gm = ', gm; call flush(6)
print *, 'alpha = '; call flush(6)
call matout(alpha(1:2,1:min(6,num_comps)),2,min(6,num_comps))
print *, 'mu = '; call flush(6)
call matout(mu(1:2,1:min(6,num_comps)),2,min(6,num_comps))
print *, 'sbeta = '; call flush(6)
call matout(sbeta(1:2,1:min(6,num_comps)),2,min(6,num_comps))
print *, 'rho = '; call flush(6)
call matout(rho(1:2,1:min(6,num_comps)),2,min(6,num_comps))
print *, 'c = '; call flush(6)
call matout(c(1:2,1:num_models),2,num_models)
print *, 'A = '; call flush(6)
call matout(A(1:2,1:min(6,num_comps)),2,min(6,num_comps))
do h = 1,num_models
print *, 'W ', h, ' = '; call flush(6)
call matout(W(1:2,1:2,h),2,2)
end do
end if
end if
call MPI_BCAST(W,nw*nw*num_models,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(wc,nw*num_models,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(comp_list,nw*num_models,MPI_INTEGER,0,seg_comm,ierr)
call MPI_BCAST(gm,num_models,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(alpha,num_mix*num_comps,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(mu,num_mix*num_comps,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(sbeta,num_mix*num_comps,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(rho,num_mix*num_comps,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
call MPI_BCAST(num_mix,num_comps,MPI_INTEGER,0,seg_comm,ierr)
if (load_rej) then
if (seg_rank == 0) then
print *, myrank+1, ': reading LLt from: ', trim(adjustl(indirparam))//'/'//outfile_LLt; call flush(6)
end if
k = 0
do j = 1,seg_nodes
if (j .eq. seg_rank+1) then
ngood = 0
do seg = 1,numsegs
open(unit=19,file=trim(adjustl(indirparam))//'/'//outfile_LLt,access='direct',recl=2*nbyte)
do i = 1,dataseg(seg)%lastdim
do jj = 1,num_models
read(19,rec=(k+i-1)*(num_models+1)+jj) dataseg(seg)%modloglik(jj,i)
end do
read(19,rec=(k+i)*(num_models+1)) dataseg(seg)%loglik(i)
end do
close(19)
jj = 1
do i = 1,dataseg(seg)%lastdim
if (sum(dataseg(seg)%modloglik(:,i)) == dble(0.0)) then
dataseg(seg)%gooddata(i) = .false.
else
dataseg(seg)%gooddata(i) = .true.
dataseg(seg)%goodinds(jj) = i
jj = jj + 1
end if
end do
dataseg(seg)%numgood = count(dataseg(seg)%gooddata)
ngood = ngood + dataseg(seg)%numgood
k = k + dataseg(seg)%lastdim
end do
end if
call MPI_BCAST(k,1,MPI_INTEGER,j-1,seg_comm,ierr)
end do
call MPI_ALLREDUCE(ngood,numgoodsum,1,MPI_INTEGER,MPI_SUM,seg_comm,ierr)
end if
!-------------------- Determine optimal block size -------------------
if (do_opt_block) then
call determine_block_size
else
call allocate_blocks
endif
print *, myrank + 1, ': block size = ', block_size
do seg = 1,numsegs
blk_size(seg) = min(dataseg(seg)%lastdim,block_size)
end do
call flush(6)
call MPI_BARRIER(seg_comm,ierr)
v = dble(1.0)/dble(num_models)
leave = .false.
print *, myrank+1, ': entering the main loop ...'; call flush(6)
!XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX main loop XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
iter = 1
numrej = 0
call system_clock(c1)
do
if (iter > max_iter) then
exit
end if
!----- get determinants
if (seg_rank == 0) then ! .and. (update_A .or. (iter == 1))) then
do h = 1,num_models
call DCOPY(nw*nw,W(:,:,h),1,Wtmp,1)
lwork = 5*nx*nx
call DGEQRF(nw,nw,Wtmp,nw,wr,work,lwork,info)
if (info < 0) then
print *, 'error getting QR!!!'
end if
Dtemp(h) = dble(0.0)
do i = 1,nw
if (Wtmp(i,i) .ne. dble(0.0)) then
Dtemp(h) = Dtemp(h) + log(abs(Wtmp(i,i)))
else
print *, 'model ', h, ' determinant zero!'; call flush(6)
Dtemp(h) = Dtemp(h) + minlog
end if
end do
end do
Dsum = Dtemp
if (print_debug) then
print *, 'Dsum = ', Dsum; call flush(6)
end if
end if
call MPI_BCAST(Dsum,num_models,MPI_DOUBLE_PRECISION,0,seg_comm,ierr)
!----- get updates: gm, alpha, mu, sbeta, rho, W
!print *, myrank+1, ' calling get_updates ....'; call flush(6)
!print *, myrank+1, ': gm = ', gm(1:min(5,num_models)); call flush(6)
!print *, myrank+1, ': alpha = ', alpha(1:min(5,2*num_mix)); call flush(6)
!print *, myrank+1, ': mu = ', mu(1:min(5,2*num_mix)); call flush(6)
!print *, myrank+1, ': sbeta = ', sbeta(1:min(5,2*num_mix)); call flush(6)
!print *, myrank+1, ': rho = ', rho(1:min(5,2*num_mix)); call flush(6)
!print *, myrank+1, ': c = ', c(1:min(5,nw)); call flush(6)
!print *, myrank+1, ': W = ', W(1:min(5,nw)); call flush(6)