Hi!

On Mo, 2010-04-12 at 15:39 -0500, Peter Cao wrote: 
> >> wrapping the native shared libraries. And it is *not* hdf-java as this
> >> does not support the H5PT but using JNA for that purpose.
> >>
> >> Still interested?
> >>     
> >
> >
> > ...
> >
> >   
> 
> It will be very helpful to see the difference of JNA and JNI. The 
> current hdf-java JNI was carefully
> designed so that the memory allocated in Java is directly used in the 
> JNI C. The overhead between
> Java and C is very small, i.e. the I/O from JNI calls is close to C.
> 
> A good way to test is to read and write a simple 1-D array, e.g. 
> float[10*1024*1024] in JNA and
> in C (directly use the HDF5 library) and see the results.

JNA with using there "direct mapping" (as I do) intends to be nearly as
fast as JNI (in the background it seems to do just the same. But it has
to do some initial additional shared library analysis).

Indeed, a JNI comparison would be interesting. Maybe I go for this
later, currently this is not my main target ...


FYI find attached my JNA java sources. Put the jna.jar to the classpath,
set the system property jna.library.path to a directory containing the
hdf libs in the same architecture as your java vm (32/64 bit) and write
some java code to access hdf files - that's it.
You have then exactly the same api as when writing plain C stuff with -
as far as i understand the jna - sharing the mem between the native and
the java part, too, as you told for hdf-java.

One of my major problems with this JNA approach remain the hdf constants
as they are not available in the hdf native libs as symbols, only as
#define's. My current workaround is to use - only for this purpose - the
hdf-java's HDF5Constants (implicating to additionally put the hdf native
libs to the java.library.path and the hdf-java jar's to the classpath).
But I will contact the helpdesk to simply add the constants additionally
as symbols to the native libs. And in the meanwhile I will do this
myself with patching and compiling the sources.
Btw the hdf-java suffers the same and has written some JNI code for this
purpose.


Best regards,
Johannes Stamminger
package net.eads.space.pcktevalwb.shared.hdf_jna;

import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;


/**
 * @author <a href="mailto:[email protected]">Johannes Stamminger</a>
 * @version : #5 $
 */
public class Hdf5 {

    static {
        Native.register("hdf5");
    }


    /**
     * Creates an HDF5 file.
     */
    public static native int H5Fcreate(final String name, final int flags, final int fcpl_id, final int fapl_id);

    public static native int H5Fclose(final int file_id);


    public static native int H5Tcreate(final int clazz, final int size);

    public static native int H5Tcopy(final int dtype_id);

    public static native int H5Tset_size(final int dtype_id, final int size);

    public static native int H5Tvlen_create(final int base_type_id);

    /**
     * hid_t H5Tarray_create2( hid_t base_type_id, unsigned rank, const hsize_t dims[&lt;rank&gt;], )
     */
    public static native int H5Tarray_create2(final int base_type_id, final int rank, final long ... dims);

    /**
     * H5_DLL herr_t H5Tset_tag(hid_t type, const char *tag)
     */
    public static native int H5Tset_tag(final int type, final String tag);



    /**
     * hid_t H5Screate_simple(int rank, const hsize_t * dims, const hsize_t * maxdims  )
     */
    public static native int H5Screate_simple(final int rank, final long[] dims, final long ... maxdims);

    /**
     * herr_t H5Sclose(hid_t space_id  )
     */
    public static native int H5Sclose(final int space_id);



    /**
     * hid_t H5Dcreate2( hid_t loc_id, const char *name, hid_t dtype_id, hid_t space_id, hid_t lcpl_id, hid_t dcpl_id, hid_t dapl_id  )
     */
    public static native int H5Dcreate2(final int loc_id,
                                        final String name,
                                        final int type_id,
                                        final int space_id,
                                        final int lcpl_id,
                                        final int dcpl_id,
                                        final int dapl_id);

    /**
     * herr_t H5Dwrite(hid_t dataset_id, hid_t mem_type_id, hid_t mem_space_id, hid_t file_space_id, hid_t xfer_plist_id, const void * buf  )
     */
    public static native int H5Dwrite(final int dataset_id,
                                      final int mem_type_id,
                                      final int mem_space_id,
                                      final int file_space_id,
                                      final int xfer_plist_id,
                                      final int ... buf);

    /**
     * See {...@link #H5Dwrite(int, int, int, int, int, int...)}.
     */
    public static native int H5Dwrite(final int dataset_id,
                                      final int mem_type_id,
                                      final int mem_space_id,
                                      final int file_space_id,
                                      final int xfer_plist_id,
                                      final Pointer buf);

    /**
     * herr_t H5Dvlen_reclaim(hid_t type_id, hid_t space_id, hid_t plist_id, void *buf  )
     */
    public static native int H5Dvlen_reclaim(final int type_id, final int space_id, final int plist_id, Pointer buf);

    /**
     * herr_t H5Dclose(hid_t dataset_id  )
     */
    public static native int H5Dclose(final int dataset_id);



    /**
     * hid_t H5Pcreate(hid_t cls_id  )
     */
    public static native int H5Pcreate(final int cls_id);

    /**
     * herr_t H5Pset_chunk(hid_t plist, int ndims, const hsize_t * dim  )
     */
    public static native int H5Pset_chunk(final int plist, final int ndims, final long ... dim);


    /**
     * typedef struct {
     *      size_t len; Length of VL data (in base type units)
     *      void *p;    Pointer to VL data
     * } hvl_t
     */
    public static class hvl_t extends Structure {
        public int len;
        public Pointer p;

        public hvl_t() { }

        public hvl_t(final int len, final byte ... blob) {
            this.len = len;
//            p = new Blob(blob);
        }

        public static class ByValue extends hvl_t implements Structure.ByValue {

            public ByValue() {
                super();
            }

            public ByValue(final int len, final byte... blob) {
                super(len, blob);
            }
        }
    }
}
package net.eads.space.pcktevalwb.shared.hdf_jna;

import com.sun.jna.Native;
import com.sun.jna.Pointer;


/**
 * @author <a href="mailto:[email protected]">Johannes Stamminger</a>
 * @version : #5 $
 */
public class Hdf5Hl extends Hdf5 {

    static {
        Native.register("hdf5_hl");
    }


    /**
     * hid_t H5PTcreate_fl( hid_t loc_id,
     *                      const char * dset_name,
     *                      hid_t dtype_id,
     *                      hsize_t chunk_size,
     *                      int compression )
     */
    public static native int H5PTcreate_fl(final int loc_id,
                                           final String dset_name,
                                           final int dtype_id,
                                           final long chunk_size,
                                           final int compression);

    /**
     * herr_t H5PTappend( hid_t table_id, hsize_t nrecords, const void* data)
     */
    public static native int H5PTappend(final int table_id, final int nrecords, final Pointer data);

    /**
     * hid_t H5PTopen( hid_t loc_id, const char *dset_name )
     */
    public static native int H5PTopen(final int loc_id, final String dset_name);

    /**
     * herr_t H5PTclose( hid_t table_id )
     */
    public static native int H5PTclose(final int table_id);

}

Attachment: signature.asc
Description: This is a digitally signed message part

_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://mail.hdfgroup.org/mailman/listinfo/hdf-forum_hdfgroup.org

Reply via email to