summaryrefslogtreecommitdiff
path: root/tests/unit/ft8/test_ft8_mtd_transaction_concurrent.f90
blob: a69b853149c104f1e3576597ff827faa2385e28e (plain)
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
program test_ft8_mtd_transaction_concurrent
  use iso_fortran_env, only : int64
  use omp_lib
  use ft8_mtd_residual
  implicit none

  integer, parameter :: frame_samples=151680

  if(omp_get_max_threads().lt.4) error stop 'four OpenMP workers are required'
  call test_independent_publishers()
  call test_duplicate_publishers()
  call test_coherent_snapshots()

contains

  subroutine test_independent_publishers()
    real, allocatable :: source(:),actual(:)
    integer :: outcomes(4),worker,tones(79)
    integer(int64) :: epoch,generation
    type(ft8_subtraction_descriptor) :: descriptor

    allocate(source(mtd_residual_samples),actual(mtd_residual_samples))
    source=0.0
    call mtd_prepare(source,4)

!$omp parallel num_threads(4) private(worker,tones,epoch,generation,descriptor)
    block
    real, allocatable :: delta(:)
    allocate(delta(frame_samples))
    worker=omp_get_thread_num()+1
    delta=0.0
    delta(1)=-real(worker)
    tones=worker
    call mtd_publish_worker(worker)
    call mtd_capture_generation(worker,epoch,generation)
    call mtd_make_descriptor(descriptor,1,tones, &
         800.0+100.0*real(worker),0.0)
    mtd_worker_residual(1,worker)=mtd_worker_residual(1,worker)+delta(1)
!$omp barrier
    call mtd_try_commit_delta(worker,mtd_worker_residual(:,worker),descriptor, &
         epoch,generation,delta,outcomes(worker))
    deallocate(delta)
    end block
!$omp end parallel

    call mtd_finish(actual)
    if(actual(1).ne.-10.0) error stop 'parallel independent updates were lost'
    if(count(outcomes.eq.mtd_commit_current).ne.1) error stop &
         'exactly one same-generation transaction must commit first'
    if(count(outcomes.eq.mtd_commit_independent).ne.3) error stop &
         'stale independent transactions did not use the fast path'
  end subroutine test_independent_publishers

  subroutine test_duplicate_publishers()
    real, allocatable :: source(:),actual(:)
    integer :: outcomes(4),worker,tones(79)
    integer(int64) :: epoch,generation
    type(ft8_subtraction_descriptor) :: descriptor

    allocate(source(mtd_residual_samples),actual(mtd_residual_samples))
    source=0.0
    call mtd_prepare(source,4)

!$omp parallel num_threads(4) private(worker,epoch,generation,descriptor,tones)
    block
    real, allocatable :: delta(:)
    allocate(delta(frame_samples))
    worker=omp_get_thread_num()+1
    delta=0.0
    delta(1)=-1.0
    tones=7
    call mtd_publish_worker(worker)
    call mtd_capture_generation(worker,epoch,generation)
    call mtd_make_descriptor(descriptor,1,tones,1000.0,0.0)
    mtd_worker_residual(1,worker)=mtd_worker_residual(1,worker)+delta(1)
!$omp barrier
    call mtd_try_commit_delta(worker,mtd_worker_residual(:,worker),descriptor, &
         epoch,generation,delta,outcomes(worker))
    deallocate(delta)
    end block
!$omp end parallel

    call mtd_finish(actual)
    if(actual(1).ne.-1.0) error stop 'duplicate transaction was applied more than once'
    if(count(outcomes.eq.mtd_commit_current).ne.1) error stop &
         'duplicate race did not produce one winner'
    if(count(outcomes.eq.mtd_commit_duplicate).ne.3) error stop &
         'duplicate race did not reject all later publishers'
  end subroutine test_duplicate_publishers

  subroutine test_coherent_snapshots()
    integer, parameter :: writer_iterations=50,reader_iterations=500
    real, allocatable :: source(:),actual(:)
    real :: snapshot_value
    integer :: worker,iteration,tones(79),outcome,writers_started,reader_ready, &
         first_commits,reader_observed
    integer(int64) :: epoch,generation
    logical :: torn_snapshot,intermediate_observed
    type(ft8_subtraction_descriptor) :: descriptor

    allocate(source(mtd_residual_samples),actual(mtd_residual_samples))
    source=0.0
    torn_snapshot=.false.
    intermediate_observed=.false.
    writers_started=0
    reader_ready=0
    first_commits=0
    reader_observed=0
    call mtd_prepare(source,4)

!$omp parallel num_threads(4) private(worker,iteration,tones,outcome,epoch, &
!$omp generation,descriptor,snapshot_value) &
!$omp shared(torn_snapshot,intermediate_observed,writers_started,reader_ready, &
!$omp first_commits,reader_observed)
    block
    real, allocatable :: delta(:)
    allocate(delta(frame_samples))
    worker=omp_get_thread_num()+1
    call mtd_publish_worker(worker)
!$omp barrier
    if(worker.le.3) then
       delta=-1.0
!$omp atomic update
       writers_started=writers_started+1
       do
!$omp atomic read
          outcome=reader_ready
          if(outcome.eq.1) exit
       enddo
       do iteration=1,writer_iterations
          tones=0
          tones(1)=iteration
          tones(2)=worker
          call mtd_make_descriptor(descriptor,1,tones, &
               700.0+100.0*real(worker),0.0)
          do
             call mtd_capture_generation(worker,epoch,generation)
             mtd_worker_residual(1:frame_samples,worker)= &
                  mtd_worker_residual(1:frame_samples,worker)+delta
             call mtd_try_commit_delta(worker,mtd_worker_residual(:,worker), &
                  descriptor,epoch,generation,delta,outcome)
             if(outcome.ne.mtd_commit_conflict) exit
          enddo
          if(iteration.eq.1) then
!$omp atomic update
             first_commits=first_commits+1
             do
!$omp atomic read
                outcome=reader_observed
                if(outcome.eq.1) exit
             enddo
          endif
       enddo
    else
       do
!$omp atomic read
          outcome=writers_started
          if(outcome.eq.3) exit
       enddo
!$omp atomic write
       reader_ready=1
       do
!$omp atomic read
          outcome=first_commits
          if(outcome.eq.3) exit
       enddo
       call mtd_publish_worker(worker)
       snapshot_value=-mtd_worker_residual(1,worker)
       intermediate_observed=snapshot_value.eq.3.0
!$omp atomic write
       reader_observed=1
       do iteration=1,reader_iterations
          call mtd_publish_worker(worker)
          if(minval(mtd_worker_residual(1:frame_samples,worker)).ne. &
               maxval(mtd_worker_residual(1:frame_samples,worker))) &
               torn_snapshot=.true.
          snapshot_value=-mtd_worker_residual(1,worker)
          if(snapshot_value.gt.0.0 .and. &
               snapshot_value.lt.real(3*writer_iterations)) &
               intermediate_observed=.true.
       enddo
    endif
    deallocate(delta)
    end block
!$omp end parallel

    if(torn_snapshot) error stop 'reader observed a torn canonical snapshot'
    if(.not.intermediate_observed) error stop &
         'coherence reader did not overlap the publishers'
    call mtd_finish(actual)
    if(actual(1).ne.-real(3*writer_iterations)) error stop &
         'coherence stress did not publish every transaction'
  end subroutine test_coherent_snapshots

end program test_ft8_mtd_transaction_concurrent