digitalmars.D - simd comparison operator?
- monarch_dodra (22/22) Feb 19 2013 Are there any plans that would allow using opEqual or opCmp
- bearophile (5/8) Feb 19 2013 SIMD instructions support those operations, so it's mostly a
- Don (8/24) Feb 19 2013 Simd comparison generally doesn't return a bool, it returns a
- bearophile (10/17) Feb 19 2013 Right, it's a design problem.
- John Colvin (9/28) Feb 19 2013 There is significant opposition to any simd operators that
- bearophile (5/14) Feb 19 2013 Can't D allocate that bool[N] on the stack?
- John Colvin (8/23) Feb 19 2013 Perhaps it could, but it would be:
- bearophile (5/8) Feb 19 2013 Then maybe:
- John Colvin (7/15) Feb 19 2013 Yes, but it's quite restrictive to force it to be declared at the
- monarch_dodra (4/23) Feb 19 2013 Hum... so long story short, it is doable, but because the result
- tn (10/41) Feb 19 2013 In many cases it would be nice, if "arr[] < x" translated to
- Marco Leise (5/9) Feb 22 2013 This would make the compiler depend on std.algorithm !
- Manu (12/30) Mar 03 2013 Don is on the money.
- bearophile (5/9) Mar 03 2013 Isn't something like this better?
- Manu (8/17) Mar 03 2013 I don't think opCmp can fit that model?
- jerro (7/12) Feb 19 2013 One possible workaround is to use simd operation eplicitly. I've
- jerro (4/9) Feb 19 2013 I've update the gist now so that it works with DMD too. To use it
Are there any plans that would allow using opEqual or opCmp operators with simd? My use case is that I have arrays, and I need to verify that ALL the elements in my array are comprised between two values (in this specific case 10 and 93). I was hoping I would be able to use: ubyte[] arr = ... if (arr []< 10 || arr[]> 93) throw Exception(); But that doesn't seem to work. Neither does simple opEqual comparison. //Make sure arr is initialized to 0 if (arr[] != 0) Is this a "never thought of this and not implemented" issue, or is there a reason that simd can't do this? I find it strange, since simd allows for straight up array to array opEquals, but not array to value? -------- Anybody have any idea as a workaround? My program has very high data throughput, and never actually handles the array elements individually, just manipulates them simd chunks at once. Validating the content is important, but performance is taking a real toll...
Feb 19 2013
monarch_dodra:is there a reason that simd can't do this? I find it strange, since simd allows for straight up array to array opEquals, but not array to value?SIMD instructions support those operations, so it's mostly a matter of designing a good semantics for D and implementing it. Bye, bearophile
Feb 19 2013
On Tuesday, 19 February 2013 at 14:03:54 UTC, monarch_dodra wrote:Are there any plans that would allow using opEqual or opCmp operators with simd? My use case is that I have arrays, and I need to verify that ALL the elements in my array are comprised between two values (in this specific case 10 and 93). I was hoping I would be able to use: ubyte[] arr = ... if (arr []< 10 || arr[]> 93) throw Exception(); But that doesn't seem to work. Neither does simple opEqual comparison. //Make sure arr is initialized to 0 if (arr[] != 0) Is this a "never thought of this and not implemented" issue, or is there a reason that simd can't do this? I find it strange, since simd allows for straight up array to array opEquals, but not array to value?Simd comparison generally doesn't return a bool, it returns a bool array, one per element. Does (arr[] < 10) mean "is every element in arr less than 10" OR "is any element of arr less than 10" OR "create a bool array which is true for each element which is less than 10" ? All make sense. That's the problem.
Feb 19 2013
Don:Simd comparison generally doesn't return a bool, it returns a bool array, one per element. Does (arr[] < 10) mean "is every element in arr less than 10" OR "is any element of arr less than 10" OR "create a bool array which is true for each element which is less than 10" ? All make sense. That's the problem.Right, it's a design problem. I think the right thing to do is to take a look at what's an efficient operation to do in hardware (and then look at what's the most commonly useful operation for users). I think the right design here is to return a bool[N]. So in this case monarch_dodra has to add some more code to test all/any. Bye, bearophile
Feb 19 2013
On Tuesday, 19 February 2013 at 16:03:58 UTC, bearophile wrote:Don:There is significant opposition to any simd operators that allocate. The reasoning is that they appear fast but are in actual fact slow (for most normal size vectors, the allocation would be much slower than the calculation itself). I love the features of numpy and matlab etc. when it comes to array operations, many of which allocate implicitly, but Walter and others were quite adamant they they do not belong in D, a position I've come to agree with.Simd comparison generally doesn't return a bool, it returns a bool array, one per element. Does (arr[] < 10) mean "is every element in arr less than 10" OR "is any element of arr less than 10" OR "create a bool array which is true for each element which is less than 10" ? All make sense. That's the problem.Right, it's a design problem. I think the right thing to do is to take a look at what's an efficient operation to do in hardware (and then look at what's the most commonly useful operation for users). I think the right design here is to return a bool[N]. So in this case monarch_dodra has to add some more code to test all/any. Bye, bearophile
Feb 19 2013
John Colvin:...I think the right design here is to return a bool[N].There is significant opposition to any simd operators that allocate. The reasoning is that they appear fast but are in actual fact slow (for most normal size vectors, the allocation would be much slower than the calculation itself). I love the features of numpy and matlab etc. when it comes to array operations, many of which allocate implicitly, but Walter and others were quite adamant they they do not belong in D, a position I've come to agree with.Can't D allocate that bool[N] on the stack? Bye, bearophile
Feb 19 2013
On Tuesday, 19 February 2013 at 16:46:36 UTC, bearophile wrote:John Colvin:Perhaps it could, but it would be: a) quite counter-intuitive. An operation between two normal, heap allocated arrays generating a stack allocated array, with the scoping rules that entails? b) very easy to cause stack overflow in a way that would be totally confusing to someone who wasn't aware of the implementation....I think the right design here is to return a bool[N].There is significant opposition to any simd operators that allocate. The reasoning is that they appear fast but are in actual fact slow (for most normal size vectors, the allocation would be much slower than the calculation itself). I love the features of numpy and matlab etc. when it comes to array operations, many of which allocate implicitly, but Walter and others were quite adamant they they do not belong in D, a position I've come to agree with.Can't D allocate that bool[N] on the stack? Bye, bearophile
Feb 19 2013
John Colvin:a) quite counter-intuitive. An operation between two normal, heap allocated arrays generating a stack allocated array, with the scoping rules that entails?Then maybe: bool[4] res = a > b; Bye, bearophile
Feb 19 2013
On Tuesday, 19 February 2013 at 17:46:56 UTC, bearophile wrote:John Colvin:Yes, but it's quite restrictive to force it to be declared at the same place as it's assigned, if that's what you're implying. Essentially what's needed in order to allow all this and other simd operations that don't work in-place on the main operands is a guarantee that the memory has been pre-allocated. Then it really doesn't matter whether it's stack or heap.a) quite counter-intuitive. An operation between two normal, heap allocated arrays generating a stack allocated array, with the scoping rules that entails?Then maybe: bool[4] res = a > b; Bye, bearophile
Feb 19 2013
On Tuesday, 19 February 2013 at 17:58:14 UTC, John Colvin wrote:On Tuesday, 19 February 2013 at 17:46:56 UTC, bearophile wrote:Hum... so long story short, it is doable, but because the result of "[]<" is ambiguous, it is not built into the language. Gonna play around with core.simd then!John Colvin:Yes, but it's quite restrictive to force it to be declared at the same place as it's assigned, if that's what you're implying. Essentially what's needed in order to allow all this and other simd operations that don't work in-place on the main operands is a guarantee that the memory has been pre-allocated. Then it really doesn't matter whether it's stack or heap.a) quite counter-intuitive. An operation between two normal, heap allocated arrays generating a stack allocated array, with the scoping rules that entails?Then maybe: bool[4] res = a > b; Bye, bearophile
Feb 19 2013
On Tuesday, 19 February 2013 at 16:28:15 UTC, John Colvin wrote:On Tuesday, 19 February 2013 at 16:03:58 UTC, bearophile wrote:In many cases it would be nice, if "arr[] < x" translated to "map!(a => a < x)(arr)" and similarly "arr1[] < arr2[]" translated to "map!((a, b) => a < b)(arr1, arr2)" or equivalent (*). Then for example, if "all" and "any" had "a => a" as a default predicate, it would be possible to write for example "if (any(arr[] < 10)) ...". As far as I understand, this does not allocate. (*) Also, it would be nice if "map!((a, b) => a < b)(arr1, arr2)" was a valid syntax. (Or if it is, then it should be documented.)Don:There is significant opposition to any simd operators that allocate. The reasoning is that they appear fast but are in actual fact slow (for most normal size vectors, the allocation would be much slower than the calculation itself). I love the features of numpy and matlab etc. when it comes to array operations, many of which allocate implicitly, but Walter and others were quite adamant they they do not belong in D, a position I've come to agree with.Simd comparison generally doesn't return a bool, it returns a bool array, one per element. Does (arr[] < 10) mean "is every element in arr less than 10" OR "is any element of arr less than 10" OR "create a bool array which is true for each element which is less than 10" ? All make sense. That's the problem.Right, it's a design problem. I think the right thing to do is to take a look at what's an efficient operation to do in hardware (and then look at what's the most commonly useful operation for users). I think the right design here is to return a bool[N]. So in this case monarch_dodra has to add some more code to test all/any. Bye, bearophile
Feb 19 2013
Am Tue, 19 Feb 2013 18:51:35 +0100 schrieb "tn" <no email.com>:In many cases it would be nice, if "arr[] < x" translated to "map!(a => a < x)(arr)" and similarly "arr1[] < arr2[]" translated to "map!((a, b) => a < b)(arr1, arr2)" or equivalent (*).This would make the compiler depend on std.algorithm ! -- Marco
Feb 22 2013
On 20 February 2013 02:03, bearophile <bearophileHUGS lycos.com> wrote:Don:Don is on the money. I drafted: bool allEqual(a,b)/anyEqual(a,b) and friends in std.simd, but I haven't written them yet (been awol for a while). They're actually quite tricky to get right+fast in a portable way. all/any comparisons are often used in a boolean way, but they may not be super fast on some architectures. If you want to do component-wise logic, the typical approach is to use component-wise selection, eg: uint4 mask = compareGreater(a, b); // <- build a bit mask of 0's (false) or 1's (true) where components of a are greater than b. auto result = select(mask, c, d); // <- select components from c or d according to the bit mask.Simd comparison generally doesn't return a bool, it returns a bool array, one per element. Does (arr[] < 10) mean "is every element in arr less than 10" OR "is any element of arr less than 10" OR "create a bool array which is true for each element which is less than 10" ? All make sense. That's the problem.Right, it's a design problem. I think the right thing to do is to take a look at what's an efficient operation to do in hardware (and then look at what's the most commonly useful operation for users). I think the right design here is to return a bool[N]. So in this case monarch_dodra has to add some more code to test all/any. Bye, bearophile
Mar 03 2013
Manu:If you want to do component-wise logic, the typical approach is to use component-wise selection, eg: uint4 mask = compareGreater(a, b); // <- build a bit mask of 0's (false)Isn't something like this better? uint4 mask = a > b; Bye, bearophile
Mar 03 2013
I don't think opCmp can fit that model? But aside from that, I think that's misleading, and suggests to the user that it's a trivial operation. Additionally, it's not necessarily even possible in a portable way. I also think most users would expect that comparison operators produce a boolean result. They'll be surprised when: if(a > b) produces an error. On 3 March 2013 23:39, bearophile <bearophileHUGS lycos.com> wrote:Manu: If you want to do component-wise logic,the typical approach is to use component-wise selection, eg: uint4 mask = compareGreater(a, b); // <- build a bit mask of 0's (false)Isn't something like this better? uint4 mask = a > b; Bye, bearophile
Mar 03 2013
Anybody have any idea as a workaround? My program has very high data throughput, and never actually handles the array elements individually, just manipulates them simd chunks at once. Validating the content is important, but performance is taking a real toll...One possible workaround is to use simd operation eplicitly. I've played around a bit with this and came up with this: https://gist.github.com/jerro/4988229 This currently only works with GDC and LDC (but it probably could be made to work with DMD too). It uses std.simd (not in phobos yet) from here: https://github.com/TurkeyMan/phobos
Feb 19 2013
One possible workaround is to use simd operation eplicitly. I've played around a bit with this and came up with this: https://gist.github.com/jerro/4988229 This currently only works with GDC and LDC (but it probably could be made to work with DMD too).I've update the gist now so that it works with DMD too. To use it with DMD, you'll need this branch from my fork of std.simd: https://github.com/jerro/phobos/tree/simd-wip
Feb 19 2013