Skip to content

Commit d009a25

Browse files
committed
Full support for filter operation for images in PM
Finally we have full support for filter operations in BitmapImage and HDRImage in ParallelME run-time. Closes #23.
1 parent 866b471 commit d009a25

13 files changed

Lines changed: 1431 additions & 219 deletions

File tree

src/parallelme-compiler/src/main/java/org/parallelme/compiler/RuntimeCommonDefinitions.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,20 @@ public String getDataVarName() {
286286
return this.getPrefix() + "data";
287287
}
288288

289+
/**
290+
* Return the base name for data variables.
291+
*/
292+
public String getDataTileVarName() {
293+
return this.getPrefix() + "dataTile";
294+
}
295+
289296
/**
290297
* Return the base name for data return variables.
291298
*/
292299
public String getDataReturnVarName() {
293300
return this.getPrefix() + "dataRet";
294301
}
295-
302+
296303
/**
297304
* Creates a method's signature that can be used in Java code.
298305
*

src/parallelme-compiler/src/main/java/org/parallelme/compiler/translation/renderscript/RSImageTranslator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ protected void fillReduceOperationCall(ST st, Operation operation) {
147147
if (operation.getExecutionType() == ExecutionType.Parallel) {
148148
st.addAggr("tileSize.{name, expression}",
149149
getTileSizeVariableName(operation), String.format(
150-
"%s.getType().getY()", commonDefinitions
150+
"%s.getType().getX()", commonDefinitions
151151
.getVariableOutName(operation.variable)));
152152
}
153153
}

src/parallelme-compiler/src/main/java/org/parallelme/compiler/translation/renderscript/RSTranslator.java

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@ public abstract class RSTranslator extends BaseUserLibraryTranslator {
6060
+ "\t\t<inputVar2> = rsGetElementAt_<varType>(<dataVar>, <x:{var|x, }><declBaseVar:{var|<baseVar> + }><xVar>);\n"
6161
+ "\t\t<inputVar1> = <userFunctionName>(<inputVar1>, <inputVar2>);\n"
6262
+ "\t}\n" + "\treturn <inputVar1>;\n";
63-
private static final String templateFilter = "\tint <varCount> = 0;\n"
64-
+ "\tfor (int <xVar>=0; <xVar>\\<rsAllocationGetDimX(<tileAllocation>); ++<xVar>) {\n"
65-
+ "<isImage:{var|\tfor (int <yVar>=0; <yVar>\\<rsAllocationGetDimY(<tileAllocation>); ++<yVar>) {\n}>"
66-
+ "\t\tint PM_value = rsGetElementAt_int(<tileAllocation>, <xVar><isImage:{var|, <yVar>}>);\n"
67-
+ "\t\tif (PM_value > 0) {\n"
68-
+ "\t\t\trsSetElementAt_<type>(<outputAllocation>, rsGetElementAt_<type>(<inputAllocation>, PM_value), <varCount>++);\n"
69-
+ "\t\t}\n" + "<isImage:{var|\t\\}\n}>" + "\t}\n";
70-
private static final String templateParallelFilterTile = "\tif (<userFunctionName>(<varName><isImage:{var|, <xVar>, <yVar>}>)) {\n"
71-
+ "\t\trsAtomicInc(&<varCounterName>);\n"
72-
+ "\t\treturn x;\n"
73-
+ "\t} else {\n" + "\t\treturn -1;\n" + "\t}\n";
7463
private static final String templateFunctionDecl = "<modifier:{var|<var.value> }><returnType><isKernel:{var| __attribute__((kernel))}> <functionName>("
7564
+ "<params:{var|<var.type> <var.name>}; separator=\", \">)";
7665
private static final String templateAllocation = "Type <allocationName>Type = new Type.Builder(<rsVarName>, Element.<rsType>(<rsVarName>))\n"
@@ -79,14 +68,27 @@ public abstract class RSTranslator extends BaseUserLibraryTranslator {
7968
+ "<declareAllocation:{var|Allocation }><allocationName> = Allocation.createTyped(<rsVarName>, <allocationName>Type);";
8069
private static String templateParallelForeachMapFunction = "\treturn <userFunction>("
8170
+ "<varName><isImage:{var|, x, y}>);\n";
82-
private static String templateSequentialFunction = "\tfor (int <xVar>=0; <xVar>\\<rsAllocationGetDimX(<readAllocation>); ++<xVar>) {\n"
83-
+ "<isImage:{var|\tfor (int <yVar>=0; <yVar>\\<rsAllocationGetDimY(<readAllocation>); ++<yVar>) {\n}>"
84-
+ "\t\t\t<body>\n"
85-
+ "<isImage:{var|\t\\}\n}>"
71+
private static String templateSequentialFunction = "<countVar:{var|int <var.name> = 0;\n}>"
72+
+ "\tfor (int <xVar>=0; <xVar>\\<rsAllocationGetDimX(<readAllocation>); ++<xVar>) {\n"
73+
+ "<isImage:{var|\t\tfor (int <yVar>=0; <yVar>\\<rsAllocationGetDimY(<readAllocation>); ++<yVar>) {\n}>"
74+
+ "\t\t<body>\n"
75+
+ "<isImage:{var|\t\t\\}\n}>"
8676
+ "\t}\n"
8777
+ "<setExternalVariables:{var|\t\trsSetElementAt_<var.type>(<var.allocationName>, <var.varName>, 0);\n}>";
8878
private static String templateSetValues = "rsSetElementAt_<typeSet>(<writeAllocation>, <userFunction>(rsGetElementAt_<typeGet>("
89-
+ "<readAllocation>, <xVar><isImage:{var|, <yVar>}>)), <xVar><isImage:{var|, <yVar>}>);";
79+
+ "<readAllocation>, <xVar><isImage:{var|, <yVar>}>)<isImage:{var|, <xVar>, <yVar>}>), <notImageMap:{var|<xVar><isImage:{var|, <yVar>}>}>"
80+
+ "<imageMap:{var|<countVar>++}>);";
81+
private static final String templateFilter = "\tint <varCount> = 0;\n"
82+
+ "\tfor (int <xVar>=0; <xVar>\\<rsAllocationGetDimX(<tileAllocation>); ++<xVar>) {\n"
83+
+ "<isImage:{var|\tfor (int <yVar>=0; <yVar>\\<rsAllocationGetDimY(<tileAllocation>); ++<yVar>) {\n}>"
84+
+ "\t\tint PM_value = rsGetElementAt_int(<tileAllocation>, <xVar><isImage:{var|, <yVar>}>);\n"
85+
+ "\t\tif (PM_value >= 0) {\n"
86+
+ "\t\t\trsSetElementAt_<type>(<outputAllocation>, rsGetElementAt_<type>(<inputAllocation>, PM_value), <varCount>++);\n"
87+
+ "\t\t}\n" + "<isImage:{var|\t\\}\n}>" + "\t}\n";
88+
private static final String templateParallelFilterTile = "\tif (<userFunctionName>(<varName><isImage:{var|, <xVar>, <yVar>}>)) {\n"
89+
+ "\t\trsAtomicInc(&<varCounterName>);\n"
90+
+ "\t\treturn x;\n"
91+
+ "\t} else {\n" + "\t\treturn -1;\n" + "\t}\n";
9092
private static String templateFilterOperationCall = "int <sizeVarName>[] = new int[1];\n"
9193
+ "<kernelName>.get_<gSizeVariableName>().copyTo(<sizeVarName>);\n"
9294
+ "if (<sizeVarName>[0] > 0) {\n"
@@ -95,7 +97,7 @@ public abstract class RSTranslator extends BaseUserLibraryTranslator {
9597
+ "\t<variables:{var|\n\n\t<kernelName>.set_<var.gVariableName>(<var.variableName>);}>"
9698
+ "<kernels:{var|\n\n\t<kernelName>.<var.rsOperationName>_<var.functionName>(<var.allocations>);}>"
9799
+ "\n}";
98-
private static String templateSequentialFilterTile = "if (<userFunction>(rsGetElementAt_<type>(<inputAllocation>, <xVar><isImage:{var|, <yVar>}>))) {\n"
100+
private static String templateSequentialFilterTile = "if (<userFunction>(rsGetElementAt_<type>(<inputAllocation>, <xVar><isImage:{var|, <yVar>}>)<isImage:{var|, <xVar>, <yVar>}>)) {\n"
99101
+ "\trsSetElementAt_int(<tileAllocation>, <xTileVar>, <xTileVar>);\n"
100102
+ "\t<sizeVarName>++;\n"
101103
+ "} else {\n"
@@ -294,11 +296,6 @@ private void fillForeachOperationCall(ST st, Operation operation) {
294296
commonDefinitions.getOperationName(operation),
295297
variableAllocation + ", " + variableAllocation, "forEach");
296298
}
297-
boolean isImage = commonDefinitions.isImage(operation.variable);
298-
String fromImageVar = commonDefinitions
299-
.getFromImageBooleanName(operation.variable);
300-
st.addAggr("fromImage.{name, value}", fromImageVar, isImage ? "true"
301-
: "false");
302299
}
303300

304301
/**
@@ -474,6 +471,13 @@ private String translateSequentialForeachMap(Operation operation) {
474471
st.add("body", getSetValuesInAllocations(operation, cType, typeGet));
475472

476473
}
474+
if (commonDefinitions.isImage(operation.variable)
475+
&& operation.operationType == OperationType.Map) {
476+
st.addAggr("countVar.{name}", commonDefinitions.getPrefix()
477+
+ "count");
478+
} else {
479+
st.add("countVar", null);
480+
}
477481
return createKernelFunction(operation, st.render(),
478482
FunctionType.BaseOperation);
479483
}
@@ -485,6 +489,15 @@ private String getSetValuesInAllocations(Operation operation,
485489
commonDefinitions.getOperationUserFunctionName(operation));
486490
st.add("typeSet", typeSet);
487491
st.add("typeGet", typeGet);
492+
boolean isImage = commonDefinitions.isImage(operation.variable);
493+
if (operation.operationType == OperationType.Map) {
494+
st.add("notImageMap", !isImage ? "" : null);
495+
st.add("imageMap", isImage ? "" : null);
496+
st.add("countVar", commonDefinitions.getPrefix() + "count");
497+
} else {
498+
st.add("notImageMap", "");
499+
st.add("imageMap", null);
500+
}
488501
setCommonParameters(operation, st);
489502
return st.render();
490503
}
@@ -572,13 +585,16 @@ protected String translateParallelReduceTile(Operation operation) {
572585
commonDefinitions.getOperationUserFunctionName(operation));
573586
st.add("dataVar", getInputDataVariableName(operation));
574587
st.add("dataVarTile", getTileVariableName(operation));
575-
st.add("sizeVar",
576-
getAllocationDimCall("X", getTileVariableName(operation)));
577588
if (commonDefinitions.isImage(operation.variable)) {
589+
st.add("sizeVar",
590+
getAllocationDimCall("Y",
591+
getInputDataVariableName(operation)));
578592
st.add("declBaseVar", null);
579593
st.add("baseVar", "x, 0");
580594
st.add("x", "");
581595
} else {
596+
st.add("sizeVar",
597+
getAllocationDimCall("X", getTileVariableName(operation)));
582598
st.add("x", null);
583599
st.add("declBaseVar", "");
584600
st.add("baseVar", getBaseVariableName());
@@ -668,6 +684,7 @@ private String translateParallelFilterTile(Operation operation) {
668684

669685
private String translateSequentialFilterTile(Operation operation) {
670686
ST st = new ST(templateSequentialFunction);
687+
st.add("countVar", null);
671688
setCommonParameters(operation, st);
672689
setExternalVariables(operation, st);
673690
ST stBody = new ST(templateSequentialFilterTile);

src/parallelme-compiler/src/main/java/org/parallelme/compiler/translation/renderscript/RenderScriptRuntimeDefinition.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public List<String> getInitializationString(String className,
8080
ST st1 = new ST(templateKernels);
8181
ST st2 = new ST(templateConstructor);
8282
String javaClassName = RuntimeCommonDefinitions.getInstance()
83-
.getJavaWrapperClassName(className, this.getTargetRuntime());
83+
.getJavaWrapperClassName(className, getTargetRuntime());
8484
st1.add("originalClassName", className);
8585
st1.add("kernelName", RuntimeCommonDefinitions.getInstance()
8686
.getKernelName(className));
@@ -125,34 +125,30 @@ public void translateOperationsAndBinds(String packageName,
125125
if (!inputBindTypes.contains(inputBind.variable.typeName)) {
126126
inputBindTypes.add(inputBind.variable.typeName);
127127
st.add("functions",
128-
this.translators.get(inputBind.variable.typeName)
128+
translators.get(inputBind.variable.typeName)
129129
.translateInputBind(className, inputBind));
130130
}
131131
}
132132
// 3. Translate operations
133133
for (Operation operation : operationsAndBinds.operations) {
134-
st.add("functions",
135-
this.translators.get(operation.variable.typeName)
136-
.translateOperation(operation));
134+
st.add("functions", translators.get(operation.variable.typeName)
135+
.translateOperation(operation));
137136
}
138137
// 4. Translate outputbinds
139138
Set<String> outputBindTypes = new HashSet<String>();
140139
for (OutputBind outputBind : operationsAndBinds.outputBinds) {
141140
if (!outputBindTypes.contains(outputBind.variable.typeName)) {
142141
outputBindTypes.add(outputBind.variable.typeName);
143142
st.add("functions",
144-
this.translators.get(outputBind.variable.typeName)
143+
translators.get(outputBind.variable.typeName)
145144
.translateOutputBind(className, outputBind));
146145
}
147146
}
148147
// 5. Write translated file
149-
FileWriter
150-
.writeFile(
151-
className + ".rs",
152-
RuntimeCommonDefinitions.getInstance()
153-
.getRSDestinationFolder(
154-
this.outputDestinationFolder,
155-
packageName), st.render());
148+
FileWriter.writeFile(
149+
className + ".rs",
150+
RuntimeCommonDefinitions.getInstance().getRSDestinationFolder(
151+
outputDestinationFolder, packageName), st.render());
156152
}
157153

158154
/**

0 commit comments

Comments
 (0)