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

时间:2017-08-25 08:49:32

标签: android matrix blas renderscript

我需要在Android中执行矩阵运算,所以我搜索了RenderScript,得到了useful information here

我在上面的答案中尝试了这个程序:

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上的矩阵操作。

1 个答案:

答案 0 :(得分:0)

这是因为来自文档: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(~2百万)的计算值都将被移出答案。

如果要对此范围内的数字进行计算,则需要将值移到输入的高位。