Hi Marco --
Chapel's semantics say that when an array A's domain is re-assigned from
one set of indices to another (say, from set 'D_1' to 'D_2'):
(1) the array will be reallocated so that A will have an element stored
for each index in D_2;
(2) array elements corresponding to indices in the intersection of
D_1 and D_2 will have their values preserved;
(3) array elements corresponding to indices in the set D_2 - D_1 will
be assigned the default value for the element type.
Thus, the reallocation is a logical reallocation of the array rather than
a memory-based one.
The language itself doesn't specify how array storage is managed -- that's
the role of the domain maps (user-specifiable recipes for the
implementation of arrays and domains), so there are no guarantees as to
how this reallocation will be done. I.e., a domain map could use a linked
list, or tiled storage, or ... to store its array, in which case the
reallocation would be handled very differently from case-to-case.
For the default implementation of arrays in the current implementation, an
array's storage is always stored in a single contiguous block of memory.
Thus, you can be guaranteed that the addresses of adjacent elements will
be adjacent in memory after the reallocation (the first of your two
possible 0..2 scenarios below).
Because of rule (2) above, we don't use C-style realloc's in general for
array resizing. For example, if I resize an array declared over the
domain {1..n, 1..n} to {1..n+1, 1..n+1}, a realloc wouldn't naturally
preserve the elements in the {1..n, 1..n} intersection. For that reason,
it is the case that our general implementation of array resizing is to
create a new buffer and copy the intersecting elements from the old array
to the new. As you know, in many common cases (like the one you show -- a
1D array resizing in which the lower bound does not change), we could
optimize the more general reallocation to use a C-level realloc, but
nobody has put in this effort yet. I suspect it would be an easy fix if
you needed it (or wanted to take it on yourself and contribute it back :)
Hope this is helpful,
-Brad
On Wed, 4 Feb 2015, Marco Postigo wrote:
Hello Vassily,
thank you for your answers.
I've just noticed that it seems that the array is not "really" reallocated.
It rather looks like it were expanded (because the old values are still in
the array after reassigning the domain). Does that mean that chapel is
reallocating memory and copying the old values automatically? Or will it only
allocate new memory and redirecting invocations to the new array indices to
the new memory that were allocated?
E.g.: if I have an array with two 32-bit integers then I assume that the
memory allocated belongs together:
address of myIntArray(0) = 0x000000000050
address of myIntArray(1) = 0x000000000054
Now I'm reassigning the arrays domain to 0..2 ... do I get new memory allocat
ion now, but all belonging together:
address of myIntArray(0) = 0x7fffffffff50
address of myIntArray(1) = 0x7fffffffff54
address of myIntArray(2) = 0xffffffffff58
or are only the first two indices together and the third were only allocated
new:
address of myIntArray(0) = 0x000000000050
address of myIntArray(1) = 0x000000000054
address of myIntArray(2) = 0xffffffffff58
I'm just asking because it would mean that my program gets a lot of cache
misses if I'm reassigning the domain too much often during my calculations
and may rather allocate a much bigger array in the beginning although I
probably won't need the space.
Best regards,
Marco
Gesendet: Mittwoch, 04. Februar 2015 um 01:05 Uhr
Von: "Vassily Litvinov" <[email protected]>
An: "Marco Postigo" <[email protected]>
Cc: [email protected]
Betreff: Re: Questions about randomFill and array reallocation
To follow up on my previous response:
* (as I mentioned) the correct way to resize an array is to update its
domain variable, in this example:
lifelinesDomain = 0..(p.z-1);
* Currently our compiler also allows you to update lifelines.domain
directly. This is a bug, do not rely on it.
* 1-D arrays also support vector operations. See the following file for
examples and limitations:
$CHPL_HOME/examples/primers/arrayVectorOps.chpl
Vass
On 01/29/15 15:04, Marco Postigo wrote:
> Hello,
> I have some questions about the randomFill(x:[]) function and array
reallocation.
> First of all I need to use the Random Module and I'm not sure what the
interval
> is of the numbers returned... I've found in a paper where it was stated
that it
> returns values between 0 and 1... inclusive? exclusive?
> Is it as in most other languages [0, 1) or are both incluse ( -> [0, 1] )?
> Second is the reallocation of arrays... I haven't found any examples so
I've
> tested a bit around and only got it worked with using a domain and
reassigning
> it. Is this the correct way or is there another "nicer" solution?
> code snippet:
> 9 /** Domain of lifeline buddies array */
> 10 var lifelinesDomain : domain(rank=1) = 0..0;
> 11 /** Lifeline buddies */
> 12 var lifelines : [lifelinesDomain] uint;
> ...
> 55 lifelinesDomain = 0..(p.z-1); // reallocation in constructor
> Sorry if this questions are duplicates or if I've just overlooked the
solution
> in your spec / docs.
> Kind regards,
> Marco
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Chapel-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-users