Skip to content
Snippets Groups Projects
Commit cb478666 authored by Florian Angerer's avatar Florian Angerer
Browse files

Avoid using 'setElement'.

parent a4fe0cd1
No related branches found
No related tags found
No related merge requests found
...@@ -43,7 +43,6 @@ import com.oracle.truffle.r.runtime.data.RDoubleVector; ...@@ -43,7 +43,6 @@ import com.oracle.truffle.r.runtime.data.RDoubleVector;
import com.oracle.truffle.r.runtime.data.RIntVector; import com.oracle.truffle.r.runtime.data.RIntVector;
import com.oracle.truffle.r.runtime.data.RList; import com.oracle.truffle.r.runtime.data.RList;
import com.oracle.truffle.r.runtime.data.RLogicalVector; import com.oracle.truffle.r.runtime.data.RLogicalVector;
import com.oracle.truffle.r.runtime.data.RRaw;
import com.oracle.truffle.r.runtime.data.RRawVector; import com.oracle.truffle.r.runtime.data.RRawVector;
import com.oracle.truffle.r.runtime.data.RStringVector; import com.oracle.truffle.r.runtime.data.RStringVector;
import com.oracle.truffle.r.runtime.data.RVector; import com.oracle.truffle.r.runtime.data.RVector;
...@@ -141,11 +140,16 @@ public abstract class Transpose extends RBuiltinNode.Arg1 { ...@@ -141,11 +140,16 @@ public abstract class Transpose extends RBuiltinNode.Arg1 {
return vector; return vector;
} }
/**
* Transposes an n x m matrix in place using a follow-the-cycle algorithm.
*/
protected RDoubleVector transposeInPlace(RDoubleVector v) { protected RDoubleVector transposeInPlace(RDoubleVector v) {
int[] dimensions = getDimensions(v); int[] dimensions = getDimensions(v);
int w = dimensions[0]; int w = dimensions[0];
int h = dimensions[1]; int h = dimensions[1];
double[] store = v.getDataWithoutCopying();
for (int start = 0; start <= w * h - 1; ++start) { for (int start = 0; start <= w * h - 1; ++start) {
int next = start; int next = start;
int i = 0; int i = 0;
...@@ -155,11 +159,11 @@ public abstract class Transpose extends RBuiltinNode.Arg1 { ...@@ -155,11 +159,11 @@ public abstract class Transpose extends RBuiltinNode.Arg1 {
} while (next > start); } while (next > start);
if (next >= start && i != 1) { if (next >= start && i != 1) {
double tmp = v.getDataAt(start); double tmp = store[start];
next = start; next = start;
do { do {
i = (next % h) * w + next / h; i = (next % h) * w + next / h;
v.setElement(next, (i == start) ? tmp : v.getDataAt(i)); store[next] = (i == start) ? tmp : store[i];
next = i; next = i;
} while (next > start); } while (next > start);
} }
...@@ -190,70 +194,80 @@ public abstract class Transpose extends RBuiltinNode.Arg1 { ...@@ -190,70 +194,80 @@ public abstract class Transpose extends RBuiltinNode.Arg1 {
@Specialization(guards = "isSquare(x)") @Specialization(guards = "isSquare(x)")
protected RVector<?> transposeSquare(RAbstractIntVector x) { protected RVector<?> transposeSquare(RAbstractIntVector x) {
RIntVector execute = (RIntVector) reuseNonShared.execute(x).materialize(); RIntVector execute = (RIntVector) reuseNonShared.execute(x).materialize();
int[] store = execute.getDataWithoutCopying();
return transposeSquareMatrixInPlace(execute, (i, j) -> { return transposeSquareMatrixInPlace(execute, (i, j) -> {
int tmp = execute.getDataAt(i); int tmp = store[i];
execute.setElement(i, execute.getDataAt(j)); store[i] = store[j];
execute.setElement(j, tmp); store[j] = tmp;
}); });
} }
@Specialization(guards = "isSquare(x)") @Specialization(guards = "isSquare(x)")
protected RVector<?> transposeSquare(RAbstractLogicalVector x) { protected RVector<?> transposeSquare(RAbstractLogicalVector x) {
RLogicalVector execute = (RLogicalVector) reuseNonShared.execute(x).materialize(); RLogicalVector execute = (RLogicalVector) reuseNonShared.execute(x).materialize();
byte[] store = execute.getDataWithoutCopying();
return transposeSquareMatrixInPlace(execute, (i, j) -> { return transposeSquareMatrixInPlace(execute, (i, j) -> {
byte tmp = execute.getDataAt(i); byte tmp = store[i];
execute.setElement(i, execute.getDataAt(j)); store[i] = store[j];
execute.setElement(j, tmp); store[j] = tmp;
}); });
} }
@Specialization(guards = "isSquare(x)") @Specialization(guards = "isSquare(x)")
protected RVector<?> transposeSquare(RAbstractDoubleVector x) { protected RVector<?> transposeSquare(RAbstractDoubleVector x) {
RDoubleVector execute = (RDoubleVector) reuseNonShared.execute(x).materialize(); RDoubleVector execute = (RDoubleVector) reuseNonShared.execute(x).materialize();
double[] store = execute.getDataWithoutCopying();
return transposeSquareMatrixInPlace(execute, (i, j) -> { return transposeSquareMatrixInPlace(execute, (i, j) -> {
double tmp = execute.getDataAt(i); double tmp = store[i];
execute.setElement(i, execute.getDataAt(j)); store[i] = store[j];
execute.setElement(j, tmp); store[j] = tmp;
}); });
} }
@Specialization(guards = "isSquare(x)") @Specialization(guards = "isSquare(x)")
protected RVector<?> transposeSquare(RAbstractComplexVector x) { protected RVector<?> transposeSquare(RAbstractComplexVector x) {
RComplexVector execute = (RComplexVector) reuseNonShared.execute(x).materialize(); RComplexVector execute = (RComplexVector) reuseNonShared.execute(x).materialize();
double[] store = execute.getDataWithoutCopying();
return transposeSquareMatrixInPlace(execute, (i, j) -> { return transposeSquareMatrixInPlace(execute, (i, j) -> {
RComplex tmp = execute.getDataAt(i); double tmpReal = store[i * 2];
execute.setElement(i, execute.getDataAt(j)); double tmpImg = store[i * 2 + 1];
execute.setElement(j, tmp); store[i * 2] = store[j * 2];
store[i * 2 + 1] = store[j * 2 + 1];
store[j * 2] = tmpReal;
store[j * 2 + 1] = tmpImg;
}); });
} }
@Specialization(guards = "isSquare(x)") @Specialization(guards = "isSquare(x)")
protected RVector<?> transposeSquare(RAbstractStringVector x) { protected RVector<?> transposeSquare(RAbstractStringVector x) {
RStringVector execute = (RStringVector) reuseNonShared.execute(x).materialize(); RStringVector execute = (RStringVector) reuseNonShared.execute(x).materialize();
String[] store = execute.getDataWithoutCopying();
return transposeSquareMatrixInPlace(execute, (i, j) -> { return transposeSquareMatrixInPlace(execute, (i, j) -> {
String tmp = execute.getDataAt(i); String tmp = store[i];
execute.setElement(i, execute.getDataAt(j)); store[i] = store[j];
execute.setElement(j, tmp); store[j] = tmp;
}); });
} }
@Specialization(guards = "isSquare(x)") @Specialization(guards = "isSquare(x)")
protected RVector<?> transposeSquare(RAbstractListVector x) { protected RVector<?> transposeSquare(RAbstractListVector x) {
RList execute = (RList) reuseNonShared.execute(x).materialize(); RList execute = (RList) reuseNonShared.execute(x).materialize();
Object[] store = execute.getDataWithoutCopying();
return transposeSquareMatrixInPlace(execute, (i, j) -> { return transposeSquareMatrixInPlace(execute, (i, j) -> {
Object tmp = execute.getDataAt(i); Object tmp = store[i];
execute.setElement(i, execute.getDataAt(j)); store[i] = store[j];
execute.setElement(j, tmp); store[j] = tmp;
}); });
} }
@Specialization(guards = "isSquare(x)") @Specialization(guards = "isSquare(x)")
protected RVector<?> transposeSquare(RAbstractRawVector x) { protected RVector<?> transposeSquare(RAbstractRawVector x) {
RRawVector execute = (RRawVector) reuseNonShared.execute(x).materialize(); RRawVector execute = (RRawVector) reuseNonShared.execute(x).materialize();
byte[] store = execute.getDataWithoutCopying();
return transposeSquareMatrixInPlace(execute, (i, j) -> { return transposeSquareMatrixInPlace(execute, (i, j) -> {
RRaw tmp = execute.getDataAt(i); byte tmp = store[i];
execute.setElement(i, execute.getDataAt(j)); store[i] = store[j];
execute.setElement(j, tmp); store[j] = tmp;
}); });
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment