File: test_open_maps.f90

package info (click to toggle)
fortran-stdlib 0.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,008 kB
  • sloc: f90: 24,178; ansic: 1,244; cpp: 623; python: 119; makefile: 13
file content (334 lines) | stat: -rwxr-xr-x 14,487 bytes parent folder | download
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
program test_open_maps
!! Test various aspects of the runtime system.
!! Running this program may require increasing the stack size to above 48 MBytes
!! or decreasing rand_power to 20 or less

    use stdlib_kinds, only: &
        dp,                 &
        int8,               &
        int32

    use stdlib_hashmaps, only : open_hashmap_type, int_depth, int_index
    use stdlib_hashmap_wrappers

    implicit none

    type dummy_type
        integer(int8), allocatable :: value(:)
    end type dummy_type

    integer(int32), parameter :: huge32 = huge(0_int32)
    real(dp), parameter       :: hugep1 = real(huge32, dp) + 1.0_dp
    integer, parameter        :: rand_power = 18
    integer, parameter        :: rand_size = 2**rand_power
    integer, parameter        :: test_size = rand_size*4
    integer, parameter        :: test_16 = 2**4
    integer, parameter        :: test_256 = 2**8

    integer                   :: index
    integer                   :: lun
    type(open_hashmap_type)   :: map
    real(dp)                  :: rand2(2)
    integer(int32)            :: rand_object(rand_size)
    integer(int8)             :: test_8_bits(test_size)


    open( newunit=lun, file="test_open_maps.txt", access="sequential", &
        action="write", form="formatted", position="rewind" )
    write(lun, '("| ", a17, " | ", a12, " | ", a15, " | ", a10, " |")') &
        'Algorithm', 'Process', 'Data Set', 'Time (s)'

    do index=1, rand_size
        call random_number(rand2)
        if (rand2(1) < 0.5_dp) then
            rand_object(index) = ceiling(-rand2(2)*hugep1, int32) - 1
        else
            rand_object(index) = floor(rand2(2)*hugep1, int32)
        end if
    end do

    test_8_bits(:) = transfer( rand_object, 0_int8, test_size )

    ! Test implicit initalization by skipping init call for first test.
    call input_random_data( map, test_16, 'FNV-1', "16 byte words" )
    call test_inquire_data( map, test_16, 'FNV-1', "16 byte words" )
    call test_get_data( map, test_16, 'FNV-1', '16 byte words' )
    call test_get_all_keys( map, test_16, 'FNV-1', '16 byte words' )
    call report_rehash_times( map, fnv_1_hasher, 'FNV-1', '16 byte words' )
    call report_hash_statistics( map, 'FNV-1', '16 byte words' )
    call report_removal_times( map, test_16, 'FNV-1', '16 byte words' )

    call map % init()   ! Test default options
    call input_random_data( map, test_256, 'FNV-1', "256 byte words" )
    call test_inquire_data( map, test_256, 'FNV-1', "256 byte words" )
    call test_get_data( map, test_256, 'FNV-1', '256 byte words' )
    call test_get_all_keys( map, test_256, 'FNV-1', '256 byte words' )
    call report_rehash_times( map, fnv_1_hasher, 'FNV-1', '256 byte words' )
    call report_hash_statistics( map, 'FNV-1', '256 byte words' )
    call report_removal_times( map, test_256, 'FNV-1', '256 byte words' )

    call map % init( fnv_1a_hasher, slots_bits=10 )
    call input_random_data( map, test_16, 'FNV-1A', "16 byte words" )
    call test_inquire_data( map, test_16, 'FNV-1A', "16 byte words" )
    call test_get_data( map, test_16, 'FNV-1A', '16 byte words' )
    call test_get_all_keys( map, test_16, 'FNV-1A', '16 byte words' )
    call report_rehash_times( map, fnv_1a_hasher, 'FNV-1', '16 byte words' )
    call report_hash_statistics( map, 'FNV-1A', '16 byte words' )
    call report_removal_times( map, test_16, 'FNV-1a', '16 byte words' )

    call map % init( fnv_1a_hasher, slots_bits=10 )
    call input_random_data( map, test_256, 'FNV-1A', "256 byte words" )
    call test_inquire_data( map, test_256, 'FNV-1A', "256 byte words" )
    call test_get_data( map, test_256, 'FNV-1A', '256 byte words' )
    call test_get_all_keys( map, test_256, 'FNV-1A', '256 byte words' )
    call report_rehash_times( map, fnv_1_hasher, 'FNV-1A', '256 byte words' )
    call report_hash_statistics( map, 'FNV-1A', '256 byte words' )
    call report_removal_times( map, test_256, 'FNV-1A', '256 byte words' )

    call map % init( seeded_nmhash32_hasher, slots_bits=10 )
    call input_random_data( map, test_16, 'Seeded_Nmhash32', "16 byte words" )
    call test_inquire_data( map, test_16, 'Seeded_Nmhash32', "16 byte words" )
    call test_get_data( map, test_16, 'Seeded_Nmhash32', '16 byte words' )
    call test_get_all_keys( map, test_16, 'Seeded_Nmhash32', '16 byte words' )
    call report_rehash_times( map, seeded_nmhash32_hasher, 'Seeded_Nmhash32', &
        '16 byte words' )
    call report_hash_statistics( map, 'Seeded_Nmhash32', '16 byte words' )
    call report_removal_times( map, test_16, 'Seeded_Nmhash32', &
        '16 byte words' )

    call map % init( seeded_nmhash32_hasher, slots_bits=10 )
    call input_random_data( map, test_256, 'Seeded_Nmhash32', "256 byte words" )
    call test_inquire_data( map, test_256, 'Seeded_Nmhash32', "256 byte words" )
    call test_get_data( map, test_256, 'Seeded_Nmhash32', '256 byte words' )
    call test_get_all_keys( map, test_256, 'Seeded_Nmhash32', '256 byte words' )
    call report_rehash_times( map, seeded_nmhash32_hasher, 'Seeded_Nmhash32', &
        '256 byte words' )
    call report_hash_statistics( map, 'Seeded_Nmhash32', '256 byte words' )
    call report_removal_times( map, test_256, 'Seeded_Nmhash32', &
        '256 byte words' )

    call map % init( seeded_nmhash32x_hasher, slots_bits=10 )
    call input_random_data( map, test_16, 'Seeded_Nmhash32x', "16 byte words" )
    call test_inquire_data( map, test_16, 'Seeded_Nmhash32x', "16 byte words" )
    call test_get_data( map, test_16, 'Seeded_Nmhash32x', '16 byte words' )
    call test_get_all_keys( map, test_16, 'Seeded_Nmhash32x', '16 byte words' )
    call report_rehash_times( map, seeded_nmhash32x_hasher, &
        'Seeded_Nmhash32x', '16 byte words' )
    call report_hash_statistics( map, 'Seeded_Nmhash32x', '16 byte words' )
    call report_removal_times( map, test_16, 'Seeded_Nmhash32x', &
        '16 byte words' )

    call map % init( seeded_nmhash32x_hasher, slots_bits=10 )
    call input_random_data( map, test_256, 'Seeded_Nmhash32x', &
        "256 byte words" )
    call test_inquire_data( map, test_256, 'Seeded_Nmhash32x', &
        "256 byte words" )
    call test_get_data( map, test_256, 'Seeded_Nmhash32x', '256 byte words' )
    call test_get_all_keys( map, test_256, 'Seeded_Nmhash32x', '256 byte words' )
    call report_rehash_times( map, seeded_nmhash32x_hasher, &
        'Seeded_Nmhash32x', '256 byte words' )
    call report_hash_statistics( map, 'Seeded_Nmhash32x', '256 byte words' )
    call report_removal_times( map, test_256, 'Seeded_Nmhash32x', &
        '256 byte words' )

    call map % init( seeded_water_hasher, slots_bits=10 )
    call input_random_data( map, test_16, 'Seeded_Water', "16 byte words" )
    call test_inquire_data( map, test_16, 'Seeded_Water', "16 byte words" )
    call test_get_data( map, test_16, 'Seeded_Water', '16 byte words' )
    call test_get_all_keys( map, test_16, 'Seeded_Water', '16 byte words' )
    call report_rehash_times( map, seeded_water_hasher, &
        'Seeded_Water', '16 byte words' )
    call report_hash_statistics( map, 'Seeded_Water', '16 byte words' )
    call report_removal_times( map, test_16, 'Seeded_Water', &
        '16 byte words' )

    call map % init( seeded_water_hasher, slots_bits=10 )
    call input_random_data( map, test_256, 'Seeded_Water', &
        "256 byte words" )
    call test_inquire_data( map, test_256, 'Seeded_Water', &
        "256 byte words" )
    call test_get_data( map, test_256, 'Seeded_Water', '256 byte words' )
    call test_get_all_keys( map, test_256, 'Seeded_Water', '256 byte words' )
    call report_rehash_times( map, seeded_water_hasher, &
        'Seeded_Water', '256 byte words' )
    call report_hash_statistics( map, 'Seeded_Water', '256 byte words' )
    call report_removal_times( map, test_256, 'Seeded_Water', &
        '256 byte words' )

contains

    subroutine input_random_data( map, test_block, hash_name, size_name )
        type(open_hashmap_type), intent(inout) :: map
        integer(int_index), intent(in) :: test_block
        character(*), intent(in) :: hash_name
        character(*), intent(in) :: size_name
       
        type(dummy_type) :: dummy_val
        integer :: index2
        type(key_type) :: key
        real :: t1, t2, tdiff
        logical :: conflict

        call cpu_time(t1)
        do index2=1, size(test_8_bits), test_block
            call set( key, test_8_bits( index2:index2+test_block-1 ) )
            
            dummy_val % value = test_8_bits( index2:index2+test_block-1 )
            
            call map % map_entry( key, dummy_val, conflict )
            if (conflict) &
                error stop "Unable to map entry because of a key conflict."
        end do
        call cpu_time(t2)
        tdiff = t2-t1
        write(lun, '("|", a18, " | ", a12, " | ", a15, " | ", f10.5, " |")') &
            trim(hash_name), 'Enter data', size_name, tdiff

    end subroutine input_random_data


    subroutine test_inquire_data( map, test_block, hash_name, size_name )
        type(open_hashmap_type), intent(inout) :: map
        integer(int_index), intent(in)          :: test_block
        character(*), intent(in)                :: hash_name, size_name
        integer :: index2
        logical :: present
        type(key_type) :: key
        real :: t1, t2, tdiff

        call cpu_time(t1)
        do index2=1, size(test_8_bits), test_block
            call set( key, test_8_bits( index2:index2+test_block-1 ) )
            call map % key_test( key, present )
            if (.not. present) &
                error stop "KEY not found in map KEY_TEST."
        end do
        call cpu_time(t2)
        tdiff = t2-t1
        write(lun, '("|", a18, " | ", a12, " | ", a15, " | ", f10.5, " |")') &
            trim(hash_name), 'Inquire data', size_name, tdiff

    end subroutine test_inquire_data


    subroutine test_get_data( map, test_block, hash_name, size_name )
        type(open_hashmap_type), intent(inout) :: map
        integer(int_index), intent(in)          :: test_block
        character(*), intent(in)                :: hash_name, size_name
        integer :: index2
        type(key_type) :: key
        class(*), allocatable :: data
        logical :: exists
        real :: t1, t2, tdiff

        call cpu_time(t1)
        do index2=1, size(test_8_bits), test_block
            call set( key, test_8_bits( index2:index2+test_block-1 ) )
            call map % get_other_data( key, data, exists )
            if (.not. exists) &
                error stop "Unable to get data because key not found in map."
        end do
        call cpu_time(t2)
        tdiff = t2-t1
        write(lun, '("|", a18, " | ", a12, " | ", a15, " | ", f10.5, " |")') &
            trim(hash_name), 'Get data', size_name, tdiff

    end subroutine test_get_data


    subroutine test_get_all_keys( map, test_block, hash_name, size_name )
        type(open_hashmap_type), intent(inout) :: map
        integer(int_index), intent(in)          :: test_block
        character(*), intent(in)                :: hash_name, size_name
        integer :: index2, key_idx
        type(key_type) :: key
        type(key_type), allocatable :: all_keys(:)
        real :: t1, t2, tdiff

        call cpu_time(t1)
        call map % get_all_keys(all_keys)
        call cpu_time(t2)
        tdiff = t2-t1

        if (size( all_keys ) /= size( test_8_bits )/test_block) &
            error stop "Number of keys is different from that of keys in a map."

        do index2=1, size(test_8_bits), test_block
            call set( key, test_8_bits( index2:index2+test_block-1 ) )

            key_idx = ( index2/test_block ) + 1
            if (.not. ( all_keys(key_idx) == key )) &
                error stop "Invalid value of a key."
        end do

        write(lun, '("|", a18, " | ", a12, " | ", a15, " | ", f10.5, " |")') &
            trim(hash_name), 'Get all keys', size_name, tdiff

    end subroutine test_get_all_keys


    subroutine report_rehash_times( map, hasher, hash_name, size_name )
        type(open_hashmap_type), intent(inout) :: map
        procedure(hasher_fun)                   :: hasher
        character(*), intent(in)                :: hash_name, size_name
        real :: t1, t2, tdiff

        call cpu_time(t1)
        call map % rehash( hasher )
        call cpu_time(t2)
        tdiff = t2-t1

        write(lun, '("|", a18, " | ", a12, " | ", a15, " | ", f10.5, " |")') &
            trim(hash_name), 'Rehash data', size_name, tdiff

    end subroutine report_rehash_times


    subroutine report_removal_times( map, test_block, hash_name, size_name )
        type(open_hashmap_type), intent(inout) :: map
        integer(int_index), intent(in)          :: test_block
        character(*), intent(in)                :: hash_name, size_name
        real :: t1, t2, tdiff
        type(key_type) :: key
        integer(int_index) :: index2
        logical :: existed

        call cpu_time(t1)
        do index2=1, size(test_8_bits), test_block
            call set( key, test_8_bits( index2:index2+test_block-1 ) )
            call map % remove(key, existed)
            if ( .not. existed ) &
                error stop "Key not found in entry removal."
        end do
        call cpu_time(t2)
        tdiff = t2-t1

        write(lun, '("|", a18, " | ", a12, " | ", a15, " | ", f10.5, " |")') &
            trim(hash_name), 'Remove data', size_name, tdiff
        flush(lun)

    end subroutine report_removal_times


    subroutine report_hash_statistics( map, hash_name, size_name )
        type(open_hashmap_type), intent(inout) :: map
        character(*), intent(in)               :: hash_name, size_name
        integer(int_depth) :: depth

        write(lun, *)
        write(lun, '("Statistics for open hash table with ",' // &
              'A, " hasher on ", A, ".")' ) hash_name, size_name
        write(lun, '("Slots = ", I0)' ) map % num_slots()
        write(lun, '("Calls = ", I0)' ) map % calls()
        write(lun, '("Entries = ", I0)' ) map % entries()
        write(lun, '("Total probes = ", I0)' ) map % map_probes()
        write(lun, '("Loading = ", ES10.3)' ) map % loading()
        depth = map % total_depth()
        write(lun, '("Total depth = ", I0)' ) depth
        write(lun, '("Relative depth = ", ES10.3)') &
            real( depth ) / real( map % entries() )

    end subroutine report_hash_statistics


end program test_open_maps