Hi Shiv —
The behavior you're observing is what I would expect from Chapel, so I'll start by explaining what's happening, and then provide some alternative approaches you could use.
Background
First, leaving the record aside, as you may know, Chapel requires array assignments to be of matching size and shape to be legal. Thus, given:
var A3, B3: [1..3] real;
var B4: [1..4] real;
A3 = B3; is legal while A3 = B4; is not (ATO). The reason for this is that array declarations allocate their storage according to their declared size, and array assignments don't automatically resize the LHS array's storage to make it match that of the RHS, but simply copy the elements from one buffer to the other.
Now, moving to records, declaring a record allocates the storage for that record "in-place". So, a local record variable will reserve space on the stack for a struct representing the record; and a typical array of 10 records will store the structs representing those records in a contiguous memory buffer. This means that, in general, assigning records has to copy the fields from the RHS record into the fields of the LHS record—essentially a field-by-field "deep copy"—in order to modify the record's storage where it exists, in-place. Basically record variables refer to a specific storage location that won't change during the variable's lifetime.
Combining the two rules above, this means that for records with array fields, the arrays must be the same size since the deep-copy of that field will essentially be equivalent to the standalone array assignments above, requiring matching array size and shape.
Your code's error
It sounds like you understood this part, but for the benefit of others who may be reading, since your array A doesn't explicitly initialize its elements, it uses the default initializer for wrpd, which will give tsz the value 0 and allocate arr as a degenerate 10x0x0 array. Then assigning a 10xTxT (10x3x3) array to it, the size mismatch error is generated, as it would for a standalone assignment between arrays of those sizes.
The workaround you just posted avoids this by ensuring that the arrays are 10xTxT from the get-go, and it has no major downside other than the additional typing required. It may even be slightly cheaper than some of the proposals I'm about to give, in that the arrays are never re-sized or re-allocated.
The two approaches I'll propose to address this will focus on using records vs. classes, which have various tradeoffs. I'll start with the record-based approach since you did, and I also find it a bit more straightforward (it's what I'd probably use for a case like this, based on what I know so far).
Record-based approaches
A challenge with declaring the record as you did is that the only way to resize an array in Chapel is to re-assign its domain, which requires declaring the domain as a distinct variable. Specifically, there's no way to re-size an array with an anonymous domain, as in your declarations of the arr field and b constant.
For this reason, a common pattern in programs that want to store arrays of varying sizes in records is to store the domain as an explicit field. One variation of your code that I wrote ended up looking like this (ATO):
record wrpd {
var tsz : uint;
var dom = {0..#10, 0..#tsz, 0..#tsz};
var arr : [dom] uint;
}
var A : [1..#32] wrpd;
const T : uint = 3;
const b : [0..#10, 0..#T, 0..#T] uint;
A[1] = new wrpd (tsz=T, arr=b);
writeln(A);
Other than introducing the explicit domain field, dom, and using it to declare the array arr, the main other change I made was to use keyword-based argument passing to provide T and b, skipping over the domain dom (we could've also passed in b.domain as the domain, but that felt like overkill).
Now, when we hit that assignment between the 10x0x0 array of A[1] and the 10x3x3 array, the field-by-field assignment assigns tsz, then dom, which causes arr to be re-allocated, then arr which now matches in size.
The other version I wrote is based on the observation that the tsz field is not particularly necessary, so just declares the dom and arr fields. I also defined explicit initializers in order to simplify the creation of new wrpd objects (ATO):
record wrpd {
var dom : domain(3);
var arr : [dom] uint;
proc init() {
}
proc init(arr: [] uint) where arr.rank == 3 {
this.dom = arr.domain;
this.arr = arr;
}
}
var A : [1..#32] wrpd;
const T : uint = 3;
const b : [0..#10, 0..#T, 0..#T] uint;
A[1] = new wrpd(b);
writeln(A);
In this version, I wanted to support new wrpd(b) without any additional arguments, so wrote a 1-argument initializer that takes an array argument, queries its domain to initialize the dom field (resulting in re-allocation of the arr field), and then assigns the arr field. Then I had to write a 0-argument initializer as well to support the default initialization of A's elements (there's a way to do this using a default value in my one-argument initializer, but that ends up feeling more complicated to me than just the two overloads).
Both of these versions provide the flexibility you were looking for, but with the cost of dynamically re-allocating the array on assignment.
Class-based approaches
The class-based approaches I'll go through next are motivated by your "replace the record with the brand new record" comment. Class objects in Chapel are allocated on the heap, and a class variable points to that heap-allocated object. Assignments between class variables are implemented by updating the LHS variable to point to the object represented by the RHS. As a result, class assignment tends to be more like assigning pointers than copying things field-by-field. And as a result, array fields of classes being assigned no longer need to match in size.
Thus, in your example, if wrpd was a class, A would be an array of 32 pointers to wrpd objects, and assigning to A[1] would just point that array element to the object you created with the new wrpd() call.
A downside to using classes is that they introduce questions of memory management (when should the class object be de-allocated?) and nilability (can class variables contain the nil value, or must they always be pointing to something?). Both of these have tradeoffs in terms of safety and additional code to write.
The set of options we could take here is large (3–4 memory management options x nilability or not) and this mail is already long, so let me just focus on owned classes, in which at most one class variable can refer to any given class object at a time, and when none do, the class will be de-allocated.
One way to do this is using nilable classes, using the following code (ATO):
class wrpd {
var tsz : uint;
var arr : [0..#10, 0..#tsz, 0..#tsz] uint;
}
var A : [1..#32] owned wrpd?;
const T : uint = 3;
const b : [0..#10, 0..#T, 0..#T] uint;
A[1] = new wrpd (T, b);
writeln(A);
Here, I've:
- changed
record to class
- declared
A's element type to be owned wrpd?, which indicates:
- that it will be the sole pointer to the objects in question (
owned)
- that it can store
nil values (indicated by the ?)
When it's initialized, A's elements will all store nil since that's the default value for nilable classes. Having done that, when we assign A[1], it will take ownership of the new object, pointing to it where it was allocated on the heap. And then when A goes out of scope and is de-allocated, the owned object(s) in its elements will be as well.
This nilable approach doesn't seem too bad, but in practice, using nilable classes can be a hassle because extra care has to be taken when reading values to avoid de-referencing nil. Since we're never really reading A's elements in this simple example, we don't really see those impacts here, but you probably would in any real computations on A.
Using non-nilable classes removes the need to worry about nilability, but requires A to be initialized in order have A's elements point to something from the get-go (ATO):
class wrpd {
var tsz : uint;
var arr : [0..#10, 0..#tsz, 0..#tsz] uint;
}
var A : [1..#32] owned wrpd = [1..#32] new owned wrpd();
const T : uint = 3;
const b : [0..#10, 0..#T, 0..#T] uint;
A[1] = new wrpd (T, b);
writeln(A);
(note that the declaration of A could be written more simply using type inference as var A = [1..#32] new owned wrpd();).
Which to use?
The choice between records and classes is important in Chapel since it has implications in terms of memory allocation, deep-vs-shallow copies, local vs. heap accesses, and syntax driven by class memory management and nilability, and which is appropriate depends a lot on the motivating context and author's preferences. This stack overflow Q&A touches on the subject a bit as well.
Hope this helps clarify things, and please let us know if it raises additional questions,
-Brad