inducer / inducer/pyopencl

Binary operations with Arrays of different memory layout

Open
#681 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
1.2k
Forks
247
Avg merge
7h 2m
Merged PRs (30d)
6

Description

For pyopencl.array.Array I get wrong results for binary operations such as adding if the memory layout differs for both arguments. Say, "A" is a C-contiguous, and "AF" is F-contiguous, then adding A+AF gives unexpected results. See this notebook for an example (tested with PyOpenCL 2022.3.1)

```python
import numpy as np
import pyopencl as cl
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
import pyopencl.array as cla
```
```python
# create simple numpy array 'a'
a = np.zeros((2,2))
a[0,1] = 1
a
```

array([[0., 1.],
[0., 0.]])

```python
# with different memory layout
af = np.asarray(a, order='F')
af
```

array([[0., 1.],
[0., 0.]])

```python
a + af
```

array([[0., 2.],
[0., 0.]])

```python
A = cla.to_device(queue, a)
AF = cla.to_device(queue, af)
```

```python
A, AF
```

(cl.Array([[0., 1.],
[0., 0.]]),
cl.Array([[0., 1.],
[0., 0.]]))

```python
# bug: A + AF gives different result than a + aF
B = A + AF
B
```

cl.Array([[0., 1.],
[1., 0.]])

Is this intended to work? Looking quickly at the source code, I could not see that the strides are taken into account. It seems that the underlying arrays are added in memory order.

Thanks
Gregor

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.