简体   繁体   English

如何计算GPU上的成对距离矩阵

[英]How to calculate pairwise distance matrix on the GPU

The bottleneck in my code is the area where I calculate a pairwise distance matrix . 我的代码中的瓶颈是我计算成对距离矩阵的区域 Since this is the slowest part by far, I have spent much time in speeding up my code. 由于这是迄今为止最慢的部分,我花了很多时间来加速我的代码。

I have found many speedups using articles online, but the gains have been minimal. 我发现许多加速在线使用文章,但收益微乎其微。 So, I am looking for a method to use my GPU to create a distance matrix in order to speed it up further. 所以, 我正在寻找一种方法来使用我的GPU来创建距离矩阵 ,以进一步加快速度。 However, I know very little about using the GPU for computation. 但是,我对使用GPU进行计算知之甚少。 Can anyone help me do this? 任何人都可以帮我这样做吗?

In my research I have found the following, but none of them used the GPU : 在我的研究中,我发现了以下内容,但没有一个使用GPU

  1. This article was useful, but the speedups were minimal. 这篇文章很有用,但加速很少。
  2. This article was informative on how to use cython and numba. 这篇文章提供了有关如何使用cython和numba的信息。

Here is an example snippet of how to calculate a pairwise distance matrix: 以下是如何计算成对距离矩阵的示例摘录:

import numpy as np
from scipy import spatial

rows = 1000
cols = 10
mat = np.random.randn(rows, cols)
d_mat = spatial.distance.cdist(mat, mat)

My graphics card is an Nvidia Quadro M2000M 我的显卡是Nvidia Quadro M2000M

I was able to use this: 我能用这个:

import numpy as np
from numba import cuda

USE_64 = True

if USE_64:
    bits = 64
    np_type = np.float64
else:
    bits = 32
    np_type = np.float32

@cuda.jit("void(float{}[:, :], float{}[:, :])".format(bits, bits))
def distance_matrix(mat, out):
    m = mat.shape[0]
    n = mat.shape[1]
    i, j = cuda.grid(2)
    d = 0
    if i < m and j < m:
        for k in range(n):
            tmp = mat[i, k] - mat[j, k]
            d += tmp * tmp
        out[i, j] = d

def gpu_dist_matrix(mat):
    rows = mat.shape[0]

    block_dim = (16, 16)
    grid_dim = (int(rows/block_dim[0] + 1), int(rows/block_dim[1] + 1))

    stream = cuda.stream()
    mat2 = cuda.to_device(np.asarray(mat, dtype=np_type), stream=stream)
    out2 = cuda.device_array((rows, rows))
    distance_matrix[grid_dim, block_dim](mat2, out2)
    out = out2.copy_to_host(stream=stream)

    return out

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中使用广播计算矩阵的成对余弦距离 - How to calculate pairwise cosine distance of matrix using broadcasting in python 在 Python 中计算加权成对距离矩阵 - Calculate weighted pairwise distance matrix in Python pytorch 如何计算矩阵成对距离? 为什么“自我”距离不为零? - How does pytorch calculate matrix pairwise distance? Why isn't 'self' distance not zero? 如何在矩阵内成对计算 - How to calculate pairwise inside a matrix 如何计算坐标之间的成对harsine距离 - How to calculate the pairwise haversine distance between coordinates 如何将不对称的成对距离矩阵转换为字典? - How to convert an asymmetric pairwise distance matrix to dictionary? Python-如何生成成对汉明距离矩阵 - Python - How to generate the Pairwise Hamming Distance Matrix numpy python:向量化距离函数以计算维度为 (m, 3) 的 2 个矩阵的成对距离 - numpy python: vectorize distance function to calculate pairwise distance of 2 matrix with a dimension of (m, 3) 如何计算集合之间的成对相似性(杰卡德距离) - how to calculate pairwise similarity (Jaccard Distance) between sets 如何计算 Python 中矩阵中所有对象之间的成对距离 - How to calculate pairwise distances among all subjects in a matrix in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM