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 
  30 import com.oracle.graal.compiler.hsail.test.infra.GraalKernelTester;
  31 
  32 /**
  33  * This version of NBody causes Graal to generate register spilling code.
  34  */
  35 public class StaticNBodySpillTest extends GraalKernelTester {
  36 
  37     static final int bodies = 5;
  38     static final float delT = .005f;
  39     static final float espSqr = 1.0f;
  40     static final float mass = 5f;
  41     static final int width = 768;
  42     static final int height = 768;
  43     // Positions xy and z of bodies.
  44     @Result private float[] inxyz = new float[bodies * 3];
  45     // Positions xy and z of bodies.
  46     @Result private float[] outxyz = new float[bodies * 3]; // positions xy and z of bodies
  47     // Velocity component of x,y and z of bodies.
  48     @Result private float[] invxyz = new float[bodies * 3];
  49     @Result private float[] outvxyz = new float[bodies * 3];
  50     static float[] seedxyz = new float[bodies * 3];
  51     {
  52         final float maxDist = width / 4;
  53         for (int body = 0; body < (bodies * 3); body += 3) {
  54             final float theta = (float) (Math.random() * Math.PI * 2);
  55             final float phi = (float) (Math.random() * Math.PI * 2);
  56             final float radius = (float) (Math.random() * maxDist);
  57             seedxyz[body + 0] = (float) (radius * Math.cos(theta) * Math.sin(phi)) + width / 2;
  58             seedxyz[body + 1] = (float) (radius * Math.sin(theta) * Math.sin(phi)) + height / 2;
  59             seedxyz[body + 2] = (float) (radius * Math.cos(phi));
  60         }
  61     }
  62 
  63     public static void run(float[] inxyz, float[] outxyz, float[] invxyz, float[] outvxyz, int gid) {
  64         final int count = bodies * 3;
  65         final int globalId = gid * 3;
  66         float accx = 0.f;
  67         float accy = 0.f;
  68         float accz = 0.f;
  69         for (int i = 0; i < count; i += 3) {
  70             final float dx = inxyz[i + 0] - inxyz[globalId + 0];
  71             final float dy = inxyz[i + 1] - inxyz[globalId + 1];
  72             final float dz = inxyz[i + 2] - inxyz[globalId + 2];
  73             final float invDist = (float) (1.0 / (Math.sqrt((dx * dx) + (dy * dy) + (dz * dz) + espSqr)));
  74             accx += mass * invDist * invDist * invDist * dx;
  75             accy += mass * invDist * invDist * invDist * dy;
  76             accz += mass * invDist * invDist * invDist * dz;
  77         }
  78         accx *= delT;
  79         accy *= delT;
  80         accz *= delT;
  81         outxyz[globalId + 0] = inxyz[globalId + 0] + (invxyz[globalId + 0] * delT) + (accx * .5f * delT);
  82         outxyz[globalId + 1] = inxyz[globalId + 1] + (invxyz[globalId + 1] * delT) + (accy * .5f * delT);
  83         outxyz[globalId + 2] = inxyz[globalId + 2] + (invxyz[globalId + 2] * delT) + (accz * .5f * delT);
  84         outvxyz[globalId + 0] = invxyz[globalId + 0] + accx;
  85         outvxyz[globalId + 1] = invxyz[globalId + 1] + accy;
  86         outvxyz[globalId + 2] = invxyz[globalId + 2] + accz;
  87     }
  88 
  89     @Override
  90     public void runTest() {
  91         System.arraycopy(seedxyz, 0, inxyz, 0, seedxyz.length);
  92         Arrays.fill(outxyz, 0f);
  93         Arrays.fill(outvxyz, 0f);
  94         Arrays.fill(invxyz, 0f);
  95         dispatchMethodKernel(bodies, inxyz, outxyz, invxyz, outvxyz);
  96     }
  97 
  98     // Marked to only run on hardware until simulator spill bug is fixed.
  99     @Test
 100     public void test() {
 101         testGeneratedHsail();
 102     }
 103 
 104 }