1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* This application is a simple scheduler for testing the lldb debugger 18 * implementation for general reduction kernels. 19 * 20 * It launches one of two simple reductions in a loop 21 */ 22 23 package com.android.rs.lldbreductiontest; 24 25 import android.app.Activity; 26 import android.content.Context; 27 import android.content.res.Resources; 28 import android.os.Handler; 29 import android.os.Bundle; 30 import android.util.Log; 31 import android.renderscript.*; 32 33 import java.lang.Float; 34 import java.lang.Math; 35 import java.util.ArrayList; 36 import java.util.Arrays; 37 import java.util.Random; 38 39 public class MainActivity extends Activity { 40 static private int idxOffset = 10; 41 static private int mX = 128; 42 static private int mY = 2; 43 static private int mZ = 2; 44 static private float mMultiplier = 2.f; 45 private RenderScript mRS; 46 private ScriptC_reduce mScript; 47 private ScriptC_reduce_auto_comb mScript_auto_comb; 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 initRS(); 53 54 int loopDelayMillis = 1000; 55 Handler loopHandler = new Handler(); 56 loopHandler.postDelayed(new Runnable() { 57 @Override 58 public void run() { 59 runRS(); 60 loopHandler.postDelayed(this, loopDelayMillis); 61 } 62 }, loopDelayMillis); 63 } 64 65 private float findMinUserTypeAutoComb( 66 RenderScript rs, ScriptC_reduce_auto_comb s, Allocation alloc) { 67 s.set_a_startval(mX); 68 s.set_b_startval(mY); 69 s.set_multiplier(mMultiplier); 70 71 return s.reduce_find_min_user_type_auto_comb(alloc).get(); 72 } 73 74 private float findMinUserType(RenderScript rs, ScriptC_reduce s, Allocation alloc) { 75 s.set_a_startval(mX); 76 s.set_b_startval(mY); 77 s.set_multiplier(mMultiplier); 78 79 return s.reduce_find_min_user_type(alloc).get(); 80 } 81 82 private float findMinUserType1DAutoComb(RenderScript rs, ScriptC_reduce_auto_comb s, int xCount) { 83 ScriptField_MinUserType minUserType = new ScriptField_MinUserType(rs, xCount); 84 for (int i = 0; i < xCount; i++) { 85 ScriptField_MinUserType.Item val = new ScriptField_MinUserType.Item(); 86 val.a = i + idxOffset; 87 val.b = i + idxOffset; 88 minUserType.set(val, i, true); 89 } 90 91 Allocation alloc = minUserType.getAllocation(); 92 93 return findMinUserTypeAutoComb(rs, s, alloc); 94 } 95 96 private float findMinUserType1D(RenderScript rs, ScriptC_reduce s, int xCount) { 97 ScriptField_MinUserType minUserType = new ScriptField_MinUserType(rs, xCount); 98 for (int i = 0; i < xCount; i++) { 99 ScriptField_MinUserType.Item val = new ScriptField_MinUserType.Item(); 100 val.a = i + idxOffset; 101 val.b = i + idxOffset; 102 minUserType.set(val, i, true); 103 } 104 105 Allocation alloc = minUserType.getAllocation(); 106 107 return findMinUserType(rs, s, alloc); 108 } 109 110 public void initRS() { 111 mRS = RenderScript.create(this, RenderScript.ContextType.NORMAL, 112 RenderScript.CREATE_FLAG_LOW_LATENCY | RenderScript.CREATE_FLAG_WAIT_FOR_ATTACH); 113 mScript = new ScriptC_reduce(mRS); 114 mScript_auto_comb = new ScriptC_reduce_auto_comb(mRS); 115 } 116 117 public void runRS() { 118 findMinUserType1D(mRS, mScript, mX); 119 findMinUserType1DAutoComb(mRS, mScript_auto_comb, mX); 120 } 121 122 public void onDestroy() { 123 mRS.finish(); 124 mRS.destroy(); 125 } 126 } 127