-
Notifications
You must be signed in to change notification settings - Fork 5
/
adjac.f95.in
1772 lines (1551 loc) · 46.8 KB
/
adjac.f95.in
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
! -*-f90-*-
!
! adjac: Automatic Differentiation for generating Jacobians.
!
! Copyright (c) 2014, Pauli Virtanen <[email protected]>
! All rights reserved.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions
! are met:
!
! 1. Redistributions of source code must retain the above copyright
! notice, this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright
! notice, this list of conditions and the following disclaimer in the
! documentation and/or other materials provided with the distribution.
!
! 3. Neither the name of the copyright holder nor the names of its
! contributors may be used to endorse or promote products derived from
! this software without specific prior written permission.
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
! POSSIBILITY OF SUCH DAMAGE.
{{default TAPELESS = False}}
module adjac
private
! NOTE: we would like to use derived type finalizers for memory
! deallocation. However, as of 2015-03-12, these are not fully
! implemented in gfortran, and will not be called e.g. on function
! returns that the code here extensively relies on.
type, public :: adjac_double
double precision :: value, vmul
{{if TAPELESS}}
! Don't use default initializers, but rely on the compiler
! handling allocatable elements correctly. Hence,
! .not.allocated(v) implies n=0.
integer :: n
double precision, dimension(:), allocatable :: v
integer, dimension(:), allocatable :: i
{{else}}
integer :: i = 0
{{endif}}
end type adjac_double
type, public :: adjac_complexan
complex(kind=kind(0d0)) :: value, vmul
{{if TAPELESS}}
integer :: n = 0
complex(kind=kind(0d0)), dimension(:), allocatable :: v
integer, dimension(:), allocatable :: i
{{else}}
integer :: i = 0
{{endif}}
end type adjac_complexan
type, public :: adjac_complex
type(adjac_double) :: re, im
end type adjac_complex
logical :: jac_product_mode = .false.
{{if TAPELESS}}
{{else}}
! DAG of differentials
!
! if (sum_map(1 + 2*(i-1)) .ne. 0) then
! ! dependent variable
! d_{i} = sum_mul(1+2*(i-1)) * d_{sum_map(1+2*(i-1))} + sum_mul(2+2*(i-1)) * d_{sum_map(2+2*(i-1))}
! else
! ! independent variable
! d_{i} = D_{i}
!
integer, parameter :: block_size = 16
integer :: free_a = 1, free_q = 1
integer, dimension(:), allocatable :: sum_map_a, sum_map_q
double precision, dimension(:), allocatable :: sum_mul_a
complex(kind=kind(0d0)), dimension(:), allocatable :: sum_mul_q
{{endif}}
{{if TAPELESS}}
{{default pure = "pure"}}
{{default elemental = "pure elemental"}}
{{else}}
{{default pure = ""}}
{{default elemental = "impure elemental"}}
{{endif}}
{{py:
def binops(name):
chrs = ['i', 'd', 'z']
ops = ['aa', 'bb', 'qq', 'ab', 'ba']
for c in chrs:
ops += ['a'+c, 'b'+c, 'q'+c, c+'a', c+'b', c+'q']
oldops = list(ops)
s = "\n".join(" module procedure " + name + "_" + op for op in ops)
return s.lstrip()
}}
public assignment(=)
interface assignment(=)
module procedure assign_ai, assign_ad
module procedure assign_bi, assign_bd, assign_bz, assign_ba
module procedure assign_qi, assign_qd, assign_qz
end interface
public operator(+)
interface operator(+)
{{binops("add")}}
module procedure pos_a, pos_b, pos_q
end interface
public operator(-)
interface operator(-)
{{binops("sub")}}
module procedure neg_a, neg_b, neg_q
end interface
public operator(*)
interface operator(*)
{{binops("mul")}}
end interface operator(*)
public operator(/)
interface operator(/)
{{binops("div")}}
end interface operator(/)
public operator(**)
interface operator(**)
module procedure pow_ai, pow_ad, pow_qi, pow_qd, pow_qz
end interface operator(**)
public matmul
interface matmul
module procedure matmul_aa, matmul_ai, matmul_ia, matmul_ad, matmul_da
module procedure matmul_bb, matmul_bz, matmul_zb
module procedure matmul_qq, matmul_qi, matmul_iq, matmul_qd, matmul_dq, matmul_qz, matmul_zq
end interface matmul
public dble
interface dble
module procedure dble_a, dble_b
end interface dble
public aimag
interface aimag
module procedure aimag_b
end interface aimag
public conjg
interface conjg
module procedure conjg_b
end interface conjg
public abs
interface abs
module procedure abs_a, abs_b
end interface abs
public exp
interface exp
module procedure exp_a, exp_b, exp_q
end interface exp
public sin
interface sin
module procedure sin_a, sin_b, sin_q
end interface sin
public cos
interface cos
module procedure cos_a, cos_b, cos_q
end interface cos
public log
interface log
module procedure log_a, log_b, log_q
end interface log
public sqrt
interface sqrt
module procedure sqrt_a, sqrt_b, sqrt_q
end interface sqrt
interface adjac_set_independent
module procedure set_independent_a, set_independent_q
module procedure set_independent_many_a, set_independent_many_q
end interface adjac_set_independent
interface sum_taylor
module procedure sum_taylor_a, sum_taylor_q
end interface sum_taylor
interface adjac_get_value
module procedure get_value_one_a, get_value_one_q
module procedure get_value_many_a, get_value_many_q
end interface adjac_get_value
interface adjac_get_dense_jacobian
module procedure get_dense_jacobian_a, get_dense_jacobian_q
end interface adjac_get_dense_jacobian
interface adjac_get_coo_jacobian
module procedure get_coo_jacobian_a, get_coo_jacobian_q
end interface adjac_get_coo_jacobian
public adjac_set_independent, adjac_get_value, &
adjac_get_dense_jacobian, adjac_get_coo_jacobian, &
adjac_reset, adjac_free
contains
subroutine fatal_error(msg)
implicit none
character(len=*), intent(in) :: msg
write(*,*) 'adjac: error: ', trim(msg)
stop
end subroutine fatal_error
subroutine adjac_reset(product_mode)
implicit none
logical, optional, intent(in) :: product_mode
if (present(product_mode)) then
jac_product_mode = product_mode
end if
{{if not TAPELESS}}
free_a = 1
free_q = 1
{{endif}}
end subroutine adjac_reset
subroutine adjac_free()
implicit none
{{if not TAPELESS}}
free_a = 1
free_q = 1
if (allocated(sum_map_a)) then
deallocate(sum_map_a)
deallocate(sum_mul_a)
end if
if (allocated(sum_map_q)) then
deallocate(sum_map_q)
deallocate(sum_mul_q)
end if
{{endif}}
end subroutine adjac_free
{{if not TAPELESS}}
pure subroutine heap_siftup(heap, nheap, initpos)
implicit none
integer, intent(in) :: nheap
integer, dimension(*), intent(inout) :: heap
integer, intent(in) :: initpos
integer :: pos, item, pos2, pos3
pos = initpos
pos2 = 2*pos
item = heap(pos)
do while (pos2 <= nheap)
pos3 = pos2 + 1
if (pos3 <= nheap .and. heap(pos3) >= heap(pos2)) then
pos2 = pos3
end if
heap(pos) = heap(pos2)
pos = pos2
pos2 = 2*pos
end do
heap(pos) = item
call heap_siftdown(heap, initpos, pos)
end subroutine heap_siftup
pure subroutine heap_siftdown(heap, initpos, pos0)
implicit none
integer, dimension(*), intent(inout) :: heap
integer, intent(in) :: initpos, pos0
integer :: item, pos, pos2
pos = pos0
item = heap(pos)
do while (pos > initpos)
pos2 = pos/2
if (heap(pos2) < item) then
heap(pos) = heap(pos2)
else
exit
end if
pos = pos2
end do
heap(pos) = item
end subroutine heap_siftdown
pure subroutine heap_push(heap, nheap, item)
implicit none
integer, intent(inout) :: nheap
integer, dimension(*), intent(inout) :: heap
integer, intent(in) :: item
nheap = nheap + 1
heap(nheap) = item
call heap_siftdown(heap, 1, nheap)
end subroutine heap_push
pure subroutine heap_pop(heap, nheap, item)
implicit none
integer, intent(inout) :: nheap
integer, dimension(*), intent(inout) :: heap
integer, intent(out) :: item
item = heap(1)
heap(1) = heap(nheap)
nheap = nheap - 1
if (nheap.gt.1) then
call heap_siftup(heap, nheap, 1)
end if
end subroutine heap_pop
pure subroutine heap_pushpop(heap, nheap, item, item_out)
implicit none
integer, intent(inout) :: nheap
integer, dimension(*), intent(inout) :: heap
integer, intent(in) :: item
integer, intent(out) :: item_out
if (nheap > 0 .and. item < heap(1)) then
item_out = heap(1)
heap(1) = item
if (nheap.gt.1) then
call heap_siftup(heap, nheap, 1)
end if
else
item_out = item
end if
end subroutine heap_pushpop
{{endif}}
{{for TYPE, FTYPE, CHR, CAST, FTYPES2 in [('adjac_double', 'double precision', 'a', 'real', [('integer', 'i'),
('double precision', 'd')]),
('adjac_complexan', 'complex(kind=kind(0d0))', 'q', 'cmplx', [('integer', 'i'),
('double precision', 'd'),
('complex(kind=kind(0d0))', 'z')])]}}
{{if TAPELESS}}
{{pure}} subroutine alloc_mem_{{CHR}}(x, n)
{{else}}
{{pure}} subroutine alloc_mem_{{CHR}}(x)
{{endif}}
implicit none
type({{TYPE}}), intent(inout) :: x
{{if TAPELESS}}
integer, intent(in) :: n
{{else}}
integer, dimension(:), allocatable :: itmp
{{FTYPE}}, dimension(:), allocatable :: tmp
integer :: sz
{{endif}}
if (jac_product_mode) then
return
end if
{{if TAPELESS}}
if (allocated(x%i)) deallocate(x%i)
if (allocated(x%v)) deallocate(x%v)
x%n = n
if (n > 0) then
allocate(x%i(n), x%v(n))
end if
{{else}}
if (.not.allocated(sum_map_{{CHR}})) then
sz = 0
else
sz = size(sum_map_{{CHR}})
end if
if (sz < 2*free_{{CHR}} + 1) then
! Enlarge work space
if (allocated(sum_map_{{CHR}})) then
sz = sz + 2*free_{{CHR}} + 1
allocate(itmp(sz), tmp(sz))
itmp(1:size(sum_map_{{CHR}})) = sum_map_{{CHR}}(:)
tmp(1:size(sum_mul_{{CHR}})) = sum_mul_{{CHR}}(:)
call move_alloc(itmp, sum_map_{{CHR}})
call move_alloc(tmp, sum_mul_{{CHR}})
else
allocate(sum_map_{{CHR}}(100), sum_mul_{{CHR}}(100))
end if
end if
x%i = free_{{CHR}}
free_{{CHR}} = free_{{CHR}} + 1
{{endif}}
end subroutine alloc_mem_{{CHR}}
pure subroutine link_mem_{{CHR}}(dst, src)
implicit none
type({{TYPE}}), intent(inout) :: dst
type({{TYPE}}), intent(in) :: src
{{if TAPELESS}}
integer :: n
if (allocated(src%v)) then
n = src%n
else
n = 0
end if
call alloc_mem_{{CHR}}(dst, n)
dst%n = n
if (n > 0) then
dst%v(1:n) = src%v(1:n)
dst%i(1:n) = src%i(1:n)
end if
{{else}}
dst%i = src%i
{{endif}}
end subroutine link_mem_{{CHR}}
pure subroutine free_mem_{{CHR}}(x)
implicit none
type({{TYPE}}), intent(inout) :: x
{{if TAPELESS}}
if (allocated(x%i)) deallocate(x%i)
if (allocated(x%v)) deallocate(x%v)
x%n = 0
{{else}}
x%i = 0
{{endif}}
end subroutine free_mem_{{CHR}}
subroutine set_independent_{{CHR}}(x, xval, j, dx)
implicit none
type({{TYPE}}), intent(out) :: x
{{FTYPE}}, intent(in) :: xval
{{FTYPE}}, optional, intent(in) :: dx
integer, intent(in) :: j
x%value = xval
if (jac_product_mode) then
if (.not.present(dx)) then
call fatal_error('no dx given to adjac_set_independent when jacobian product mode is active')
end if
x%vmul = dx
else
x%vmul = 1
{{if TAPELESS}}
call alloc_mem_{{CHR}}(x, 1)
x%v(1) = 1
x%i(1) = j
{{else}}
call alloc_mem_{{CHR}}(x)
sum_map_{{CHR}}(1 + 2*(x%i-1)) = 0
sum_map_{{CHR}}(2 + 2*(x%i-1)) = j
{{endif}}
end if
end subroutine set_independent_{{CHR}}
subroutine set_independent_many_{{CHR}}(x, xval, dx)
implicit none
type({{TYPE}}), dimension(:), intent(inout) :: x
{{FTYPE}}, dimension(size(x)), intent(in) :: xval
{{FTYPE}}, dimension(size(x)), optional, intent(in) :: dx
integer :: j
if (present(dx)) then
do j = 1, size(x,1)
call set_independent_{{CHR}}(x(j), xval(j), j, dx(j))
end do
else
do j = 1, size(x,1)
call set_independent_{{CHR}}(x(j), xval(j), j)
end do
end if
end subroutine set_independent_many_{{CHR}}
subroutine get_value_one_{{CHR}}(y, val, dy)
implicit none
type({{TYPE}}), intent(in) :: y
{{FTYPE}}, intent(out) :: val
{{FTYPE}}, optional, intent(out) :: dy
val = y%value
if (present(dy)) then
if (.not. jac_product_mode) then
call fatal_error('call to adjac_get_value with dy when jacobian product mode is not active')
end if
dy = y%vmul
end if
end subroutine get_value_one_{{CHR}}
subroutine get_value_many_{{CHR}}(y, val, dy)
implicit none
type({{TYPE}}), dimension(:), intent(in) :: y
{{FTYPE}}, dimension(size(y,1)), intent(out) :: val
{{FTYPE}}, dimension(size(y,1)), optional, intent(out) :: dy
integer :: j
do j = 1, size(val,1)
val(j) = y(j)%value
end do
if (present(dy)) then
if (.not. jac_product_mode) then
call fatal_error('call to adjac_get_value with dy when jacobian product mode is not active')
end if
do j = 1, size(val,1)
dy(j) = y(j)%vmul
end do
end if
end subroutine get_value_many_{{CHR}}
{{py:
def walk_tape(ftype, chr, assign, accumulate=None, clear=None):
if accumulate is None:
accumulate = """
do concurrent (k=1:block_size)
work(k,ia) = work(k,ia) + sum_mul_{chr}(1+2*(j-1)) * work(k,j)
end do
do concurrent (k=1:block_size)
work(k,ib) = work(k,ib) + sum_mul_{chr}(2+2*(j-1)) * work(k,j)
end do
""".format(chr=chr)
if clear is None:
clear = """
do concurrent (k=1:block_size)
work(k,j) = 0
end do
"""
return """
! Traverse the tape
j_next = 0
if (nwork > 0) then
call heap_pop(iwork, nwork, j_next)
end if
do while (j_next > 0)
j = j_next
j_next = 0
if (256*nwork > j) then
! Heap is too big, probably contains nearly all j values,
! and we are better off just looping through them
nwork = j
exit
end if
ia = sum_map_{chr}(1+2*(j-1))
ib = sum_map_{chr}(2+2*(j-1))
if (ia == 0) then
{assign}
else
{accumulate}
if (imask(ia) == 0 .and. imask(ib) == 0) then
call heap_push(iwork, nwork, ia)
call heap_pushpop(iwork, nwork, ib, j_next)
imask(ia) = 1
imask(ib) = 1
else if (imask(ia) == 0) then
call heap_pushpop(iwork, nwork, ia, j_next)
imask(ia) = 1
else if (imask(ib) == 0) then
call heap_pushpop(iwork, nwork, ib, j_next)
imask(ib) = 1
end if
end if
{clear}
imask(j) = 0
if (nwork > 0 .and. j_next == 0) then
call heap_pop(iwork, nwork, j_next)
end if
end do
do j = nwork, 1, -1
if (imask(j).ne.0) then
ia = sum_map_{chr}(1+2*(j-1))
ib = sum_map_{chr}(2+2*(j-1))
if (ia == 0) then
{assign}
else
{accumulate}
imask(ia) = 1
imask(ib) = 1
end if
{clear}
imask(j) = 0
end if
end do
""".format(ftype=ftype, chr=chr, assign=assign, accumulate=accumulate, clear=clear)
}}
subroutine get_dense_jacobian_{{CHR}}(y, jac_dense)
implicit none
type({{TYPE}}), dimension(:), intent(inout) :: y
{{FTYPE}}, dimension(:,:), intent(out) :: jac_dense
{{if not TAPELESS}}
{{FTYPE}}, dimension(block_size,free_{{CHR}}) :: work
integer, dimension(free_{{CHR}}) :: iwork, imask
integer :: k, j, ia, ib, kmin, kmax, nwork, j_next
if (jac_product_mode) then
call fatal_error('call to adjac_get_dense_jacobian when jacobian product mode is active')
end if
jac_dense = 0
work = 0
imask = 0
do kmin = 1, size(y,1), block_size
kmax = min(kmin + block_size - 1, size(y,1))
nwork = 0
do k = kmin, kmax, 1
if (y(k)%i == 0) cycle
work(k-kmin+1, y(k)%i) = y(k)%vmul
call heap_push(iwork, nwork, y(k)%i)
imask(y(k)%i) = 1
end do
{{walk_tape(FTYPE, CHR, "jac_dense(kmin:kmax,ib) = work(1:(kmax-kmin+1),j)")}}
end do
{{else}}
integer :: i, p
if (jac_product_mode) then
call fatal_error('call to adjac_get_dense_jacobian when jacobian product mode is active')
end if
jac_dense = 0
do concurrent (i=1:size(y,1))
if (allocated(y(i)%v)) then
do p = 1, y(i)%n
jac_dense(i, y(i)%i(p)) = jac_dense(i, y(i)%i(p)) &
+ y(i)%vmul * y(i)%v(p)
end do
end if
end do
{{endif}}
end subroutine get_dense_jacobian_{{CHR}}
subroutine get_coo_jacobian_{{CHR}}(y, nnz, jac_val, jac_i, jac_j)
implicit none
type({{TYPE}}), dimension(:), intent(inout) :: y
{{FTYPE}}, dimension(:), allocatable, intent(inout) :: jac_val
integer, dimension(:), allocatable, intent(inout) :: jac_i, jac_j
integer, intent(out) :: nnz
{{if not TAPELESS}}
{{FTYPE}}, dimension(block_size,free_{{CHR}}) :: work
integer, dimension(free_{{CHR}}) :: iwork, imask
integer, dimension(:), allocatable :: itmp
{{FTYPE}}, dimension(:), allocatable :: vtmp
integer :: kmin, kmax, k, j, ia, ib, nwork, j_next, sz
if (jac_product_mode) then
call fatal_error('call to adjac_get_coo_jacobian when jacobian product mode is active')
end if
if (allocated(jac_val)) deallocate(jac_val)
if (allocated(jac_i)) deallocate(jac_i)
if (allocated(jac_j)) deallocate(jac_j)
sz = free_{{CHR}} + 10
allocate(jac_val(sz), jac_i(sz), jac_j(sz))
nnz = 0
work = 0
imask = 0
do kmin = 1, size(y,1), block_size
kmax = min(kmin + block_size - 1, size(y,1))
nwork = 0
do k = kmin, kmax, 1
if (y(k)%i == 0) cycle
work(k-kmin+1, y(k)%i) = y(k)%vmul
call heap_push(iwork, nwork, y(k)%i)
imask(y(k)%i) = 1
end do
{{walk_tape(FTYPE, CHR, """
if (nnz + (kmax-kmin) + 1 >= sz) then
! Exponential overallocation
sz = 2*sz + (kmax-kmin) + 1
allocate(itmp(sz))
itmp(1:nnz) = jac_i(1:nnz)
call move_alloc(itmp, jac_i)
allocate(itmp(sz))
itmp(1:nnz) = jac_j(1:nnz)
call move_alloc(itmp, jac_j)
allocate(vtmp(sz))
vtmp(1:nnz) = jac_val(1:nnz)
call move_alloc(vtmp, jac_val)
end if
do k = kmin, kmax
if (work(k-kmin+1,j).ne.0) then
nnz = nnz + 1
jac_i(nnz) = k
jac_j(nnz) = ib
jac_val(nnz) = work(k-kmin+1,j)
end if
end do
""")}}
end do
if (nnz .eq. 0) then
if (allocated(jac_val)) deallocate(jac_val)
if (allocated(jac_i)) deallocate(jac_i)
if (allocated(jac_j)) deallocate(jac_j)
else if (nnz < sz) then
! Shrink to size
allocate(itmp(nnz))
itmp(1:nnz) = jac_i(1:nnz)
call move_alloc(itmp, jac_i)
allocate(itmp(nnz))
itmp(1:nnz) = jac_j(1:nnz)
call move_alloc(itmp, jac_j)
allocate(vtmp(nnz))
vtmp(1:nnz) = jac_val(1:nnz)
call move_alloc(vtmp, jac_val)
end if
{{else}}
integer :: i, k
if (jac_product_mode) then
call fatal_error('call to adjac_get_coo_jacobian when jacobian product mode is active')
end if
if (allocated(jac_val)) deallocate(jac_val)
if (allocated(jac_i)) deallocate(jac_i)
if (allocated(jac_j)) deallocate(jac_j)
nnz = 0
do i = 1, size(y,1)
if (allocated(y(i)%v)) then
nnz = nnz + y(i)%n
end if
end do
allocate(jac_val(nnz), jac_i(nnz), jac_j(nnz))
k = 1
do i = 1, size(y,1)
if (allocated(y(i)%v)) then
if (y(i)%n > 0) then
jac_i(k:k+y(i)%n-1) = i
jac_j(k:k+y(i)%n-1) = y(i)%i(1:y(i)%n)
jac_val(k:k+y(i)%n-1) = y(i)%vmul * y(i)%v(1:y(i)%n)
k = k + y(i)%n
end if
end if
end do
{{endif}}
end subroutine get_coo_jacobian_{{CHR}}
{{if TAPELESS}}
pure subroutine sparse_vector_sum_{{CHR}}(alpha, beta, na, nb, nc, ia, ib, ic, va, vb, vc)
! Sum sparse vectors c = alpha*a + beta*b, with index and data arrays (ia,va), (ib,vb), (ic,vc)
! The output arrays are assumed to be big enough to hold the data.
implicit none
integer, intent(in) :: na, nb, ia(*), ib(*)
integer, intent(inout) :: nc
integer, intent(out) :: ic(*)
{{FTYPE}}, intent(in) :: alpha, beta, va(*), vb(*)
{{FTYPE}}, intent(out) :: vc(*)
integer :: ja, jb, jc
ja = 1
jb = 1
jc = 1
do while (ja <= na .and. jb <= nb)
if (ia(ja) < ib(jb)) then
vc(jc) = alpha * va(ja)
if (vc(jc) .ne. 0) then
ic(jc) = ia(ja)
jc = jc + 1
end if
ja = ja + 1
else if (ia(ja) > ib(jb)) then
vc(jc) = beta * vb(jb)
if (vc(jc) .ne. 0) then
ic(jc) = ib(jb)
jc = jc + 1
end if
jb = jb + 1
else
vc(jc) = alpha * va(ja) + beta * vb(jb)
if (vc(jc) .ne. 0) then
ic(jc) = ia(ja)
jc = jc + 1
end if
ja = ja + 1
jb = jb + 1
end if
end do
do while (ja <= na)
vc(jc) = alpha * va(ja)
if (vc(jc) .ne. 0) then
ic(jc) = ia(ja)
jc = jc + 1
end if
ja = ja + 1
end do
do while (jb <= nb)
vc(jc) = beta * vb(jb)
if (vc(jc) .ne. 0) then
ic(jc) = ib(jb)
jc = jc + 1
end if
jb = jb + 1
end do
nc = jc - 1
end subroutine sparse_vector_sum_{{CHR}}
{{endif}}
{{pure}} subroutine sum_taylor_{{CHR}}(alphap, betap, a, b, c)
! c := alpha*a + beta*b
use iso_c_binding
implicit none
{{FTYPE}}, intent(in) :: alphap, betap
type({{TYPE}}), intent(in) :: a, b
type({{TYPE}}), intent(inout) :: c
if (jac_product_mode) then
c%vmul = alphap * a%vmul + betap * b%vmul
else
{{if TAPELESS}}
if (allocated(a%v) .and. allocated(b%v) .and. a%n > 0 .and. b%n > 0) then
call alloc_mem_{{CHR}}(c, a%n + b%n)
call sparse_vector_sum_{{CHR}}(alphap*a%vmul, betap*b%vmul, a%n, b%n, c%n, &
a%i, b%i, c%i, &
a%v, b%v, c%v)
c%vmul = 1
else if (allocated(a%v) .and. a%n > 0) then
call alloc_mem_{{CHR}}(c, a%n)
c%i(1:a%n) = a%i(1:a%n)
c%v(1:a%n) = a%v(1:a%n)
c%n = a%n
c%vmul = alphap * a%vmul
else if (allocated(b%v) .and. b%n > 0) then
call alloc_mem_{{CHR}}(c, b%n)
c%i(1:b%n) = b%i(1:b%n)
c%v(1:b%n) = b%v(1:b%n)
c%n = b%n
c%vmul = betap * b%vmul
else
c%n = 0
c%vmul = 0
end if
{{else}}
if (a%vmul == 0 .or. a%i == 0 .or. alphap == 0) then
c%vmul = betap * b%vmul
c%i = b%i
else if (b%vmul == 0 .or. b%i == 0 .or. betap == 0) then
c%vmul = alphap * a%vmul
c%i = a%i
else if (a%i == b%i) then
c%vmul = alphap * a%vmul + betap * b%vmul
c%i = a%i
else
call alloc_mem_{{CHR}}(c)
c%vmul = 1
sum_map_{{CHR}}(1 + 2*(c%i-1)) = a%i
sum_map_{{CHR}}(2 + 2*(c%i-1)) = b%i
sum_mul_{{CHR}}(1 + 2*(c%i-1)) = alphap * a%vmul
sum_mul_{{CHR}}(2 + 2*(c%i-1)) = betap * b%vmul
end if
{{endif}}
end if
end subroutine sum_taylor_{{CHR}}
!--------------------------------------------------------------------------
! Overloaded operators
!--------------------------------------------------------------------------
!!
!! assignment(=)
!!
{{for FTYPE2, CHR2 in FTYPES2}}
pure elemental subroutine assign_{{CHR}}{{CHR2}}(x, y)
implicit none
type({{TYPE}}), intent(inout) :: x
{{FTYPE2}}, intent(in) :: y
call free_mem_{{CHR}}(x)
x%value = y
x%vmul = 0
end subroutine assign_{{CHR}}{{CHR2}}
{{endfor}}
{{if TYPE == "adjac_double"}}
{{for FTYPE2, CHR2 in FTYPES2}}
pure elemental subroutine assign_b{{CHR2}}(x, y)
implicit none
type(adjac_complex), intent(inout) :: x
{{FTYPE2}}, intent(in) :: y
x%re = dble(y)
x%im = 0d0
end subroutine assign_b{{CHR2}}
{{endfor}}
pure elemental subroutine assign_bz(x, y)
implicit none
type(adjac_complex), intent(inout) :: x
complex(kind=kind(0d0)), intent(in) :: y
x%re = dble(y)
x%im = aimag(y)
end subroutine assign_bz
pure elemental subroutine assign_ba(x, y)
implicit none
type(adjac_complex), intent(inout) :: x
type({{TYPE}}), intent(in) :: y
x%re = y
x%im = 0d0
end subroutine assign_ba
{{endif}}
!!
!! operator(+)
!!
! X + Y = x + y + (x_j + y_j) dj
{{elemental}} function add_{{CHR}}{{CHR}}(x, y) result(z)
implicit none
type({{TYPE}}), intent(in) :: x, y
type({{TYPE}}) :: z
z%value = x%value + y%value
call sum_taylor({{CAST}}(1d0, kind=kind(0d0)), {{CAST}}(1d0, kind=kind(0d0)), x, y, z)
end function add_{{CHR}}{{CHR}}
{{for FTYPE2, CHR2 in FTYPES2}}
pure elemental function add_{{CHR}}{{CHR2}}(x, y) result(z)
implicit none
type({{TYPE}}), intent(in) :: x
{{FTYPE2}}, intent(in) :: y
type({{TYPE}}) :: z
z%value = x%value + y
z%vmul = x%vmul
call link_mem_{{CHR}}(z, x)
end function add_{{CHR}}{{CHR2}}
pure elemental function add_{{CHR2}}{{CHR}}(x, y) result(z)
implicit none
{{FTYPE2}}, intent(in) :: x
type({{TYPE}}), intent(in) :: y
type({{TYPE}}) :: z
z = y + x
end function add_{{CHR2}}{{CHR}}
{{endfor}}
{{if TYPE == "adjac_double"}}
pure elemental function add_az(x, y) result(z)
implicit none
type({{TYPE}}), intent(in) :: x
complex(kind=kind(0d0)), intent(in) :: y
type(adjac_complex) :: z
z%re = x + dble(y)
z%im = aimag(y)
end function add_az
pure elemental function add_za(x, y) result(z)
implicit none
complex(kind=kind(0d0)), intent(in) :: x
type({{TYPE}}), intent(in) :: y
type(adjac_complex) :: z
z%re = dble(x) + y
z%im = aimag(x)
end function add_za
{{elemental}} function add_bb(x, y) result(z)