Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 1 | #include "RenderScript.h" |
| 2 | #include <sys/time.h> |
| 3 | |
| 4 | #include "ScriptC_latency.h" |
| 5 | |
| 6 | using namespace android; |
Tim Murray | 9eb7f4b | 2012-11-16 14:02:18 -0800 | [diff] [blame] | 7 | using namespace RSC; |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 8 | |
| 9 | int main(int argc, char** argv) |
| 10 | { |
| 11 | int iters = 100; |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 12 | int numElems = 1000; |
Tim Murray | 97960af | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 13 | bool forceCpu = false; |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 14 | bool synchronous = false; |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 15 | |
| 16 | if (argc >= 2) { |
| 17 | iters = atoi(argv[1]); |
| 18 | if (iters <= 0) { |
| 19 | printf("iters must be positive\n"); |
| 20 | return 1; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | printf("iters = %d\n", iters); |
| 25 | |
| 26 | if (argc >= 3) { |
| 27 | numElems = atoi(argv[2]); |
| 28 | if (numElems <= 0) { |
| 29 | printf("numElems must be positive\n"); |
| 30 | return 1; |
| 31 | } |
| 32 | } |
| 33 | |
Tim Murray | 97960af | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 34 | if (argc >= 4) { |
| 35 | int temp = atoi(argv[3]); |
| 36 | if (temp != 0) |
| 37 | forceCpu = true; |
| 38 | } |
| 39 | |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 40 | if (argc >= 5) { |
| 41 | int temp = atoi(argv[4]); |
| 42 | if (temp != 0) |
| 43 | synchronous = true; |
| 44 | } |
| 45 | |
Tim Murray | 97960af | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 46 | if (forceCpu) |
| 47 | printf("forcing CPU\n"); |
| 48 | |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 49 | if (synchronous) |
| 50 | printf("forcing synchronous\n"); |
| 51 | |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 52 | printf("numElems = %d\n", numElems); |
| 53 | |
| 54 | sp<RS> rs = new RS(); |
| 55 | |
Tim Murray | 5253429 | 2013-09-11 12:38:29 -0700 | [diff] [blame] | 56 | uint32_t flags = 0; |
| 57 | if (forceCpu) flags |= RS_INIT_LOW_LATENCY; |
| 58 | if (synchronous) flags |= RS_INIT_SYNCHRONOUS; |
| 59 | |
Tim Murray | caf4126 | 2013-12-13 12:54:37 -0800 | [diff] [blame] | 60 | bool r = rs->init("/system/bin", flags); |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 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 | |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 68 | uint32_t *buf = new uint32_t[numElems]; |
| 69 | |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 70 | sp<Allocation> ain = Allocation::createTyped(rs, t); |
| 71 | sp<Allocation> aout = Allocation::createTyped(rs, t); |
| 72 | |
Stephen Hines | 41efe87 | 2013-08-22 00:39:29 -0700 | [diff] [blame] | 73 | sp<ScriptC_latency> sc = new ScriptC_latency(rs); |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 74 | |
| 75 | struct timeval start, stop; |
| 76 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame^] | 77 | gettimeofday(&start, nullptr); |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 78 | |
| 79 | for (int i = 0; i < iters; i++) { |
| 80 | sc->forEach_root(ain, aout); |
| 81 | } |
| 82 | |
Tim Murray | a4cbc2b | 2012-11-14 17:18:08 -0800 | [diff] [blame] | 83 | rs->finish(); |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 84 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame^] | 85 | gettimeofday(&stop, nullptr); |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 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 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame^] | 91 | gettimeofday(&start, nullptr); |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 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 | |
Chris Wailes | 44bef6f | 2014-08-12 13:51:10 -0700 | [diff] [blame^] | 101 | gettimeofday(&stop, nullptr); |
Tim Murray | 4d252d6 | 2012-11-29 14:37:59 -0800 | [diff] [blame] | 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 | |
Tim Murray | 943eb67 | 2012-11-06 14:38:06 -0800 | [diff] [blame] | 106 | sc.clear(); |
| 107 | t.clear(); |
| 108 | e.clear(); |
| 109 | ain.clear(); |
| 110 | aout.clear(); |
| 111 | } |