inducer / inducer/pycuda

broadcasting for distance calculation

Open
#196 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
2.1k
Forks
298
Avg merge
4m
Merged PRs (30d)
1

Description

I would like to compute squared euclidean distance between all (m x n) combinations of two lists with length m and n using pycuda. Here is some numpy-compatible code:

```python
def sqdist(a, b):
m = len(a)
n = len(b)
diff = a.reshape(m,1,2) - b.reshape(1,n,2)
diff **= 2
return diff.sum(axis=2)
```

This doesn't work with pycuda, so I wrote a similar chunk of test code using pycuda:

```python
import numpy as np
import pycuda.gpuarray as gpuarray
import pycuda.autoinit
a = np.random.random((10, 1, 2)).astype(np.float32)
b = np.random.random((1, 10, 2)).astype(np.float32)
a_gpu = gpuarray.to_gpu(a)
b_gpu = gpuarray.to_gpu(b)
out = a_gpu - b_gpu
```

The error in both cases is as follows:

```
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
in ()
12 a_gpu = gpuarray.to_gpu(a)
13 b_gpu = gpuarray.to_gpu(b)
---> 14 out = a_gpu - b_gpu
15
16 # sqdist(a_gpu, b_gpu).shape

~/anaconda3/lib/python3.6/site-packages/pycuda/gpuarray.py in __sub__(self, other)
449 if isinstance(other, GPUArray):
450 result = self._new_like_me(_get_common_dtype(self, other))
--> 451 return self._axpbyz(1, other, -1, result)
452 else:
453 if other == 0:

~/anaconda3/lib/python3.6/site-packages/pycuda/gpuarray.py in _axpbyz(self, selffac, other, otherfac, out, add_timer, stream)
331 """Compute ``out = selffac * self + otherfac*other``,
332 where `other` is a vector.."""
--> 333 assert self.shape == other.shape
334 if not self.flags.forc or not other.flags.forc:
335 raise RuntimeError("only contiguous arrays may "

AssertionError:
```

Does pycuda not support broadcasting? I checked the FAQ and searched the documentation for "broadcasting" and couldn't find any clear statement on this. Or is it necessary to write a custom kernel to handle this? Thank you :)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.