Discussion:
[Pdl-porters] truncating piddles in PP code
Diab Jerius
2014-09-03 21:15:50 UTC
Permalink
Is there a PP accessible means of truncating piddles?

In code I'm writing the output piddle length will be less than the input
length, but the exact value isn't known until the end of processing the
input (so RedoDims doesn't apply).

Currently I pass back the number of valid elements and truncate the piddles
in the Perl wrapper, but I'd prefer to do it in the PP code.

Thanks,

Diab
David Mertens
2014-09-04 12:43:01 UTC
Permalink
Hey Diab,

I am not aware of any technique, and I don't believe it's possible. The
problem is that any code written using PDL::PP will automatically thread to
higher dimensions. Data-dependent truncation would naively try to truncate
each "thread" to its own data-dependent length, which is obviously
incommensurate with the uniform allocations assumed by PDL.

The closest thing to a PDL-endorsed method of doing this is to set all
trailing values to BAD. It might be a good idea to have a slicing routine
that trims off bad values from the end of the data, in a threadable way, to
make this sort of operation easier to handle in the general case.

By the way, I have used an approach similar to yours to achieve the same
ends, but that's only in code that I use for my own research and not for
code distributed to CPAN.

David
Post by Diab Jerius
Is there a PP accessible means of truncating piddles?
In code I'm writing the output piddle length will be less than the input
length, but the exact value isn't known until the end of processing the
input (so RedoDims doesn't apply).
Currently I pass back the number of valid elements and truncate the
piddles in the Perl wrapper, but I'd prefer to do it in the PP code.
Thanks,
Diab
_______________________________________________
PDL-porters mailing list
http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
Diab Jerius
2014-09-04 14:16:31 UTC
Permalink
David,

I'd convinced myself that this code couldn't be threadable, but now you've
instilled a bit of doubt and I'll have to go and test it...

The idea of filling the unused elements with a BAD value is great. But,
what about PDL installations without bad values? I could provide an option
to specify a fill value, but that means all code using this module (which I
do plan on releasing to CPAN) will need to first check if they're bad value
enabled to determine which value to expect.

Another option would be to return an appropriately dimensioned piddle
recording the valid extents. The caller could then slice the returned data
as appropriate.

A combination of the two works best, I think.

Thanks,

Diab
Post by David Mertens
Hey Diab,
I am not aware of any technique, and I don't believe it's possible. The
problem is that any code written using PDL::PP will automatically thread to
higher dimensions. Data-dependent truncation would naively try to truncate
each "thread" to its own data-dependent length, which is obviously
incommensurate with the uniform allocations assumed by PDL.
The closest thing to a PDL-endorsed method of doing this is to set all
trailing values to BAD. It might be a good idea to have a slicing routine
that trims off bad values from the end of the data, in a threadable way, to
make this sort of operation easier to handle in the general case.
By the way, I have used an approach similar to yours to achieve the same
ends, but that's only in code that I use for my own research and not for
code distributed to CPAN.
David
Post by Diab Jerius
Is there a PP accessible means of truncating piddles?
In code I'm writing the output piddle length will be less than the input
length, but the exact value isn't known until the end of processing the
input (so RedoDims doesn't apply).
Currently I pass back the number of valid elements and truncate the
piddles in the Perl wrapper, but I'd prefer to do it in the PP code.
Thanks,
Diab
_______________________________________________
PDL-porters mailing list
http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
Burke, Douglas
2014-09-04 15:23:24 UTC
Permalink
Post by Diab Jerius
David,
I'd convinced myself that this code couldn't be threadable, but now you've
instilled a bit of doubt and I'll have to go and test it...
The idea of filling the unused elements with a BAD value is great. But,
what about PDL installations without bad values? I could provide an option
to specify a fill value, but that means all code using this module (which I
do plan on releasing to CPAN) will need to first check if they're bad value
enabled to determine which value to expect.
Or even integer types for which you may not have a "spare" value that could
be used for a 'bad' value.
Post by Diab Jerius
Another option would be to return an appropriately dimensioned piddle
recording the valid extents. The caller could then slice the returned data
as appropriate.
A combination of the two works best, I think.
Thanks,
Diab
Post by David Mertens
Hey Diab,
I am not aware of any technique, and I don't believe it's possible. The
problem is that any code written using PDL::PP will automatically thread to
higher dimensions. Data-dependent truncation would naively try to truncate
each "thread" to its own data-dependent length, which is obviously
incommensurate with the uniform allocations assumed by PDL.
The closest thing to a PDL-endorsed method of doing this is to set all
trailing values to BAD. It might be a good idea to have a slicing routine
that trims off bad values from the end of the data, in a threadable way, to
make this sort of operation easier to handle in the general case.
By the way, I have used an approach similar to yours to achieve the same
ends, but that's only in code that I use for my own research and not for
code distributed to CPAN.
David
Post by Diab Jerius
Is there a PP accessible means of truncating piddles?
In code I'm writing the output piddle length will be less than the input
length, but the exact value isn't known until the end of processing the
input (so RedoDims doesn't apply).
Currently I pass back the number of valid elements and truncate the
piddles in the Perl wrapper, but I'd prefer to do it in the PP code.
Thanks,
Diab
_______________________________________________
PDL-porters mailing list
http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
_______________________________________________
PDL-porters mailing list
http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
Craig DeForest
2014-09-04 14:26:09 UTC
Permalink
It's not possible to set the size of a thread dim inside a PP code snippet, because of, well, the threading and the different-lengths-for-different-iterations problem -- but it *is* of course possible to do very tricky things if you duck out of the autothreading and use direct C manipulation of the code. Depending on your application, you can return the size of your active dim in a secondary output variable and truncate as needed in the wrapper (I think this is what you said you are doing now) or condition the input variable to make sure there are no thread dims, and then manipulate the pdl struct explicitly inside PP (I forget the names of them, but there are PDL methods that do explicit threadloops at the Perl level, which can help encapsulate your non-threadable operator and make it act like a regular PDL operator).

That second approach prevents you from using the autogenerated thread loops in the PP, but you still get access to its nice macros for accessing the data and looping over active dims.
Post by Diab Jerius
David,
I'd convinced myself that this code couldn't be threadable, but now you've instilled a bit of doubt and I'll have to go and test it...
The idea of filling the unused elements with a BAD value is great. But, what about PDL installations without bad values? I could provide an option to specify a fill value, but that means all code using this module (which I do plan on releasing to CPAN) will need to first check if they're bad value enabled to determine which value to expect.
Another option would be to return an appropriately dimensioned piddle recording the valid extents. The caller could then slice the returned data as appropriate.
A combination of the two works best, I think.
Thanks,
Diab
Hey Diab,
I am not aware of any technique, and I don't believe it's possible. The problem is that any code written using PDL::PP will automatically thread to higher dimensions. Data-dependent truncation would naively try to truncate each "thread" to its own data-dependent length, which is obviously incommensurate with the uniform allocations assumed by PDL.
The closest thing to a PDL-endorsed method of doing this is to set all trailing values to BAD. It might be a good idea to have a slicing routine that trims off bad values from the end of the data, in a threadable way, to make this sort of operation easier to handle in the general case.
By the way, I have used an approach similar to yours to achieve the same ends, but that's only in code that I use for my own research and not for code distributed to CPAN.
David
Is there a PP accessible means of truncating piddles?
In code I'm writing the output piddle length will be less than the input length, but the exact value isn't known until the end of processing the input (so RedoDims doesn't apply).
Currently I pass back the number of valid elements and truncate the piddles in the Perl wrapper, but I'd prefer to do it in the PP code.
Thanks,
Diab
_______________________________________________
PDL-porters mailing list
http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it." -- Brian Kernighan
_______________________________________________
PDL-porters mailing list
http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
Chris Marshall
2014-09-04 20:41:47 UTC
Permalink
I think you'll be more future-proof if you stick to truncating
at the perl level in your routine. If you use the PMCode
key for the pp_def() you can have the fixup code in the
same place as you PP code.

--Chris
Post by Diab Jerius
Is there a PP accessible means of truncating piddles?
In code I'm writing the output piddle length will be less than the input
length, but the exact value isn't known until the end of processing the
input (so RedoDims doesn't apply).
Currently I pass back the number of valid elements and truncate the piddles
in the Perl wrapper, but I'd prefer to do it in the PP code.
Thanks,
Diab
_______________________________________________
PDL-porters mailing list
http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
Diab Jerius
2014-09-04 21:09:34 UTC
Permalink
Thanks for all of the advice. I'll stick with Perl side actions.
Post by Chris Marshall
I think you'll be more future-proof if you stick to truncating
at the perl level in your routine. If you use the PMCode
key for the pp_def() you can have the fixup code in the
same place as you PP code.
--Chris
Post by Diab Jerius
Is there a PP accessible means of truncating piddles?
In code I'm writing the output piddle length will be less than the input
length, but the exact value isn't known until the end of processing the
input (so RedoDims doesn't apply).
Currently I pass back the number of valid elements and truncate the
piddles
Post by Diab Jerius
in the Perl wrapper, but I'd prefer to do it in the PP code.
Thanks,
Diab
_______________________________________________
PDL-porters mailing list
http://mailman.jach.hawaii.edu/mailman/listinfo/pdl-porters
Loading...