13 . More Chare Array Features

The basic array features described previously (creation, messaging, broadcasts, and reductions) are needed in almost every Charm++ program. The more advanced techniques that follow are not universally needed, but represent many useful optimizations.

13 . 1 Local Access

It is possible to get direct access to a local array element using the proxy's ckLocal method, which returns an ordinary C++ pointer to the element if it exists on the local processor, and NULL if the element does not exist or is on another processor.

 A1 *a=a1[i].ckLocal();

if (a==NULL) // remote- send message

else // local- directly use members and methods of a

Note that if the element migrates or is deleted, any pointers obtained with ckLocal are no longer valid. It is best, then, to either avoid ckLocal or else call ckLocal each time the element may have migrated; e.g., at the start of each entry method. An example of this usage is available in examples/charm++/topology/matmul3d .

13 . 2 Advanced Array Creation

There are several ways to control the array creation process. You can adjust the map and bindings before creation, change the way the initial array elements are created, create elements explicitly during the computation, and create elements implicitly, ``on demand''. You can create all of an arrays elements using any one of these methods, or create different elements using different methods. An array element has the same syntax and semantics no matter how it was created.

13 . 2 . 1 Configuring Array Characteristics Using CkArrayOptions

The array creation method ckNew actually takes a parameter of type CkArrayOptions . This object describes several optional attributes of the new array. The most common form of CkArrayOptions is to set the number of initial array elements. A CkArrayOptions object will be constructed automatically in this special common case. Thus the following code segments all do exactly the same thing:

 //Implicit CkArrayOptions

//Explicit CkArrayOptions

//Separate CkArrayOptions
  CkArrayOptions opts(nElements);

Note that the ``numElements'' in an array element is simply the numElements passed in when the array was created. The true number of array elements may grow or shrink during the course of the computation, so numElements can become out of date. This ``bulk'' constructor approach should be preferred where possible, especially for large arrays. Bulk construction is handled via a broadcast which will be significantly more efficient in the number of messages required than inserting each element individually, which will require one message send per element.

Examples of bulk construction are commonplace, see examples/charm++/jacobi3d-sdag for a demonstration of the slightly more complicated case of multidimensional chare array bulk construction.

CkArrayOptions can also be used for bulk creation of sparse arrays when the sparsity of the array can be described in terms of a start index, an end index, and a step index. The start, end, and step can either be passed into the CkArrayOptions constructor, or set one at a time. The following shows two different ways to create CkArrayOptions for a 2D array with only the odd indices from (1,1) to (10,10) being populated:

 // Set at construction

CkArrayOptions options(CkArrayIndex2D(1,1),

// Set one at a time

CkArrayOptions options;


The default for start is $0^d$ and the default for step is $1^d$ (where $d$ is the dimension of the array), so the following are equivalent:

 // Specify just the number of elements

CkArrayOptions options(nElements);

// Specify just the end index

CkArrayOptions options;


// Specify all three indices

CkArrayOptions options;


In addition to controlling how many elements and at which indices to create them, CkArrayOptions contains a few flags that the runtime can use to optimize handling of a given array. If the array elements will only migrate at controlled points (such as periodic load balancing with AtASync() ), this is signaled to the runtime by calling opts.setAnytimeMigration(false) 13 . 1 . If all array elements will be inserted by bulk creation or by fooArray[x].insert() calls, signal this by calling opts.setStaticInsertion(true) 13 . 2 .

13 . 2 . 2 Initial Placement Using Map Objects

You can use CkArrayOptions to specify a ``map object'' for an array. The map object is used by the array manager to determine the ``home'' PE of each element. The home PE is the PE upon which it is initially placed, which will retain responsibility for maintaining the location of the element.

There is a default map object, which maps 1D array indices in a block fashion to processors, and maps other array indices based on a hash function. Some other mappings such as round-robin ( RRMap ) also exist, which can be used similar to custom ones described below.

A custom map object is implemented as a group which inherits from CkArrayMap and defines these virtual methods:

 class CkArrayMap : public Group

  //Return an ``arrayHdl'', given some information about the array
  virtual int registerArray(CkArrayIndex& numElements,CkArrayID aid);
  //Return the home processor number for this element of this array
  virtual int procNum(int arrayHdl,const CkArrayIndex &element);

For example, a simple 1D blockmapping scheme. Actual mapping is handled in the procNum function.

 class BlockMap : public CkArrayMap 
  BlockMap(void) {}
  BlockMap(CkMigrateMessage *m){}
  int registerArray(CkArrayIndex& numElements,CkArrayID aid) {
    return 0;
  int procNum(int /*arrayHdl*/,const CkArrayIndex &idx) {
    int elem=*(int *);
    int penum =  (elem/(32/CkNumPes()));
    return penum;

Note that the first argument to the procNum method exists for reasons internal to the runtime system and is not used in the calculation of processor numbers.

Once you've instantiated a custom map object, you can use it to control the location of a new array's elements using the setMap method of the CkArrayOptions object described above. For example, if you've declared a map object named ``BlockMap'':

 //Create the map group
  CProxy_BlockMap myMap=CProxy_BlockMap::ckNew();
//Make a new array using that map
  CkArrayOptions opts(nElements);

An example which constructs one element per physical node may be found in examples/charm++/PUP/pupDisk

Other 3D Torus network oriented map examples are in examples/charm++/topology

13 . 2 . 3 Initial Elements

The map object described above can also be used to create the initial set of array elements in a distributed fashion. An array's initial elements are created by its map object, by making a call to populateInitial on each processor.

You can create your own set of elements by creating your own map object and overriding this virtual function of CkArrayMap :

   virtual void populateInitial(int arrayHdl,int numInitial,
 void *msg,CkArray *mgr)

In this call, arrayHdl is the value returned by registerArray , numInitial is the number of elements passed to CkArrayOptions , msg is the constructor message to pass, and mgr is the array to create.

populateInitial creates new array elements using the method void CkArray::insertInitial(CkArrayIndex idx,void *ctorMsg) . For example, to create one row of 2D array elements on each processor, you would write:

 void xyElementMap::populateInitial(int arrayHdl,int numInitial,
 void *msg,CkArray *mgr)
  if (numInitial==0) return; //No initial elements requested
  //Create each local element
  int y=CkMyPe();
  for (int x=0;x<numInitial;x++) {

Thus calling ckNew(10) on a 3-processor machine would result in 30 elements being created.

13 . 2 . 4 Bound Arrays

You can ``bind'' a new array to an existing array using the bindTo method of CkArrayOptions . Bound arrays act like separate arrays in all ways except for migration- corresponding elements of bound arrays always migrate together. For example, this code creates two arrays A and B which are bound together- A[i] and B[i] will always be on the same processor.

 //Create the first array normally
//Create the second array bound to the first
  CkArrayOptions opts(nElements);

An arbitrary number of arrays can be bound together- in the example above, we could create yet another array C and bind it to A or B. The result would be the same in either case- A[i], B[i], and C[i] will always be on the same processor.

There is no relationship between the types of bound arrays- it is permissible to bind arrays of different types or of the same type. It is also permissible to have different numbers of elements in the arrays, although elements of A which have no corresponding element in B obey no special semantics. Any method may be used to create the elements of any bound array.

Bound arrays are often useful if A[i] and B[i] perform different aspects of the same computation, and thus will run most efficiently if they lie on the same processor. Bound array elements are guaranteed to always be able to interact using ckLocal (see section  13.1 ), although the local pointer must be refreshed after any migration. This should be done during the pup routine. When migrated, all elements that are bound together will be created at the new processor before pup is called on any of them, ensuring that a valid local pointer to any of the bound objects can be obtained during the pup routine of any of the others.

For example, an array Alibrary is implemented as a library module. It implements a certain functionality by operating on a data array dest which is just a pointer to some user provided data. A user defined array UserArray is created and bound to the array Alibrary to take advantage of the functionality provided by the library. When bound array element migrated, the data pointer in UserArray is re-allocated in pup() , thus UserArray is responsible to refresh the pointer dest stored in Alibrary .

 class Alibrary: public CProxy_Alibrary {

  void set_ptr(double *ptr) { dest = ptr; }
  virtual void pup(PUP::er &p);

  double *dest;           // point to user data in user defined bound array

class UserArray: public CProxy_UserArray {

  virtual void pup(PUP::er &p) {
                if(p.isUnpacking()) { 
                  data = new double[len];
                  Alibrary *myfellow = AlibraryProxy(thisIndex).ckLocal();
                  myfellow->set_ptr(data);    // refresh data in bound array
                p(data, len);

  CProxy_Alibrary  AlibraryProxy;   // proxy to my bound array
  double *data;          // user allocated data pointer
  int len;

A demonstration of bound arrays can be found in tests/charm++/startupTest

13 . 2 . 5 Dynamic Insertion

In addition to creating initial array elements using ckNew, you can also create array elements during the computation.

You insert elements into the array by indexing the proxy and calling insert. The insert call optionally takes parameters, which are passed to the constructor; and a processor number, where the element will be created. Array elements can be inserted in any order from any processor at any time. Array elements need not be contiguous.

If using insert to create all the elements of the array, you must call CProxy_Array::doneInserting before using the array.

 //In the .C file:

int x,y,z;

CProxy_A1 a1=CProxy_A1::ckNew();  //Creates a new, empty 1D array

for (x=...) {
   a1[x  ].insert(parameters);  //Bracket syntax
   a1(x+1).insert(parameters);  // or equivalent parenthesis syntax


CProxy_A2 a2=CProxy_A2::ckNew();   //Creates 2D array

for (x=...) for (y=...)
   a2(x,y).insert(parameters);  //Can't use brackets!


CProxy_A3 a3=CProxy_A3::ckNew();   //Creates 3D array

for (x=...) for (y=...) for (z=...)


CProxy_AF aF=CProxy_AF::ckNew();   //Creates user-defined index array

for (...) {
   aF[CkArrayIndexFoo(...)].insert(parameters); //Use brackets...
   aF(CkArrayIndexFoo(...)).insert(parameters); //  ...or parenthesis


The doneInserting call starts the reduction manager (see ``Array Reductions'') and load balancer (see  7.1 )- since these objects need to know about all the array's elements, they must be started after the initial elements are inserted. You may call doneInserting multiple times, but only the first call actually does anything. You may even insert or destroy elements after a call to doneInserting , with different semantics- see the reduction manager and load balancer sections for details.

If you do not specify one, the system will choose a processor to create an array element on based on the current map object.

A demonstration of dynamic insertion is available: examples/charm++/hello/fancyarray

13 . 2 . 6 Demand Creation

Demand Creation is a specialized form of dynamic insertion. Normally, invoking an entry method on a nonexistent array element is an error. But if you add the attribute [createhere] or [createhome] to an entry method, the array manager will ``demand create'' a new element to handle the message.

With [createhome] , the new element will be created on the home processor, which is most efficient when messages for the element may arrive from anywhere in the machine. With [createhere] , the new element is created on the sending processor, which is most efficient if when messages will often be sent from that same processor.

The new element is created by calling its default (taking no parameters) constructor, which must exist and be listed in the .ci file. A single array can have a mix of demand-creation and classic entry methods; and demand-created and normally created elements.

A simple example of demand creation tests/charm++/demand_creation

13 . 2 . 7 Asynchronous Array Creation

Normally, CProxy_Array::ckNew call must always be made from PE 0. However, asynchronous array creation can be used to lift this restriction and let the array creation be made from any PE. To do this, CkCallback must be given as an argument for ckNew to provide the created chare array's CkArrayID to the callback function.

 CProxy_SomeProxy::ckNew(parameters, nElements, CkCallback(CkIndex_MyClass::someFunction(NULL), thisProxy));

void someFunction(CkArrayCreatedMsg *m) {
    CProxy_AnotherProxy(m->aid)[index].myFunction();    // m->aid is CkArrayID
    delete m;

Similar to the standard array creation method, arguments to the array element constructor calls are taken first, followed by the dimensions of the array. Note that the parameters field can be optional, if the default constructor is expected to be used.

Alternatively, CkArrayOptions can be used in place of nElements to further configure array characteristics.

 // Creating a 3-dimensional chare array with 2 parameters

CkArrayOptions options(dimX, dimY, dimZ);

CProxy_AnotherProxy::ckNew(param1, param2, options, CkCallback(CkIndex_SomeClass::anotherFunction(NULL), thisProxy));

A demonstration of asynchronous array creation can be found in examples/charm++/hello/darray

13 . 3 User-defined Array Indices

Charm++ array indices are arbitrary collections of integers. To define a new array index, you create an ordinary C++ class which inherits from CkArrayIndex and sets the ``nInts'' member to the length, in integers, of the array index.

For example, if you have a structure or class named ``Foo'', you can use a Foo object as an array index by defining the class:

 #include <charm++.h>

class CkArrayIndexFoo:public CkArrayIndex {
    Foo f;

    CkArrayIndexFoo(const Foo &in) 
    //Not required, but convenient: cast-to-foo operators
    operator Foo &() {return f;}
    operator const Foo &() const {return f;}

Note that Foo's size must be an integral number of integers- you must pad it with zero bytes if this is not the case. Also, Foo must be a simple class- it cannot contain pointers, have virtual functions, or require a destructor. Finally, there is a Charm++ configuration-time option called CK_ARRAYINDEX_MAXLEN which is the largest allowable number of integers in an array index. The default is 3; but you may override this to any value by passing ``-DCK_ARRAYINDEX_MAXLEN=n'' to the Charm++ build script as well as all user code. Larger values will increase the size of each message.

You can then declare an array indexed by Foo objects with

 //in the .ci file:

array [Foo] AF { entry AF(); ... }

//in the .h file:

class AF : public CBase_AF
{ public: AF() {} ... }

//in the .C file:
    Foo f;
    CProxy_AF a=CProxy_AF::ckNew();

Note that since our CkArrayIndexFoo constructor is not declared with the explicit keyword, we can equivalently write the last line as:


When you implement your array element class, as shown above you can inherit from CBase _ClassName, a class templated by the index type Foo. In the old syntax, you could also inherit directly from ArrayElementT . The array index (an object of type Foo) is then accessible as ``thisIndex''. For example:

//in the .C file:

    Foo myF=thisIndex;

A demonstration of user defined indices can be seen in examples/charm++/hello/fancyarray