C++ interface

The "Chunk" and "Task" base classes

In the C++ realization of the Chunks and Tasks model, the user defines chunk types and task types by creating new classes inheriting from the "Chunk" and "Task" base classes, as shown in the figure below.


Logo

Defining a chunk type

To define a new chunk type, the user creates a class inheriting from the cht::Chunk base class, for example like this:

struct CInt: public cht::Chunk { // Functions required for a Chunk void writeToBuffer(char * dataBuffer, size_t const bufferSize) const; size_t getSize() const; void assignFromBuffer(char const * dataBuffer, size_t const bufferSize); // CInt specific functionality CInt(int x_) : x(x_) { } CInt() { } operator int() const { return x; } private: int x; // The number itself CHT_CHUNK_TYPE_DECLARATION; };

The writeToBuffer() and assignFromBuffer() routines need to be provided so that the used Chunks and Tasks library (the runtime library) can serialize chunks if needed, e.g. to send chunks over the network if the program is run on a distributed memory cluster.

Defining a task type

To define a new task type, the user creates a class inheriting from the cht::Task base class, for example like this:

struct Add: public cht::Task { cht::ID execute(CInt const & n1, CInt const & n2); CHT_TASK_INPUT((CInt, CInt)); CHT_TASK_OUTPUT((CInt)); CHT_TASK_TYPE_DECLARATION; };

The work to be performed by the task is specified by implementing the execute() function, for example like this:

cht::ID Add::execute(CInt const & n1, CInt const & n2) { CInt result_chunk = n1+n2; cht::ChunkID cid_result = registerChunk(new CInt(result_chunk)); return cid_result; }

Inside the execute() function, new chunks may be registered using the registerChunk() function, as in the example above. The execute() function can also include registration of new tasks using the registerTask() function.