Home | History | Annotate | Download | only in latency
      1 #include "RenderScript.h"
      2 #include <sys/time.h>
      3 
      4 #include "ScriptC_latency.h"
      5 
      6 using namespace android;
      7 using namespace RSC;
      8 
      9 int main(int argc, char** argv)
     10 {
     11     int iters = 100;
     12     int numElems = 1000;
     13     bool forceCpu = false;
     14     bool synchronous = false;
     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 
     34     if (argc >= 4) {
     35         int temp = atoi(argv[3]);
     36         if (temp != 0)
     37             forceCpu = true;
     38     }
     39 
     40     if (argc >= 5) {
     41         int temp = atoi(argv[4]);
     42         if (temp != 0)
     43             synchronous = true;
     44     }
     45 
     46     if (forceCpu)
     47         printf("forcing CPU\n");
     48 
     49     if (synchronous)
     50         printf("forcing synchronous\n");
     51 
     52     printf("numElems = %d\n", numElems);
     53 
     54     sp<RS> rs = new RS();
     55 
     56     uint32_t flags = 0;
     57     if (forceCpu) flags |= RS_INIT_LOW_LATENCY;
     58     if (synchronous) flags |= RS_INIT_SYNCHRONOUS;
     59 
     60     bool r = rs->init(flags);
     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, NULL);
     78 
     79     for (int i = 0; i < iters; i++) {
     80         sc->forEach_root(ain, aout);
     81     }
     82 
     83     rs->finish();
     84 
     85     gettimeofday(&stop, NULL);
     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, NULL);
     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, NULL);
    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