compute.cpp revision e443dd2ae7be6323a422626e7c8dc9d936d8f9bb
1
2#include "RenderScript.h"
3
4#include "ScriptC_multiply.h"
5
6int main(int argc, char** argv)
7{
8
9    uint32_t numElems = 1024;
10    uint32_t stride = 1025;
11
12    if (argc >= 2) {
13        int tempStride = atoi(argv[1]);
14        if (tempStride < 1024) {
15            printf("stride must be greater than or equal to 1024\n");
16            return 1;
17        }
18        stride = (uint32_t) tempStride;
19    }
20
21    sp<RS> rs = new RS();
22
23    bool r = rs->init("/system/bin");
24
25    sp<const Element> e = Element::U32(rs);
26
27    Type::Builder tb(rs, e);
28    tb.setX(numElems);
29    tb.setY(numElems);
30    sp<const Type> t = tb.create();
31
32    sp<Allocation> ain = Allocation::createTyped(rs, t);
33    sp<Allocation> aout = Allocation::createTyped(rs, t);
34
35    sp<ScriptC_multiply> sc = new ScriptC_multiply(rs);
36
37    uint32_t* buf = (uint32_t*) malloc(stride * numElems * sizeof(uint32_t));
38    if (!buf) {
39        printf("malloc failed\n");
40        return 1;
41    }
42
43    for (uint32_t i = 0; i < numElems; i++) {
44        for (uint32_t ct=0; ct < numElems; ct++) {
45            *(buf+(stride*i)+ct) = (uint32_t)ct + (i * numElems);
46        }
47    }
48
49    ain->copy2DStridedFrom(buf, stride * sizeof(uint32_t));
50
51    sc->forEach_multiply(ain, aout);
52
53    aout->copy2DStridedTo(buf, stride * sizeof(uint32_t));
54
55    for (uint32_t i = 0; i < numElems; i++) {
56        for (uint32_t ct=0; ct < numElems; ct++) {
57            if (*(buf+(stride*i)+ct) != (uint32_t)(ct + (i * numElems)) * 2) {
58                printf("Mismatch at location %d, %d: %u\n", i, ct, *(buf+(stride*i)+ct));
59                return 1;
60            }
61        }
62    }
63
64    printf("Test successful with %u stride!\n", stride);
65
66    sc.clear();
67    t.clear();
68    e.clear();
69    ain.clear();
70    aout.clear();
71}
72