使用RenderScript Intrinsic BLAS在Android中执行矩阵操作,但是我得到了错误的结果?

问题描述:

我需要在Android的进行矩阵运算,所以我搜索有关的renderScript,和我useful information here使用RenderScript Intrinsic BLAS在Android中执行矩阵操作,但是我得到了错误的结果?

我在上面这样的答案试图程序:

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]); 
    } 
} 

我gradle这个文件是这样的以下:

targetSdkVersion 25 
renderscriptTargetApi 25 
renderscriptSupportModeEnabled true 
renderscriptSupportModeBlasEnabled true 

但我得到错误的结果,在矩阵C的所有项目为零:

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 

有人知道如何解决这个问题吗?

此外,我刚刚发现,可以用ScriptIntrinsicBLAS处理的矩阵的维度似乎有限制? here is a question about this 任何人都知道这个限制?如果限制是真实的,恐怕我必须找出另一种方法来处理Android上的矩阵操作。

+0

你不能乘3x2与另一个3x2,第二矩阵需要2x3。 – sakridge

+0

@sakridge谢谢你的回答。但BNNM函数执行如下矩阵操作:C = A *移调(B),[API文档在这里](https://developer.android.com/reference/android/renderscript/ScriptIntrinsicBLAS.html)。所以每个矩阵的尺寸都是正确的。否则,如果操作无法完成,则会抛出异常。 – Yuan

这是因为这从文档: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.

因此,任何计算值小于2^21(200万〜)将被移出的答案。

如果您想对此范围内的数字进行计算,则需要将您的值转换为输入的高位。

+0

谢谢你的慷慨帮助!我在你的指导下解决了我的问题! BNNM将在输出之前将21位移位,如果我将其移回,我们可以纠正它。 'BLAS.BNNM(A,0,B,0,C,0,1 Yuan