Just for clarity, `b[:] *= 2` still allocates the same new memory as
`b *= 2`, however, the `[:]` ensures that the result gets copied back
to the shared array, rather than changing the binding for the variable
b.

This is probably what you wanted anyways, but I thought it might be
good to clarify that this is not exactly the same (e.g. not nearly as
fast) as Tim Holy's suggestion.

-Jameson

On Wed, Feb 19, 2014 at 4:54 PM, Madeleine Udell
<madeleine.ud...@gmail.com> wrote:
> Tim, thanks for the explanation! It looks like
>
> b = Base.shmem_randn(10)
> b[:] *= 2
>
> should work without allocating new memory for normal and shared
> arrays, and is slightly more concise.
>
> On Wed, Feb 19, 2014 at 12:11 PM, Tim Holy <tim.h...@gmail.com> wrote:
>> Unfortunately, I'm pretty sure you can't do what you're hoping for. Consider:
>>
>> julia> A = rand(2,2)
>> 2x2 Array{Float64,2}:
>>  0.63259   0.109017
>>  0.667425  0.112597
>>
>> julia> pointer(A)
>> Ptr{Float64} @0x000000000446f6a0
>>
>> julia> A *= 2
>> 2x2 Array{Float64,2}:
>>  1.26518  0.218033
>>  1.33485  0.225195
>>
>> julia> pointer(A)
>> Ptr{Float64} @0x0000000005696650
>>
>> I think A *= 2 gets translated by the parser into A = A*2. There are some old
>> posts that can do a better job than I explaining why this is important.
>>
>> So I think your only bet is to do something like this:
>>
>> for i = 1:length(A)
>>     A[i] *= 2
>> end
>>
>> which does work in-place.
>>
>> --Tim
>>
>> On Wednesday, February 19, 2014 11:26:48 AM Madeleine Udell wrote:
>>> Currently in-place arithmetic operations turn shared arrays into normal
>>> ones: eg
>>>
>>> b = Base.shmem_randn(10)
>>> b *= 2
>>>
>>> results in b becoming a (not-shared) array. I'd like to overload these
>>> in-place arithmetic operations so that instead of writing
>>>
>>> typeof(b)==SharedArray? b.s *= 2 : b *= 2
>>>
>>> I can just write
>>>
>>> b *= 2
>>>
>>> and know that the shared status of my array will be preserved. How might
>>> one overload these operations?
>>>
>>> (Note: I don't want c = b + b to automatically create a shared array c,
>>> since that allocates new shared memory.)
>
>
>
> --
> Madeleine Udell
> PhD Candidate in Computational and Mathematical Engineering
> Stanford University
> www.stanford.edu/~udell

Reply via email to