GetDirectBufferAddress returns 0x0

Hi,

The function env->GetDirectBufferAddress always returns 0x0 even when the buffer.isDirect() returns true. This leads to the problem of cudaFreeHost() not being able to free the allocated page-locked memory.

As a workaround, I modified the function Java_jcuda_runtime_JCuda_cudaFreeHostNative in JCuda-All-0.4.1-src/JCudaRuntimeJNI/src/JCudaRuntime.cpp . The line
void *nativePtr = env->GetDirectBufferAddress(ptr);
was changed into
void *nativePtr = getPointer(env, ptr);

Does anybody observe the same problem? Here is a program which does nothing but allocate then free 1GB of pinned memory 100 times.

import jcuda.Sizeof;
import jcuda.runtime.JCuda;

public class PinnedMemTest {
  public static void main(String args[]) {
    for (int k = 0; k < 100; k++) {
      int n = 1<<28;
      Pointer host = new Pointer();
      JCuda.cudaHostAlloc(host, n*Sizeof.FLOAT, JCuda.cudaHostAllocDefault);
      JCuda.cudaFreeHost(host);
    }
  }
}

I run it using Centos 5.6 amd64, Java 1.7.0_03, and Cuda toolkit 4.1.

Hello

You are right, that’s not the correct method. In fact, it would be the right method, but should not be applied to the Pointer, but to the Buffer that it points to. However, the address that is stored in the Pointer is the same as the address of the direct buffer, so the fix that you proposed should be right.

I’ll test and verify this, and make sure it is fixed in the next release.

Thanks for pointing this out!

bye
Marco