Example #1
0
def call_multi_iteration(client):
 
    vec_a = None # the query
    binary_codes = buffer('') # binary codes to match
 
    if client.sendall('next') == None:
        length = client.recv(1024)
        if not length: raise ValueError('Socket Error')

        #print 'length: ', int(length)

        if client.sendall('next') == None:

            # receive query

            vec_a = client.recv(int(length))

            if not vec_a: raise ValueError('Socket Error')

            #print len(vec_a)
            vec_a = numpy.frombuffer(vec_a, dtype = numpy.dtype(numpy.uint64))
            
            if client.sendall('next') == None:

                binary_codes = conn.recv_long_vector(client)
                    
            else:
                raise ValueError('Socket Error')

    else:
        raise ValueError('Socket Error')


    data = client.recv(1024)
    if data != 'ready':
        #print data
        raise ValueError('Socket Error')

    # hamming_distances: uint8 numpy array
    hamming_distances = cuda_hamming_obj.multi_iteration(vec_a, binary_codes)

    log_info(hamming_distances)

    hamming_distances = hamming_distances[0].astype(numpy.uint8)

    conn.send_long_vector(client, hamming_distances, 1)
Example #2
0
def call_multi_iteration(client):

    vec_a = None  # the query
    binary_codes = buffer('')  # binary codes to match

    if client.sendall('next') == None:
        length = client.recv(1024)
        if not length: raise ValueError('Socket Error')

        #print 'length: ', int(length)

        if client.sendall('next') == None:

            # receive query

            vec_a = client.recv(int(length))

            if not vec_a: raise ValueError('Socket Error')

            #print len(vec_a)
            vec_a = numpy.frombuffer(vec_a, dtype=numpy.dtype(numpy.uint64))

            if client.sendall('next') == None:

                binary_codes = conn.recv_long_vector(client)

            else:
                raise ValueError('Socket Error')

    else:
        raise ValueError('Socket Error')

    data = client.recv(1024)
    if data != 'ready':
        #print data
        raise ValueError('Socket Error')

    # hamming_distances: uint8 numpy array
    hamming_distances = cuda_hamming_obj.multi_iteration(vec_a, binary_codes)

    log_info(hamming_distances)

    hamming_distances = hamming_distances[0].astype(numpy.uint8)

    conn.send_long_vector(client, hamming_distances, 1)
Example #3
0
    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')
Example #4
0
def call_cuda_hamming_dist_in_compressed_domain(client):

    vec_a = None
    columns_vector = []

    if client.sendall('next') == None:
        length = client.recv(1024)
        if not length: raise ValueError('Socket Error')

        #print 'length: ', int(length)

        if client.sendall('next') == None:

            # receive query

            vec_a = client.recv(int(length))

            if not vec_a: raise ValueError('Socket Error')

            #print len(vec_a)
            vec_a = numpy.frombuffer(vec_a, dtype=numpy.dtype(numpy.uint64))
            #print vec_a

            if client.sendall('next') == None:
                # begin to receive columns

                cols_vec_length = client.recv(1024)
                if not cols_vec_length: raise ValueError('Socket Error')
                #print "cols_vec_length: ", cols_vec_length

                cols_vec_length = int(cols_vec_length)

                if client.sendall('next') == None:

                    while cols_vec_length > 0:

                        cols_length = client.recv(1024)
                        if not cols_length: raise ValueError('Socket Error')
                        #print "cols_length: ", cols_length

                        cols_length = int(cols_length)

                        if client.sendall('next') == None:

                            columns = conn.recv_long_vector(client, None)
                            columns_vector.append(columns)

                            cols_vec_length -= 1
                        else:
                            raise ValueError('Socket Error')

                    done = client.recv(1024)
                    if done != 'done': raise ValueError('Socket Error')
                    if client.sendall('ok') != None:
                        raise ValueError('Socket Error')

                else:
                    raise ValueError('Socket Error')

            else:
                raise ValueError('Socket Error')

        else:
            raise ValueError('Socket Error')

    else:
        raise ValueError('Socket Error')

    image_ids_leng = client.recv(1024)
    if not image_ids_leng: raise ValueError('Socket Error')

    #print "image ids length: ", image_ids_leng

    image_ids = numpy.zeros(int(image_ids_leng)).tolist()

    if client.sendall('next') == None:
        vlq_mode = client.recv(1024)
        if not vlq_mode: raise ValueError('Socket Error')

        if not client.sendall('done') == None:
            raise ValueError('Socket Error')
    else:
        raise ValueError('Socket Error')

    if client.recv(1024) != 'ready':
        raise ValueError('Socket Error')

    reshape_columns_vector = []
    for columns in columns_vector:
        idx = 0
        reshape_columns = []
        np_columns = numpy.frombuffer(columns, dtype=numpy.uint32)
        while idx < np_columns.shape[0]:
            length = np_columns[idx]
            np_array = np_columns[idx + 1:idx + 1 + length]
            if vlq_mode == 'y': np_array = np_array.astype(numpy.uint8)
            reshape_columns.append(buffer(np_array))
            idx = idx + 1 + length

        reshape_columns_vector.append(reshape_columns)

    # hamming_distances: uint8 numpy array
    hamming_distances = cuda_hamming_obj.cuda_hamming_dist_in_compressed_domain(
        vec_a, reshape_columns_vector, image_ids, vlq_mode)

    log_info(hamming_distances)

    hamming_distances = hamming_distances[0].astype(numpy.uint8)

    conn.send_long_vector(client, hamming_distances, 1)
Example #5
0
    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')
Example #6
0
def call_cuda_hamming_dist_in_compressed_domain(client):

    vec_a = None
    columns_vector = []

    if client.sendall('next') == None:
        length = client.recv(1024)
        if not length: raise ValueError('Socket Error')

        #print 'length: ', int(length)

        if client.sendall('next') == None:

            # receive query

            vec_a = client.recv(int(length))

            if not vec_a: raise ValueError('Socket Error')

            #print len(vec_a)
            vec_a = numpy.frombuffer(vec_a, dtype = numpy.dtype(numpy.uint64))
            #print vec_a
            
            if client.sendall('next') == None:
                # begin to receive columns
 
                cols_vec_length = client.recv(1024)
                if not cols_vec_length: raise ValueError('Socket Error')
                #print "cols_vec_length: ", cols_vec_length

                cols_vec_length = int(cols_vec_length)

                if client.sendall('next') == None:

                    while cols_vec_length > 0:
                    
                        cols_length = client.recv(1024)
                        if not cols_length: raise ValueError('Socket Error')
                        #print "cols_length: ", cols_length
                    
                        cols_length = int(cols_length)

                        if client.sendall('next') == None:
                    
                            columns = conn.recv_long_vector(client, None)
                            columns_vector.append(columns)
                            
                            cols_vec_length -= 1
                        else:
                            raise ValueError('Socket Error')
                    
                    done = client.recv(1024)
                    if done != 'done': raise ValueError('Socket Error')
                    if client.sendall('ok') != None:
                        raise ValueError('Socket Error')

                else:
                    raise ValueError('Socket Error')

            else:
                raise ValueError('Socket Error')

        else:
            raise ValueError('Socket Error')

    else:
        raise ValueError('Socket Error')


    image_ids_leng = client.recv(1024)
    if not image_ids_leng: raise ValueError('Socket Error')

    #print "image ids length: ", image_ids_leng

    image_ids = numpy.zeros(int(image_ids_leng)).tolist()

    if client.sendall('next') == None:
        vlq_mode = client.recv(1024)
        if not vlq_mode: raise ValueError('Socket Error')

        if not client.sendall('done') == None:
            raise ValueError('Socket Error')
    else:
        raise ValueError('Socket Error')    

    if client.recv(1024) != 'ready':
        raise ValueError('Socket Error')

    reshape_columns_vector = []
    for columns in columns_vector:
        idx = 0
        reshape_columns = []
        np_columns = numpy.frombuffer(columns, dtype = numpy.uint32)
        while idx < np_columns.shape[0]:
            length = np_columns[idx]
            np_array = np_columns[idx + 1: idx + 1 + length]
            if vlq_mode == 'y': np_array = np_array.astype(numpy.uint8)
            reshape_columns.append(buffer(np_array))
            idx = idx + 1 + length

        reshape_columns_vector.append(reshape_columns)

    # hamming_distances: uint8 numpy array
    hamming_distances = cuda_hamming_obj.cuda_hamming_dist_in_compressed_domain(vec_a, reshape_columns_vector, image_ids, vlq_mode)

    log_info(hamming_distances)
 
    hamming_distances = hamming_distances[0].astype(numpy.uint8)

    conn.send_long_vector(client, hamming_distances, 1)