使用双数组的最佳实践

Best practice to use double arrays

本文关键字:最佳 数组      更新时间:2023-10-16

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

在本例中,调用过程hybrd1_(Fortran sub,由MINPACK提供)来计算非线性方程组。用户(Java端)提供类aSolver,该类包含方法solveStepsolveStep有两个参数(双数组),其中x-参数的向量;f-NLES中右侧的向量。问题是,当迭代继续时,我不知道hybrd1_过程将哪些向量传输到solveStep。但我知道,这些向量可以在x、fvec或wa(解算器对象内部的字段)中找到。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

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

我想我找到了一个解决方案,如果有人有类似的问题,我会把它放在这里。我将所有工作数组放在一个数组中,并将其从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++;
}
}