Hi all, is there a way for optimize the gpu process in my code below
# Python3 implementation of above approach
from math import floor, ceil
from numba import cuda,guvectorize
import numpy
import datetime
# Function to calculate the
# Jaro Similarity of two s
@cuda.jit(device=True)
def jaro_distance(s1, s2,hash_s1,hash_s2):
# Length of two s
len1 = len(s1)
len2 = len(s2)
# Maximum distance upto which matching
# is allowed
max_dist = floor(max(len1, len2) / 2) - 1
# Count of matches
match = 0
# Traverse throgh the first
for i in range(len1):
# Check if there is any matches
for j in range(max(0, i - max_dist),
min(len2, i + max_dist + 1)):
# If there is a match
if (s1[i] == s2[j] and hash_s2[j] == 0):
hash_s1[i] = 1
hash_s2[j] = 1
match += 1
break
# If there is no match
if (match == 0):
return 0.0
# Number of transpositions
t = 0
point = 0
# Count number of occurances
# where two characters match but
# there is a third matched character
# in between the indices
for i in range(len1):
if (hash_s1[i]):
# Find the next matched character
# in second
while (hash_s2[point] == 0):
point += 1
if (s1[i] != s2[point]):
point += 1
t += 1
t = t// 2
# Return the Jaro Similarity
return (match/ len1 + match / len2 +
(match - t + 1) / match)/ 3.0
# Driver code
@guvectorize(['(float32[:,:], float32[:,:],float32[:],float32[:],float32[:,:])'], '(m,n),(p,q),(t),(u)->(m,p)', target='cuda')
def full_jaro_gpu_array(vector1, vector2,hashv1,hashv2,result):
for i in range(len(vector1)):
for j in range(len(vector2)):
for k in range(len(hashv1)):
hashv1[k] = 0
for l in range(len(hashv2)):
hashv2[l] = 0
result[i][j] = jaro_distance(vector1[i],vector2[j],hashv1,hashv2)
# Prjaro Similarity of two s
def textToNumpy(filename):
def convertStringToNum(string):
if string == 'a':
return 11
if string == 'b':
return 37
if string == 'c':
return 35
if string == 'd':
return 12
if string == 'e':
return 13
if string == 'f':
return 14
if string == 'g':
return 15
if string == 'h':
return 16
if string == 'i':
return 17
if string == 'j':
return 18
if string == 'k':
return 19
if string == 'l':
return 20
if string == 'm':
return 21
if string == 'n':
return 22
if string == 'o':
return 23
if string == 'p':
return 24
if string == 'q':
return 25
if string == 'r':
return 26
if string == 's':
return 27
if string == 't':
return 28
if string == 'u':
return 29
if string == 'v':
return 30
if string == 'w':
return 31
if string == 'x':
return 32
if string == 'y':
return 33
if string == 'z':
return 34
def listAngka(string):
data = [float(convertStringToNum(x)) for x in string]
return data
with open(filename) as file:
datas = file.readlines()
datas = [x.replace("\n", "") for x in datas]
datas = [listAngka(data) for data in datas]
return datas
data = [x for x in textToNumpy("small")]
data2 = [x for x in textToNumpy("small")]
length = max(map(len, data))
length2 = max(map(len, data2))
A=numpy.array([xi+[0]*(length-len(xi)) for xi in data],dtype=numpy.float32)
B=numpy.array([xi+[0]*(length-len(xi)) for xi in data2],dtype=numpy.float32)
hash1 = numpy.zeros(A.shape[1])
hash2 = numpy.zeros(B.shape[1])
begin_time = datetime.datetime.now()
hasil = full_jaro_gpu_array(A,B,hash1,hash2)
print("GPU Time Result")
print(datetime.datetime.now() - begin_time)
print(hasil)
# This code is contributed by mohit kumar 29