Dear support,
I am currently sitting with a performance problem reading data from an HDF. I
have currently
Over 2mil cells in one file, but it takes about 5 seconds to read the
information. Is there a way
Where reading data from C# HDF.PInvoke can be optimized ?
I have attached my Code
Regards
Johan Ferguson
using System;
using HDF.PInvoke;
using hid_t = System.Int64;
using hsize_t = System.UInt64;
using hssize_t = System.Int64;
using System.Runtime.InteropServices;
namespace HDFPInvoke
{
class Program
{
/// <summary>
///
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
string m_v0_test_file_name = "C:\\UMLINDI\\rr16.h5";
try
{
Open2(ref m_v0_test_file_name);
}
catch(Exception exc)
{
Console.WriteLine(exc.Message);
Console.ReadLine();
}
}
/// <summary>
///
/// </summary>
/// <param name="file"></param>
private static void Open(ref string file)
{
try
{
//Open HDF
hid_t test = H5F.open(file, H5F.ACC_RDONLY);
//Open Dataset to read from
hid_t dset = H5D.open(test, "rr1601");
//Get the type of the dataset
hid_t type = H5D.get_type(dset);
//Get ref to path
hid_t space = H5D.get_space(dset);
//copy an instance of the type
hid_t mem_type = H5T.copy(type);
//get the length of the dataset
hssize_t len = H5S.get_simple_extent_npoints(space);
hsize_t[] tdims = new hsize_t[len];
//create pointer array
IntPtr[] rdata = new IntPtr[len];
GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);
//read pointer values into the array
H5D.read(dset, mem_type, H5S.ALL, H5S.ALL, H5P.DEFAULT,
hnd.AddrOfPinnedObject());
//loop through pointers
for (int i = 0; i < rdata.Length; ++i)
{
int leni = 0;
while (Marshal.ReadByte(rdata[i], leni) != 0) { ++leni; }
}
Console.WriteLine(dset.ToString());
Console.ReadLine();
}
catch(Exception exc)
{
Console.WriteLine(exc);
Console.ReadLine();
}
}
/// <summary>
///
/// </summary>
/// <param name="file"></param>
private static void Open2(ref string file)
{
try
{
DateTime start = DateTime.UtcNow;
hid_t sfile = H5F.open(file, H5F.ACC_RDONLY);
hid_t dset = H5D.open(sfile, "rr1601");
// this is a handle to the in-file datatype
hid_t ftype = H5D.get_type(dset);
// this is a handle to the in-memory (native) datatype
hid_t mtype = H5T.get_native_type(ftype,
H5T.direction_t.ASCEND);
hid_t space = H5D.get_space(dset);
hssize_t len = H5S.get_simple_extent_npoints(space);
// "rr1601" is a 2D dataset in the file, but we read it as a 1D
buffer
float[] rdata = new float[len];
GCHandle hnd = GCHandle.Alloc(rdata, GCHandleType.Pinned);
H5D.read(dset, mtype, H5S.ALL, H5S.ALL, H5P.DEFAULT,
hnd.AddrOfPinnedObject());
hnd.Free();
// Do something with RDATA...
for (int i = 0; i < rdata.Length; ++i)
{
int leni = 0;
while (Marshal.ReadByte(rdata[i], leni) != 0)
{
++leni;
}
}
// Don't forget to close the open handles!
H5S.close(space);
H5T.close(mtype);
H5T.close(ftype);
H5D.close(dset);
H5F.close(sfile);
DateTime end = DateTime.UtcNow;
var seconds = System.Math.Abs((end - start).TotalSeconds);
Console.WriteLine(seconds.ToString("0.000") + " : seconds");
Console.ReadLine();
}
catch (Exception exc)
{
}
}
}
}_______________________________________________
Hdf-forum is for HDF software users discussion.
[email protected]
http://lists.hdfgroup.org/mailman/listinfo/hdf-forum_lists.hdfgroup.org
Twitter: https://twitter.com/hdf5