Project

General

Profile

Feature #14328

SIMD vectorization

Added by ahorek (Pavel Rosický) 6 months ago. Updated about 20 hours ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:84688]

Description

Hello,
in order to make ruby faster, I'd like to propose an optional SIMD optimization for some cases. I want to target SSE2 which is available in all modern x86 processors. (Pentium 4, Athlon 64 and newer).

this is usually automatically handled by GCC during compilation time, but because of dynamic nature of ruby, redefinitions etc. It's very hard to preoptimize it before the actual execution.

use auto-vectorization provided by JIT ( https://bugs.ruby-lang.org/issues/12589 )

GCC can do that, but I'm not sure how reliable and effective it is today

Pros:
we don't have to do anything, let GCC do the job
bigger scope for optimizations

Cons:
slower compilation

specialize known bottlenecks by hand

Pros:
predictable performace
without increased compilation time

Cons:
code complexity

unfortunatelly using SIMD isn't for free, there's an overhead, it needs a large data set to be effective. It's useful mainly for math operations, sum, min, max, arrays, matrixes, string manipulations etc. There probably won't be any significant benefit for appliactions like Rails.

what do you think about it?

History

#1 [ruby-core:84832] Updated by naruse (Yui NARUSE) 6 months ago

I had tried to use SIMD in some parts.
But its performance improvement is limited.

Of course it can improve performance so much, but it is only in special use cases.
In usual Ruby handles small data and they can't ignore SIMD overhead.

math operations

Ruby uses GMP if exist.

sum, min, max, arrays, matrixes

Normal array can store any type.
To use SIMD power, the array should be typed array like NArray.
It's not Ruby itself's issue.

string manipulations

I tried to use SSE2 for coderange_scan() in string.c, but it doesn't improve performance so much.

SSE 4.2 STTNI is also interesting but I don't find a good use case which can pay for increasing code complexity.

#2 Updated by nobu (Nobuyoshi Nakada) 11 days ago

  • Status changed from Open to Closed

#3 Updated by nobu (Nobuyoshi Nakada) 1 day ago

  • Status changed from Closed to Open

#4 [ruby-core:87958] Updated by ahorek (Pavel Rosický) about 20 hours ago

naruse (Yui NARUSE) I saw your blank implementation, impressive
https://github.com/ruby/ruby/commit/e6bc209abf81d53c2e3374dc52c2a128570c6055

the complexity for a hand written simd code is probably too high. Ruby supports a lot of platforms, so we have to duplicate the code (compatibility paths) or make a portable interface for it.

here's also an interesting implementation of "strip" method
https://github.com/lemire/despacer

I don't like the idea of exposing simd types like NArray to the developer, but some languages did it this way (like Dart)

The best solution is to teach JIT how to vertorize at least basic loops like

for (int i = 0; i < N; ++i)
  A[i] = B[i] + C[i];

->

for (int i = 0; i < N/8; ++i)
  VECTOR_ADD(A + i, B + i, C + i);

unfortunatelly it's not always as simple as this example

Also available in: Atom PDF