Array distribution in the Chapel

Unlike other languages, in Chapel there is no allocate or new syntax to allocate arrays on the heap, but rather the usual "declaration" syntax. For example, in the following code, I “declare” two arrays A and B in a function based on formal (dummy) arguments:

 proc test( n, D ) { var A: [1..n] real; // local array var B: [D] real; // local array writeln( "A.domain = ", A.domain ); writeln( "B.domain = ", B.domain ); } test( 3, {2..5} ); // request small arrays test( 10**7, {-10**7..10**7} ); // request large arrays 

This gives the following result:

 A.domain = {1..3} B.domain = {2..5} A.domain = {1..10000000} B.domain = {-10000000..10000000} 

Due to the absence (despite the large size of B ), is it possible to assume that the above syntax always allocates A and B on the heap regardless of their size?

In addition, the assignment (or reassignment) of a domain variable, apparently, plays the role of distribution (or redistribution) of the array. For example, the following code works as expected. In this case, the distribution always occurs on the heap (again)?

 var domC: domain(1); var C: [domC] real; writeln( "C = ", C ); domC = { 1..3 }; // assign a domain C = 7.0; // assign some value to the array writeln( "C = ", C ); domC = { -1..5 }; // re-assign a domain writeln( "C = ", C ); 

Result:

 C = C = 7.0 7.0 7.0 C = 0.0 0.0 7.0 7.0 7.0 0.0 0.0 

Finally, does the user need to use deallocate or delete these arrays manually, but does the system automatically free them if necessary?

+5
source share
1 answer

Can we assume that the above syntax always allocates A and B on the heap regardless of their size?

In Chapel 1.15.0, chapel array elements are always allocated on the heap. We discussed adding mechanisms (for example, custom domain maps) or, possibly, optimizations that can be used to store array elements on the stack when necessary, but have not yet pursued such functions. Note that while array elements are allocated on the heap, arrays are also implemented using a run-time descriptor that is allocated "in place" (for example, on the stack in your examples) --- this descriptor refers to elements allocated by the heap.

In addition, the assignment (or reassignment) of a domain variable, apparently, plays the role of distribution (or redistribution) of the array.

This is correct, and it is worth emphasizing that this is a logical and not a physically oriented concept of redistribution. In particular, when the array domain is reassigned, A[i] will continue to keep the same value if i was both in the index sets of the old domain and in the new one. Therefore, when you changed domC from {1..3} to {-1..5} in your code above, A[1..3] was saved, since it represents the intersection of two sets.

Is distribution always performed on the heap (again)?

Yes, as with the original distribution of the array.

Does the user need to delete or delete these arrays manually, and does the system automatically free them if necessary?

That's right, array memory management is usually handled by the implementation. By manually managing the memory, the array will have a class variable with an array field (since classes are manually managed).

+3
source

Source: https://habr.com/ru/post/1268576/


All Articles