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

vector feature #71

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions src/main/java/com/aparapi/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ public class Config extends ConfigJNI{

public static final boolean enableARRAY = !Boolean.getBoolean(propPkgName + ".disable.ARRAY");

public static final boolean enableVECTOR = !Boolean.getBoolean(propPkgName + ".disable.VECTOR");

public static final boolean enableNEW = Boolean.getBoolean(propPkgName + ".enable.NEW");

public static final boolean enableATHROW = Boolean.getBoolean(propPkgName + ".enable.ATHROW");
Expand Down
42 changes: 41 additions & 1 deletion src/main/java/com/aparapi/Kernel.java
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,14 @@ public abstract class Kernel implements Cloneable {
boolean atomic64() default false;
}

/**
* This annotation is for internal use only
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface OpenCLMappingPattern {
String mapTo() default "";
}

public abstract class Entry {
public abstract void run();

Expand Down Expand Up @@ -2612,7 +2620,7 @@ private static String toClassShortNameIfAny(final Class<?> retClass) {
final String mapping = typeToLetterMap.get(strRetClass);
// System.out.println("strRetClass = <" + strRetClass + ">, mapping = " + mapping);
if (mapping == null)
return "[" + retClass.getName() + ";";
return "L" + retClass.getName() + ";";
return mapping;
}

Expand Down Expand Up @@ -2688,6 +2696,38 @@ public static boolean isOpenCLDelegateMethod(MethodReferenceEntry methodReferenc
return (isMapped);
}

public static String getMethodMappedPattern(MethodReferenceEntry _methodReferenceEntry) {
final String name = _methodReferenceEntry.getClassEntry().getNameUTF8Entry().getUTF8().replace("/",".");

final Class<?> currentClass;
try {
currentClass = Class.forName(name);
} catch (ClassNotFoundException e) {
return null;
}

String methodSignature = toSignature(_methodReferenceEntry).replace("/", ".");

//System.out.println("? - "+methodSignature);

for (final Method method : currentClass.getMethods()) {
if (method.isAnnotationPresent(OpenCLMappingPattern.class)) {

//System.out.println(toSignature(method));

if (methodSignature.equals(toSignature(method))) {
final OpenCLMappingPattern annotation = method.getAnnotation(OpenCLMappingPattern.class);
final String mapTo = annotation.mapTo();
if (!mapTo.equals("")) {
return mapTo;
}
}
}
}

return null;
}

public static boolean usesAtomic32(MethodReferenceEntry methodReferenceEntry) {
if (CacheEnabler.areCachesEnabled())
return getProperty(atomic32Cache, methodReferenceEntry, false);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/aparapi/internal/jni/KernelArgJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public abstract class KernelArgJNI{
* @see KernelRunnerJNI#ARG_EXPLICIT
* @see KernelRunnerJNI#ARG_EXPLICIT_WRITE
* @see KernelRunnerJNI#ARG_OBJ_ARRAY_STRUCT
* @see KernelRunnerJNI#ARG_VECTOR_2
* @see KernelRunnerJNI#ARG_VECTOR_3
* @see KernelRunnerJNI#ARG_VECTOR_4
* @see KernelRunnerJNI#ARG_VECTOR_8
* @see KernelRunnerJNI#ARG_VECTOR_16
*/
@UsedByJNICode protected int type;

Expand Down
56 changes: 53 additions & 3 deletions src/main/java/com/aparapi/internal/jni/KernelRunnerJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,64 @@ public abstract class KernelRunnerJNI{

/**
* This 'bit' indicates that a particular <code>KernelArg</code> represents a <code>static</code> field (array or primitive).
*
*
*
*
* @see com.aparapi.internal.annotation.UsedByJNICode
*
*
* @author gfrost
*/
@UsedByJNICode protected static final int ARG_STATIC = 1 << 22;

/**
* This 'bit' indicates that a particular <code>KernelArg</code> represents a <code>vector2</code> field (array or primitive).
*
*
* @see com.aparapi.internal.annotation.UsedByJNICode
*
* @author gfrost
*/
@UsedByJNICode protected static final int ARG_VECTOR_2 = 1 << 25;

/**
* This 'bit' indicates that a particular <code>KernelArg</code> represents a <code>vector3</code> field (array or primitive).
*
*
* @see com.aparapi.internal.annotation.UsedByJNICode
*
* @author gfrost
*/
@UsedByJNICode protected static final int ARG_VECTOR_3 = 1 << 26;

/**
* This 'bit' indicates that a particular <code>KernelArg</code> represents a <code>vector4</code> field (array or primitive).
*
*
* @see com.aparapi.internal.annotation.UsedByJNICode
*
* @author gfrost
*/
@UsedByJNICode protected static final int ARG_VECTOR_4 = 1 << 27;

/**
* This 'bit' indicates that a particular <code>KernelArg</code> represents a <code>vector8</code> field (array or primitive).
*
*
* @see com.aparapi.internal.annotation.UsedByJNICode
*
* @author gfrost
*/
@UsedByJNICode protected static final int ARG_VECTOR_8 = 1 << 28;

/**
* This 'bit' indicates that a particular <code>KernelArg</code> represents a <code>vector16</code> field (array or primitive).
*
*
* @see com.aparapi.internal.annotation.UsedByJNICode
*
* @author gfrost
*/
@UsedByJNICode protected static final int ARG_VECTOR_16 = 1 << 29;

/**
* This 'bit' indicates that we wish to enable profiling from the JNI code.
*
Expand Down
74 changes: 61 additions & 13 deletions src/main/java/com/aparapi/internal/kernel/KernelRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ to national security controls as identified on the Commerce Control List (curren
import com.aparapi.internal.writer.*;
import com.aparapi.opencl.*;

import com.aparapi.opencl.vector.Float2;
import java.lang.reflect.*;
import java.nio.*;
import java.util.*;
Expand Down Expand Up @@ -943,7 +944,24 @@ private void extractOopConversionBuffer(KernelArg arg) throws AparapiException {
private void restoreObjects() throws AparapiException {
for (int i = 0; i < argc; i++) {
final KernelArg arg = args[i];
if ((arg.getType() & ARG_OBJ_ARRAY_STRUCT) != 0) {

//TODO: code it properly
if ((arg.getType() & ARG_VECTOR_2) != 0) {
Object obj = arg.getArray();

float[] array = (float[])obj;

try {
Float2[] ref = (Float2[])arg.getField().get(kernel);

for (int j = 0; j < ref.length; j++) {
ref[j] = Float2.create(array[j * 2 + 0], array[j * 2 + 1]);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}

} else if ((arg.getType() & ARG_OBJ_ARRAY_STRUCT) != 0) {
extractOopConversionBuffer(arg);
}
}
Expand Down Expand Up @@ -979,6 +997,11 @@ private boolean updateKernelArrayRefs() throws AparapiException {
if ((arg.getType() & ARG_OBJ_ARRAY_STRUCT) != 0) {
prepareOopConversionBuffer(arg);
} else {
//TODO: handle vectors
if ((arg.getType() & ARG_VECTOR_2) != 0) {
usesOopConversion = true;
newArrayRef = new float[arrayLength * 2];
}
// set up JNI fields for normal arrays
arg.setJavaArray(newArrayRef);
arg.setNumElements(arrayLength);
Expand Down Expand Up @@ -1415,9 +1438,19 @@ else if (Config.enableShowGeneratedOpenCL) {
// args[i].type |= ARG_GLOBAL;

if (type.getName().startsWith("[L")) {
args[i].setArray(null); // will get updated in updateKernelArrayRefs
args[i].setType(args[i].getType()
| (ARG_ARRAY | ARG_OBJ_ARRAY_STRUCT | ARG_WRITE | ARG_READ));
//TODO: get flags from annotation
if ("[Lcom.aparapi.opencl.vector.Float2;".equals(type.getName())) {
args[i].setArray(null);
args[i].setType(
args[i].getType() | (ARG_ARRAY | ARG_FLOAT | ARG_VECTOR_2 | ARG_WRITE | ARG_READ)
);
} else {
// will get updated in updateKernelArrayRefs
args[i].setArray(null);
args[i].setType(
args[i].getType() | (ARG_ARRAY | ARG_OBJ_ARRAY_STRUCT | ARG_WRITE | ARG_READ)
);
}

if (logger.isLoggable(Level.FINE)) {
logger.fine("tagging " + args[i].getName() + " as (ARG_ARRAY | ARG_OBJ_ARRAY_STRUCT | ARG_WRITE | ARG_READ)");
Expand Down Expand Up @@ -1633,24 +1666,39 @@ private int getCurrentPassLocal() {
}

private int getPrimitiveSize(int type) {
int size = 0;
if ((type & ARG_FLOAT) != 0) {
return 4;
size = 4;
} else if ((type & ARG_INT) != 0) {
return 4;
size = 4;
} else if ((type & ARG_BYTE) != 0) {
return 1;
size = 1;
} else if ((type & ARG_CHAR) != 0) {
return 2;
size = 2;
} else if ((type & ARG_BOOLEAN) != 0) {
return 1;
size = 1;
} else if ((type & ARG_SHORT) != 0) {
return 2;
size = 2;
} else if ((type & ARG_LONG) != 0) {
return 8;
size = 8;
} else if ((type & ARG_DOUBLE) != 0) {
return 8;
size = 8;
}
return 0;

int vectorFactor = 1;
if ((type & ARG_VECTOR_2) != 0) {
vectorFactor = 2;
} else if ((type & ARG_VECTOR_3) != 0) {
vectorFactor = 3;
} else if ((type & ARG_VECTOR_4) != 0) {
vectorFactor = 4;
} else if ((type & ARG_VECTOR_8) != 0) {
vectorFactor = 8;
} else if ((type & ARG_VECTOR_16) != 0) {
vectorFactor = 16;
}

return size * vectorFactor;
}

private void setMultiArrayType(KernelArg arg, Class<?> type) throws AparapiException {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/aparapi/internal/model/Entrypoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ public Entrypoint(ClassModel _classModel, MethodModel _methodModel, Object _k) t
for (final MethodCall methodCall : methodModel.getMethodCalls()) {

ClassModelMethod m = resolveCalledMethod(methodCall, classModel);
if ((m != null) && !methodMap.keySet().contains(m) && !noCL(m)) {
if ((m != null) && !methodMap.keySet().contains(m) && !noCL(m) && !isInternal(m)) {
final MethodModel target = new MethodModel(m, this);
methodMap.put(m, target);
methodModel.getCalledMethods().add(target);
Expand Down Expand Up @@ -805,10 +805,14 @@ else if (instruction instanceof I_INVOKEVIRTUAL) {
}

private boolean noCL(ClassModelMethod m) {
boolean found = m.getClassModel().getNoCLMethods().contains(m.getName());
return found;
return m.getClassModel().getNoCLMethods().contains(m.getName());
}

private boolean isInternal(ClassModelMethod m) {
//TODO: use mapped cache
return m.getOwnerClassModel().getClassWeAreModelling().getName().startsWith("com.aparapi.opencl.vector");
}

private FieldEntry getSimpleGetterField(MethodModel method) {
return method.getAccessorVariableFieldEntry();
}
Expand Down
55 changes: 46 additions & 9 deletions src/main/java/com/aparapi/internal/writer/KernelWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ to national security controls as identified on the Commerce Control List (curren
import com.aparapi.internal.model.ClassModel.*;
import com.aparapi.internal.model.ClassModel.ConstantPool.*;

import com.aparapi.internal.tool.InstructionHelper.StringWriter;
import java.util.*;

public abstract class KernelWriter extends BlockWriter{
Expand Down Expand Up @@ -160,7 +161,13 @@ public abstract class KernelWriter extends BlockWriter{
javaToCLIdentifierMap.put("globalBarrier()V", "barrier(CLK_GLOBAL_MEM_FENCE)");
}

/**
private final static Map<String, String> mapping = new HashMap<>();
static {
mapping.put("Lcom/aparapi/opencl/vector/Float2;", "float2");
}


/**
* These three convert functions are here to perform
* any type conversion that may be required between
* Java and OpenCL.
Expand All @@ -170,6 +177,11 @@ public abstract class KernelWriter extends BlockWriter{
* @return Suitably converted string, "char*", etc
*/
@Override public String convertType(String _typeDesc, boolean useClassModel, boolean isLocal) {
String mapTo = mapping.get(_typeDesc);
if (mapTo != null) {
return mapTo + " ";
}

if (_typeDesc.equals("Z") || _typeDesc.equals("boolean")) {
return (cvtBooleanToChar);
} else if (_typeDesc.equals("[Z") || _typeDesc.equals("boolean[]")) {
Expand Down Expand Up @@ -226,6 +238,26 @@ public abstract class KernelWriter extends BlockWriter{
write(barrierAndGetterMappings);
}
} else {
final String pattern = Kernel.getMethodMappedPattern(_methodEntry);
if (pattern != null) {
//System.out.println(pattern);

String[] params = new String[argc];

for (int arg = 0; arg < argc; arg++) {

StringWriter sw = new StringWriter();

sw.writeInstruction(_methodCall.getArg(arg));

params[arg] = sw.toString();
}

write(String.format(pattern, params));

return;
}

final boolean isSpecial = _methodCall instanceof I_INVOKESPECIAL;
MethodModel m = entryPoint.getCallTarget(_methodEntry, isSpecial);

Expand Down Expand Up @@ -381,15 +413,20 @@ public void writePragma(String _name, boolean _enable) {
}

// If it is a converted array of objects, emit the struct param
String className = null;
if (signature.startsWith("L")) {
// Turn Lcom/codegen/javalabs/opencl/demo/DummyOOA; into com_amd_javalabs_opencl_demo_DummyOOA for example
className = (signature.substring(1, signature.length() - 1)).replace('/', '_');
// if (logger.isLoggable(Level.FINE)) {
// logger.fine("Examining object parameter: " + signature + " new: " + className);
// }
argLine.append(className);
thisStructLine.append(className);
String mapTo = mapping.get(signature);
if (mapTo != null) {
argLine.append(mapTo);
thisStructLine.append(mapTo);
} else {
// Turn Lcom/codegen/javalabs/opencl/demo/DummyOOA; into com_amd_javalabs_opencl_demo_DummyOOA for example
String className = (signature.substring(1, signature.length() - 1)).replace('/', '_');
// if (logger.isLoggable(Level.FINE)) {
// logger.fine("Examining object parameter: " + signature + " new: " + className);
// }
argLine.append(className);
thisStructLine.append(className);
}
} else {
argLine.append(convertType(ClassModel.typeName(signature.charAt(0)), false, false));
thisStructLine.append(convertType(ClassModel.typeName(signature.charAt(0)), false, false));
Expand Down
Loading