From: James Tursa on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <humhgn$gb4$1(a)fred.mathworks.com>...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <humfoi$oeg$1(a)fred.mathworks.com>...
> >
> > Now a more interesting question IMO is: does Matlab need to run over the entire imaginary array everytime to check for zero whenever an element is modified? It seems like a very inefficient strategy. How to disable this "feature" (IMO it's not possible)?
>
> There is a status word in the mxArray header stuff. e.g., they have a status bit for "real double scalar", etc. But I am unaware of a status bit for "imaginary part is presently all zero" that could be exploited. I will check ...

Just checked. None of the bits in the status word change when I compared a real variable with complex(same real variable). So in what I assume would be the easiest case to exploit and set such a bit, nothing is being set. So it would appear that nothing is used for this condition, at least in this particular status word that I am aware of. So I would doubt that MATLAB exploits this in general. As to your second question, I am also unaware of any way to change the MATLAB behavior with regards to freeing imaginary parts when they are all zeros.

James Tursa
From: Bruno Luong on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <humjtk$hcd$1(a)fred.mathworks.com>...
>
>
> Just checked. None of the bits in the status word change when I compared a real variable with complex(same real variable). So in what I assume would be the easiest case to exploit and set such a bit, nothing is being set. So it would appear that nothing is used for this condition, at least in this particular status word that I am aware of. So I would doubt that MATLAB exploits this in general.

The "status" is somewhat known by checking the Pi pointer is NULL or not. Even if there exists effectively such status word/bit, I can't imagine how possibly implementing an intelligent way of checking reliably whereas a memory segment contains all-zero could be done without scanning it.

Bruno
From: Walter Roberson on
Bruno Luong wrote:
> "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message
> <humjtk$hcd$1(a)fred.mathworks.com>...
>>
>>
>> Just checked. None of the bits in the status word change when I
>> compared a real variable with complex(same real variable). So in what
>> I assume would be the easiest case to exploit and set such a bit,
>> nothing is being set. So it would appear that nothing is used for this
>> condition, at least in this particular status word that I am aware of.
>> So I would doubt that MATLAB exploits this in general.
>
> The "status" is somewhat known by checking the Pi pointer is NULL or
> not. Even if there exists effectively such status word/bit, I can't
> imagine how possibly implementing an intelligent way of checking
> reliably whereas a memory segment contains all-zero could be done
> without scanning it.

As the matrix of new imaginary values is being constructed, the new
values could be tested; set a "non-zero found" flag as a non-zero is
found. If any of the new values were non-zero then there is no desire to
delete the imaginary area; if no non-zero values were found in the new
values, then the _rest_ of the values could be tested, stopping if a
non-zero was found.

I do not know if it is the case in x86 processors, but I know the old
Motorola 680xx line automatically tested the value being stored by a
MOVE instruction, allowing a BEQ or BNE (to zero) after the MOVE without
having to specifically test the value, or (68000 family) allowing a SNE
(Set if Not Equal to Zero) instruction followed by an OR instruction to
the commulative flag. Anyhow, the point is that if you are moving data
around, testing it for non-zero may be "nearly free" in some architectures.
From: Bruno Luong on
Walter Roberson <roberson(a)hushmail.com> wrote in message <9FFPn.2526$1Q5.1727(a)newsfe08.iad>...
> Bruno Luong wrote:
> > "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message
> > <humjtk$hcd$1(a)fred.mathworks.com>...
> >>
> >>
> >> Just checked. None of the bits in the status word change when I
> >> compared a real variable with complex(same real variable). So in what
> >> I assume would be the easiest case to exploit and set such a bit,
> >> nothing is being set. So it would appear that nothing is used for this
> >> condition, at least in this particular status word that I am aware of.
> >> So I would doubt that MATLAB exploits this in general.
> >
> > The "status" is somewhat known by checking the Pi pointer is NULL or
> > not. Even if there exists effectively such status word/bit, I can't
> > imagine how possibly implementing an intelligent way of checking
> > reliably whereas a memory segment contains all-zero could be done
> > without scanning it.
>
> As the matrix of new imaginary values is being constructed, the new
> values could be tested; set a "non-zero found" flag as a non-zero is
> found. If any of the new values were non-zero then there is no desire to
> delete the imaginary area; if no non-zero values were found in the new
> values, then the _rest_ of the values could be tested, stopping if a
> non-zero was found.

In order to know which contains zeros which are the rest, there should be somehow a logical array to stores this information, possibly compressed in byte or even bit for each element (It seems unlikely bit is a preferable mode for Intel processor). Now instead of scanning one large array (of double), we have to scan a smaller one. We could save somehow the time, but I doubt much gain could be taken on the modern CPU. In any case, there is no such logical array anywhere in the mxArray internal structure beside what under the Pi pointer itself.

Bruno
From: James Tursa on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <hun7fr$ihg$1(a)fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <humjtk$hcd$1(a)fred.mathworks.com>...
> >
> >
> > Just checked. None of the bits in the status word change when I compared a real variable with complex(same real variable). So in what I assume would be the easiest case to exploit and set such a bit, nothing is being set. So it would appear that nothing is used for this condition, at least in this particular status word that I am aware of. So I would doubt that MATLAB exploits this in general.
>
> The "status" is somewhat known by checking the Pi pointer is NULL or not. Even if there exists effectively such status word/bit, I can't imagine how possibly implementing an intelligent way of checking reliably whereas a memory segment contains all-zero could be done without scanning it.

I'm not talking about that status. There is a 32-bit word in the mxArray header that contains several status bits. One of them is set whenever the variable is a double full scalar (1x1) variable. As many functions require this (e.g. integer class array multiplication) for one of the operands it is easy and a little bit quicker to check this one status bit as opposed to checking a bunch of other stuff. There are also status bits in this 32-bit word for being a temporary variable (e.g., created on the fly in the argument list), being sparse, etc. etc. One could imagine that they could have defined a status bit for "all imaginary values are zero" and exploited it in some of their built-in functions. For their built-in functions they could probably maintain the integrity of the bit, but for mex functions that get at and set the data directly there is probably no way to maintain this
integrity unless the bit was forced to be 0 in all of the mxCreate___ functions.

James Tursa