/** * Constructor initializes xyz and vxyz arrays. * * @param _bodies */ public NBodyKernel(Range _range) { range = _range; localStuff = new float[range.getLocalSize(0) * 3]; xyz = new float[range.getGlobalSize(0) * 3]; vxyz = new float[range.getGlobalSize(0) * 3]; final float maxDist = 20f; for (int body = 0; body < (range.getGlobalSize(0) * 3); body += 3) { final float theta = (float) (Math.random() * Math.PI * 2); final float phi = (float) (Math.random() * Math.PI * 2); final float radius = (float) (Math.random() * maxDist); // get the 3D dimensional coordinates xyz[body + 0] = (float) (radius * Math.cos(theta) * Math.sin(phi)); xyz[body + 1] = (float) (radius * Math.sin(theta) * Math.sin(phi)); xyz[body + 2] = (float) (radius * Math.cos(phi)); // divide into two 'spheres of bodies' by adjusting x if ((body % 2) == 0) { xyz[body + 0] += maxDist * 1.5; } else { xyz[body + 0] -= maxDist * 1.5; } } setExplicit(true); }
public static void main(String[] _args) { final int size = 512; final float[] a = new float[size]; final float[] b = new float[size]; for (int i = 0; i < size; i++) { a[i] = (float) (Math.random() * 100); b[i] = (float) (Math.random() * 100); } final float[] sum = new float[size]; Kernel kernel = new Kernel() { @Override public void run() { int gid = getGlobalId(); sum[gid] = a[gid] + b[gid]; } }; kernel.execute(Range.create(512)); for (int i = 0; i < size; i++) { System.out.printf("%6.2f + %6.2f = %8.2f\n", a[i], b[i], sum[i]); } kernel.dispose(); }
public static void main(String[] args) { final int WIDTH = 1024 * 16; final int HEIGHT = 1024 * 8; final int BIN_SIZE = 128; final int GROUP_SIZE = 128; final int SUB_HISTOGRAM_COUNT = ((WIDTH * HEIGHT) / (GROUP_SIZE * BIN_SIZE)); final byte[] data = new byte[WIDTH * HEIGHT]; for (int i = 0; i < (WIDTH * HEIGHT); i++) { data[i] = (byte) ((Math.random() * BIN_SIZE) / 2); } final byte[] sharedArray = new byte[GROUP_SIZE * BIN_SIZE]; final int[] binResult = new int[SUB_HISTOGRAM_COUNT * BIN_SIZE]; System.out.println("binResult size=" + binResult.length); final int[] histo = new int[BIN_SIZE]; final int[] refHisto = new int[BIN_SIZE]; final Device device = KernelManager.instance().bestDevice(); if (device != null) { System.out.println(((OpenCLDevice) device).getOpenCLPlatform().getName()); final Range rangeBinSize = device.createRange(BIN_SIZE); final Range range = Range.create((WIDTH * HEIGHT) / BIN_SIZE, GROUP_SIZE); if (device instanceof OpenCLDevice) { final OpenCLDevice openclDevice = (OpenCLDevice) device; final HistogramKernel histogram = openclDevice.bind( HistogramKernel.class, Histogram.class .getClassLoader() .getResourceAsStream("com/amd/aparapi/sample/extension/HistogramKernel.cl")); long start = System.nanoTime(); histogram .begin() // .put(data) // .histogram256(range, data, sharedArray, binResult, BIN_SIZE) // // by leaving binResult on the GPU we can save two 1Mb transfers .bin256(rangeBinSize, histo, binResult, SUB_HISTOGRAM_COUNT) // .get(histo) // .end(); System.out.println("opencl " + ((System.nanoTime() - start) / 1000000)); start = System.nanoTime(); for (int i = 0; i < (WIDTH * HEIGHT); i++) { refHisto[data[i]]++; } System.out.println("java " + ((System.nanoTime() - start) / 1000000)); for (int i = 0; i < 128; i++) { if (refHisto[i] != histo[i]) { System.out.println(i + " " + histo[i] + " " + refHisto[i]); } } } } else { System.out.println("no GPU device"); } }
/** * Render all particles to the OpenGL context * * @param gl */ protected void render(GL2 gl) { gl.glBegin(GL2.GL_QUADS); for (int i = 0; i < (range.getGlobalSize(0) * 3); i += 3) { gl.glTexCoord2f(0, 1); gl.glVertex3f(xyz[i + 0], xyz[i + 1] + 1, xyz[i + 2]); gl.glTexCoord2f(0, 0); gl.glVertex3f(xyz[i + 0], xyz[i + 1], xyz[i + 2]); gl.glTexCoord2f(1, 0); gl.glVertex3f(xyz[i + 0] + 1, xyz[i + 1], xyz[i + 2]); gl.glTexCoord2f(1, 1); gl.glVertex3f(xyz[i + 0] + 1, xyz[i + 1] + 1, xyz[i + 2]); } gl.glEnd(); }
public static void main(String _args[]) { final NBodyKernel kernel = new NBodyKernel(Range.create(Integer.getInteger("bodies", 8192), 256)); final JFrame frame = new JFrame("NBody"); final JPanel panel = new JPanel(new BorderLayout()); final JPanel controlPanel = new JPanel(new FlowLayout()); panel.add(controlPanel, BorderLayout.SOUTH); final JButton startButton = new JButton("Start"); startButton.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { running = true; startButton.setEnabled(false); } }); 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("FPS")); final JTextField framesPerSecondTextField = new JTextField("0", 5); controlPanel.add(framesPerSecondTextField); controlPanel.add(new JLabel("Score(")); final JLabel miniLabel = new JLabel("<html><small>calcs</small><hr/><small>µsec</small></html>"); controlPanel.add(miniLabel); controlPanel.add(new JLabel(")")); final JTextField positionUpdatesPerMicroSecondTextField = new JTextField("0", 5); controlPanel.add(positionUpdatesPerMicroSecondTextField); final GLCapabilities caps = new GLCapabilities(null); caps.setDoubleBuffered(true); caps.setHardwareAccelerated(true); final GLCanvas canvas = new GLCanvas(caps); final Dimension dimension = new Dimension(Integer.getInteger("width", 742), Integer.getInteger("height", 742)); canvas.setPreferredSize(dimension); canvas.addGLEventListener( new GLEventListener() { private double ratio; private final float xeye = 0f; private final float yeye = 0f; private final float zeye = 100f; private final float xat = 0f; private final float yat = 0f; private final float zat = 0f; public final float zoomFactor = 1.0f; private int frames; private long last = System.currentTimeMillis(); @Override public void dispose(GLAutoDrawable drawable) {} @Override public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glLoadIdentity(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glColor3f(1f, 1f, 1f); final GLU glu = new GLU(); glu.gluPerspective(45f, ratio, 0f, 1000f); glu.gluLookAt(xeye, yeye, zeye * zoomFactor, xat, yat, zat, 0f, 1f, 0f); if (running) { kernel.execute(kernel.range); if (kernel.isExplicit()) { kernel.get(kernel.xyz); } final List<ProfileInfo> profileInfo = kernel.getProfileInfo(); if ((profileInfo != null) && (profileInfo.size() > 0)) { for (final ProfileInfo p : profileInfo) { System.out.print( " " + p.getType() + " " + p.getLabel() + ((p.getEnd() - p.getStart()) / 1000) + "us"); } System.out.println(); } } kernel.render(gl); final long now = System.currentTimeMillis(); final long time = now - last; frames++; if (time > 1000) { // We update the frames/sec every second if (running) { final float framesPerSecond = (frames * 1000.0f) / time; final 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; } gl.glFlush(); } @Override public void init(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2(); gl.glShadeModel(GLLightingFunc.GL_SMOOTH); gl.glEnable(GL.GL_BLEND); gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE); try { final InputStream textureStream = Local.class.getResourceAsStream("particle.jpg"); final Texture texture = TextureIO.newTexture(textureStream, false, null); texture.enable(gl); } catch (final IOException e) { e.printStackTrace(); } catch (final GLException e) { e.printStackTrace(); } } @Override public void reshape(GLAutoDrawable drawable, int x, int y, int _width, int _height) { width = _width; height = _height; final GL2 gl = drawable.getGL().getGL2(); gl.glViewport(0, 0, width, height); ratio = (double) width / (double) height; } }); panel.add(canvas, BorderLayout.CENTER); frame.getContentPane().add(panel, BorderLayout.CENTER); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.pack(); frame.setVisible(true); final FPSAnimator animator = new FPSAnimator(canvas, 100); animator.start(); }