1 /* 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package com.oracle.graal.compiler.hsail.test; 25 26 import java.util.*; 27 28 import org.junit.*; 29 import com.oracle.graal.compiler.hsail.test.infra.GraalKernelTester; 30 31 /** 32 * Unit test of NBody demo app. 33 */ 34 public class StaticNBodyTest extends GraalKernelTester { 35 36 static final int bodies = 1024; 37 static final float delT = .005f; 38 static final float espSqr = 1.0f; 39 static final float mass = 5f; 40 static final int width = 768; 41 static final int height = 768; 42 // Positions xy and z of bodies. 43 @Result private float[] inxyz = new float[bodies * 3]; 44 // Positions xy and z of bodies. 45 @Result private float[] outxyz = new float[bodies * 3]; 46 // Velocity component of x,y and z of bodies. 47 @Result private float[] invxyz = new float[bodies * 3]; 48 @Result private float[] outvxyz = new float[bodies * 3]; 49 static float[] seedxyz = new float[bodies * 3]; 50 { 51 final float maxDist = width / 4; 52 for (int body = 0; body < (bodies * 3); body += 3) { 53 final float theta = (float) (Math.random() * Math.PI * 2); 54 final float phi = (float) (Math.random() * Math.PI * 2); 55 final float radius = (float) (Math.random() * maxDist); 56 seedxyz[body + 0] = (float) (radius * Math.cos(theta) * Math.sin(phi)) + width / 2; 57 seedxyz[body + 1] = (float) (radius * Math.sin(theta) * Math.sin(phi)) + height / 2; 58 seedxyz[body + 2] = (float) (radius * Math.cos(phi)); 59 } 60 } 61 62 public static void run(float[] inxyz, float[] outxyz, float[] invxyz, float[] outvxyz, int gid) { 63 final int count = bodies * 3; 64 final int globalId = gid * 3; 65 float accx = 0.f; 66 float accy = 0.f; 67 float accz = 0.f; 68 for (int i = 0; i < count; i += 3) { 69 final float dx = inxyz[i + 0] - inxyz[globalId + 0]; 70 final float dy = inxyz[i + 1] - inxyz[globalId + 1]; 71 final float dz = inxyz[i + 2] - inxyz[globalId + 2]; 72 final float invDist = (float) (1.0 / (Math.sqrt((dx * dx) + (dy * dy) + (dz * dz) + espSqr))); 73 accx += mass * invDist * invDist * invDist * dx; 74 accy += mass * invDist * invDist * invDist * dy; 75 accz += mass * invDist * invDist * invDist * dz; 76 } 77 accx *= delT; 78 accy *= delT; 79 accz *= delT; 80 outxyz[globalId + 0] = inxyz[globalId + 0] + (invxyz[globalId + 0] * delT) + (accx * .5f * delT); 81 outxyz[globalId + 1] = inxyz[globalId + 1] + (invxyz[globalId + 1] * delT) + (accy * .5f * delT); 82 outxyz[globalId + 2] = inxyz[globalId + 2] + (invxyz[globalId + 2] * delT) + (accz * .5f * delT); 83 outvxyz[globalId + 0] = invxyz[globalId + 0] + accx; 84 outvxyz[globalId + 1] = invxyz[globalId + 1] + accy; 85 outvxyz[globalId + 2] = invxyz[globalId + 2] + accz; 86 } 87 88 @Override 89 public void runTest() { 90 System.arraycopy(seedxyz, 0, inxyz, 0, seedxyz.length); 91 Arrays.fill(outxyz, 0f); 92 Arrays.fill(outvxyz, 0f); 93 Arrays.fill(invxyz, 0f); 94 dispatchMethodKernel(bodies, inxyz, outxyz, invxyz, outvxyz); 95 } 96 97 @Test 98 public void test() { 99 testGeneratedHsail(); 100 } 101 }