【发布时间】:2017-02-10 19:26:05
【问题描述】:
我试图为 Android 编写 MINPACK-Fortran 端口,但我无法理解在 JNI 中正确使用双数组。研究论坛上的问题,我意识到 JNI 在处理数组时需要大量无用的代码。我想错了,所以把代码带到这里。
在此示例中,调用了过程 hybrd1_(Fortran sub,由 MINPACK 提供)来计算非线性方程组。 用户(Java 端)提供类 aSolver,其中包含方法 solveStep。 solveStep 有两个参数(双精度数组),其中 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