Re: Question about using an array as an index [message #6486] |
Wed, 26 June 1996 00:00 |
Thomas A. McGlynn
Messages: 23 Registered: March 1996
|
Junior Member |
|
|
David Schwab wrote:
>
> Can anyone explain why these two codes produce different results?
>
> IDL> a=[0,1,1,2,2,2]
> IDL> b=intarr(3)
> IDL> b(a)=b(a)+1
> IDL> print,b
> 1 1 1
> and
>
> IDL> a=[0,1,1,2,2,2]
> IDL> b=intarr(3)
> IDL> for i=0,5 do b(a(i))=b(a(i))+1
> IDL> print,b
> 1 2 3
>
> Thanks!
> --
> Dr. David J. Schwab
> NOAA Great Lakes Environmental Research Laboratory
> 2205 Commonwealth Blvd.
> Ann Arbor, MI 48105
> 313-741-2120
> 313-741-2055 (FAX)
I've been caught by this one myself. Basically when you do the statment
b(a)=b(a)+1
IDL acts as if all of the additions are being carried out in parallel,
not sequentially. I imagine that in cases where you are running on
a vectorizing system the operations are made parallel in fact. Since
the operations are parallel rather than sequential each of the additions
starts with b[a] = 0 so that the result is 1. I'd be interested in
comments as to whether the result is gauranteed to be 1 or if it's possible
that on some implementations it could differ. It would be nice to
get the kind of sequential behavior you're looking for (as an option)
but I imagine this is quite non-trivial and would probably slow down the
calculation to the rate it takes in explicit loops.
Tom McGlynn
tam@silk.gsfc.nasa.gov
|
|
|