Skip to content
Snippets Groups Projects
Commit 33090c9b authored by Ryan R. LaMothe's avatar Ryan R. LaMothe
Browse files

Added ability to execute 'convolution' from Ant via JTP and GPU

parent 3bcd27d2
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="/com.amd.aparapi/dist/aparapi.jar" sourcepath="/com.amd.aparapi">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="com.amd.aparapi.jni/dist"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/com.amd.aparapi/dist/aparapi.jar" sourcepath="/com.amd.aparapi">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="com.amd.aparapi.jni/dist"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="classes"/>
</classpath>
/classes/
<?xml version="1.0"?>
<project name="convolution" default="build" basedir=".">
<target name="build" depends="clean">
<mkdir dir="classes"/>
<javac srcdir="src" destdir="classes" debug="on" includeantruntime="false" >
<classpath>
<pathelement path="../../com.amd.aparapi/dist/aparapi.jar"/>
</classpath>
</javac>
<copy file="src\com\amd\aparapi\sample\convolution\convolution.cl" toDir="classes\com\amd\aparapi\sample\convolution"/>
<jar jarfile="${ant.project.name}.jar" basedir="classes"/>
</target>
<target name="clean">
<delete dir="classes"/>
<delete file="${ant.project.name}.jar"/>
</target>
<!-- Ideally this would be the latest version of Java but not everyone is going to have it installed -->
<!-- Additionally we want to avoid "Class not found: javac1.8" errors from old Ant versions (i.e. Eclipse) -->
<property name="build.compiler" value="javac1.7" />
<property name="ant.build.javac.source" value="1.7" />
<property name="ant.build.javac.target" value="1.7" />
<path id="compiler.class.path">
<pathelement path="../../com.amd.aparapi/dist/aparapi.jar" />
</path>
<path id="runtime.class.path" cache="true">
<path refid="compiler.class.path" />
<pathelement path="${ant.project.name}.jar" />
</path>
<target name="build" depends="clean">
<mkdir dir="classes" />
<javac srcdir="src" destdir="classes" debug="on" includeantruntime="false">
<classpath>
<pathelement path="../../com.amd.aparapi/dist/aparapi.jar" />
</classpath>
</javac>
<copy file="src\com\amd\aparapi\sample\convolution\convolution.cl" toDir="classes\com\amd\aparapi\sample\convolution" />
<jar jarfile="${ant.project.name}.jar" basedir="classes" />
</target>
<target name="clean">
<delete dir="classes" />
<delete file="${ant.project.name}.jar" />
</target>
<target name="run-jtp">
<java classname="com.amd.aparapi.sample.convolution.Convolution" fork="true">
<classpath refid="runtime.class.path" />
<sysproperty key="java.library.path" path="..\..\com.amd.aparapi.jni\dist" />
<sysproperty key="com.amd.aparapi.executionMode" value="JTP" />
</java>
</target>
<target name="run-gpu">
<java classname="com.amd.aparapi.sample.convolution.Convolution" fork="true">
<classpath refid="runtime.class.path" />
<sysproperty key="java.library.path" path="..\..\com.amd.aparapi.jni\dist" />
<sysproperty key="com.amd.aparapi.executionMode" value="GPU" />
</java>
</target>
<target name="run" depends="run-gpu" />
</project>
......@@ -34,7 +34,7 @@ to national security controls as identified on the Commerce Control List (curren
of EAR). For the most current Country Group listings, or for additional information about the EAR or your obligations
under those regulations, please refer to the U.S. Bureau of Industry and Security's website at http://www.bis.doc.gov/.
*/
*/
package com.amd.aparapi.sample.convolution;
......@@ -42,75 +42,79 @@ import java.io.File;
import com.amd.aparapi.Kernel;
public class Convolution{
public class Convolution {
final static class ImageConvolution extends Kernel{
private float convMatrix3x3[];
public static void main(final String[] _args) {
private int width, height;
final File file = new File(_args.length == 1 ? _args[0] : "testcard.jpg");
private byte imageIn[], imageOut[];
final ImageConvolution convolution = new ImageConvolution();
public void processPixel(int x, int y, int w, int h) {
float accum = 0f;
int count = 0;
for (int dx = -3; dx < 6; dx += 3) {
for (int dy = -1; dy < 2; dy += 1) {
int rgb = 0xff & imageIn[((y + dy) * w) + (x + dx)];
final float convMatrix3x3[] = new float[] {
0f,
-10f,
0f,
-10f,
40f,
-10f,
0f,
-10f,
0f,
};
accum += rgb * convMatrix3x3[count++];
new ConvolutionViewer(file, convMatrix3x3) {
private static final long serialVersionUID = 7858079467616904028L;
@Override
protected void applyConvolution(float[] _convMatrix3x3, byte[] _inBytes, byte[] _outBytes, int _width,
int _height) {
convolution.applyConvolution(_convMatrix3x3, _inBytes, _outBytes, _width, _height);
}
};
}
final static class ImageConvolution extends Kernel {
private float convMatrix3x3[];
private int width, height;
private byte imageIn[], imageOut[];
public void processPixel(int x, int y, int w, int h) {
float accum = 0f;
int count = 0;
for (int dx = -3; dx < 6; dx += 3) {
for (int dy = -1; dy < 2; dy += 1) {
final int rgb = 0xff & imageIn[((y + dy) * w) + (x + dx)];
accum += rgb * convMatrix3x3[count++];
}
}
}
byte value = (byte) (max(0, min((int) accum, 255)));
imageOut[y * w + x] = value;
}
@Override public void run() {
int x = getGlobalId(0) % (width * 3);
int y = getGlobalId(0) / (width * 3);
if (x > 3 && x < (width * 3 - 3) && y > 1 && y < (height - 1)) {
processPixel(x, y, width * 3, height);
}
}
public void applyConvolution(float[] _convMatrix3x3, byte[] _imageIn, byte[] _imageOut, int _width, int _height) {
imageIn = _imageIn;
imageOut = _imageOut;
width = _width;
height = _height;
convMatrix3x3 = _convMatrix3x3;
execute(3 * width * height);
}
}
public static void main(final String[] _args) {
File file = new File(_args.length == 1 ? _args[0] : "testcard.jpg");
final ImageConvolution convolution = new ImageConvolution();
float convMatrix3x3[] = new float[] {
0f,
-10f,
0f,
-10f,
40f,
-10f,
0f,
-10f,
0f,
};
new ConvolutionViewer(file, convMatrix3x3){
@Override protected void applyConvolution(float[] _convMatrix3x3, byte[] _inBytes, byte[] _outBytes, int _width,
int _height) {
convolution.applyConvolution(_convMatrix3x3, _inBytes, _outBytes, _width, _height);
}
};
}
}
final byte value = (byte) (max(0, min((int) accum, 255)));
imageOut[(y * w) + x] = value;
}
@Override
public void run() {
final int x = getGlobalId(0) % (width * 3);
final int y = getGlobalId(0) / (width * 3);
if ((x > 3) && (x < ((width * 3) - 3)) && (y > 1) && (y < (height - 1))) {
processPixel(x, y, width * 3, height);
}
}
public void applyConvolution(float[] _convMatrix3x3, byte[] _imageIn, byte[] _imageOut, int _width, int _height) {
imageIn = _imageIn;
imageOut = _imageOut;
width = _width;
height = _height;
convMatrix3x3 = _convMatrix3x3;
execute(3 * width * height);
}
}
}
\ No newline at end of file
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