From 33090c9bf7a30f49b31cd365916f11d4a83edec1 Mon Sep 17 00:00:00 2001
From: "Ryan R. LaMothe" <Ryan.LaMothe@pnnl.gov>
Date: Fri, 26 Sep 2014 15:48:05 -0700
Subject: [PATCH] Added ability to execute 'convolution' from Ant via JTP and
 GPU

---
 samples/convolution/.classpath                |  23 ++-
 samples/convolution/.gitignore                |   1 +
 samples/convolution/build.xml                 |  63 ++++++--
 .../sample/convolution/Convolution.java       | 138 +++++++++---------
 4 files changed, 131 insertions(+), 94 deletions(-)
 create mode 100644 samples/convolution/.gitignore

diff --git a/samples/convolution/.classpath b/samples/convolution/.classpath
index d0b04da9..253bb278 100644
--- a/samples/convolution/.classpath
+++ b/samples/convolution/.classpath
@@ -1,12 +1,11 @@
-<?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>
diff --git a/samples/convolution/.gitignore b/samples/convolution/.gitignore
new file mode 100644
index 00000000..840e7d31
--- /dev/null
+++ b/samples/convolution/.gitignore
@@ -0,0 +1 @@
+/classes/
diff --git a/samples/convolution/build.xml b/samples/convolution/build.xml
index 0bf8e643..036a4642 100644
--- a/samples/convolution/build.xml
+++ b/samples/convolution/build.xml
@@ -1,21 +1,54 @@
 <?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>
diff --git a/samples/convolution/src/com/amd/aparapi/sample/convolution/Convolution.java b/samples/convolution/src/com/amd/aparapi/sample/convolution/Convolution.java
index 4a7b0883..fc70267e 100644
--- a/samples/convolution/src/com/amd/aparapi/sample/convolution/Convolution.java
+++ b/samples/convolution/src/com/amd/aparapi/sample/convolution/Convolution.java
@@ -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
-- 
GitLab