Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement and benchmark ArrowOperationPlus node #10150

Merged
merged 43 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
a56aa05
Test for +[Int8] behavior
JaroslavTulach May 31, 2024
a9e07f1
Generalizing parser to support +
JaroslavTulach May 31, 2024
f6d7e84
Trivial implementation of ArrowOperationPlus
JaroslavTulach Jun 3, 2024
2f78ea9
Merge remote-tracking branch 'origin/develop' into wip/jtulach/PocArr…
JaroslavTulach Jun 3, 2024
a044cb0
Benchmark Arrow + implementation against Table one
JaroslavTulach Jun 3, 2024
9c70d17
Invoke foreign function with arguments
JaroslavTulach Jun 3, 2024
a008b3d
Allow Int64 value to be null
JaroslavTulach Jun 3, 2024
a6248a1
Support isNull arguments
JaroslavTulach Jun 3, 2024
2f16308
Measure just arrow_plus performance
JaroslavTulach Jun 3, 2024
2bfba6e
Speeding up by using different interop library for different element
JaroslavTulach Jun 3, 2024
760a106
Tests for Int64 buffer
JaroslavTulach Jun 3, 2024
f9a802e
For some reasons there are nulls in the array
JaroslavTulach Jun 3, 2024
68d9306
Fix byte mask used for index calculation
hubertp Jun 3, 2024
1dbbae1
Tell the compiler static values are final constants
JaroslavTulach Jun 4, 2024
ea36864
Prefer final fields and hope they will be constant
JaroslavTulach Jun 4, 2024
82ca3e7
Use null bitmapBuffer when there are no null values
JaroslavTulach Jun 4, 2024
c36d22e
Make sure unit tests work
JaroslavTulach Jun 4, 2024
33f8694
Measure arrow x + u (and not y)
JaroslavTulach Jun 4, 2024
ac3eda6
Inline ByteBuffer.getLong and putLong methods
JaroslavTulach Jun 4, 2024
869fd76
Using Specialized Libraries
JaroslavTulach Jun 4, 2024
87ff552
Brute force speedup
JaroslavTulach Jun 4, 2024
d78e305
Eliminating write overhead
JaroslavTulach Jun 4, 2024
13e244b
Let's abstract direct buffer access with a Truffle Iterator
JaroslavTulach Jun 5, 2024
62cf4fb
Merge remote-tracking branch 'origin/develop' into wip/jtulach/PocArr…
JaroslavTulach Jun 5, 2024
1b126a8
Benchmarking overflowing plus
JaroslavTulach Jun 7, 2024
fa140c0
Introducing AppendNode
JaroslavTulach Jun 7, 2024
f8eefc3
Using (unoptimized) builder.append again
JaroslavTulach Jun 7, 2024
801184a
Cache logical layout in the plus operation
JaroslavTulach Jun 7, 2024
5dc5d77
Value to number conversion node
JaroslavTulach Jun 7, 2024
46f28d0
AppendNode via ValueToNumberNode
JaroslavTulach Jun 7, 2024
fc42f06
Removing unused crap
JaroslavTulach Jun 7, 2024
c993a91
Runtime.assert that the Column results are the same as Arrow results
JaroslavTulach Jun 7, 2024
f10bd06
PutNode to optimize AppendNode
JaroslavTulach Jun 7, 2024
a661aca
Merging with develop
JaroslavTulach Jun 7, 2024
72c89a1
Proper, but slow, support for nulls in the arrow vectors
JaroslavTulach Jun 7, 2024
5e54a10
Manipulate bitmapBuffer from nodes
JaroslavTulach Jun 8, 2024
5fcba2d
Introducing NullIterator that reads bitmapBuffer
JaroslavTulach Jun 8, 2024
d03e484
Introducing executeOp and eliminating overhead of ArithmeticException
JaroslavTulach Jun 8, 2024
3f49737
Making the graph smaller by eliminating handling of exceptional state
JaroslavTulach Jun 8, 2024
55ee7cc
Math.addExact is intrinsified. Use it! But only until first Arithmeti…
JaroslavTulach Jun 8, 2024
2bb6ebc
Benchmark work with an array with occational Nothing values
JaroslavTulach Jun 8, 2024
bce1892
Merge branch 'develop' into wip/jtulach/PocArrowPlus10056
mergify[bot] Jun 10, 2024
807fd6d
create_arrow_columns function
JaroslavTulach Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,42 @@ private ArrowParser() {}
public record Result(PhysicalLayout physicalLayout, LogicalLayout logicalLayout, Mode mode) {}

public static Result parse(Source source) {
String src = source.getCharacters().toString();
Matcher m = NEW_ARRAY_CONSTR.matcher(src);
String src = source.getCharacters().toString().replace('\n', ' ').trim();
Matcher m = PATTERN.matcher(src);
if (m.find()) {
try {
var layout = LogicalLayout.valueOf(m.group(1));
return new Result(PhysicalLayout.Primitive, layout, Mode.Allocate);
var layout = LogicalLayout.valueOf(m.group(2));
var mode = Mode.parse(m.group(1));
if (layout != null && mode != null) {
return new Result(PhysicalLayout.Primitive, layout, mode);
}
} catch (IllegalArgumentException iae) {
// propagate warning
return null;
}
}

m = CAST_PATTERN.matcher(src);
if (m.find()) {
try {
var layout = LogicalLayout.valueOf(m.group(1));
return new Result(PhysicalLayout.Primitive, layout, Mode.Cast);
} catch (IllegalArgumentException iae) {
// propagate warning
return null;
}
}
return null;
}

private static final Pattern NEW_ARRAY_CONSTR = Pattern.compile("^new\\[(.+)\\]$");
private static final Pattern CAST_PATTERN = Pattern.compile("^cast\\[(.+)\\]$");
private static final Pattern PATTERN = Pattern.compile("^([a-z\\+]+)\\[(.+)\\]$");

public enum Mode {
Allocate,
Cast
Allocate("new"),
Cast("cast"),
Plus("+");

private final String op;

private Mode(String text) {
this.op = text;
}

static Mode parse(String operation) {
for (var m : values()) {
if (m.op.equals(operation)) {
return m;
}
}
return null;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import com.oracle.truffle.api.nodes.RootNode;
import org.enso.interpreter.arrow.ArrowLanguage;
import org.enso.interpreter.arrow.ArrowParser;
import org.enso.interpreter.arrow.runtime.ArrowCastToFixedSizeArrayFactory;
import org.enso.interpreter.arrow.runtime.ArrowFixedSizeArrayFactory;
import org.enso.interpreter.arrow.runtime.ArrowOperationPlus;

public class ArrowEvalNode extends RootNode {
private final ArrowParser.Result code;

@Child private ArrowFixedSizeNode fixedPhysicalLayout = ArrowFixedSizeNode.create();
@Child private ArrowCastFixedSizeNode castToFixedPhysicalLayout = ArrowCastFixedSizeNode.create();

public static ArrowEvalNode create(ArrowLanguage language, ArrowParser.Result code) {
return new ArrowEvalNode(language, code);
}
Expand All @@ -25,8 +25,10 @@ private ArrowEvalNode(ArrowLanguage language, ArrowParser.Result code) {
public Object execute(VirtualFrame frame) {
return switch (code.physicalLayout()) {
case Primitive -> switch (code.mode()) {
case Allocate -> fixedPhysicalLayout.execute(code.logicalLayout());
case Cast -> castToFixedPhysicalLayout.execute(code.logicalLayout());
case Allocate -> new ArrowFixedSizeArrayFactory(code.logicalLayout());
case Cast -> new ArrowCastToFixedSizeArrayFactory(code.logicalLayout());
case Plus -> new ArrowOperationPlus(code.logicalLayout());
default -> throw CompilerDirectives.shouldNotReachHere("unsupported mode");
};
default -> throw CompilerDirectives.shouldNotReachHere("unsupported physical layout");
};
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
package org.enso.interpreter.arrow.runtime;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.ImportStatic;
import com.oracle.truffle.api.dsl.NeverDefault;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.InvalidArrayIndexException;
import com.oracle.truffle.api.interop.StopIterationException;
import com.oracle.truffle.api.interop.TruffleObject;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.profiles.InlinedExactClassProfile;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
import org.enso.interpreter.arrow.LogicalLayout;

@ExportLibrary(InteropLibrary.class)
Expand All @@ -27,7 +37,24 @@ public LogicalLayout getUnit() {
}

@ExportMessage
public boolean hasArrayElements() {
boolean hasArrayElements() {
return true;
}

@ExportMessage
Object getIterator(
@Cached(value = "this.getUnit()", allowUncached = true) LogicalLayout cachedUnit)
throws UnsupportedMessageException {
if (cachedUnit == LogicalLayout.Int64) {
var dataIt = new LongIterator(buffer.getDataBuffer(), cachedUnit.sizeInBytes());
var nullIt = new NullIterator(dataIt, buffer.getBitmapBuffer());
return nullIt;
}
return new GenericIterator(this);
}

@ExportMessage
boolean hasIterator() {
return true;
}

Expand Down Expand Up @@ -65,13 +92,18 @@ public static Object doInt(ArrowFixedArrayInt receiver, long index)
}

@Specialization(guards = "receiver.getUnit() == Int64")
public static Object doLong(ArrowFixedArrayInt receiver, long index)
public static Object doLong(
ArrowFixedArrayInt receiver,
long index,
@Bind("$node") Node node,
@CachedLibrary("receiver") InteropLibrary iop,
@Cached InlinedExactClassProfile bufferClazz)
throws UnsupportedMessageException, InvalidArrayIndexException {
var at = adjustedIndex(receiver.buffer, receiver.unit, receiver.size, index);
var at = adjustedIndex(receiver.buffer, LogicalLayout.Int64, receiver.size, index);
if (receiver.buffer.isNull((int) index)) {
return NullValue.get();
}
return receiver.buffer.getLong(at);
return receiver.buffer.getLong(at, iop, bufferClazz);
}
}

Expand All @@ -96,4 +128,133 @@ boolean isArrayElementReadable(long index) {
private static int typeAdjustedIndex(long index, SizeInBytes unit) {
return Math.toIntExact(index * unit.sizeInBytes());
}

@ExportLibrary(InteropLibrary.class)
static final class LongIterator implements TruffleObject {
private int at;
private final ByteBuffer buffer;
@NeverDefault final int step;

LongIterator(ByteBuffer buffer, int step) {
assert step != 0;
this.buffer = buffer;
this.step = step;
}

@ExportMessage
Object getIteratorNextElement(
@Bind("$node") Node node,
@Cached("this.step") int step,
@Cached InlinedExactClassProfile bufferTypeProfile)
throws StopIterationException {
var buf = bufferTypeProfile.profile(node, buffer);
try {
var res = buf.getLong(at);
at += step;
return res;
} catch (BufferOverflowException ex) {
CompilerDirectives.transferToInterpreter();
throw StopIterationException.create();
}
}
JaroslavTulach marked this conversation as resolved.
Show resolved Hide resolved

@ExportMessage
boolean isIterator() {
return true;
}

@ExportMessage
boolean hasIteratorNextElement() throws UnsupportedMessageException {
return at < buffer.limit();
}
}

@ExportLibrary(value = InteropLibrary.class)
static final class NullIterator implements TruffleObject {
private final TruffleObject it;
private final ByteBuffer buffer;
private byte byteMask;
private byte byteValue;

NullIterator(TruffleObject delegate, ByteBuffer buffer) {
this.it = delegate;
this.buffer = buffer;
}

final TruffleObject it() {
return it;
}

@ExportMessage(limit = "3")
Object getIteratorNextElement(
@Bind("$node") Node node,
@CachedLibrary("this.it()") InteropLibrary iopIt,
@Cached InlinedExactClassProfile bufferTypeProfile)
throws StopIterationException, UnsupportedMessageException {
var element = iopIt.getIteratorNextElement(it);
if (buffer != null) {
var buf = bufferTypeProfile.profile(node, buffer);
if (byteMask == 0) {
// (byte) (0x01 << 8) ==> 0
byteValue = buf.get();
byteMask = 0x01;
}
var include = byteValue & byteMask;
byteMask = (byte) (byteMask << 1);
if (include == 0) {
return NullValue.get();
}
}
return element;
}

@ExportMessage
boolean isIterator() {
return true;
}

@ExportMessage(limit = "3")
boolean hasIteratorNextElement(@CachedLibrary("this.it()") InteropLibrary iopIt)
throws UnsupportedMessageException {
return iopIt.hasIteratorNextElement(it);
}
}

@ExportLibrary(InteropLibrary.class)
static final class GenericIterator implements TruffleObject {
private int at;
private final TruffleObject array;

GenericIterator(TruffleObject array) {
assert InteropLibrary.getUncached().hasArrayElements(array);
this.array = array;
}

TruffleObject array() {
return array;
}

@ExportMessage(limit = "3")
Object getIteratorNextElement(@CachedLibrary("this.array()") InteropLibrary iop)
throws StopIterationException {
try {
var res = iop.readArrayElement(array, at);
at++;
return res;
} catch (UnsupportedMessageException | InvalidArrayIndexException ex) {
throw StopIterationException.create();
}
}

@ExportMessage
boolean isIterator() {
return true;
}

@ExportMessage(limit = "3")
boolean hasIteratorNextElement(@CachedLibrary("this.array()") InteropLibrary iop)
throws UnsupportedMessageException {
return at < iop.getArraySize(array);
}
}
}
Loading
Loading