【问题标题】:Best practice to use double arrays使用双数组的最佳实践
【发布时间】:2017-02-10 19:26:05
【问题描述】:

我试图为 Android 编写 MINPACK-Fortran 端口,但我无法理解在 JNI 中正确使用双数组。研究论坛上的问题,我意识到 JNI 在处理数组时需要大量无用的代码。我想错了,所以把代码带到这里。

在此示例中,调用了过程 hybrd1_(Fortran sub,由 MINPACK 提供)来计算非线性方程组。 用户(Java 端)提供类 aSolver,其中包含方法 solveStepsolveStep 有两个参数(双精度数组),其中 x - 参数向量; f - NLES 中右侧的向量。问题是我不知道 hybrd1_ 过程将哪些向量传输到 solveStep 中,而迭代仍在继续。但我知道,这些向量可以在 x、fvec 或 wa(aSolver 对象内的字段)中找到。 hybrd1_ 调用 rhs 过程 (cdecl) - 我需要做很多可怕的事情来将两个数组传输到 Java,然后将它们带回 c++。正如您所见,rhs 过程调用非常频繁,这些 JNI 转换正在破坏整个过程。

C++ 方面

//struct which allow me to talk with Java from rhs procedure
struct fake_n
{
    int n;
    jobject aSolver;
    jmethodID meth;
    JNIEnv* env;
};

//iterator. the place where i need help
void rhs(int * n, double * x, double * f, int * flag)
{
    fake_n* f_n = (fake_n *) n;
    // creating array parametres 
    jdoubleArray x_row = ((*f_n).env)->NewDoubleArray((*f_n).n);
    jdoubleArray f_row = ((*f_n).env)->NewDoubleArray((*f_n).n);
    // filling array parametres 
    ((*f_n).env)->SetDoubleArrayRegion(x_row, 0, (*f_n).n, x);
    // launch object method
    (*f_n).env->CallVoidMethod((*f_n).aSolver, (*f_n).meth, x_row, f_row);
    // copy result back to c++ - why do i need? 
    jdouble *res = ((*f_n).env)->GetDoubleArrayElements(f_row, NULL);
    for (int i =0; i< (*f_n).n; i++) { f[i] = res[i]; }        
}

JNIEXPORT void JNICALL Java_com_example_myapplication_MainActivity_hybrd_1NLES(JNIEnv* env,
                                                            jclass clazz,
                                                            jobject aSolver)
{
    jclass cls = env->GetObjectClass(aSolver);
    jmethodID solvestep = env->GetMethodID(cls, "solveStep", "([D[D)V");
    if (solvestep == 0) {
        return;
    }

    jfieldID afields[4];
    char* atype[4] = {(char *) "[I", (char *)"[D", (char *)"[D", (char *)"[D"};
    char* aname[4] = {(char *)"n", (char *)"x", (char *)"fvec", (char *)"wa"};
    jobject objs[4];

    for (int i = 0; i < 4; i++) {
        afields[i] = env->GetFieldID(cls, aname[i], atype[i]);
        objs[i] = env->GetObjectField(aSolver, afields[i]);
    }
    jintArray *na = reinterpret_cast<jintArray *>(&(objs[0]));
    jdoubleArray *xa = reinterpret_cast<jdoubleArray *>(&(objs[1]));
    jdoubleArray *fveca = reinterpret_cast<jdoubleArray *>(&(objs[2]));
    jdoubleArray *waa = reinterpret_cast<jdoubleArray *>(&(objs[3]));

    int *n = env->GetIntArrayElements(*na, NULL);
    double *x = env->GetDoubleArrayElements(*xa, NULL);
    double *fvec = env->GetDoubleArrayElements(*fveca, NULL);
    double *wa = env->GetDoubleArrayElements(*waa, NULL);

    fake_n f_n;
    f_n.n = *n;
    f_n.aSolver = aSolver;
    f_n.meth = solvestep;
    f_n.env = env;

    // tol, lwa, info - local variables
    // this procedure calls the iterator (rhs) a lot of times   
    hybrd1_(rhs, (int *)&f_n, x, fvec, &tol, &info, wa, &lwa);

    // should i ?
    env->ReleaseIntArrayElements(*na, n, 0);
    env->ReleaseDoubleArrayElements(*xa, x, 0);
    env->ReleaseDoubleArrayElements(*fveca, fvec, 0);
    env->ReleaseDoubleArrayElements(*waa, wa, 0);
}

这里是 JAVA 方面

public class aSolver {
    public double[] x;
    public double[] fvec;
    public double[] wa;
    public int[] n;
    public int rc;

    public aSolver() {
        n = new int[1];
        n[0] = 2;
        x = new double[n[0]];
        fvec = new double[n[0]];
        wa = new double[n[0] * (3 * n[0] + 13) / 2];
        x[0] = 0;
        x[1] = 1;
        rc = 0;
        solveStep(x, fvec);
    }

    // method, which calling from iterator (rhs)
    public void solveStep(double[] xv, double[] fv ) {
        // it is important: fv != fvec
        // it is important: xv != x
        fv[0] = 2.0 * xv[0] + 3.0 * xv[1] + 6.0;
        fv[1] = 5.0 * xv[0] - 3.0 * xv[1] - 27.0;
        rc++;
    }
}

//----
aSolver s = new aSolver();
hybrd_NLES(s);
//----

public native void hybrd_NLES(aSolver solver);

编写的代码看起来既繁琐又低效。此外,在ReleaseDoubleArrayElements 中有一种rhs 子需要的感觉,但我不明白为什么它需要在那里。请帮我写一个高效的包装器。

【问题讨论】:

    标签: java android c++ arrays android-ndk


    【解决方案1】:

    首先,从定义上看,JNI 代码确实很麻烦,因为您必须使用 'env' 来创建某些变量,并且您必须进行一些编码才能使用 Java 代码获取所需的数据并与它。 我真的不能说如何让你的代码更有效率,因为我真的不明白你想做什么。你可能有一些不必要的复制,我不确定。如果您审查了您的代码并且仍然认为您可以改进它,请进一步解释您的目标是什么。 关于您关于“ReleaseDoubleArrayElements”的问题 - NewDoubleArray 仅创建本地引用,因此您不需要释放它。

    【讨论】:

    • 感谢您对 ReleaseDoubleArrayElements 的解释。 “不必要的复制”怎么办?我在代码中添加了额外的 cmets
    • 在你上次编辑之前,看起来有一些不必要的操作,但现在看 - 看起来还可以。
    【解决方案2】:

    如果有人遇到类似问题,我想我找到了解决方案并将其放在这里。我将所有工作数组放在一个中,并将其从 Java 传递给 C++。然后在 rhs 中计算向量 x 和 f 的内存偏移量,并将它们带入 solveStep (现在我只向 Java 发送偏移量)。 这是更改的代码:

    C++ 方面

    struct fake_n
    {
        int n;
    
        jobject aSolver;
        jmethodID meth;
        JNIEnv* env;
        double* dbls;
    };
    
    void rhs(int * n, double * x, double * f, int * flag)
    {
        fake_n* f_n = (fake_n *) n;
        // calculate the offsets to x and f vectors
        jint x_row = (jint)(x - (*f_n).dbls);
        jint f_row = (jint)(f - (*f_n).dbls);
        // and call the method of aSolver object
        (*f_n).env->CallVoidMethod((*f_n).aSolver, (*f_n).meth, x_row, f_row);
    }
    JNIEXPORT jint JNICALL Java_com_example_myapplication_MainActivity_hybrd_1NLES(JNIEnv* env,
                                                                jclass clazz,
                                                                jobject aSolver)
    {
        jclass cls = env->GetObjectClass(aSolver);
        jmethodID solvestep = env->GetMethodID(cls, "solveStep", "(II)V");
        if (solvestep == 0) {
            return -1;
        }
        int info = 0;
    
        //JNIlist - helper class to connect to JNI and to hold the variables data
        //nothing unusual - jast a couple of simple wrappers
        //original code is shown above
        JNIlist * lJNI = new JNIlist(env, aSolver, cls);
        lJNI->add(JNI_DT_INT,          (char *)"n");
        lJNI->add(JNI_DT_DOUBLE_ARRAY, (char *)"dbls");
        lJNI->connectJNI();
    
        int n = *((int*)lJNI->data(0));
        int lwa = n * (3 * n + 13) / 2 + 1;
        double tol = rel_TOL;
    
        fake_n f_n;
        f_n.n = n;
        f_n.aSolver = aSolver;
        f_n.meth = solvestep;
        f_n.env = env;
        f_n.dbls = (double*)(lJNI->data(1)); //pointer to the REAL memory section
        // with the array of doubles "dbl" 
        //cause GetDoubleArrayElements with the iCopy parameter which equals JNI_FALSE
        // gives access to the real memory section
    
        hybrd1_(rhs, (int *)&f_n, f_n.dbls, f_n.dbls + n, &tol, &info, f_n.dbls + 2*n, &lwa);
    
        delete lJNI;
    
        return info;
    }
    

    Java 端

    public class aSolver {
        public double[] dbls;
        public int lwa;
        public int n;
        public int info;
        public int rc;
        private int xoffset, foffset;
    
        public aSolver() {
            n = 2;
            lwa = (n*(3 * n + 13)/2 + 1);
            dbls = new double[2 * n + lwa];
            info = 0;
            dbls[0] = 0;
            dbls[1] = 1;
            rc = 0;
            solveStep(0, 1);
        }
    
        public void setOffsets(int xv, int fv ) {
            xoffset = xv;
            foffset = fv;
        }
    
        public void setf(int p, double val){
            dbls[foffset + p] = val;
        }
    
        public double x(int p){
            return dbls[xoffset + p];
        }
    
        public void solveStep(int xv, int fv ) {
            setOffsets(xv, fv);
            setf(0, 2.0 * x(0) + 3.0 * x(1) + 6.0);
            setf(1, 5.0 * x(0) - 3.0 * x(1) - 27.0);
            rc++;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 2018-06-25
      • 1970-01-01
      • 2012-02-25
      • 2010-11-06
      • 2012-12-31
      • 1970-01-01
      相关资源
      最近更新 更多