Skip to content

[question] column-wise dot products faster than row-wise dot products. why? #14979

Closed

Description

@nschloe

I've got two n-by-3 arrays and I'd like to find the fastest method of computing the row-by-row dot products. einsum is faster than multiplying and summing up, so I compared row-dot-products and column-dot-products with the contiguous transposed arrays. The results:
rel

Apparently, column dot products of 3-by-n matrices are never worse, and up to 2.3 times as fast as row dot products of n-by-3 matrices.

Any idea why that is? We're perhaps venturing into the area of cache hits/misses here.

Code to reproduce the plot:

import numpy
import perfplot


def setup(n):
    a = numpy.random.rand(n, 3)
    b = numpy.random.rand(n, 3)
    aT = numpy.ascontiguousarray(a.T)
    bT = numpy.ascontiguousarray(b.T)
    return (a, b), (aT, bT)


perfplot.save(
    "rel.png",
    setup=setup,
    n_range=[2 ** k for k in range(1, 26)],
    kernels=[
        lambda data: numpy.einsum("ij, ij->i", data[0][0], data[0][1]),
        lambda data: numpy.einsum("ij, ij->j", data[1][0], data[1][1]),
    ],
    labels=["einsum", "einsum.T"],
    logx=True,
    xlabel="len(a), len(b)",
    relative_to=1,
    title=f"numpy {numpy.__version__}",
)

Activity

  1. seberg commented on Nov 25, 2019

    @seberg
    Member

    Not sure, you would have to dig in the actual code I think. My expectation is caching/memory speed is at least the reason why it has the same speed for large n. For small n it is likely you are only measuring overheads. For intermediate n, I expect the actual code is somewhat different. Maybe due to SSE usage. If you really want to know, you probably need to look at and compare the result of perf or so (or maybe just look by hand at the code that gets called).

  2. added
    57 - Close?Issues which may be closable unless discussion continued
    on Dec 2, 2019
  3. rossbar commented on Jul 18, 2020

    @rossbar
    Contributor

    I'm going to go ahead and close this due to lack of followup. Please feel free to reopen if you are interested in investigating in more detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    33 - QuestionQuestion about NumPy usage or development57 - Close?Issues which may be closable unless discussion continuedcomponent: numpy.einsum

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions