Skip to content

Commit

Permalink
add suport for double16 and VectorDouble16 types
Browse files Browse the repository at this point in the history
  • Loading branch information
mikepapadim committed Nov 27, 2023
1 parent bb6c226 commit 15872c1
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
* Copyright (c) 2023, APT Group, Department of Computer Science,
* The University of Manchester. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* GNU Classpath is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GNU Classpath is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Classpath; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* Linking this library statically or dynamically with other modules is
* making a combined work based on this library. Thus, the terms and
* conditions of the GNU General Public License cover the whole
* combination.
*
* As a special exception, the copyright holders of this library give you
* permission to link this library with independent modules to produce an
* executable, regardless of the license terms of these independent
* modules, and to copy and distribute the resulting executable under
* terms of your choice, provided that you also meet, for each linked
* independent module, the terms and conditions of the license of that
* module. An independent module is a module which is not derived from
* or based on this library. If you modify this library, you may extend
* this exception to your version of the library, but you are not
* obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
*/
package uk.ac.manchester.tornado.api.types.collections;

import java.nio.DoubleBuffer;

import uk.ac.manchester.tornado.api.types.arrays.DoubleArray;
import uk.ac.manchester.tornado.api.types.common.PrimitiveStorage;
import uk.ac.manchester.tornado.api.types.vectors.Double16;

public class VectorDouble16 implements PrimitiveStorage<DoubleBuffer> {

public static final Class<VectorDouble16> TYPE = VectorDouble16.class;

private static final int ELEMENT_SIZE = 16;
/**
* backing array.
*/
protected final DoubleArray storage;
/**
* number of elements in the storage.
*/
private final int numElements;

/**
* Creates a vector using the provided backing array.
*
* @param numElements
* @param array
*/
protected VectorDouble16(int numElements, DoubleArray array) {
this.numElements = numElements;
this.storage = array;
}

/**
* Creates a vector using the provided backing array.
*/
public VectorDouble16(DoubleArray array) {
this(array.getSize() / ELEMENT_SIZE, array);
}

/**
* Creates an empty vector with.
*
* @param numElements
*/
public VectorDouble16(int numElements) {
this(numElements, new DoubleArray(numElements * ELEMENT_SIZE));
}

public int vectorWidth() {
return ELEMENT_SIZE;
}

private int toIndex(int index) {
return (index * ELEMENT_SIZE);
}

/**
* Returns the float at the given index of this vector.
*
* @param index
* @return value
*/
public Double16 get(int index) {
return loadFromArray(storage, toIndex(index));
}

private Double16 loadFromArray(final DoubleArray array, int index) {
final Double16 result = new Double16();
for (int i = 0; i < ELEMENT_SIZE; i++) {
result.set(i, array.get(index + i));
}
return result;
}

/**
* Sets the float at the given index of this vector.
*
* @param index
* @param value
*/
public void set(int index, Double16 value) {
storeToArray(value, storage, toIndex(index));
}

private void storeToArray(Double16 value, DoubleArray array, int index) {
for (int i = 0; i < ELEMENT_SIZE; i++) {
array.set(index + i, value.get(i));
}
}

/**
* Sets the elements of this vector to that of the provided vector.
*
* @param values
*/
public void set(VectorDouble16 values) {
for (int i = 0; i < numElements; i++) {
set(i, values.get(i));
}
}

/**
* Sets the elements of this vector to that of the provided array.
*
* @param values
*/
public void set(DoubleArray values) {
VectorDouble16 vector = new VectorDouble16(values);
for (int i = 0; i < numElements; i++) {
set(i, vector.get(i));
}
}

public void fill(double value) {
for (int i = 0; i < storage.getSize(); i++) {
storage.set(i, value);
}
}

/**
* Duplicates this vector.
*
* @return
*/
public VectorDouble16 duplicate() {
VectorDouble16 vector = new VectorDouble16(numElements);
vector.set(this);
return vector;
}

public String toString() {
if (this.numElements > ELEMENT_SIZE) {
return String.format("VectorDouble16 <%d>", this.numElements);
}
StringBuilder tempString = new StringBuilder();
for (int i = 0; i < numElements; i++) {
tempString.append(" ").append(this.get(i).toString());
}
return tempString.toString();
}

public Double16 sum() {
Double16 result = new Double16();
for (int i = 0; i < numElements; i++) {
result = Double16.add(result, get(i));
}
return result;
}

public Double16 min() {
Double16 result = new Double16();
for (int i = 0; i < numElements; i++) {
result = Double16.min(result, get(i));
}
return result;
}

public Double16 max() {
Double16 result = new Double16();
for (int i = 0; i < numElements; i++) {
result = Double16.max(result, get(i));
}
return result;
}

@Override
public void loadFromBuffer(DoubleBuffer buffer) {
asBuffer().put(buffer);
}

@Override
public DoubleBuffer asBuffer() {
return DoubleBuffer.wrap(storage.toHeapArray());
}

public DoubleBuffer asBuffer(DoubleBuffer buffer) {
return asBuffer().put(buffer);
}

@Override
public int size() {
return storage.getSize();
}

public int getLength() {
return numElements;
}

public DoubleArray getArray() {
return storage;
}

public void clear() {
storage.clear();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import uk.ac.manchester.tornado.api.types.arrays.ShortArray;
import uk.ac.manchester.tornado.api.types.vectors.Byte3;
import uk.ac.manchester.tornado.api.types.vectors.Byte4;
import uk.ac.manchester.tornado.api.types.vectors.Double16;
import uk.ac.manchester.tornado.api.types.vectors.Double2;
import uk.ac.manchester.tornado.api.types.vectors.Double3;
import uk.ac.manchester.tornado.api.types.vectors.Double4;
Expand Down Expand Up @@ -145,6 +146,7 @@ public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, Va
registerVectorPlugins(ps, plugins, OCLKind.DOUBLE3, DoubleArray.class, double.class);
registerVectorPlugins(ps, plugins, OCLKind.DOUBLE4, DoubleArray.class, double.class);
registerVectorPlugins(ps, plugins, OCLKind.DOUBLE8, DoubleArray.class, double.class);
registerVectorPlugins(ps, plugins, OCLKind.DOUBLE16, DoubleArray.class, double.class);

registerVectorCollectionsPlugins(plugins, OCLKind.VECTORFLOAT2, FloatArray.class, Float2.class);
registerVectorCollectionsPlugins(plugins, OCLKind.VECTORFLOAT3, FloatArray.class, Float3.class);
Expand All @@ -161,6 +163,7 @@ public boolean handleInvoke(GraphBuilderContext b, ResolvedJavaMethod method, Va
registerVectorCollectionsPlugins(plugins, OCLKind.VECTORDOUBLE3, DoubleArray.class, Double3.class);
registerVectorCollectionsPlugins(plugins, OCLKind.VECTORDOUBLE4, DoubleArray.class, Double4.class);
registerVectorCollectionsPlugins(plugins, OCLKind.VECTORDOUBLE8, DoubleArray.class, Double8.class);
registerVectorCollectionsPlugins(plugins, OCLKind.VECTORDOUBLE16, DoubleArray.class, Double16.class);

registerVectorCollectionsPlugins(plugins, OCLKind.MATRIX2DFLOAT4, FloatArray.class, Float4.class);
registerVectorCollectionsPlugins(plugins, OCLKind.MATRIX3DFLOAT4, FloatArray.class, Float4.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import jdk.vm.ci.meta.PlatformKind;
import jdk.vm.ci.meta.ResolvedJavaType;
import uk.ac.manchester.tornado.api.internal.annotations.Vector;
import uk.ac.manchester.tornado.api.types.collections.VectorDouble16;
import uk.ac.manchester.tornado.api.types.collections.VectorDouble2;
import uk.ac.manchester.tornado.api.types.collections.VectorDouble3;
import uk.ac.manchester.tornado.api.types.collections.VectorDouble4;
Expand All @@ -57,6 +58,7 @@
import uk.ac.manchester.tornado.api.types.matrix.Matrix4x4Float;
import uk.ac.manchester.tornado.api.types.vectors.Byte3;
import uk.ac.manchester.tornado.api.types.vectors.Byte4;
import uk.ac.manchester.tornado.api.types.vectors.Double16;
import uk.ac.manchester.tornado.api.types.vectors.Double2;
import uk.ac.manchester.tornado.api.types.vectors.Double3;
import uk.ac.manchester.tornado.api.types.vectors.Double4;
Expand Down Expand Up @@ -153,6 +155,7 @@ public enum OCLKind implements PlatformKind {
FLOAT8(8, Float8.TYPE, FLOAT),
DOUBLE8(8, Double8.TYPE, DOUBLE),
VECTORDOUBLE8(8, VectorDouble8.TYPE, DOUBLE),
VECTORDOUBLE16(16, VectorDouble16.TYPE, DOUBLE),
VECTORINT8(8, VectorInt8.TYPE, INT),
VECTORFLOAT8(8, VectorFloat8.TYPE, FLOAT),
VECTORFLOAT16(16, VectorFloat16.TYPE, FLOAT),
Expand All @@ -165,7 +168,7 @@ public enum OCLKind implements PlatformKind {
UINT16(16, null, UINT),
LONG16(16, null, LONG),
ULONG16(16, null, ULONG),
DOUBLE16(16, null, DOUBLE),
DOUBLE16(16, Double16.TYPE, DOUBLE),
FLOAT16(16, Float16.TYPE, FLOAT),

ILLEGAL(0, null),
Expand Down
Loading

0 comments on commit 15872c1

Please sign in to comment.