Suppose i have 2 arrays of int
int[10] A; //[1,1,1,1,1,1,1,1,1,1]
int[10] B; //[2,2,2,2,2,2,2,2,2,2]
And the result of array C should be
int[10] C; //[3,3,3,3,3,3,3,3,3,3]
How should I get the result C instead of using a for loop to iterate through each A and B? In another words, is it possible to do parallel computation to save computation time by using the GPU?
I have read that by using RenderScript, it is possible to do such calculation. How should I go about doing it? I would be glad if someone can guide me along or point me to a reference site.
I have already read this and still confused: How to pass array values to and from Android RenderScript using Allocations
Will it be possible to use OpenGl ES to do this too? I read in this post that it is not possible to declare array: How to define constant array in GLSL (OpenGL ES 2.0)?
Current solution:
RenderScript Snippet
int32_t *A;
int32_t *B;
int32_t __attribute__((kernel)) kernelAdd(int32_t in) {
int32_t C;
C = A[in] + B[in];
return C;
}
Java Snippet:
private void intAdd3(int[] A, int[] B) {
RenderScript rs = RenderScript.create(this);
ScriptC_rsintadd intaddscript = new ScriptC_rsintadd(rs,
getResources(), R.raw.rsintadd);
mScript = intaddscript;
// Create allocation for arrays
Allocation a = Allocation.createSized(rs, Element.I32(rs), A.length);
a.copyFrom(A);
intaddscript.bind_A(a);
Allocation b = Allocation.createSized(rs, Element.I32(rs), B.length);
b.copyFrom(B);
intaddscript.bind_B(b);
Allocation array = Allocation.createSized(rs, Element.I32(rs), SIZE);
int[] array_size = new int[SIZE];
for (int i = 0; i < SIZE; i++) {
array_size[i] = i;
}
array.copyFrom(array_size);
// create blank memory for c
int[] C = new int[SIZE];
Allocation c = Allocation.createSized(rs, Element.I32(rs), C.length);
intaddscript.forEach_kernelAdd(array, c);
c.copyTo(C);
for (int i = 0; i < SIZE; i++) {
System.out.println("intadd3" + i + ": " + C[i]);
}
}