GPU621/Chapel
Introduction
The Chapel is an open-source programming language designed for productive parallel computing at scale. The Chapel compiler is written in C and C++14. The backend is LLVM, written in C++. The goal of Chapel is to improve the programmability of parallel computers in general.
Installation
- The release note: https://chapel-lang.org/releaseNotes.html
- Download Page: https://chapel-lang.org/download.html
Basic
Variables
Variables are declared with the var keyword. Variable declarations must have a type, initializer, or both.
var myVariable1: int; writeln("myVariable1 = ", myVariable1);
const and param can be used to declare runtime constants and compile-time constants respectively. A const must be initialized in place, but can have its value generated at runtime. A param must be known at compile time.
const myConst: real = sqrt(myVariable2); param myParam = 3.14; writeln("myConst = ", myConst, ", myParam = ", myParam);
At module scope, all three variable kinds can be qualified by the config keyword. This allows the initial value to be overridden on the command line. A config var or config const may be overridden when the program is executed; a config param may be overridden when the program is compiled. Similarly, type aliases maybe be qualified by the config keyword. The comment following each declaration shows how the value can be modified.
config var cfgVar = "hello"; // ./variables --cfgVar="world" config const cfgConst: bool = false; // ./variables --cfgConst=true config param cfgParam = 4; // chpl variables.chpl -s cfgParam=1 config type cfgType = complex; // chpl variables.chpl -s cfgType=imag writeln("cfgVar = ", cfgVar, ", cfgConst = ", cfgConst, ", cfgParam = ", cfgParam, ", cfgType = ", cfgType:string);
Procedure
The procedures includes overloading, argument intents and dynamic dispatch.
Overloading
The Chapel allows the user to do the overload function and overload operators.
Argument Intents
Normal (default) intent means that a formal argument cannot be modified in the body of a procedure.
- To allow changing the formal (but not the actual), use the in intent
- The inout intent will write back the final value of a formal parameter when the procedure exits.
- Similar to the inout intent, the ref intent causes the value of the actual to change depending on the function. However, while the inout copies the argument in upon entering the function and copies the new value out upon exiting, using a ref intent causes any updates to the formal to immediately affect the call site.
- The out intent causes the actual argument to be ignored when the procedure starts. The actual is assigned the value of the corresponding formal when the routine exits.
Class
Like C++, the Chapel allows the user to create a new type that can contain variables and constants, called fields, as well as functions and iterators called methods. A new class type is declared using the class keyword.
class C { var a, b: int; proc printFields() { writeln("a = ", a, " b = ", b); } }
Record
A record type can be declared using the record keyword. A record is a type that can contain variables and constants, called fields, as well as functions and iterators called methods. Records have many similarities to classes, but there are several important differences:
- Records do not support inheritance and virtual dispatch
- Record variables refer to distinct memory
- copy-initialization and assignment can be implemented for records
record Color { var red: uint(8); var green: uint(8); var blue: uint(8); }
Special Methods
- An initializer named init is called when creating an instance of the class or record.
- A method named init= is called a copy initializer and accepts a single argument.
- If a method named postinit that accepts zero arguments exists for a class or record type, it will automatically be called after the initializer call completes.
- The deinit method will deinitialize a record when it leaves scope, or a class when delete is called on it.
- The this method gives the record the ability to be accessed like an array.
- An iterator named these that can accept zero arguments is automatically called when a record or class instance is used in the iterator position of a for loop.
- The writeThis and readThis methods allows user to read or write the instance.
Nothing Variable
A nothing is removed by the compiler and doesn’t exist during program execution. The value none has type nothing and can be assigned to a nothing variable, or returned from a function with nothing return type.
var nothingVar: nothing;
The value none is the only value of the nothing type.
var nothingVar2 = none;
The following (commented out) statements are compile time errors:
nothingVar = 1;
Parallel Iterators
Chapel has two main types of parallel iterators:
- Standalone parallel iterators are the simpler form and can be used to define parallelism for a simple forall loop.
iter fibonacci(n: int): int { var i1 = 0, i2 = 1; for i in 1..#n { yield i1; var i3 = i1 + i2; i1 = i2; i2 = i3; } } config const n =10; for (i,idVar) in zip(0..n, fibonacci(n)) do writeln("#", i, " - ", idVar);
- Leader-follower iterators are a more involved form that support zippered forall loops.
forall (a, b, c) in zip(A, B, C) do //in here, A is the leader; and A, B, C is followers
Task Parallelism
Chapel’s parallel tasking features, namely the begin, cobegin, and coforall statements.
- The begin statement spawns a thread of execution that is independent of the current (main) thread of execution.
writeln("1: ### The begin statement ###"); begin writeln("1: output from spawned task");
- The cobegin statement can be used to spawn a block of tasks, one for each statement.
writeln("2: ### The cobegin statement ###"); cobegin { writeln("2: output from spawned task 1"); writeln("2: output from spawned task 2"); }
- This loop form is like a for loop in which each iteration of the loop is executed by a distinct task.
writeln("4: ### The coforall loop ###"); coforall i in 1..n { writeln("4: output from spawned task 1 (iteration ", i, ")"); writeln("4: output from spawned task 2 (iteration ", i, ")"); }
Sync/Singles
Sync and single are type qualifiers that can be applied to all Chapel primitive types except strings and complex. Sync and single variables have an associated state that is either full or empty. Single variables are sync variables that can only be written once. If a sync or single variable is declared with an initializing expression, its state is set to full and its value is that of the expression. Without an initializing expression, a sync or single variable’s state is set to empty and its value is the default value for the base type.
config const n = 7; var sy$: sync int=1; // state = full, value = 1 var si$: single bool; // state = empty, value = false
The methods defined on sync / single variables:
- The reset() method, defined for sync variables, sets the value of the variable to the default value for the type and the state to empty.
- The isFull method returns true if the sync or single variable is in the full state, false otherwise.
- The writeEF() method, defined for sync and single variables, blocks until the state is empty and then assigns the value argument to the variable and then sets the state to full.
- The readFE() method, defined for sync variables, blocks until the state is full and then reads the value of the variable, sets the state to empty, and then returns the value.
- The readFF() method, defined for sync and single variables, blocks until the state is full and then reads the value of the variable and returns the value.
- The writeXF() method, defined for sync variables, assigns the value argument to the variable and then sets the state to full
- The readXX() method, defined for sync and single variables, returns the value of the variable regardless of the state.
- The writeFF() method, defined for sync variables, blocks until the state is full and then and then assigns the value argument to the variable.
Locales
In Chapel, the locale type refers to a unit of the machine resources on which your program is running. Locales have the capability to store variables and to run Chapel tasks.
Data Parallelism
Range
Ranges represent regular sequences of values, such as integers, and are typically defined in terms of a low and high bound.
const r = 1..10, // 1, 2, 3, ..., 10 r2 = 0..n, // 0, 1, 2, ..., n r3 = -3..3; // -3, -2, -1, ..., 3
Domain
A domain is a first-class representation of an index set used to specify iteration spaces, define arrays, and aggregate operations such as slicing.
var RD: domain(3) = {1..n, 1..n, 1..n};
- The expand method returns a new domain that is expanded or contracted depending on the sign of the offset argument.
- The exterior method returns a new domain that is the exterior portion of the current domain.
- The interior method returns a new domain that is the interior portion of the current domain.
- The translate method returns a new domain that is the current domain translated by the offset.
- A subdomain is a domain that is declared in terms of a parent domain, causing it to have the same type as their parent. A subdomain represents a subset of its parent domain’s index set, though this constraint is not currently enforced by the implementation.
Array
Arrays in Chapel are specified using a square-bracketed expression that specifies the array’s index set, followed by the array’s element type.
config const n = 5;
Forall loop
The forall loop is a way to leverage data parallelism or engage user-defined parallel iterators.
config const n = 5; var A: [1..n] real; forall i in 1..n { A[i] = i; }
Library Utilities
File I/O
config var n = 9, filename = "Arr.dat"; config const num = 128*1024; config const example = 0; config const testfile = "test.bin"; config const epsilon = 10e-13; use FileSystem; use IO;
It initializes an array and writes its size and data to a file. It then opens the file, uses the size in the file to declare a new domain and array, and reads in the array data.
if example == 0 || example == 1 { // Create a domain of the specified problem size. const ADom = {1..n, 1..n}; // Create and initialize an array of the specified size. var A: [ADom] real = [(i,j) in ADom] i + j/10.0; // Write the problem size and array out to the specified filename. writeSquareArray(n, A, filename); // Read an array in from the specified filename, storing in a new variable, B. var B = readArray(filename); // Print out B as a debugging step. writeln("B is:\n", B); // Verify that the values in A and B are within tolerance. const numErrors = + reduce [i in ADom] (abs(A(i) - B(i)) > epsilon); if (numErrors > 0) { writeln("FAILURE"); } else { writeln("SUCCESS"); } }
This procedure writes a square array out to a file.
proc writeSquareArray(n, X, filename) { // Create and open an output file with the specified filename in write mode. var outfile = open(filename, iomode.cw); var writer = outfile.writer(); // Write the problem size in each dimension to the file. writer.writeln(n, " ", n); // Write out the array itself. writer.write(X); // Close the file. writer.close(); outfile.close(); }
This procedure reads a new array out of a file and returns it.
proc readArray(filename) { // Open an input file with the specified filename in read mode. var infile = open(filename, iomode.r); var reader = infile.reader(); // Read the number of rows and columns in the array in from the file. var m = reader.read(int), n = reader.read(int); // Declare an array of the specified dimensions. var X: [1..m, 1..n] real; // Read in the array (row-major order is used for whole-array reads // like this). reader.read(X); // Close the file. reader.close(); infile.close(); // Return the array. return X; }
List
The Chapel List module provides the implementation of the list type. Lists are useful for building up and iterating over a collection of values in a structured manner.
private use List; config const quiet: bool = false;
- The append() method appends the following element into the array.
- The sort() method sorts the list in ascending order.
- The pop() method pops the element at the specified index.
- The clear() clears all elements from the list.
- The indexOf() retrieves the index of the value specified, returns -1 if not found.
- The insert() method inserts the value at the specified index.
Timer
The Chapel allows user to use Timer from the timer module.
use Time; /* Create a Timer t */ var t: Timer; t.start(); writeln(“Operation start”); sleep(5); writeln(“Operation end”); t.stop(); /* return time in milliseconds that was recorded by the timer */ writeln(t.elapsed(TimeUnits.milliseconds)); t.clear();
Presentation
Reference
- The Chapel Programming Language Offical Site: https://chapel-lang.org/
- The Chapel Introduction Vidro: https://www.youtube.com/watch?v=7Qk8T7_bevo
- The Chapel Documentation: https://chapel-lang.org/docs/#
- Full-Length Chapel Tutorial: https://chapel-lang.org/tutorials.html
- Specific Chapel Concepts or Features: https://chapel-lang.org/docs/primers/
- The Chapel Promotional Video: https://youtu.be/2yye1yJPcsg
- The Chapel Overview Talk Video: https://youtu.be/ko11tLuchvg
- The Chapel Overview Talk Slide: https://chapel-lang.org/presentations/ChapelForHPCKM-presented.pdf
- Comparative Performance and Optimization of Chapel: https://chapel-lang.org/CHIUW/2017/kayraklioglu-slides.pdf
- The Parallel Research Kernels: https://www.nas.nasa.gov/assets/pdf/ams/2016/AMS_20161013_VanDerWijngaart.pdf
- The Chapel Tutorial: https://www.youtube.com/watch?v=QiyLkfYHEvg
- The Chapel Tutorial: https://www.youtube.com/watch?v=kCI0riVEjlk