1#include "RenderScript.h"
2#include <sys/time.h>
3
4#include "ScriptC_latency.h"
5
6int main(int argc, char** argv)
7{
8    int iters = 100;
9    int numElems = 1000;
10    bool forceCpu = false;
11    bool synchronous = false;
12
13    if (argc >= 2) {
14        iters = atoi(argv[1]);
15        if (iters <= 0) {
16            printf("iters must be positive\n");
17            return 1;
18        }
19    }
20
21    printf("iters = %d\n", iters);
22
23    if (argc >= 3) {
24        numElems = atoi(argv[2]);
25        if (numElems <= 0) {
26            printf("numElems must be positive\n");
27            return 1;
28        }
29    }
30
31    if (argc >= 4) {
32        int temp = atoi(argv[3]);
33        if (temp != 0)
34            forceCpu = true;
35    }
36
37    if (argc >= 5) {
38        int temp = atoi(argv[4]);
39        if (temp != 0)
40            synchronous = true;
41    }
42
43    if (forceCpu)
44        printf("forcing CPU\n");
45
46    if (synchronous)
47        printf("forcing synchronous\n");
48
49    printf("numElems = %d\n", numElems);
50
51    sp<RS> rs = new RS();
52
53    uint32_t flags = 0;
54    if (forceCpu) flags |= RS_INIT_LOW_LATENCY;
55    if (synchronous) flags |= RS_INIT_SYNCHRONOUS;
56
57    if (!rs->init("/system/bin", flags)) {
58        printf("Could not initialize RenderScript\n");
59        return 1;
60    }
61
62    sp<const Element> e = Element::U32(rs);
63
64    Type::Builder tb(rs, e);
65    tb.setX(numElems);
66    sp<const Type> t = tb.create();
67
68    uint32_t *buf = new uint32_t[numElems];
69
70    sp<Allocation> ain = Allocation::createTyped(rs, t);
71    sp<Allocation> aout = Allocation::createTyped(rs, t);
72
73    sp<ScriptC_latency> sc = new ScriptC_latency(rs);
74
75    struct timeval start, stop;
76
77    gettimeofday(&start, nullptr);
78
79    for (int i = 0; i < iters; i++) {
80        sc->forEach_root(ain, aout);
81    }
82
83    rs->finish();
84
85    gettimeofday(&stop, nullptr);
86
87    long long elapsed = (stop.tv_sec * 1000000) - (start.tv_sec * 1000000) + (stop.tv_usec - start.tv_usec);
88    printf("elapsed time : %lld microseconds\n", elapsed);
89    printf("time per iter: %f microseconds\n", (double)elapsed / iters);
90
91    gettimeofday(&start, nullptr);
92
93    for (int i = 0; i < iters; i++) {
94        ain->copy1DFrom(buf);
95        sc->forEach_root(ain, aout);
96        aout->copy1DTo(buf);
97    }
98
99    rs->finish();
100
101    gettimeofday(&stop, nullptr);
102    elapsed = (stop.tv_sec * 1000000) - (start.tv_sec * 1000000) + (stop.tv_usec - start.tv_usec);
103    printf("elapsed time with copy : %lld microseconds\n", elapsed);
104    printf("time per iter with copy: %f microseconds\n", (double)elapsed / iters);
105
106    sc.clear();
107    t.clear();
108    e.clear();
109    ain.clear();
110    aout.clear();
111}
112