[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Oct files: How to get read-only pointers to data for arguments of un
From: |
Stefan |
Subject: |
Re: Oct files: How to get read-only pointers to data for arguments of unknown type? |
Date: |
Tue, 28 Feb 2012 05:52:44 -0800 (PST) |
Hello,
I figured out how to get a pointer to the data in an octave_value object,
regardless of the type, without having octave make a copy:
void *octGetData(const octave_value &in)
{
if (in.is_complex_type() && !in.is_scalar_type()) {
// handle complex data types separately, but only if not scalar!
if (in.is_double_type()) {
const ComplexNDArray t = in.complex_array_value();
return (void*) t.data();
} else if (in.is_single_type()) {
const FloatComplexNDArray t = in.float_complex_array_value();
return (void*) t.data();
} else {
error("Data type not implemented.");
return NULL;
}
} else {
// handle bulk of data types with mex_get_data()
return in.mex_get_data();
}
}
So there is an octave_value method mex_get_data() that emulates Matlab's
mxGetData() function, which handles most cases. For complex data types,
mex_get_data() appears to make a copy of the data to emulate Matlab's
behavior to return only the real part. This isn't what I wanted, since my
library expects complex inputs in octave's storage order. Hence the
data-type specific solution for complex data, which - in contrast to my
thinking in my previous post - does *NOT* make a copy, UNLESS you happen to
have a scalar type (is_scalar_type() is true).
A single complex number was my test case for my previous post, which failed
so badly. Fortunately, mex_get_data() works for complex scalars, since the
data doesn't have to be rearranged in this case.
I believe the above function does not interfere with octave's reference
counting, so you can use it to pass arguments to an oct file by reference:
DEFUN_DLD(oct_test, args, nargout, "Test")
{
void *data = octGetData(args(0));
*(Complex*)data = Complex(7.,8.);
return octave_value();
}
In octave:
octave:1> x = 1+i
x = 1 + 1i
octave:2> oct_test(x)
octave:3> x
x = 7 + 8i
Great, if the data in x fills up half of your computers memory and your
point of writing an oct file in the first place was to have access to a fast
C library that modifies your data in-place.
Thanks everybody for your time,
Stefan
--
View this message in context:
http://octave.1599824.n4.nabble.com/Oct-files-How-to-get-read-only-pointers-to-data-for-arguments-of-unknown-type-tp4425650p4428192.html
Sent from the Octave - General mailing list archive at Nabble.com.