summaryrefslogtreecommitdiff
path: root/tests/java_api/RSUnitTests/supportlibsrc_gen/com/android/rs/unittest/UT_reduce_backward.java
blob: 50cbd25cab4485d7a0c32a6d687475b86d7ce071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// This file is automatically generated from
// frameworks/rs/tests/java_api/RSUnitTests/RSUnitTests.py
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* This is a much simpler version of UT_reduce.java that
 * exercises pragmas after the functions (backward reference),
 * whereas the other test case exercises the pragmas before the
 * functions (forward reference).
 */

package com.android.rs.unittest;

import android.content.Context;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.Float2;
import android.support.v8.renderscript.Int2;
import android.support.v8.renderscript.Int3;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicHistogram;
import android.support.v8.renderscript.Type;
import android.util.Log;

import java.util.Random;

public class UT_reduce_backward extends UnitTest {
    private static final String TAG = "reduce_backward";

    public UT_reduce_backward(Context ctx) {
        super("reduce_backward", ctx);
    }

    private byte[] createInputArrayByte(int len, int seed) {
        byte[] array = new byte[len];
        (new Random(seed)).nextBytes(array);
        return array;
    }

    private float[] createInputArrayFloat(int len, int seed) {
        Random rand = new Random(seed);
        float[] array = new float[len];
        for (int i = 0; i < len; ++i)
            array[i] = rand.nextFloat();
        return array;
    }

    private int[] createInputArrayInt(int len, int seed) {
        Random rand = new Random(seed);
        int[] array = new int[len];
        for (int i = 0; i < len; ++i)
            array[i] = rand.nextInt();
        return array;
    }

    private int[] createInputArrayInt(int len, int seed, int eltRange) {
        Random rand = new Random(seed);
        int[] array = new int[len];
        for (int i = 0; i < len; ++i)
            array[i] = rand.nextInt(eltRange);
        return array;
    }

    private <T extends Number> boolean result(String testName, T javaRslt, T rsRslt) {
        final boolean success = javaRslt.equals(rsRslt);
        Log.i(TAG,
                testName + ": java " + javaRslt + ", rs " + rsRslt + ": " +
                        (success ? "PASSED" : "FAILED"));
        return success;
    }

    private boolean result(String testName, Float2 javaRslt, Float2 rsRslt) {
        final boolean success = (javaRslt.x == rsRslt.x) && (javaRslt.y == rsRslt.y);
        Log.i(TAG,
                testName +
                        ": java (" + javaRslt.x + ", " + javaRslt.y + ")" +
                        ", rs (" + rsRslt.x + ", " + rsRslt.y + ")" +
                        ": " + (success ? "PASSED" : "FAILED"));
        return success;
    }

    private boolean result(String testName, Int2 javaRslt, Int2 rsRslt) {
        final boolean success = (javaRslt.x == rsRslt.x) && (javaRslt.y == rsRslt.y);
        Log.i(TAG,
                testName +
                        ": java (" + javaRslt.x + ", " + javaRslt.y + ")" +
                        ", rs (" + rsRslt.x + ", " + rsRslt.y + ")" +
                        ": " + (success ? "PASSED" : "FAILED"));
        return success;
    }

    ///////////////////////////////////////////////////////////////////

    private int addint(int[] input) {
        int rslt = 0;
        for (int idx = 0; idx < input.length; ++idx)
            rslt += input[idx];
        return rslt;
    }

    private boolean addint1D(RenderScript RS, ScriptC_reduce_backward s) {
        final int[] input = createInputArrayInt(100000, 0, 1 << 13);

        final int javaRslt = addint(input);
        final int rsRslt = s.reduce_addint(input).get();

        return result("addint1D", javaRslt, rsRslt);
    }

    private boolean addint2D(RenderScript RS, ScriptC_reduce_backward s) {
        final int dimX = 450, dimY = 225;

        final int[] inputArray = createInputArrayInt(dimX * dimY, 1, 1 << 13);
        Type.Builder typeBuilder = new Type.Builder(RS, Element.I32(RS));
        typeBuilder.setX(dimX).setY(dimY);
        Allocation inputAllocation = Allocation.createTyped(RS, typeBuilder.create());
        inputAllocation.copy2DRangeFrom(0, 0, dimX, dimY, inputArray);

        final int javaRslt = addint(inputArray);
        final int rsRslt = s.reduce_addint(inputAllocation).get();

        return result("addint2D", javaRslt, rsRslt);
    }

    ///////////////////////////////////////////////////////////////////

    private Int2 findMinAndMax(float[] input) {
        float minVal = Float.POSITIVE_INFINITY;
        int minIdx = -1;
        float maxVal = Float.NEGATIVE_INFINITY;
        int maxIdx = -1;

        for (int idx = 0; idx < input.length; ++idx) {
            if (input[idx] < minVal) {
                minVal = input[idx];
                minIdx = idx;
            }
            if (input[idx] > maxVal) {
                maxVal = input[idx];
                maxIdx = idx;
            }
        }

        return new Int2(minIdx, maxIdx);
    }

    private boolean findMinAndMax(RenderScript RS, ScriptC_reduce_backward s) {
        final float[] input = createInputArrayFloat(100000, 4);

        final Int2 javaRslt = findMinAndMax(input);
        final Int2 rsRslt = s.reduce_findMinAndMax(input).get();

        // Note that the Java and RenderScript algorithms are not
        // guaranteed to find the same cells -- but they should
        // find cells of the same value.
        final Float2 javaVal = new Float2(input[javaRslt.x], input[javaRslt.y]);
        final Float2 rsVal = new Float2(input[rsRslt.x], input[rsRslt.y]);

        return result("findMinAndMax", javaVal, rsVal);
    }

    ///////////////////////////////////////////////////////////////////

    private boolean fz(RenderScript RS, ScriptC_reduce_backward s) {
        final int inputLen = 100000;
        int[] input = createInputArrayInt(inputLen, 5);
        // just in case we got unlucky
        input[(new Random(6)).nextInt(inputLen)] = 0;

        final int rsRslt = s.reduce_fz(input).get();

        final boolean success = (input[rsRslt] == 0);
        Log.i(TAG,
                "fz: input[" + rsRslt + "] == " + input[rsRslt] + ": " +
                        (success ? "PASSED" : "FAILED"));
        return success;
    }

    ///////////////////////////////////////////////////////////////////

    private boolean fz2(RenderScript RS, ScriptC_reduce_backward s) {
        final int dimX = 225, dimY = 450;
        final int inputLen = dimX * dimY;

        int[] inputArray = createInputArrayInt(inputLen, 7);
        // just in case we got unlucky
        inputArray[(new Random(8)).nextInt(inputLen)] = 0;

        Type.Builder typeBuilder = new Type.Builder(RS, Element.I32(RS));
        typeBuilder.setX(dimX).setY(dimY);
        Allocation inputAllocation = Allocation.createTyped(RS, typeBuilder.create());
        inputAllocation.copy2DRangeFrom(0, 0, dimX, dimY, inputArray);

        final Int2 rsRslt = s.reduce_fz2(inputAllocation).get();

        final int cellVal = inputArray[rsRslt.x + dimX * rsRslt.y];
        final boolean success = (cellVal == 0);
        Log.i(TAG,
                "fz2: input[" + rsRslt.x + ", " + rsRslt.y + "] == " + cellVal + ": " +
                        (success ? "PASSED" : "FAILED"));
        return success;
    }

    ///////////////////////////////////////////////////////////////////

    private boolean fz3(RenderScript RS, ScriptC_reduce_backward s) {
        final int dimX = 59, dimY = 48, dimZ = 37;
        final int inputLen = dimX * dimY * dimZ;

        int[] inputArray = createInputArrayInt(inputLen, 9);
        // just in case we got unlucky
        inputArray[(new Random(10)).nextInt(inputLen)] = 0;

        Type.Builder typeBuilder = new Type.Builder(RS, Element.I32(RS));
        typeBuilder.setX(dimX).setY(dimY).setZ(dimZ);
        Allocation inputAllocation = Allocation.createTyped(RS, typeBuilder.create());
        inputAllocation.copy3DRangeFrom(0, 0, 0, dimX, dimY, dimZ, inputArray);

        final Int3 rsRslt = s.reduce_fz3(inputAllocation).get();

        final int cellVal = inputArray[rsRslt.x + dimX * rsRslt.y + dimX * dimY * rsRslt.z];
        final boolean success = (cellVal == 0);
        Log.i(TAG,
                "fz3: input[" + rsRslt.x + ", " + rsRslt.y + ", " + rsRslt.z + "] == " + cellVal + ": " +
                        (success ? "PASSED" : "FAILED"));
        return success;
    }

    ///////////////////////////////////////////////////////////////////

    private static final int histogramBucketCount = 256;

    private long[] histogram(RenderScript RS, final byte[] inputArray) {
        Allocation inputAllocation = Allocation.createSized(RS, Element.U8(RS), inputArray.length);
        inputAllocation.copyFrom(inputArray);

        Allocation outputAllocation = Allocation.createSized(RS, Element.U32(RS), histogramBucketCount);

        ScriptIntrinsicHistogram scriptHsg = ScriptIntrinsicHistogram.create(RS, Element.U8(RS));
        scriptHsg.setOutput(outputAllocation);
        scriptHsg.forEach(inputAllocation);

        int[] outputArrayMistyped = new int[histogramBucketCount];
        outputAllocation.copyTo(outputArrayMistyped);

        long[] outputArray = new long[histogramBucketCount];
        for (int i = 0; i < histogramBucketCount; ++i)
            outputArray[i] = outputArrayMistyped[i] & (long) 0xffffffff;
        return outputArray;
    }

    private boolean histogram(RenderScript RS, ScriptC_reduce_backward s) {
        final byte[] inputArray = createInputArrayByte(100000, 11);

        final long[] javaRslt = histogram(RS, inputArray);
        _RS_ASSERT("javaRslt unexpected length: " + javaRslt.length, javaRslt.length == histogramBucketCount);
        final long[] rsRslt = s.reduce_histogram(inputArray).get();
        _RS_ASSERT("rsRslt unexpected length: " + rsRslt.length, rsRslt.length == histogramBucketCount);

        for (int i = 0; i < histogramBucketCount; ++i) {
            if (javaRslt[i] != rsRslt[i]) {
                Log.i(TAG,
                        "histogram[" + i + "]: java " + javaRslt[i] + ", rs " + rsRslt[i] + ": FAILED");
                return false;
            }
        }

        Log.i(TAG, "histogram: PASSED");
        return true;
    }

    //-----------------------------------------------------------------

    private Int2 mode(RenderScript RS, final byte[] inputArray) {
        long[] hsg = histogram(RS, inputArray);

        int modeIdx = 0;
        for (int i = 1; i < hsg.length; ++i)
            if (hsg[i] > hsg[modeIdx]) modeIdx = i;
        return new Int2(modeIdx, (int) hsg[modeIdx]);
    }

    private boolean mode(RenderScript RS, ScriptC_reduce_backward s) {
        final byte[] inputArray = createInputArrayByte(100000, 12);

        final Int2 javaRslt = mode(RS, inputArray);
        final Int2 rsRslt = s.reduce_mode(inputArray).get();

        return result("mode", javaRslt, rsRslt);
    }

    ///////////////////////////////////////////////////////////////////

    public void run() {
        RenderScript pRS = createRenderScript(false);
        ScriptC_reduce_backward s = new ScriptC_reduce_backward(pRS);
        s.set_negInf(Float.NEGATIVE_INFINITY);
        s.set_posInf(Float.POSITIVE_INFINITY);

        boolean pass = true;
        pass &= addint1D(pRS, s);
        pass &= addint2D(pRS, s);
        pass &= findMinAndMax(pRS, s);
        pass &= fz(pRS, s);
        pass &= fz2(pRS, s);
        pass &= fz3(pRS, s);
        pass &= histogram(pRS, s);
        pass &= mode(pRS, s);

        pRS.finish();
        pRS.destroy();

        Log.i(TAG, pass ? "PASSED" : "FAILED");
        if (pass)
            passTest();
        else
            failTest();
    }
}