我尝试为Android编写MINPACK-Fortran端口,但我无法理解在JNI中正确使用双数组。在论坛上研究这个问题,我意识到在使用数组时JNI需要大量无用的代码。我想错了,所以把代码带到这里。
在此示例中,调用过程 hybrd1 _ (Fortran sub,由MINPACK提供)来计算非线性方程组。 用户(Java端)提供类aSolver,其中包含方法 solveStep 。 solveStep 有两个参数(double的数组),其中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 子需要,但我不明白为什么需要它。请帮我写一个高效的包装。
答案 0 :(得分:0)
首先,根据定义,JNI代码确实很麻烦,因为您必须使用'env'来创建某些变量,并且您必须进行一些编码以获取Java代码所需的数据并与之通信它。 我真的不能说如何使你的代码更有效率,因为我真的不明白你想做什么。你可能有一些不必要的复制,我不确定。如果您查看代码并仍然认为可以改进它,请进一步解释一下您的目标是什么。 关于'ReleaseDoubleArrayElements'的问题 - NewDoubleArray仅创建本地引用,因此您不需要发布它。
答案 1 :(得分:0)
我想我找到了一个解决方案,如果有人遇到类似问题,我会把它放在这里。我将所有工作数组放在一个并从Java传递给C ++。然后在 rhs 内部,我计算向量x和f的内存偏移量,并将它们带入 solveStep (现在我只发送给Java偏移量)。 这是更改的代码:
C ++方
b
Java方
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;
}