Skip to content

Binary operations with Arrays of different memory layout #681

@geggo

Description

@geggo

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)

import numpy as np
import pyopencl as cl
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
import pyopencl.array as cla
# create simple numpy array 'a'
a = np.zeros((2,2))
a[0,1] = 1
a
array([[0., 1.],
       [0., 0.]])
# with different memory layout
af = np.asarray(a, order='F')
af
array([[0., 1.],
       [0., 0.]])
a + af
array([[0., 2.],
       [0., 0.]])
A = cla.to_device(queue, a)
AF = cla.to_device(queue, af)
A, AF
(cl.Array([[0., 1.],
        [0., 0.]]),
 cl.Array([[0., 1.],
        [0., 0.]]))
# 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions