Reputation: 74
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]);
}
}
Upvotes: 1
Views: 170
Reputation: 550
Assuming you have already allocated your arrays and they are available in the scope:
void __attribute__((kernel)) kernelAdd(uint32_t x) {
C[x] = A[x] + B[x];
}
Take a look at http://developer.android.com/guide/topics/renderscript/compute.html#writing-an-rs-kernel
Upvotes: 1
Reputation: 1576
It May Help You
int[] a = { 1, 2 };
int[] b = { 3, 4 };
int[] l;
for (int i = 0; i < a.length; i++) {
int c=i;
for(int j=0;j<=b.length;j++)
{
int d=j;
int e=c*d;
// Make List Array here To insert e
myList.add(e);
}
}
System.out.println(myList);
This is not Perfect Solution but HelpFull
Upvotes: 0