-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcafhash.f90
123 lines (96 loc) · 2.79 KB
/
cafhash.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
! Copyright 2014 The University of Edinburgh
! Licensed under the Apache License, Version 2.0 (the "License");
! you may not use this file except in compliance with the License.
! You may obtain a copy of the License at
! http://www.apache.org/licenses/LICENSE-2.0
! Unless required by applicable law or agreed to in writing, software
! distributed under the License is distributed on an "AS IS" BASIS,
! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! See the License for the specific language governing permissions and
! limitations under the License.
module initialise
use iso_fortran_env
implicit none
integer :: hashlen, collisions,localhashlen
integer :: numi, inum
integer, allocatable :: hashtab(:)[:],hashcount(:)[:]
type(lock_type) :: iLock[*]
contains
subroutine inithash()
numi=num_images()
inum=this_image()
hashlen=2*NHASH*numi+1
localhashlen=2*NHASH + 1
collisions=0
allocate(hashtab(localhashlen)[*],hashcount(localhashlen)[*])
sync all
end subroutine inithash
subroutine finhash()
deallocate(hashtab,hashcount)
end subroutine finhash
subroutine hashlookup(o,v)
integer :: o,v
integer :: desti, destpos,localCount,localHash,lv
lv=v
do while (1.gt.0)
! determine whom the hash belongs too
desti = v /localhashlen
if(desti*localhashlen.lt.v) then
desti=desti+1
end if
destpos = v - (desti - 1)*localhashlen
! lock the data
lock(iLock[desti])
localhash=hashtab(destpos)[desti]
! get the hash
if(localhash.eq.0) then
! insert the entry
hashtab(destpos)[desti]=o
hashcount(destpos)[desti]=1
! unlock before return
unlock(ilock[desti])
return
else
! check to see if it is a collision
if(localhash.eq.o) then
! its a repetition
hashcount(destpos)[desti]=hashcount(destpos)[desti]+1
unlock(iLock[desti])
return
else
! its a collision
collisions=collisions+1
v=v+1
if(v.gt.hashlen) then
v=1
end if
! unlock before going round the loop
unlock(iLock[desti])
end if
end if
end do
end subroutine hashlookup
end module initialise
program DHT
use initialise
integer :: pb,b,v,i,j
call inithash()
do j=1,2
call fresetvalue(numi,inum)
do i=1,NHASH
call fnew(pb,b,v,numi)
call hashlookup(b,v)
end do
end do
sync all
if(inum.eq.1) then
do j=1,numi
do i=1,localhashlen
if(hashcount(i)[j].gt.0) then
write(*,*) j,i,hashtab(i)[j],hashcount(i)[j]
end if
end do
end do
end if
call finhash()
end program DHT