开发者

How to populate values in a 2d array in JNI and return it as an object array?

开发者 https://www.devze.com 2023-01-30 19:11 出处:网络
I am trying to perform matrix multiplication which involves two dimensional double arrays. I followed an article on the SUN website but still couldn\'t get it right.Here\'s a brief description of my p

I am trying to perform matrix multiplication which involves two dimensional double arrays. I followed an article on the SUN website but still couldn't get it right. Here's a brief description of my problem and the way I handled it:

I've two 2-dimensional double arrays which I am passing to JNI from my java function. I passed these arrays as JObectArray. In my JNI function, I created two local arrays of type double and copied the JObjectArrays in these local arrays. I then performed matrix multiplication and stored the result in another local array called result. Now I want to return this result array back to Java function but I don't know how to do this.

Can someone please help me out with my query?

Here's my code:

JNIEXPORT jobjectArray JNICALL Java_prj_anyapp_JNIForMFCC_compute(JNIEnv *env, jclass jClass, jint Am, jint Bm, jint Bn, jobjectArray A, jobjectArray B)
{

    jobjectArray retC;
    int i,j,k;
    double sum;
    int localAm, localBm, localBn;
    localAm = Am;
    localBm = Bm;
    localBn = Bn;
    double localArrayCopyB[localBm][localBn];       
    开发者_如何学Cdouble localArrayCopyA[localAm][localBm];
    double localArrayCopyC[localAm][localBn];

    for(i=0; i<localAm; i++) {
        jdoubleArray oneDimA= 
        (jdoubleArray)(*env)->GetObjectArrayElement(env,A, i);

        jint *elementA = (*env)->GetIntArrayElements(env,oneDimA, 0);
        for(j=0; j<localBm; j++) {

            localArrayCopyA[i][j]= elementA[j];
        }


    for(i=0; i<localBm; i++) {
        jdoubleArray oneDimB= 
        (jdoubleArray)(*env)->GetObjectArrayElement(env,B, i);

        jint *elementB = (*env)->GetIntArrayElements(env,oneDimB, 0);
        for(j=0; j<localBn; j++) {

            localArrayCopyB[i][j]= elementB[j];
        }

    }

        for(i=0; i<localAm; i++){
            for(j=0; j<localBm; j++){
                for(k=0; k<localBn; k++){
                    sum += localArrayCopyA[i][k] * localArrayCopyB[k][j];
                }
                localArrayCopyC[i][j] = sum;
            }
        }
    }       
}
0

精彩评论

暂无评论...
验证码 换一张
取 消