-
Notifications
You must be signed in to change notification settings - Fork 3
/
cuda_hamming_client.py
176 lines (131 loc) · 6.27 KB
/
cuda_hamming_client.py
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
import numpy
import array
import time
import math
import argparse
import conn
import socket
def cudaclient(client_type = 'local', options = {}):
if client_type == 'local':
from cuda_hamming import CudaHamming
return CudaHamming()
elif client_type == 'net':
if options == {}:
options = {'host': 'localhost', 'port': 8080}
return CudaHammingNetClient(options)
else:
raise ValueError("CUDA Client must be local or net type.")
class CudaHammingNetClient(object):
def __init__(self, options = {'host': 'localhost', 'port': 8080}):
self.host = options['host']
self.port = options['port']
# two numpy array of dtype uint64
def multi_iteration(self, vec_a, vec_b):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.host, self.port))
if s.sendall('multi_iteration') == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
if s.sendall(str(vec_a.shape[0] * 8)) == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
if s.sendall(vec_a) == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
conn.send_long_vector(s, vec_b)
else:
raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
#print "ready to receive hamming results."
# receive results
if s.sendall('ready') == None:
distances = conn.recv_long_vector(s, numpy.uint8)
print distances
print distances.shape
return (distances, 0)
else:
raise ValueError('Socket Error')
# vec_a: numpy array of dtype uint64
# compressed_columns_vec: array of buffers
def cuda_hamming_dist_in_compressed_domain(self, vec_a, compressed_columns_vec, image_ids, vlq_mode):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.host, self.port))
if s.sendall('cuda_hamming_dist_in_compressed_domain') == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
if s.sendall(str(vec_a.shape[0] * 8)) == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
if s.sendall(vec_a) == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
print "compressed_columns_vec len: ", len(compressed_columns_vec)
# tell server how many columns_vector to send
if s.sendall(str(len(compressed_columns_vec))) == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
for columns in compressed_columns_vec:
#print 'columns leg: ', len(columns)
# how many columns in a columns vector
# should be 64 columns for 64 bits
if s.sendall(str(len(columns))) == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
concate_columns = numpy.array([])
for column in columns:
if vlq_mode == 'y':
np_array = numpy.frombuffer(column, dtype = numpy.uint8)
np_array = np_array.astype(numpy.uint32)
concate_columns = numpy.concatenate((concate_columns, numpy.array([np_array.shape[0]]).astype(numpy.uint32)))
concate_columns = numpy.concatenate((concate_columns, np_array))
else:
np_array = numpy.frombuffer(column, dtype = numpy.uint32)
concate_columns = numpy.concatenate((concate_columns, numpy.array([np_array.shape[0]]).astype(numpy.uint32)))
concate_columns = numpy.concatenate((concate_columns, np_array))
conn.send_long_vector(s, concate_columns.astype(numpy.uint32), 4)
if s.sendall("done") == None:
data = s.recv(1024)
if data != 'ok': raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
# send image id length
if s.sendall(str(len(image_ids))) == None:
data = s.recv(1024)
if data != 'next': raise ValueError('Socket Error')
if s.sendall(vlq_mode) == None:
if s.recv(1024) != 'done': raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
else:
raise ValueError('Socket Error')
# receive results
if s.sendall('ready') == None:
distances = conn.recv_long_vector(s, numpy.uint8)
print distances
print distances.shape
return (distances, 0)
else:
raise ValueError('Socket Error')
def send_query(self, datas):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((self.host, self.port))
for data in datas:
s.send(buffer(data))
r_data = s.recv(1024)
s.close()
print "Received", r_data
if __name__ == "__main__":
client = cudaclient('net', {'host': 'localhost', 'port': 8080})
client.send_query(['test'])
#client.cuda_hamming_dist_in_compressed_domain(numpy.array([123]), [[numpy.array([123]), numpy.array([456])]], [], True)