1

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]);
    }
}
Community
  • 1
  • 1
Benjie
  • 74
  • 5

2 Answers2

1

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

Aladin Q
  • 550
  • 5
  • 12
  • do you mind telling me how you define the scope and link it with java code. – Benjie Feb 04 '15 at 18:30
  • Above the function, you would need to have something like: uint8_t * A; uint8_t * B; uint8_t * C; They will need to be allocated on the java side (see http://developer.android.com/guide/topics/renderscript/compute.html#using-rs-from-java) with Allocation; but you could also allocate them in C via static int A[10]; (however static array will never be reachable from java) . The initialization of the arrays can be done in C, or in java via copyFrom or via invoking a renderscript function for this purpose. You can retrieve the content from the renderscript side to java with copyTo. – Aladin Q Feb 04 '15 at 19:35
  • How should I invoke this function? Will it be using forEach_kerneladd() or running the function under a for loop – Benjie Feb 04 '15 at 19:51
  • Yes, with forEach_kernelAdd(...). You will still need to pass an array of a size 10 based on your example, just to indicate to renderscript the number of elements to process. It's also possible to read each element value of this array from RS, but the prototype of the kernel function I gave above will not be exactly the same. – Aladin Q Feb 04 '15 at 20:03
  • While this will work, you would get MUCH better performance putting your array data into a RenderScript Allocation and using the kernel input & output rather than index the arrays yourself. – R. Jason Sams Feb 05 '15 at 01:14
  • Do you mind elaborating on how I can go about doing it? – Benjie Feb 08 '15 at 15:31
0

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

Amitsharma
  • 1,577
  • 1
  • 17
  • 29