Skip to content

Commit bf40b7c

Browse files
committed
[GR-2798] Complex vector test added to testrffi.
PullRequest: fastr/1894
2 parents 25f0afc + e8f420e commit bf40b7c

File tree

9 files changed

+134
-34
lines changed

9 files changed

+134
-34
lines changed

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/NativeDataAccess.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,14 @@ static double getDataI(RComplexVector vector, double[] data, int index) {
854854
}
855855
}
856856

857+
static double getComplexPart(RComplexVector vector, double[] data, int index) {
858+
if (noComplexNative.isValid() || data != null) {
859+
return data[index];
860+
} else {
861+
return getDoubleNativeMirrorData(vector.getNativeMirror(), index);
862+
}
863+
}
864+
857865
static int getDataLength(RComplexVector vector, double[] data) {
858866
if (noComplexNative.isValid() || data != null) {
859867
return data.length >> 1;
@@ -895,6 +903,16 @@ static void setData(RComplexVector vector, double[] data, int index, double re,
895903
}
896904
}
897905

906+
static void setData(RComplexVector vector, double[] data, int index, double value) {
907+
if (noComplexNative.isValid() || data != null) {
908+
data[index] = value;
909+
} else {
910+
long address = ((NativeMirror) vector.getNativeMirror()).dataAddress;
911+
assert address != 0;
912+
UnsafeAdapter.UNSAFE.putDouble(address + index * Unsafe.ARRAY_DOUBLE_INDEX_SCALE, value);
913+
}
914+
}
915+
898916
static void setDataLength(RStringVector vector, CharSXPWrapper[] data, int length) {
899917
if (noStringNative.isValid() || data != null) {
900918
asPointer(vector);

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/RComplexVector.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,22 @@ public void setDataAt(Object store, int index, RComplex value) {
128128
NativeDataAccess.setData(this, (double[]) store, index, value.getRealPart(), value.getImaginaryPart());
129129
}
130130

131+
@Override
132+
public void setDataAt(Object store, int index, double value) {
133+
assert data == store;
134+
NativeDataAccess.setData(this, (double[]) store, index, value);
135+
}
136+
131137
@Override
132138
public RComplex getDataAt(int index) {
133139
return NativeDataAccess.getData(this, data, index);
134140
}
135141

142+
@Override
143+
public double getComplexPartAt(int index) {
144+
return NativeDataAccess.getComplexPart(this, data, index);
145+
}
146+
136147
@Override
137148
public double[] getDataCopy() {
138149
if (data != null) {

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/model/RAbstractComplexVector.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -43,6 +43,16 @@ default void setDataAt(Object store, int index, RComplex value) {
4343
throw new UnsupportedOperationException();
4444
}
4545

46+
@SuppressWarnings("unused")
47+
default void setDataAt(Object store, int index, double value) {
48+
throw new UnsupportedOperationException();
49+
}
50+
51+
@SuppressWarnings("unused")
52+
default double getComplexPartAt(int index) {
53+
throw new UnsupportedOperationException();
54+
}
55+
4656
@Override
4757
default RType getRType() {
4858
return RType.Complex;

com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/ffi/VectorRFFIWrapper.java

Lines changed: 77 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@
5959
import com.oracle.truffle.r.runtime.data.RObject;
6060
import com.oracle.truffle.r.runtime.data.RRawVector;
6161
import com.oracle.truffle.r.runtime.data.RStringVector;
62-
import com.oracle.truffle.r.runtime.data.model.RAbstractContainer;
63-
import com.oracle.truffle.r.runtime.data.model.RAbstractLogicalVector;
62+
import com.oracle.truffle.r.runtime.ffi.VectorRFFIWrapperFactory.AtomicVectorGetterNodeGen;
6463
import com.oracle.truffle.r.runtime.ffi.VectorRFFIWrapperFactory.AtomicVectorSetterNodeGen;
6564
import com.oracle.truffle.r.runtime.ffi.VectorRFFIWrapperFactory.NumberToIntNodeGen;
6665
import com.oracle.truffle.r.runtime.ffi.VectorRFFIWrapperFactory.VectorRFFIWrapperNativePointerFactory.DispatchAllocateNodeGen;
@@ -283,35 +282,11 @@ protected Object access(VectorRFFIWrapper receiver) {
283282
abstract static class VectorWrapperReadNode extends Node {
284283
@Child private Node readMsg = Message.READ.createNode();
285284
@Child private NumberToInt getIndexNode = NumberToIntNodeGen.create();
286-
private final ConditionProfile isStringVectorProfile = ConditionProfile.createBinaryProfile();
287-
private final ConditionProfile isLogicalVectorProfile = ConditionProfile.createBinaryProfile();
288-
private final ConditionProfile isContainerProfile = ConditionProfile.createBinaryProfile();
289-
private final BranchProfile isNAProfile = BranchProfile.create();
285+
@Child private AtomicVectorGetterNode getElemNode = AtomicVectorGetterNodeGen.create();
290286

291287
public Object access(VectorRFFIWrapper receiver, Object index) {
292288
int i = getIndexNode.executeInteger(index);
293-
if (isStringVectorProfile.profile(receiver.vector instanceof RStringVector)) {
294-
((RStringVector) receiver.vector).wrapStrings();
295-
// TODO: for now character vector shouldn't return plain java.lang.String,
296-
// otherwise we'd need to make sure that all the places that expect CharSXP
297-
// can also deal with java.lang.String
298-
return ((RStringVector) receiver.vector).getWrappedDataAt(i);
299-
} else if (isLogicalVectorProfile.profile(receiver.vector instanceof RAbstractLogicalVector)) {
300-
byte ret = ((RAbstractLogicalVector) receiver.vector).getDataAt(i);
301-
if (ret == RRuntime.LOGICAL_NA) {
302-
isNAProfile.enter();
303-
return RRuntime.INT_NA;
304-
}
305-
return ret;
306-
} else if (isContainerProfile.profile(receiver.vector instanceof RAbstractContainer)) {
307-
return ((RAbstractContainer) receiver.vector).getDataAtAsObject(i);
308-
} else {
309-
try {
310-
return ForeignAccess.sendRead(readMsg, receiver.vector, index);
311-
} catch (InteropException e) {
312-
throw RInternalError.shouldNotReachHere(e);
313-
}
314-
}
289+
return getElemNode.execute(receiver.vector, i);
315290
}
316291
}
317292

@@ -381,6 +356,72 @@ protected int doDouble(double x) {
381356
}
382357
}
383358

359+
public abstract static class AtomicVectorGetterNode extends Node {
360+
@Child private Node readMsgNode;
361+
362+
public abstract Object execute(Object vector, int index);
363+
364+
@Specialization
365+
protected Object doStringVector(RStringVector vector, int index) {
366+
vector.wrapStrings();
367+
// TODO: for now character vector shouldn't return plain java.lang.String,
368+
// otherwise we'd need to make sure that all the places that expect CharSXP
369+
// can also deal with java.lang.String
370+
return vector.getWrappedDataAt(index);
371+
}
372+
373+
@Specialization
374+
protected Object doIntVector(RIntVector vector, int index) {
375+
return vector.getDataAt(index);
376+
}
377+
378+
@Specialization
379+
protected Object doDoubleVector(RDoubleVector vector, int index) {
380+
return vector.getDataAt(index);
381+
}
382+
383+
@Specialization
384+
protected Object doComplexVector(RComplexVector vector, int index) {
385+
return vector.getComplexPartAt(index);
386+
}
387+
388+
@Specialization
389+
protected Object doRawVector(RRawVector vector, int index) {
390+
return vector.getRawDataAt(index);
391+
}
392+
393+
@Specialization
394+
protected Object doLogicalVector(RLogicalVector vector, int index,
395+
@Cached("create()") BranchProfile naProfile) {
396+
byte ret = vector.getDataAt(index);
397+
if (ret == RRuntime.LOGICAL_NA) {
398+
naProfile.enter();
399+
return RRuntime.INT_NA;
400+
}
401+
return ret;
402+
}
403+
404+
@Specialization
405+
protected Object doList(RList vector, int index) {
406+
return vector.getDataAt(index);
407+
}
408+
409+
@Fallback
410+
protected Object doOther(Object target, int index) {
411+
assert target instanceof TruffleObject;
412+
try {
413+
if (readMsgNode == null) {
414+
CompilerDirectives.transferToInterpreterAndInvalidate();
415+
readMsgNode = insert(Message.READ.createNode());
416+
}
417+
return ForeignAccess.sendRead(readMsgNode, (TruffleObject) target, index);
418+
} catch (InteropException e) {
419+
throw RInternalError.shouldNotReachHere(e);
420+
}
421+
}
422+
423+
}
424+
384425
public abstract static class AtomicVectorSetterNode extends Node {
385426
@Child private Node writeMsgNode;
386427

@@ -460,8 +501,14 @@ protected Object doStringVector(RStringVector vector, int index, CharSXPWrapper
460501
return vector;
461502
}
462503

463-
Node createWriteMessageNode() {
464-
return Message.WRITE.createNode();
504+
@Specialization
505+
protected Object doComplexVector(RComplexVector vector, int index, double value, @Cached("create()") BranchProfile naProfile) {
506+
if (RRuntime.isNA(value)) {
507+
naProfile.enter();
508+
vector.setComplete(false);
509+
}
510+
vector.setDataAt(vector.getInternalStore(), index, value);
511+
return vector;
465512
}
466513

467514
@Fallback

com.oracle.truffle.r.test.native/packages/testrffi/testrffi/R/testrffi.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,7 @@ rffi.test_setVar <- function(symbol, value, env) {
289289
rffi.test_setAttribDimDoubleVec <- function(vec, dimDoubleVec) {
290290
.Call('test_Rf_setAttribDimDoubleVec', vec, dimDoubleVec)
291291
}
292+
293+
rffi.test_sort_complex <- function(complexVec) {
294+
.Call('test_sort_complex', complexVec)
295+
}

com.oracle.truffle.r.test.native/packages/testrffi/testrffi/src/init.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -99,6 +99,7 @@ static const R_CallMethodDef CallEntries[] = {
9999
CALLDEF(test_R_nchar, 1),
100100
CALLDEF(test_forceAndCall, 3),
101101
CALLDEF(test_constant_types, 0),
102+
CALLDEF(test_sort_complex, 1),
102103
#include "init_api.h"
103104
{NULL, NULL, 0}
104105
};

com.oracle.truffle.r.test.native/packages/testrffi/testrffi/src/testrffi.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,4 +917,8 @@ SEXP test_Rf_setAttribDimDoubleVec(SEXP vec, SEXP dimDoubleVec) {
917917
return R_NilValue;
918918
}
919919

920-
920+
SEXP test_sort_complex(SEXP complexVec) {
921+
Rcomplex *cpl = COMPLEX(complexVec);
922+
R_csort(cpl, LENGTH(complexVec));
923+
return complexVec;
924+
}

com.oracle.truffle.r.test.native/packages/testrffi/testrffi/src/testrffi.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -140,3 +140,5 @@ extern SEXP shareDoubleElement(SEXP x, SEXP xIndex, SEXP y, SEXP yIndex);
140140
extern SEXP shareStringElement(SEXP x, SEXP xIndex, SEXP y, SEXP yIndex);
141141

142142
extern SEXP shareListElement(SEXP x, SEXP xIndex, SEXP y, SEXP yIndex);
143+
144+
extern SEXP test_sort_complex(SEXP complexVec);

com.oracle.truffle.r.test.native/packages/testrffi/testrffi/tests/simpleTests.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,6 @@ d <- c(2.0, 3.0)
379379
rffi.test_setAttribDimDoubleVec(v, d)
380380
print(dim(v))
381381

382+
# Complex vectors
383+
x <- c(4+3i,2+1i)
384+
rffi.test_sort_complex(x)

0 commit comments

Comments
 (0)