Re: [julia-users] sub-ranges within CartesianRange

2016-02-14 Thread Tim Holy
On Sunday, February 14, 2016 12:55:42 AM Greg Plowman wrote:
> No, not "Cartesian" offsets, but "linear" offsets transformed to Cartesian
> equivalent.
> That's why I think I need the dimensions of the parent range.

You can't do that in general with a cartesian iterator using Julia's for-loop 
syntax. Consider the case where you're iterating over a 5x5 domain, and your 
starting index is 7. You can, however, do this manually using start, done, and 
next.

> As you point out, R here is effectively an empty iterator.
> However for my use, that same range R could be non-empty if it is a
> sub-range of a larger enclosing range.
> Say CartesianSubRange(CartesianRange((8,8,8)), I1, I2))
> So I want to iterate within the parent CartesianRange dimensions, from I1
> to I2.

I guess I don't understand. Can you be explicit about what range that would 
actually produce? I suspect that you're not interpreting I2 as the "stop 
index" but as something else, but I am not having much luck guessing what that 
is.

Best,
--Tim



Re: [julia-users] sub-ranges within CartesianRange

2016-02-14 Thread Tim Holy
The docs on SharedArrays give an example where you partition just over one 
axis (e.g., the 3rd axis). That's easy and tends to work well in many 
applications.

Best,
--Tim

On Sunday, February 14, 2016 12:38:55 AM Greg Plowman wrote:
> On Saturday, February 13, 2016 at 1:57:11 AM UTC+11, Stefan Karpinski wrote:
> > I'm kind of curious what the use case is. How are you using
> > CartesianRanges?
> 
> I want parallelise a simulation iterating over a CartesianRange.
> This entails partitioning the CartesianRange into sub-ranges and calling
> the sim function with each sub-range as an argument, using pmap.
> It's quite easy to use linear indexes to sub-divide into integer intervals,
> but then I need to use ind2sub or equivalent at each iteration, which I
> think is somewhat slower than iterating over a CartesianRange.



Re: [julia-users] sub-ranges within CartesianRange

2016-02-14 Thread Greg Plowman

On Saturday, February 13, 2016 at 1:57:11 AM UTC+11, Stefan Karpinski wrote:
>
> I'm kind of curious what the use case is. How are you using 
> CartesianRanges?
>
>
I want parallelise a simulation iterating over a CartesianRange.
This entails partitioning the CartesianRange into sub-ranges and calling 
the sim function with each sub-range as an argument, using pmap.
It's quite easy to use linear indexes to sub-divide into integer intervals, 
but then I need to use ind2sub or equivalent at each iteration, which I 
think is somewhat slower than iterating over a CartesianRange.



Re: [julia-users] sub-ranges within CartesianRange

2016-02-14 Thread Greg Plowman
Thanks for your reply Tim.

Are x and y offsets with respect to the "parent" range cr? 
>
> If so, you can achieve this with a 1-liner, 
>
> CartesianRange(cr.start+x-1, cr.start+y-1) 
>

No, not "Cartesian" offsets, but "linear" offsets transformed to Cartesian 
equivalent. 
That's why I think I need the dimensions of the parent range.


julia> I1 = CartesianIndex((3,3,3)) 
> CartesianIndex{3}((3,3,3)) 
>
> julia> I2 = CartesianIndex((5,1,7)) 
> CartesianIndex{3}((5,1,7)) 
>
> julia> R = CartesianRange(I1, I2) 
> CartesianRange{CartesianIndex{3}}(CartesianIndex{3}((3,3,3)),CartesianIndex{3}
>  
>
> ((5,1,7))) 
>
 
As you point out, R here is effectively an empty iterator.
However for my use, that same range R could be non-empty if it is a 
sub-range of a larger enclosing range.
Say CartesianSubRange(CartesianRange((8,8,8)), I1, I2))
So I want to iterate within the parent CartesianRange dimensions, from I1 
to I2.



Re: [julia-users] sub-ranges within CartesianRange

2016-02-12 Thread Stefan Karpinski
I'm kind of curious what the use case is. How are you using CartesianRanges?

On Thu, Feb 11, 2016 at 9:00 PM, Greg Plowman 
wrote:

> Suppose I have a CartesianRange and want to access a sub-range.
>
> start = CartesianIndex((1,1,1))
> stop = CartesianIndex((5,5,5))
> cr = CartesianRange{start, stop)
>
>
> Something like:
> sub_cr = SubCartesianRange(cr,x,y) where x,y are arbitrary
> CartesianIndexes within cr.
>
>
> then I could do
>
> for a in sub_cr
>...
> end
>
> length(sub_cr)
>
> etc.
>
> I note that constructing CartesianRange(x,y) is valid code, but won't
> work because, we need to somehow embed start, stop into the sub-range.
> Also, it is possible for some x[i] > y[i], and I think the logic for
> CartesianRanges requires x[i] <= y[i] for all i.
>
> Is there something for sub-ranges like this already?
>
>


Re: [julia-users] sub-ranges within CartesianRange

2016-02-12 Thread Tim Holy
Are x and y offsets with respect to the "parent" range cr?

If so, you can achieve this with a 1-liner,

CartesianRange(cr.start+x-1, cr.start+y-1)

although you need https://github.com/JuliaLang/julia/pull/15030 to support the 
"- 1". On julia 0.4, that would have to be

@generated function Base.(:-){N}(I::CartesianIndex{N}, i::Integer)
args = [:(I[$d]-i) for d = 1:N]
:(CartesianIndex(tuple($(args...
end

As a homework exercise, one could add bounds-checking if desired. But:

> I note that constructing CartesianRange(x,y) is valid code, but won't work
> because, we need to somehow embed start, stop into the sub-range.
> Also, it is possible for some x[i] > y[i], and I think the logic for
> CartesianRanges requires x[i] <= y[i] for all i.

Already ahead of you there :-).

julia> I1 = CartesianIndex((3,3,3))
CartesianIndex{3}((3,3,3))

julia> I2 = CartesianIndex((5,1,7))
CartesianIndex{3}((5,1,7))

julia> R = CartesianRange(I1, I2)
CartesianRange{CartesianIndex{3}}(CartesianIndex{3}((3,3,3)),CartesianIndex{3}
((5,1,7)))

julia> s = start(R)
CartesianIndex{3}((3,3,8))

julia> done(R, s)
true

meaning that it is an empty iterator (the body of a for loop would never 
execute).

Best,
--Tim



[julia-users] sub-ranges within CartesianRange

2016-02-11 Thread Greg Plowman
Suppose I have a CartesianRange and want to access a sub-range.

start = CartesianIndex((1,1,1))
stop = CartesianIndex((5,5,5))
cr = CartesianRange{start, stop)


Something like: 
sub_cr = SubCartesianRange(cr,x,y) where x,y are arbitrary CartesianIndexes 
within 
cr.


then I could do

for a in sub_cr
   ...
end

length(sub_cr)

etc.

I note that constructing CartesianRange(x,y) is valid code, but won't work 
because, we need to somehow embed start, stop into the sub-range.
Also, it is possible for some x[i] > y[i], and I think the logic for 
CartesianRanges requires x[i] <= y[i] for all i.

Is there something for sub-ranges like this already?