0

I need to perform matrix operations in Android, so I searched about the RenderScript, and I got useful information here

I tried the program in the answer above like this:

private void compute(){
    mRs = RenderScript.create(this);

    Type.Builder builder = new Type.Builder(mRs, Element.U8(mRs));
    Type a_type = builder.setX(3).setY(2).create();
    Type b_type = builder.setX(3).setY(2).create();
    Type c_type = builder.setX(2).setY(2).create();
    Allocation A = Allocation.createTyped(mRs, a_type);
    Allocation B = Allocation.createTyped(mRs, b_type);
    Allocation C = Allocation.createTyped(mRs, c_type);

    A.copyFrom(new byte[]{1, 2, 3, 1, 2, 3});
    B.copyFrom(new byte[]{1, 1, 1, 0, 1, 0});

    ScriptIntrinsicBLAS BLAS =  ScriptIntrinsicBLAS.create(mRs);
    BLAS.BNNM(A, 0, B, 0, C, 0, 1);

    byte[] result = new byte[]{1,2,3,4};
    C.copyTo(result);

    for(int i = 0; i < result.length; ++i){
        Log.i(TAG, i + " " + result[i]);
    }
}

My gradle file is like this following:

targetSdkVersion 25
renderscriptTargetApi 25
renderscriptSupportModeEnabled true
renderscriptSupportModeBlasEnabled true

But I got the wrong results that all items in the matrix C is zero:

08-25 16:31:05.384 30771-30771/cn.jy.testsiblas I/tag: 0 0
08-25 16:31:05.384 30771-30771/cn.jy.testsiblas I/tag: 1 0
08-25 16:31:05.384 30771-30771/cn.jy.testsiblas I/tag: 2 0
08-25 16:31:05.384 30771-30771/cn.jy.testsiblas I/tag: 3 0

Someone knows how to solve this?

Besides, I have just found there seems to be limitation in the dimensions of matrix that can be handled with ScriptIntrinsicBLAS? here is a question about this Anyone know about this limitation? If limitation is real, I'm afraid I have to find out another way to handle matrix operation on Android.

Yuan
  • 1
  • 1
  • You cannot multiply 3x2 with another 3x2, 2nd matrix needs to be 2x3. – sakridge Aug 26 '17 at 02:04
  • @sakridge Thank you for your answer as well. But the function BNNM performs matrix operation like this : C = A * Transpose(B) ,[API document here](https://developer.android.com/reference/android/renderscript/ScriptIntrinsicBLAS.html). so the dimensions of each matrix is correct. Otherwise, an exception will be thrown if the operation can't be done. – Yuan Aug 28 '17 at 00:50

1 Answers1

0

It's because of this from the docs: Calculations are done in 1.10.21 fixed-point format for the final output, just before there's a shift down to drop the fractional parts.

So any calculated value which is less than 2^21 (~2million) will be shifted out of the answer.

If you want to do calculations on numbers in this range you need to shift your values into the high bits of the input.

sakridge
  • 578
  • 3
  • 9
  • Thank you for your generous help! I have solved my problem under your direction! The BNNM will shift the number 21 bits before output, we can correct it if I shift it back like this. `BLAS.BNNM(A, 0, B, 0, C, 0, 1 << 21); ` Finally I got the right result. – Yuan Aug 31 '17 at 02:21