Calculate the sum of values ​​in an array using renderscript

Hi, I am new and trying to execute code in Renderscript . I would like to know how I can execute the sum of elements in an array using a render script. Is there a way to pass the output back to the script for sequential addition? my problem is this: Vector sum

Description: calculate the sum of the values ​​in an array.

Input: an array of integers

Output: Integer

Any help would be greatly appreciated!

+1
source share
3 answers

I'm afraid this is a little more complicated than it sounds, but I will do my best to explain here a possible route that you can take to implement this.

, , , , , - , "" . - . CUDA OpenCL , , , , Google CUDA reduction, .

, , , , , . , , . , :

Parallel reduction

, , 16- . , 8- , 8 .

, 4 - . ...

, - .

RenderScript :

Java:

int[] ints; // Your data is held here.

Allocation data = Allocation.createSized(rs, Element.I32(rs), ints.length, Allocation.USAGE_SCRIPT);
data.copy1DRangeFrom(0, ints.length, ints);

ScriptC_Reduce script = new ScriptC_Reduce(rs);
script.bind_data(data);

for (int stride = ints.length / 2; stride > 0; stride /= 2) {
    script.set_stride(stride);
    script.forEach_root(input, output);
}

data.copyTo(ints);
int totalsum = ints[0];

Renderscript:

#pragma version(1)
#pragma rs java_package_name(...[your package here]...)

int stride;
int * data;

void root(const int32_t *v_in, int32_t *v_out, uint32_t x) {
    if (x < stride) data[x] += data[x + stride];
}

RS, :

  • , "v_in" "v_out" RS , , , . , " " int, Java , , ​​ .
  • Java, , . , previuos , " [x + ]" . RS , , , ​​ . , __syncthreads() CUDA, .

, . . , , , , , ints.length , .

, , 2, . 0-pad . , , 0-padding .

, , , , 64 . , , "" 64 . , ( ), 64 . , 64 - , . 2, , , 16 32. , .

EDIT:. , RenderScript , , . , , , , .

+4

. - , 1 . . , 4 .

RenderScript: Entries:1 Total: 3 Time: 0.067ms
Simple Loop : Entries:1 Total: 3 Time: 0.001ms
RenderScript: Entries:2 Total: 97 Time: 0.614ms
Simple Loop : Entries:2 Total: 97 Time: 0.001ms
RenderScript: Entries:4 Total: 227 Time: 0.28ms
Simple Loop : Entries:4 Total: 227 Time: 0.002ms
RenderScript: Entries:8 Total: 320 Time: 0.445ms
Simple Loop : Entries:8 Total: 320 Time: 0.002ms
RenderScript: Entries:16 Total: 700 Time: 0.486ms
Simple Loop : Entries:16 Total: 700 Time: 0.002ms
RenderScript: Entries:32 Total: 1807 Time: 0.595ms
Simple Loop : Entries:32 Total: 1807 Time: 0.002ms
RenderScript: Entries:64 Total: 3218 Time: 0.624ms
Simple Loop : Entries:64 Total: 3218 Time: 0.002ms
RenderScript: Entries:128 Total: 6230 Time: 0.737ms
Simple Loop : Entries:128 Total: 6230 Time: 0.003ms
RenderScript: Entries:256 Total: 12968 Time: 0.769ms
Simple Loop : Entries:256 Total: 12968 Time: 0.005ms
RenderScript: Entries:512 Total: 26253 Time: 0.895ms
Simple Loop : Entries:512 Total: 26253 Time: 0.01ms
RenderScript: Entries:1024 Total: 52345 Time: 0.987001ms
Simple Loop : Entries:1024 Total: 52345 Time: 0.017ms
RenderScript: Entries:2048 Total: 100223 Time: 1.715ms
Simple Loop : Entries:2048 Total: 100223 Time: 0.034ms
RenderScript: Entries:4096 Total: 200375 Time: 1.213ms
Simple Loop : Entries:4096 Total: 200375 Time: 0.065ms
RenderScript: Entries:8192 Total: 403713 Time: 1.196ms
Simple Loop : Entries:8192 Total: 403713 Time: 0.163001ms
RenderScript: Entries:16384 Total: 812411 Time: 1.929ms
Simple Loop : Entries:16384 Total: 812411 Time: 0.41ms
RenderScript: Entries:32768 Total: 1620542 Time: 1.822ms
Simple Loop : Entries:32768 Total: 1620542 Time: 0.617ms
RenderScript: Entries:65536 Total: 3250733 Time: 5.955ms
Simple Loop : Entries:65536 Total: 3250733 Time: 1.384ms
RenderScript: Entries:131072 Total: 6478866 Time: 2.622ms
Simple Loop : Entries:131072 Total: 6478866 Time: 2.008ms
RenderScript: Entries:262144 Total: 12980832 Time: 3.979999ms
Simple Loop : Entries:262144 Total: 12980832 Time: 4.377001ms
RenderScript: Entries:524288 Total: 25956676 Time: 10.163ms
Simple Loop : Entries:524288 Total: 25956676 Time: 8.326ms
RenderScript: Entries:1048576 Total: 51897168 Time: 12.723001ms
Simple Loop : Entries:1048576 Total: 51897168 Time: 15.871999ms
RenderScript: Entries:2097152 Total: 103867356 Time: 32.229001ms
Simple Loop : Entries:2097152 Total: 103867356 Time: 31.367ms
RenderScript: Entries:4194304 Total: 207646704 Time: 61.628999ms
Simple Loop : Entries:4194304 Total: 207646704 Time: 63.378ms
RenderScript: Entries:8388608 Total: 415058480 Time: 103.734999ms
Simple Loop : Entries:8388608 Total: 415058480 Time: 140.088ms

renderscript. , ( rs.finish(), , renderscript ).

#pragma version(1)
#pragma rs java_package_name(com.photoembroidery.tat.olsennoise)

int stride;
int * data;

void root(const int32_t *v_in, int32_t *v_out, uint32_t x) {
    data[x] += data[x + stride];
}

. , 2. , , , , , . .

//int[] array = //array of your data//;

        ScriptC_reduce script = new ScriptC_reduce(mRS);
        Allocation data = Allocation.createSized(mRS, Element.I32(mRS), array.length, Allocation.USAGE_SCRIPT);
        data.copy1DRangeFrom(0, array.length, array);
        script.bind_data(data);

        int smallest2ExpBiggerThanLength = 1;
        for (int length = arraysize; length != 0; length >>= 1,smallest2ExpBiggerThanLength <<= 1);

        int end = smallest2ExpBiggerThanLength / 2;
        int start = smallest2ExpBiggerThanLength - arraysize;
        if (start == end) {
            start = 0;
            end = end/2;
        }

        while (end > 0) {
            launchOptions.setX(start, end);
            script.set_stride(end - start);
            script.forEach_root(data, data, launchOptions);
            script.forEach_root(data,data);
            end = end >> 1;
            start = 0;
        }
        data.copyTo(array);
        int total = array[0];

- . get-go, . 4x. , renderscript. - - , 1 , .

+3

"" Android. (, ) . AOSP, /. ​​ 2 → 1, .

, invokable rsGetElementAt _ *() . , Java, ( ).

+2

All Articles