-
Notifications
You must be signed in to change notification settings - Fork 19
/
gp_predict.f95
5290 lines (4182 loc) · 212 KB
/
gp_predict.f95
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
! HND XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
! HND X
! HND X GAP (Gaussian Approximation Potental)
! HND X
! HND X
! HND X Portions of GAP were written by Albert Bartok-Partay, Gabor Csanyi,
! HND X and Sascha Klawohn. Copyright 2006-2021.
! HND X
! HND X Portions of GAP were written by Noam Bernstein as part of
! HND X his employment for the U.S. Government, and are not subject
! HND X to copyright in the USA.
! HND X
! HND X GAP is published and distributed under the
! HND X Academic Software License v1.0 (ASL)
! HND X
! HND X GAP is distributed in the hope that it will be useful for non-commercial
! HND X academic research, but WITHOUT ANY WARRANTY; without even the implied
! HND X warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! HND X ASL for more details.
! HND X
! HND X You should have received a copy of the ASL along with this program
! HND X (e.g. in a LICENSE.md file); if not, you can write to the original licensors,
! HND X Gabor Csanyi or Albert Bartok-Partay. The ASL is also published at
! HND X http://github.com/gabor1/ASL
! HND X
! HND X When using this software, please cite the following reference:
! HND X
! HND X A. P. Bartok et al Physical Review Letters vol 104 p136403 (2010)
! HND X
! HND X When using the SOAP kernel or its variants, please additionally cite:
! HND X
! HND X A. P. Bartok et al Physical Review B vol 87 p184115 (2013)
! HND X
! HND XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
!XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
!X
!X Gaussian Process module
!X
!% Module for general GP function interpolations.
!% A gp object contains the training set (fitting points and function values),
!% important temporary matrices, vectors and parameters.
!X
!XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include "error.inc"
module gp_predict_module
use iso_c_binding, only : C_NULL_CHAR
! use libatoms_module
use error_module
#ifdef _OPENMP
use omp_lib
#endif
use system_module, only : idp, dp, qp, optional_default, reallocate, NUMERICAL_ZERO, &
system_timer, string_to_numerical, print_warning, progress, progress_timer, &
current_times, InOutput, OUTPUT, increase_to_multiple, i2si, PRINT_VERBOSE
use units_module
use linearalgebra_module
use extendable_str_module
use dictionary_module
use paramreader_module
use descriptors_module
use fox_wxml
use FoX_sax, only: xml_t, dictionary_t, haskey, getvalue, parse, &
open_xml_string, close_xml_t
use CInOutput_module, only : quip_md5sum
use task_manager_module
use matrix_module
use MPI_context_module, only : scatterv
implicit none
private
integer, parameter :: besseli_max_n = 20
real(dp), dimension(besseli_max_n), parameter :: besseli0_c = (/ &
0.125_dp, &
7.03125E-002_dp, &
7.32421875E-002_dp, &
0.112152099609375_dp, &
0.22710800170898438_dp, &
0.57250142097473145_dp, &
1.7277275025844574_dp, &
6.0740420012734830_dp, &
24.380529699556064_dp, &
110.01714026924674_dp, &
551.33589612202059_dp, &
3038.0905109223841_dp, &
18257.755474293175_dp, &
118838.42625678326_dp, &
832859.30401628942_dp, &
6252951.4934347980_dp, &
50069589.531988934_dp, &
425939216.50476694_dp, &
3836255180.2304339_dp, &
36468400807.065559_dp /)
real(dp), dimension(besseli_max_n), parameter :: besseli1_c = (/ &
-0.375_dp, &
-0.1171875_dp, &
-0.1025390625_dp, &
-0.144195556640625_dp, &
-0.27757644653320313_dp, &
-0.67659258842468262_dp, &
-1.9935317337512970_dp, &
-6.8839142681099474_dp, &
-27.248827311268542_dp, &
-121.59789187653587_dp, &
-603.84407670507017_dp, &
-3302.2722944808525_dp, &
-19718.375912236628_dp, &
-127641.27264617461_dp, &
-890297.87670706783_dp, &
-6656367.7188176867_dp, &
-53104110.109685220_dp, &
-450278600.30503929_dp, &
-4043620325.1077542_dp, &
-38338575207.427895_dp /)
real(dp), parameter :: besseli_max_x = 18.0_dp
real(dp), parameter :: THETA_MIN = 1.0e-8_dp
integer, parameter, public :: GP_SPARSE_RANDOM = 1
integer, parameter, public :: GP_SPARSE_PIVOT = 2
integer, parameter, public :: GP_SPARSE_CLUSTER = 3
integer, parameter, public :: GP_SPARSE_UNIFORM = 4
integer, parameter, public :: GP_SPARSE_KMEANS = 5
integer, parameter, public :: GP_SPARSE_COVARIANCE = 6
integer, parameter, public :: GP_SPARSE_UNIQ = 7
integer, parameter, public :: GP_SPARSE_FUZZY = 8
integer, parameter, public :: GP_SPARSE_FILE = 9
integer, parameter, public :: GP_SPARSE_INDEX_FILE = 10
integer, parameter, public :: GP_SPARSE_CUR_COVARIANCE = 11
integer, parameter, public :: GP_SPARSE_CUR_POINTS = 12
integer, parameter, public :: GP_SPARSE_NONE = 13
integer, parameter, public :: GP_SPARSE_SKIP = 99 ! internal use for MPI
integer, parameter, public :: COVARIANCE_NONE = 0
integer, parameter, public :: COVARIANCE_ARD_SE = 1
integer, parameter, public :: COVARIANCE_DOT_PRODUCT = 2
integer, parameter, public :: COVARIANCE_BOND_REAL_SPACE = 3
integer, parameter, public :: COVARIANCE_PP = 4
integer, parameter, public :: PP_Q = 1
! loop iterations per OpenMP thread, 0: each thread gets a single block of similar size
integer, public, save :: openmp_chunk_size = 0
type gpCovariance_bond_real_space
integer :: n
real(dp) :: delta
real(dp) :: atom_sigma
logical :: initialised = .false.
endtype gpCovariance_bond_real_space
type gpCovariance_atom_real_space
integer :: l_max = 0
real(dp) :: atom_sigma, delta, zeta
real(dp) :: cutoff, cutoff_transition_width
logical :: initialised = .false.
endtype gpCovariance_atom_real_space
public :: gpCovariance_bond_real_space
public :: gpCovariance_bond_real_space_Calc
public :: gpCoordinates_gpCovariance_bond_real_space_Initialise
public :: gpCovariance_atom_real_space
public :: gpCovariance_atom_real_space_Calc
type gpCoordinates
integer :: d = 0, n_x, n_xPrime, n_sparseX, n_permutations
! dimension of descriptors, number of descriptors, number of derivatives of descriptors
integer :: current_x, current_xPrime
! pointers to the last added values
real(dp), dimension(:,:), allocatable :: x, xPrime
! descriptors (d,n_x), derivatives of descriptors (d, n_xPrime)
! for real space covariance descriptors (max(x_size),n_x), derivatives of descriptors (max(x_size),n_xPrime)
real(dp), dimension(:), allocatable :: cutoff, cutoffPrime
integer, dimension(:), allocatable :: x_size, xPrime_size
real(dp), dimension(:), allocatable :: covarianceDiag_x_x, covarianceDiag_xPrime_xPrime
real(dp), dimension(:,:), allocatable :: sparseX, covarianceDiag_x_xPrime
real(dp), dimension(:), allocatable :: sparseCutoff
! sparse points stored as real array
! for real space covariance descriptors
integer, dimension(:), allocatable :: sparseX_size
real(dp), dimension(:), allocatable :: covarianceDiag_sparseX_sparseX
real(dp), dimension(:,:,:), allocatable :: sparseX_permuted
real(dp), dimension(:), allocatable :: sparseCovariance
real(dp), dimension(:), allocatable :: theta
! range parameters (d) for descriptors in each directions
real(dp) :: zeta = 0.0_dp
real(dp), dimension(:), allocatable :: alpha
!
real(dp) :: delta, f0 = 0.0_dp, variance_estimate_regularisation = 0.0_dp
! range of GP (function value) and baseline of function
integer, dimension(:), allocatable :: map_x_y, map_xPrime_yPrime, map_xPrime_x, config_type
! which descriptor is used for a given function value, which derivative descriptor is used for a given derivative function, which descriptor is differentiated
integer, dimension(:), allocatable :: map_sparseX_globalSparseX
! sparse point in this descriptor type -> all sparse points in gpFull
integer, dimension(:), allocatable :: sparseX_index
! sparse points stored as indices of the x array
integer, dimension(:,:), allocatable :: permutations
! Lists the permutations symmetries of the coordinates
logical, dimension(:,:), allocatable :: permutation_distance_mask
! pairwise distances that may occur given all permutations
type(gpCovariance_bond_real_space) :: bond_real_space_cov
integer :: covariance_type = COVARIANCE_NONE
type(extendable_str) :: descriptor_str
type(LA_Matrix) :: LA_k_mm
logical :: initialised = .false.
logical :: sparsified = .false.
logical :: variance_estimate_initialised = .false.
logical :: sparse_covariance_initialised = .false.
endtype gpCoordinates
public :: gpCoordinates
type gpFull
integer :: n_y, n_yPrime
! number of function values, number of derivative function values
integer :: n_globalSparseX
! number of all sparse points in every descriptor type
integer :: n_coordinate
! number of different descriptors
integer :: current_y, current_yPrime
real(dp) :: sparse_jitter = 1.0e-5_dp
real(dp), dimension(:), allocatable :: y, yPrime
! function values, derivative function values
real(dp), dimension(:), allocatable :: sigma_y, sigma_yPrime
! estimated error of function values, derivatives
real(dp), dimension(:,:), allocatable :: covariance_subY_y, covariance_subY_subY, covariance_y_y
! covariance matrix
real(dp), dimension(:), allocatable :: covarianceDiag_y_y, lambda, alpha
! covariance matrix
integer, dimension(:), allocatable :: map_y_globalY, map_yPrime_globalY
type(gpCoordinates), dimension(:), allocatable :: coordinate
logical :: do_subY_subY = .true.
logical :: initialised = .false.
endtype gpFull
type gpSparse
integer :: n_coordinate ! number of different descriptors
type(gpCoordinates), dimension(:), allocatable :: coordinate
logical :: initialised = .false.
logical :: fitted = .false.
logical :: do_export_R = .false.
real(dp), dimension(:, :), allocatable :: R
endtype gpSparse
type cplx_1d_array
complex(dp), dimension(:), allocatable :: value
endtype cplx_1d_array
type cplx_2d_array
complex(dp), dimension(:,:), allocatable :: value
endtype cplx_2d_array
type neighbour_descriptor
type(cplx_1d_array), dimension(:), allocatable :: spherical_harmonics
real(dp) :: r
integer :: n
endtype neighbour_descriptor
logical, save :: parse_matched_label, parse_in_gpCoordinates, parse_in_gpFull, parse_in_gpSparse, parse_in_sparseX, parse_sliced, parse_sparseX_separate_file
integer, save :: parse_i_sparseX, parse_i_x, parse_i_xPrime, parse_i_permutation, parse_slice_start, parse_slice_end
type(gpCoordinates), pointer :: parse_gpCoordinates
type(gpFull), pointer :: parse_gpFull
type(gpSparse), pointer :: parse_gpSparse
type(extendable_str), save :: parse_cur_data
integer, dimension(:,:), allocatable :: parse_in_permutations
character(len=1024), save :: parse_gpCoordinates_label, parse_gpFull_label, parse_gpSparse_label
public :: gpFull, gpSparse
public :: gpFull_print_covariances_lambda_globalY
public :: gpSparse_fit
interface initialise
module procedure gpSparse_initialise
endinterface initialise
public :: initialise
interface finalise
module procedure gpFull_Finalise, gpCoordinates_Finalise, gpSparse_finalise, gpNeighbourDescriptor_Finalise
endinterface finalise
public :: finalise
interface gp_setTheta
module procedure gpCoordinates_setTheta, gpFull_setTheta
endinterface gp_setTheta
public :: gp_setTheta
interface gp_setThetaFactor
module procedure gpFull_setTheta_thetaFactor !, gpFull_setTheta_thetaFactorArray, gpFull_setTheta_thetaFactorUniform
endinterface gp_setThetaFactor
public :: gp_setThetaFactor
interface gp_setParameters
module procedure gpFull_setParameters, gpFull_gpCoordinates_setParameters, gpCoordinates_setParameters, &
gpCoordinates_setParameters_sparse, gpSparse_setParameters
endinterface gp_setParameters
public :: gp_setParameters
interface gp_setPermutations
module procedure gpCoordinates_setPermutations, gpFull_setPermutations, gpSparse_setPermutations
endinterface gp_setPermutations
public :: gp_setPermutations
interface gp_addFunctionValue
module procedure gpFull_addFunctionValue
endinterface gp_addFunctionValue
public :: gp_addFunctionValue
interface gp_addFunctionDerivative
module procedure gpFull_addFunctionDerivative
endinterface gp_addFunctionDerivative
public :: gp_addFunctionDerivative
interface gp_addCoordinates
module procedure gpFull_addCoordinates_1Darray, gpFull_addCoordinates_2Darray
endinterface gp_addCoordinates
public :: gp_addCoordinates
interface gp_addCoordinateDerivatives
module procedure gpFull_addCoordinateDerivatives_1Darray, gpFull_addCoordinateDerivatives_2Darray
endinterface gp_addCoordinateDerivatives
public :: gp_addCoordinateDerivatives
interface gp_addDescriptor
module procedure gpFull_addDescriptor
endinterface gp_addDescriptor
public :: gp_addDescriptor
interface gp_printXML
module procedure gpCoordinates_printXML, gpFull_printXML, gpSparse_printXML
endinterface gp_PrintXML
public :: gp_printXML
interface gp_readXML
module procedure gpCoordinates_readXML, gpFull_readXML, gpSparse_readXML, &
gpCoordinates_readXML_string, gpFull_readXML_string, gpSparse_readXML_string
endinterface gp_readXML
public :: gp_readXML
interface gp_covariance_sparse
module procedure gpFull_covarianceMatrix_sparse
endinterface gp_covariance_sparse
public :: gp_covariance_sparse
interface gp_covariance_full
module procedure gpFull_covarianceMatrix
endinterface gp_covariance_full
public :: gp_covariance_full
interface gp_Predict
module procedure gpCoordinates_Predict
endinterface gp_Predict
public :: gp_Predict
interface gp_log_likelihood
module procedure gpCoordinates_log_likelihood
endinterface gp_log_likelihood
public :: gp_log_likelihood
public :: gpCoordinates_Covariance
public :: gpCoordinates_initialise_variance_estimate
public :: covariancePP
public :: gp_write_covariance
contains
#ifdef _OPENMP
function get_chunk_size(n) result(res)
integer, intent(in) :: n
integer :: res
integer :: t
if (openmp_chunk_size == 0) then
! We can't emulate OpenMP default exactly, so we use ceil(n / t)
t = omp_get_num_threads()
res = (n + t - 1) / t
else
res = openmp_chunk_size
end if
end function get_chunk_size
#endif
subroutine gpFull_setParameters(this, n_coordinate, n_y, n_yPrime, sparse_jitter, error)
type(gpFull), intent(inout) :: this
integer, intent(in) :: n_coordinate, n_y, n_yPrime
real(dp), intent(in) :: sparse_jitter
integer, optional, intent(out) :: error
INIT_ERROR(error)
if(this%initialised) call finalise(this,error)
this%n_coordinate = n_coordinate
this%n_y = n_y
this%n_yPrime = n_yPrime
this%current_y = 0
this%current_yPrime = 0
this%sparse_jitter = sparse_jitter
allocate( this%coordinate(n_coordinate) )
allocate( this%y(n_y), this%yPrime(n_yPrime) )
allocate( this%map_y_globalY(n_y), this%map_yPrime_globalY(n_yPrime) )
allocate( this%sigma_y(n_y), this%sigma_yPrime(n_yPrime) )
this%initialised = .true.
endsubroutine gpFull_setParameters
subroutine gpFull_gpCoordinates_setParameters(this, i, d, n_x, n_xPrime, delta, f0, covariance_type, x_size_max, xPrime_size_max, error)
type(gpFull), intent(inout) :: this
integer, intent(in) :: i, d, n_x, n_xPrime
real(dp), intent(in) :: delta, f0
integer, optional, intent(in) :: covariance_type
integer, optional, intent(in) :: x_size_max, xPrime_size_max
integer, optional, intent(out) :: error
INIT_ERROR(error)
if( .not. this%initialised ) then
RAISE_ERROR('gpFull_set_gpCoordinates_parameters: object not initialised',error)
endif
if( i > this%n_coordinate ) then
RAISE_ERROR( 'gpFull_set_gpCoordinates_parameters: access to descriptor '//i//' is not possible as number of descriptors is '//this%n_coordinate,error )
endif
call gpCoordinates_setParameters(this%coordinate(i), d, n_x, n_xPrime, delta, f0, covariance_type = covariance_type, x_size_max=x_size_max, xPrime_size_max=xPrime_size_max, error=error)
endsubroutine gpFull_gpCoordinates_setParameters
subroutine gpCoordinates_setParameters(this, d, n_x, n_xPrime, delta, f0, covariance_type, x_size_max, xPrime_size_max, error)
type(gpCoordinates), intent(inout) :: this
integer, intent(in) :: d, n_x, n_xPrime
real(dp), intent(in) :: delta, f0
integer, optional, intent(in) :: covariance_type
integer, optional, intent(in) :: x_size_max, xPrime_size_max
integer, optional, intent(out) :: error
integer :: i
INIT_ERROR(error)
if(this%initialised) call finalise(this,error)
if( d < 0 ) then
RAISE_ERROR("gpCoordinates_setParameters: negative value of d = "//d,error)
else
this%d = d
endif
if( n_x < 0 ) then
RAISE_ERROR("gpCoordinates_setParameters: negative value of n_x = "//n_x,error)
else
this%n_x = n_x
endif
if( n_xPrime < 0 ) then
RAISE_ERROR("gpCoordinates_setParameters: negative value of n_xPrime = "//n_xPrime,error)
else
this%n_xPrime = n_xPrime
endif
this%delta = delta
this%f0 = f0
this%current_x = 0
this%current_xPrime = 0
this%n_sparseX = 0
this%n_permutations = 1
this%covariance_type = optional_default(COVARIANCE_ARD_SE, covariance_type)
if(present(x_size_max)) then
allocate( this%x(x_size_max,n_x) )
else
allocate( this%x(d,n_x) )
endif
this%x = 0.0_dp
if(present(xPrime_size_max)) then
allocate( this%xPrime(xPrime_size_max,n_xPrime) )
else
allocate( this%xPrime(d,n_xPrime) )
endif
this%xPrime = 0.0_dp
allocate(this%cutoff(n_x))
this%cutoff = 1.0_dp
allocate(this%cutoffPrime(n_xPrime))
this%cutoffPrime = 0.0_dp
allocate( this%config_type(n_x) )
this%config_type = 0
allocate( this%map_x_y(n_x), this%map_xPrime_yPrime(n_xPrime), this%map_xPrime_x(n_xPrime) )
this%map_x_y = 0
this%map_xPrime_yPrime = 0
this%map_xPrime_x = 0
allocate(this%covarianceDiag_x_x(n_x), this%covarianceDiag_xPrime_xPrime(n_xPrime))
this%covarianceDiag_x_x = 1.0_dp
this%covarianceDiag_xPrime_xPrime = 1.0_dp
select case(this%covariance_type)
case(COVARIANCE_BOND_REAL_SPACE)
allocate( this%x_size(n_x), this%xPrime_size(n_xPrime) )
this%x_size = d
this%xPrime_size = 0
allocate( this%theta(1), this%permutations(1,1) )
this%theta = 0.0_dp
this%permutations = 1
case(COVARIANCE_DOT_PRODUCT)
allocate( this%theta(1), this%permutations(1,1) )
this%theta = 0.0_dp
this%permutations = 1
case(COVARIANCE_ARD_SE,COVARIANCE_PP)
allocate( this%theta(d), this%permutations(d,1) )
this%theta = 0.0_dp
this%permutations(:,1) = (/ (i, i=1, d) /)
allocate(this%permutation_distance_mask(this%d,this%d))
this%permutation_distance_mask = .false.
forall(i=1:this%d) this%permutation_distance_mask(i,i) = .true.
endselect
this%sparsified = .false.
this%initialised = .true.
endsubroutine gpCoordinates_setParameters
subroutine gpCoordinates_setParameters_sparse(this, d, n_sparseX, delta, f0, covariance_type, sparseX_size_max, error)
type(gpCoordinates), intent(inout) :: this
integer, intent(in) :: d, n_sparseX
real(dp), intent(in) :: delta, f0
integer, optional, intent(in) :: covariance_type
integer, optional, intent(in) :: sparseX_size_max
integer, optional, intent(out) :: error
integer :: i
INIT_ERROR(error)
if(this%initialised) call finalise(this,error)
this%d = d
this%n_x = 0
this%n_xPrime = 0
this%delta = delta
this%f0 = f0
this%current_x = 0
this%current_xPrime = 0
this%n_sparseX = n_sparseX
this%n_permutations = 1
this%covariance_type = optional_default(COVARIANCE_ARD_SE, covariance_type)
if(present(sparseX_size_max)) then
allocate( this%sparseX(sparseX_size_max,n_sparseX) )
else
allocate( this%sparseX(d,n_sparseX) )
endif
allocate( this%alpha(n_sparseX) )
allocate( this%sparseCutoff(n_sparseX) )
allocate( this%covarianceDiag_sparseX_sparseX(n_sparseX) )
this%covarianceDiag_sparseX_sparseX = 1.0_dp
select case(this%covariance_type)
case(COVARIANCE_BOND_REAL_SPACE)
allocate( this%sparseX_size(n_sparseX) )
this%sparseX_size = d
allocate( this%theta(1), this%permutations(1,1) )
this%theta = 0.0_dp
this%permutations = 1
case(COVARIANCE_DOT_PRODUCT)
allocate( this%theta(1), this%permutations(1,1) )
this%theta = 0.0_dp
this%permutations = 1
case(COVARIANCE_ARD_SE,COVARIANCE_PP)
allocate( this%theta(d), this%permutations(d,1) )
this%theta = 0.0_dp
this%permutations(:,1) = (/ (i, i=1, d) /)
allocate(this%permutation_distance_mask(this%d,this%d))
this%permutation_distance_mask = .false.
forall(i=1:this%d) this%permutation_distance_mask(i,i) = .true.
endselect
this%sparsified = .true.
this%initialised = .true.
endsubroutine gpCoordinates_setParameters_sparse
subroutine gpSparse_setParameters(this,n_coordinate,error)
type(gpSparse), intent(inout) :: this
integer, intent(in) :: n_coordinate
integer, optional, intent(out) :: error
INIT_ERROR(error)
if(this%initialised) call finalise(this,error)
this%n_coordinate = n_coordinate
allocate( this%coordinate(this%n_coordinate) )
endsubroutine gpSparse_setParameters
subroutine gpCoordinates_setPermutations(this,permutations,error)
type(gpCoordinates), intent(inout) :: this
integer, dimension(:,:), intent(in) :: permutations
integer, optional, intent(out) :: error
real(dp), dimension(this%d) :: theta
integer :: i, d
INIT_ERROR(error)
this%n_permutations = size(permutations,2)
select case(this%covariance_type)
case(COVARIANCE_ARD_SE,COVARIANCE_PP)
call reallocate(this%permutations,this%d,this%n_permutations,zero=.true.)
this%permutations = permutations
! Symmetrise theta wrt permutations
theta = this%theta
this%theta = 0.0_dp
do i = 1, this%n_permutations
this%theta = this%theta + theta(this%permutations(:,i))
enddo
this%theta = this%theta / real(this%n_permutations,kind=dp)
this%permutation_distance_mask = .false.
do i = 1, this%n_permutations
do d = 1, this%d
this%permutation_distance_mask(d,this%permutations(d,i)) = .true.
enddo
enddo
case default
endselect
endsubroutine gpCoordinates_setPermutations
subroutine gpFull_setPermutations(this,i_coordinate,permutations,error)
type(gpFull), intent(inout) :: this
integer :: i_coordinate
integer, dimension(:,:), intent(in) :: permutations
integer, optional, intent(out) :: error
INIT_ERROR(error)
if( i_coordinate > this%n_coordinate ) then
RAISE_ERROR( 'gpFull_setPermutations: access to descriptor '//i_coordinate//' is not possible as number of descriptors is set '//this%n_coordinate,error )
endif
call gpCoordinates_setPermutations(this%coordinate(i_coordinate),permutations,error)
endsubroutine gpFull_setPermutations
subroutine gpSparse_setPermutations(this,i_coordinate,permutations,error)
type(gpSparse), intent(inout) :: this
integer :: i_coordinate
integer, dimension(:,:), intent(in) :: permutations
integer, optional, intent(out) :: error
INIT_ERROR(error)
if( i_coordinate > this%n_coordinate ) then
RAISE_ERROR( 'gpSparse_setPermutations: access to descriptor '//i_coordinate//' is not possible as number of descriptors is set '//this%n_coordinate,error )
endif
call gpCoordinates_setPermutations(this%coordinate(i_coordinate),permutations,error)
endsubroutine gpSparse_setPermutations
subroutine gpSparse_initialise(this, from, error)
type(gpSparse), intent(inout) :: this
type(gpFull), intent(in) :: from
integer, optional, intent(out) :: error
integer :: i
if( .not. from%initialised ) then
RAISE_ERROR('gpSparse_initialise: gpFull object not initialised',error)
endif
if(this%initialised) call finalise(this,error)
call gpSparse_setParameters(this, from%n_coordinate)
do i = 1, this%n_coordinate
if( from%coordinate(i)%covariance_type == COVARIANCE_BOND_REAL_SPACE ) then
call gpCoordinates_setParameters_sparse(this%coordinate(i), &
from%coordinate(i)%d, from%coordinate(i)%n_sparseX, from%coordinate(i)%delta, from%coordinate(i)%f0, covariance_type = from%coordinate(i)%covariance_type, &
sparseX_size_max=maxval(from%coordinate(i)%sparseX_size), error=error)
else
call gpCoordinates_setParameters_sparse(this%coordinate(i), &
from%coordinate(i)%d, from%coordinate(i)%n_sparseX, from%coordinate(i)%delta, from%coordinate(i)%f0, covariance_type = from%coordinate(i)%covariance_type, &
error=error)
endif
this%coordinate(i)%alpha = 0.0
this%coordinate(i)%sparseX = from%coordinate(i)%sparseX
this%coordinate(i)%covarianceDiag_sparseX_sparseX = from%coordinate(i)%covarianceDiag_sparseX_sparseX
if(from%coordinate(i)%covariance_type == COVARIANCE_BOND_REAL_SPACE) then
this%coordinate(i)%sparseX_size = from%coordinate(i)%sparseX_size
endif
this%coordinate(i)%theta = from%coordinate(i)%theta
this%coordinate(i)%zeta = from%coordinate(i)%zeta
this%coordinate(i)%descriptor_str = from%coordinate(i)%descriptor_str
this%coordinate(i)%sparseCutoff = from%coordinate(i)%sparseCutoff
call gpSparse_setPermutations(this,i,from%coordinate(i)%permutations,error)
enddo
this%initialised = .true.
end subroutine gpSparse_initialise
subroutine gpSparse_fit(this, from, task_manager, condition_number_norm, error)
type(gpSparse), intent(inout) :: this
type(gpFull), intent(inout) :: from ! actually input; intent(inout) to free memory early
type(task_manager_type), intent(in) :: task_manager
character(len=*), optional, intent(in) :: condition_number_norm
integer, optional, intent(out) :: error
character(len=STRING_LENGTH) :: my_condition_number_norm
integer :: i, j, mb_A, nb_A
integer :: i_coordinate, i_sparseX, i_global_sparseX, n_globalSparseX, n_globalY, i_y, i_yPrime, &
i_globalY, i_global_yPrime, nrows
#ifdef HAVE_QR
real(qp) :: rcond
real(qp), dimension(:,:), allocatable :: c_subYY_sqrtInverseLambda, factor_c_subYsubY, a
real(qp), dimension(:), allocatable :: globalY, alpha
type(LA_Matrix) :: LA_c_subYsubY, LA_q_subYsubY
#else
real(qp), dimension(:,:), allocatable :: c_subYY_inverseLambda, c_subYY_inverseLambda_c_YsubY!, &
! inverse_q_subYsubY, inverse_c_subYsubY
real(qp), dimension(:), allocatable :: globalY, alpha
type(LA_Matrix) :: LA_q_subYsubY
#endif
INIT_ERROR(error)
my_condition_number_norm = optional_default(' ', condition_number_norm)
call gpSparse_initialise(this, from, error)
n_globalSparseX = from%n_globalSparseX
n_globalY = from%n_y + from%n_yPrime
#ifdef HAVE_QR
call system_timer('Build linear system')
allocate(c_subYY_sqrtInverseLambda(n_globalSparseX,n_globalY))
call matrix_product_vect_asdiagonal_sub(c_subYY_sqrtInverseLambda,from%covariance_subY_y,sqrt(1.0_qp/from%lambda)) ! O(NM)
if (allocated(from%covariance_subY_y)) deallocate(from%covariance_subY_y) ! free input component to save memory
if (from%do_subY_subY) then
allocate(factor_c_subYsubY(n_globalSparseX,n_globalSparseX))
call initialise(LA_c_subYsubY,from%covariance_subY_subY,use_allocate=.false.)
call LA_Matrix_Factorise(LA_c_subYsubY,factor_c_subYsubY,error=error)
call finalise(LA_c_subYsubY)
if (allocated(from%covariance_subY_subY)) deallocate(from%covariance_subY_subY) ! free input component to save memory
do i = 1, n_globalSparseX-1
do j = i+1, n_globalSparseX
factor_c_subYsubY(j,i) = 0.0_qp
end do
end do
end if
allocate(alpha(n_globalSparseX))
if (task_manager%active) then
nrows = task_manager%idata(1)
mb_A = task_manager%idata(2)
nb_A = task_manager%idata(3)
allocate(globalY(nrows))
allocate(a(nrows,n_globalSparseX))
alpha = 0.0_qp
globalY = 0.0_qp
a = 0.0_qp
else
allocate(globalY(n_globalY+n_globalSparseX))
allocate(a(n_globalY+n_globalSparseX,n_globalSparseX))
end if
a(1:n_globalY,:) = transpose(c_subYY_sqrtInverseLambda)
if (allocated(c_subYY_sqrtInverseLambda)) deallocate(c_subYY_sqrtInverseLambda)
if (task_manager%active) then
if (.not. allocated(factor_c_subYsubY)) then
! tks: make sure we are not passing in an unallocated array as the input to
! mpi_scatterv on the processes which are receiving data. This was found to
! be an issue on GCC 11 with -O2 and above.
allocate(factor_c_subYsubY(0, 0))
end if
call scatter_shared_task(task_manager, factor_c_subYsubY, a, n_globalY, n_globalSparseX, from%do_subY_subY)
else
a(n_globalY+1:,:) = factor_c_subYsubY
end if
if (allocated(factor_c_subYsubY)) deallocate(factor_c_subYsubY)
if (my_condition_number_norm(1:1) /= ' ') then
if (task_manager%active) then
call print_warning("Condition number of distributed matrix is not implemented.")
else
rcond = matrix_condition_number(a, my_condition_number_norm(1:1))
call print("Condition number (log10) of matrix A (norm "//my_condition_number_norm(1:1)//"): "//-log10(rcond))
end if
end if
globalY = 0.0_qp
do i_y = 1, from%n_y
! loop over all function values
i_globalY = from%map_y_globalY(i_y)
! find unique function value/derivative identifier
globalY(i_globalY) = from%y(i_y)*sqrt(1.0_qp/from%lambda(i_globalY))
enddo
do i_yPrime = 1, from%n_yPrime
! loop over all function values
i_global_yPrime = from%map_yPrime_globalY(i_yPrime)
! find unique function value/derivative identifier
globalY(i_global_yPrime) = from%yPrime(i_yPrime)*sqrt(1.0_qp/from%lambda(i_global_yPrime))
enddo
call system_timer('Build linear system')
call system_timer('Solve linear system')
if (task_manager%active) then
call print("Using ScaLAPACK to solve QR")
call SP_Matrix_QR_Solve(a, globalY, alpha, task_manager%ScaLAPACK_obj, mb_A, nb_A, this%R, this%do_export_R)
else
call print("Using LAPACK to solve QR")
call initialise(LA_q_subYsubY, a, use_allocate=.false.)
call LA_Matrix_QR_Solve_Vector(LA_q_subYsubY, globalY, alpha)
call finalise(LA_q_subYsubY)
end if
call system_timer('Solve linear system')
do i_coordinate = 1, from%n_coordinate
do i_sparseX = 1, from%coordinate(i_coordinate)%n_sparseX
i_global_sparseX = from%coordinate(i_coordinate)%map_sparseX_globalSparseX(i_sparseX)
this%coordinate(i_coordinate)%alpha(i_sparseX) = real(alpha(i_global_sparseX),kind=dp)
enddo
enddo
if(allocated(a)) deallocate(a)
if(allocated(globalY)) deallocate(globalY)
if(allocated(alpha)) deallocate(alpha)
#else
allocate( c_subYY_inverseLambda(n_globalSparseX,n_globalY), c_subYY_inverseLambda_c_YsubY(n_globalSparseX,n_globalSparseX), &
! inverse_q_subYsubY(n_globalSparseX,n_globalSparseX), inverse_c_subYsubY(n_globalSparseX,n_globalSparseX), &
alpha(n_globalSparseX), globalY(n_globalY))
call matrix_product_vect_asdiagonal_sub(c_subYY_inverseLambda,from%covariance_subY_Y,1.0_qp/from%lambda) ! O(NM)
c_subYY_inverseLambda_c_YsubY = matmul(c_subYY_inverseLambda,transpose(from%covariance_subY_Y))
call initialise(LA_q_subYsubY,from%covariance_subY_subY + c_subYY_inverseLambda_c_YsubY)
globalY = 0.0_qp
do i_y = 1, from%n_y
! loop over all function values
i_globalY = from%map_y_globalY(i_y)
! find unique function value/derivative identifier
globalY(i_globalY) = from%y(i_y) !*sqrt(1.0_qp/from%lambda(i_globalY))
enddo
do i_yPrime = 1, from%n_yPrime
! loop over all function values
i_global_yPrime = from%map_yPrime_globalY(i_yPrime)
! find unique function value/derivative identifier
globalY(i_global_yPrime) = from%yPrime(i_yPrime) !*sqrt(1.0_qp/from%lambda(i_global_yPrime))
enddo
call Matrix_Solve(LA_q_subYsubY,matmul(c_subYY_inverseLambda, globalY),alpha)
call finalise(LA_q_subYsubY)
do i_coordinate = 1, from%n_coordinate
do i_sparseX = 1, from%coordinate(i_coordinate)%n_sparseX
i_global_sparseX = from%coordinate(i_coordinate)%map_sparseX_globalSparseX(i_sparseX)
this%coordinate(i_coordinate)%alpha(i_sparseX) = real(alpha(i_global_sparseX),kind=dp)
enddo
enddo
if(allocated(c_subYY_inverseLambda)) deallocate(c_subYY_inverseLambda)
if(allocated(c_subYY_inverseLambda_c_YsubY)) deallocate(c_subYY_inverseLambda_c_YsubY)
! if(allocated(inverse_q_subYsubY)) deallocate(inverse_q_subYsubY)
! if(allocated(inverse_c_subYsubY)) deallocate(inverse_c_subYsubY)
if(allocated(alpha)) deallocate(alpha)
if(allocated(globalY)) deallocate(globalY)
#endif
this%fitted = .true.
endsubroutine gpSparse_fit
! put L part at the end of local A (take info from last task of each worker)
subroutine scatter_shared_task(task_manager, factor_c_subYsubY, a, n_globalY, n_globalSparseX, do_subY_subY)
type(task_manager_type), intent(in) :: task_manager
real(qp), intent(inout) :: factor_c_subYsubY(:,:)
real(qp), intent(inout) :: a(:,:)
integer, intent(in) :: n_globalY
integer, intent(in) :: n_globalSparseX
logical, intent(in) :: do_subY_subY
integer :: n, t, w
integer, allocatable :: counts(:)
real(dp), allocatable :: tmp(:,:)
! scattering works with cols, so transposing input and output
if (do_subY_subY) then
factor_c_subYsubY = transpose(factor_c_subYsubY)
call get_shared_task_counts(task_manager, n_globalSparseX, counts)
else
allocate(counts(1), source=0)
end if
w = task_manager%my_worker_id
t = task_manager%workers(w)%n_tasks
n = task_manager%workers(w)%tasks(t)%idata(1)
allocate(tmp(n_globalSparseX,n))
tmp = 0.0_dp
call scatterv(task_manager%MPI_obj, factor_c_subYsubY, tmp, counts)
a(n_globalY+1:n_globalY+n,:) = transpose(tmp)
end subroutine scatter_shared_task
subroutine get_shared_task_counts(task_manager, ncols, counts)
type(task_manager_type), intent(in) :: task_manager
integer, intent(in) :: ncols
integer, intent(out), allocatable :: counts(:)
integer :: n, o, t, w
allocate(counts(task_manager%n_workers))
counts = 0
o = 0
do w = 1, task_manager%n_workers
t = task_manager%workers(w)%n_tasks
n = task_manager%workers(w)%tasks(t)%idata(1)
counts(w) = n * ncols