compute.cpp revision 863237215cab4812df373b63ba3bbf2bc1d8647d
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 if (!rs->init("/system/bin")) { 24 printf("Could not initialize RenderScript\n"); 25 return 1; 26 } 27 28 sp<const Element> e = Element::U32(rs); 29 30 Type::Builder tb(rs, e); 31 tb.setX(numElems); 32 tb.setY(numElems); 33 sp<const Type> t = tb.create(); 34 35 sp<Allocation> ain = Allocation::createTyped(rs, t); 36 sp<Allocation> aout = Allocation::createTyped(rs, t); 37 38 sp<ScriptC_multiply> sc = new ScriptC_multiply(rs); 39 40 uint32_t* buf = (uint32_t*) malloc(stride * numElems * sizeof(uint32_t)); 41 if (!buf) { 42 printf("malloc failed\n"); 43 return 1; 44 } 45 46 for (uint32_t i = 0; i < numElems; i++) { 47 for (uint32_t ct=0; ct < numElems; ct++) { 48 *(buf+(stride*i)+ct) = (uint32_t)ct + (i * numElems); 49 } 50 } 51 52 ain->copy2DStridedFrom(buf, stride * sizeof(uint32_t)); 53 54 sc->forEach_multiply(ain, aout); 55 56 aout->copy2DStridedTo(buf, stride * sizeof(uint32_t)); 57 58 for (uint32_t i = 0; i < numElems; i++) { 59 for (uint32_t ct=0; ct < numElems; ct++) { 60 if (*(buf+(stride*i)+ct) != (uint32_t)(ct + (i * numElems)) * 2) { 61 printf("Mismatch at location %d, %d: %u\n", i, ct, *(buf+(stride*i)+ct)); 62 return 1; 63 } 64 } 65 } 66 67 printf("Test successful with %u stride!\n", stride); 68 69 sc.clear(); 70 t.clear(); 71 e.clear(); 72 ain.clear(); 73 aout.clear(); 74} 75