From 84cc0fddb4da1add636978afecf58df01fb3c35b Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Wed, 21 Oct 2009 17:22:29 +0200 Subject: initial import of jocl-demos project. added first demo: HelloJOCL. --- .gitignore | 5 + build.xml | 74 +++ manifest.mf | 3 + nbproject/build-impl.xml | 704 +++++++++++++++++++++ nbproject/genfiles.properties | 8 + nbproject/project.properties | 69 ++ nbproject/project.xml | 25 + .../mbien/opencl/demos/hellojocl/HelloJOCL.java | 97 +++ src/com/mbien/opencl/demos/hellojocl/VectorAdd.cl | 12 + 9 files changed, 997 insertions(+) create mode 100644 .gitignore create mode 100644 build.xml create mode 100644 manifest.mf create mode 100644 nbproject/build-impl.xml create mode 100644 nbproject/genfiles.properties create mode 100644 nbproject/project.properties create mode 100644 nbproject/project.xml create mode 100644 src/com/mbien/opencl/demos/hellojocl/HelloJOCL.java create mode 100644 src/com/mbien/opencl/demos/hellojocl/VectorAdd.cl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e29c5a9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build +dist +gensrc +**~ +nbproject/private diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..18de08c --- /dev/null +++ b/build.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + Builds, tests, and runs the project jocl-demos. + + + diff --git a/manifest.mf b/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/nbproject/build-impl.xml b/nbproject/build-impl.xml new file mode 100644 index 0000000..9a40f47 --- /dev/null +++ b/nbproject/build-impl.xml @@ -0,0 +1,704 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + + + + + + java -cp "${run.classpath.with.dist.jar}" ${main.class} + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + diff --git a/nbproject/genfiles.properties b/nbproject/genfiles.properties new file mode 100644 index 0000000..148ba77 --- /dev/null +++ b/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=ce6ccff8 +build.xml.script.CRC32=49b29f6c +build.xml.stylesheet.CRC32=958a1d3e@1.26.2.45 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=ce6ccff8 +nbproject/build-impl.xml.script.CRC32=07893c7b +nbproject/build-impl.xml.stylesheet.CRC32=5c621a33@1.26.2.45 diff --git a/nbproject/project.properties b/nbproject/project.properties new file mode 100644 index 0000000..63f2a2e --- /dev/null +++ b/nbproject/project.properties @@ -0,0 +1,69 @@ +application.title=JOCL Demos +application.vendor=mbien +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/jocl-demos.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +file.reference.gluegen-rt.jar=/home/mbien/NetBeansProjects/JOGL/gluegen/build/gluegen-rt.jar +includes=** +jar.compress=false +javac.classpath=\ + ${reference.jocl.jar}:\ + ${file.reference.gluegen-rt.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit.classpath}:\ + ${libs.junit_4.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=com.mbien.opencl.demos.hellojocl.HelloJOCL +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +platform.active=default_platform +project.jocl=../jocl +reference.jocl.jar=${project.jocl}/dist/jocl.jar +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value +# or test-sys-prop.name=value to set system properties for unit tests): +run.jvmargs=-Djava.library.path="${basedir}/../jocl/build/natives/jocl" +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/nbproject/project.xml b/nbproject/project.xml new file mode 100644 index 0000000..893191e --- /dev/null +++ b/nbproject/project.xml @@ -0,0 +1,25 @@ + + + org.netbeans.modules.java.j2seproject + + + jocl-demos + + + + + + + + + + JOCL + jar + + jar + clean + jar + + + + diff --git a/src/com/mbien/opencl/demos/hellojocl/HelloJOCL.java b/src/com/mbien/opencl/demos/hellojocl/HelloJOCL.java new file mode 100644 index 0000000..31cbdef --- /dev/null +++ b/src/com/mbien/opencl/demos/hellojocl/HelloJOCL.java @@ -0,0 +1,97 @@ +package com.mbien.opencl.demos.hellojocl; + +import com.mbien.opencl.CL; +import com.mbien.opencl.CLBuffer; +import com.mbien.opencl.CLCommandQueue; +import com.mbien.opencl.CLContext; +import com.mbien.opencl.CLKernel; +import com.mbien.opencl.CLProgram; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Random; + +import static java.lang.System.*; +import static com.sun.gluegen.runtime.BufferFactory.*; + +/** + * Hello Java OpenCL example. Adds all elements of buffer A to buffer B + * and stores the result in buffer C.
+ * Sample was inspired by the Nvidia VectorAdd example written in C/C++ + * which is bundled in the Nvidia OpenCL SDK. + * @author Michael Bien + */ +public class HelloJOCL { + + public static void main(String[] args) throws IOException { + + int elementCount = 11444777; // Length of float arrays to process + int localWorkSize = 256; // set and log Global and Local work size dimensions + int globalWorkSize = roundUp(localWorkSize, elementCount); // rounded up to the nearest multiple of the localWorkSize + + // set up + CLContext context = CLContext.create(); + + CLProgram program = context.createProgram(HelloJOCL.class.getResourceAsStream("VectorAdd.cl")).build(); + + CLBuffer clBufferA = context.createBuffer(CL.CL_MEM_READ_ONLY, globalWorkSize*SIZEOF_INT); + CLBuffer clBufferB = context.createBuffer(CL.CL_MEM_READ_ONLY, globalWorkSize*SIZEOF_INT); + CLBuffer clBufferC = context.createBuffer(CL.CL_MEM_WRITE_ONLY, globalWorkSize*SIZEOF_INT); + + out.println("used device memory: " + + (clBufferA.buffer.capacity()+clBufferB.buffer.capacity()+clBufferC.buffer.capacity())/1000000 +"MB"); + + // fill read buffers with random numbers (just to have test data; seed is fixed -> results will not change between). + fillBuffer(clBufferA.buffer, 12345); + fillBuffer(clBufferB.buffer, 67890); + + // get a reference to the kernel functon with the name 'VectorAdd' and map the buffers to its input parameters. + CLKernel kernel = program.getCLKernels().get("VectorAdd"); + kernel.setArg(0, SIZEOF_LONG, clBufferA) + .setArg(1, SIZEOF_LONG, clBufferB) + .setArg(2, SIZEOF_LONG, clBufferC) + .setArg(3, SIZEOF_INT, elementCount); + + // create command queue on first device. + CLCommandQueue queue = context.getCLDevices()[0].createCommandQueue(); + + // asynchronous write of data to GPU device, blocking read later to get the computed results back. + long time = nanoTime(); + queue.putWriteBuffer(clBufferA, false) + .putWriteBuffer(clBufferB, false) + .putNDRangeKernel(kernel, 1, null, new long[]{ globalWorkSize }, new long[]{ localWorkSize }) + .putReadBuffer(clBufferC, true); + time = nanoTime() - time; + + // cleanup all resources associated with this context. + context.release(); + + // print first few elements of the resulting buffer to the console. + out.println("a+b=c results snapshot: "); + for(int i = 0; i < 10; i++) + out.print(clBufferC.buffer.getInt() + ", "); + out.println("...; " + clBufferC.buffer.remaining()/SIZEOF_INT + " more"); + + System.out.println("computation took: "+(time/1000000)+"ms"); + + } + + public static final void fillBuffer(ByteBuffer buffer, int seed) { + + Random rnd = new Random(seed); + + while(buffer.remaining() != 0) + buffer.putInt(rnd.nextInt()); + + buffer.rewind(); + } + + public static final int roundUp(int groupSize, int globalSize) { + int r = globalSize % groupSize; + if (r == 0) { + return globalSize; + } else { + return globalSize + groupSize - r; + } + } + +} diff --git a/src/com/mbien/opencl/demos/hellojocl/VectorAdd.cl b/src/com/mbien/opencl/demos/hellojocl/VectorAdd.cl new file mode 100644 index 0000000..b53fc41 --- /dev/null +++ b/src/com/mbien/opencl/demos/hellojocl/VectorAdd.cl @@ -0,0 +1,12 @@ + + // OpenCL Kernel Function for element by element vector addition + __kernel void VectorAdd(__global const int* a, __global const int* b, __global int* c, int iNumElements) { + // get index into global data array + int iGID = get_global_id(0); + // bound check (equivalent to the limit on a 'for' loop for standard/serial C code + if (iGID >= iNumElements) { + return; + } + // add the vector elements + c[iGID] = a[iGID] + b[iGID]; + } -- cgit v1.2.3