From 41c7fb405037b6b774e7b692f64dfedd3150f76e Mon Sep 17 00:00:00 2001 From: Gary Frost <frost.gary@gmail.com> Date: Tue, 2 Oct 2012 05:17:20 +0000 Subject: [PATCH] --- .../aparapi/examples/javaonedemo/Life.java | 59 ++++++++-------- .../aparapi/examples/javaonedemo/NBody.java | 69 ++++++++++++------- 2 files changed, 72 insertions(+), 56 deletions(-) diff --git a/examples/javaonedemo/src/com/amd/aparapi/examples/javaonedemo/Life.java b/examples/javaonedemo/src/com/amd/aparapi/examples/javaonedemo/Life.java index d439dada..e68fbf4c 100644 --- a/examples/javaonedemo/src/com/amd/aparapi/examples/javaonedemo/Life.java +++ b/examples/javaonedemo/src/com/amd/aparapi/examples/javaonedemo/Life.java @@ -114,15 +114,15 @@ public class Life{ private int toBase; - public LifeKernel(int _width, int _height, BufferedImage _image, Kernel.EXECUTION_MODE _mode) { - setExecutionMode(_mode); + public LifeKernel(int _width, int _height, BufferedImage _image) { + imageData = ((DataBufferInt) _image.getRaster().getDataBuffer()).getData(); width = _width; height = _height; range = Range.create(width * height, 256); System.out.println("range = " + range); - setExplicit(_mode.equals(Kernel.EXECUTION_MODE.GPU)); // This gives us a performance boost for GPU mode. + setExplicit(true); // This gives us a performance boost for GPU mode. fromBase = height * width; toBase = 0; @@ -132,9 +132,8 @@ public class Life{ imageData[toBase + i] = LifeKernel.ALIVE; imageData[fromBase + i] = LifeKernel.ALIVE; } - if (isExplicit()) { - put(imageData); // Because we are using explicit buffer management we must put the imageData array - } + + put(imageData); // Because we are using explicit buffer management we must put the imageData array } @@ -184,7 +183,7 @@ public class Life{ static boolean running = false; - static LifeKernel lifeKernel = null; + // static LifeKernel lifeKernel = null; static double generationsPerSecondField = 0; @@ -199,37 +198,35 @@ public class Life{ // and bottom to top in alternate generation passses. The LifeKernel will track which pass is which final BufferedImage image = new BufferedImage(width, height * 2, BufferedImage.TYPE_INT_RGB); - final LifeKernel lifeKernelJTP = new LifeKernel(width, height, image, Kernel.EXECUTION_MODE.JTP); - - final LifeKernel lifeKernelGPU = new LifeKernel(width, height, image, Kernel.EXECUTION_MODE.GPU); - lifeKernel = lifeKernelJTP; + final LifeKernel lifeKernel = new LifeKernel(width, height, image); + lifeKernel.setExecutionMode(Kernel.EXECUTION_MODE.JTP); final Font font = new Font("Garamond", Font.BOLD, 100); // Create a component for viewing the offsecreen image @SuppressWarnings("serial") JComponent viewer = new JComponent(){ @Override public void paintComponent(Graphics g) { g.setFont(font); - if (lifeKernel != null) { - if (lifeKernel.isExplicit()) { - lifeKernel.get(lifeKernel.imageData); // We only pull the imageData when we intend to use it. - List<ProfileInfo> profileInfo = lifeKernel.getProfileInfo(); - if (profileInfo != null) { - for (ProfileInfo p : profileInfo) { - System.out.print(" " + p.getType() + " " + p.getLabel() + " " + (p.getStart() / 1000) + " .. " - + (p.getEnd() / 1000) + " " + (p.getEnd() - p.getStart()) / 1000 + "us"); - } - System.out.println(); + // if (lifeKernel != null) { + if (lifeKernel.isExplicit()) { + lifeKernel.get(lifeKernel.imageData); // We only pull the imageData when we intend to use it. + List<ProfileInfo> profileInfo = lifeKernel.getProfileInfo(); + if (profileInfo != null) { + for (ProfileInfo p : profileInfo) { + System.out.print(" " + p.getType() + " " + p.getLabel() + " " + (p.getStart() / 1000) + " .. " + + (p.getEnd() / 1000) + " " + (p.getEnd() - p.getStart()) / 1000 + "us"); } + System.out.println(); } - // We copy one half of the offscreen buffer to the viewer, we copy the half that we just mutated. - if (lifeKernel.fromBase == 0) { - g.drawImage(image, 0, 0, width, height, 0, 0, width, height, this); - } else { - g.drawImage(image, 0, 0, width, height, 0, height, width, 2 * height, this); - } - g.drawString(String.format("%5.2f", generationsPerSecondField), 20, 100); - } + // We copy one half of the offscreen buffer to the viewer, we copy the half that we just mutated. + if (lifeKernel.fromBase == 0) { + g.drawImage(image, 0, 0, width, height, 0, 0, width, height, this); + } else { + g.drawImage(image, 0, 0, width, height, 0, height, width, 2 * height, this); + } + g.drawString(String.format("%5.2f", generationsPerSecondField), 20, 100); + + // } } }; @@ -262,11 +259,11 @@ public class Life{ // modeButton = gpuMandelBrot; // } else if (item.equals(choices[0])) { - lifeKernel = lifeKernelJTP; + lifeKernel.setExecutionMode(Kernel.EXECUTION_MODE.JTP); // modeButton = javaMandelBrot; } else if (item.equals(choices[1])) { - lifeKernel = lifeKernelGPU; + lifeKernel.setExecutionMode(Kernel.EXECUTION_MODE.GPU); // modeButton = javaMandelBrotMultiThread; } } diff --git a/examples/javaonedemo/src/com/amd/aparapi/examples/javaonedemo/NBody.java b/examples/javaonedemo/src/com/amd/aparapi/examples/javaonedemo/NBody.java index 33bc80ad..5260a946 100644 --- a/examples/javaonedemo/src/com/amd/aparapi/examples/javaonedemo/NBody.java +++ b/examples/javaonedemo/src/com/amd/aparapi/examples/javaonedemo/NBody.java @@ -42,6 +42,8 @@ import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.ItemEvent; +import java.awt.event.ItemListener; import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -57,16 +59,17 @@ import javax.media.opengl.awt.GLCanvas; import javax.media.opengl.fixedfunc.GLLightingFunc; import javax.media.opengl.glu.GLU; import javax.swing.JButton; +import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; -import javax.swing.JTextField; import javax.swing.WindowConstants; import com.amd.aparapi.Kernel; import com.amd.aparapi.ProfileInfo; import com.amd.aparapi.Range; import com.jogamp.opengl.util.FPSAnimator; +import com.jogamp.opengl.util.gl2.GLUT; import com.jogamp.opengl.util.texture.Texture; import com.jogamp.opengl.util.texture.TextureIO; @@ -197,8 +200,8 @@ public class NBody{ public static void main(String _args[]) { - final NBodyKernel kernel = new NBodyKernel(Range.create(Integer.getInteger("bodies", 8192))); - + final NBodyKernel kernel = new NBodyKernel(Range.create(Integer.getInteger("bodies", 12288))); + kernel.setExecutionMode(Kernel.EXECUTION_MODE.JTP); JFrame frame = new JFrame("NBody"); JPanel panel = new JPanel(new BorderLayout()); @@ -216,28 +219,46 @@ public class NBody{ controlPanel.add(startButton); controlPanel.add(new JLabel(kernel.getExecutionMode().toString())); - controlPanel.add(new JLabel(" Particles")); - controlPanel.add(new JTextField("" + kernel.range.getGlobalSize(0), 5)); + // controlPanel.add(new JLabel(" Particles")); + + final String[] choices = new String[] { + // "Java Sequential", + "Java Threads", + "GPU OpenCL" + }; + + final JComboBox modeButton = new JComboBox(choices); - controlPanel.add(new JLabel("FPS")); - final JTextField framesPerSecondTextField = new JTextField("0", 5); + modeButton.addItemListener(new ItemListener(){ + @Override public void itemStateChanged(ItemEvent e) { + String item = (String) modeButton.getSelectedItem(); - controlPanel.add(framesPerSecondTextField); - controlPanel.add(new JLabel("Score(")); - JLabel miniLabel = new JLabel("<html><small>calcs</small><hr/><small>µsec</small></html>"); + // if (item.equals(choices[2])) { + // modeButton = gpuMandelBrot; + // } else + if (item.equals(choices[0])) { + kernel.setExecutionMode(Kernel.EXECUTION_MODE.JTP); + + // modeButton = javaMandelBrot; + } else if (item.equals(choices[1])) { + // lifeKernel = lifeKernelGPU; + // modeButton = javaMandelBrotMultiThread; + kernel.setExecutionMode(Kernel.EXECUTION_MODE.GPU); + } + } - controlPanel.add(miniLabel); - controlPanel.add(new JLabel(")")); + }); + controlPanel.add(modeButton); - final JTextField positionUpdatesPerMicroSecondTextField = new JTextField("0", 5); + controlPanel.add(new JLabel(" " + kernel.range.getGlobalSize(0) + " Particles")); - controlPanel.add(positionUpdatesPerMicroSecondTextField); GLCapabilities caps = new GLCapabilities(null); final GLProfile profile = caps.getGLProfile(); caps.setDoubleBuffered(true); caps.setHardwareAccelerated(true); final GLCanvas canvas = new GLCanvas(caps); + final GLUT glut = new GLUT(); Dimension dimension = new Dimension(Integer.getInteger("width", 742 - 64), Integer.getInteger("height", 742 - 64)); canvas.setPreferredSize(dimension); @@ -297,18 +318,16 @@ public class NBody{ long time = now - last; frames++; - if (time > 1000) { // We update the frames/sec every second - if (running) { - float framesPerSecond = (frames * 1000.0f) / time; - int updatesPerMicroSecond = (int) ((framesPerSecond * kernel.range.getGlobalSize(0) * kernel.range - .getGlobalSize(0)) / 1000000); - framesPerSecondTextField.setText(String.format("%5.2f", framesPerSecond)); - positionUpdatesPerMicroSecondTextField.setText(String.format("%4d", updatesPerMicroSecond)); - } - frames = 0; - last = now; + if (running) { + int framesPerSecond = (int) ((frames * 1000.0f) / time); + + gl.glColor3f(.5f, .5f, .5f); + gl.glRasterPos2i(-40, 38); + glut.glutBitmapString(8, String.format("%5d fps", framesPerSecond)); + gl.glFlush(); } - gl.glFlush(); + frames = 0; + last = now; } -- GitLab