Difference between revisions of "GPU621/Chapel"
Lhuangtang (talk | contribs) (→Procedure) |
Lhuangtang (talk | contribs) (→Procedure) |
||
Line 42: | Line 42: | ||
*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. | *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. | *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 prime 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. | Like C++, the prime 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 { | class C { | ||
Line 50: | Line 50: | ||
} | } | ||
} | } | ||
− | + | == 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: | 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 | *Records do not support inheritance and virtual dispatch | ||
Line 61: | Line 61: | ||
} | } | ||
− | + | == Special Methods == | |
*An initializer named init is called when creating an instance of the class or record. | *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. | *A method named init= is called a copy initializer and accepts a single argument. | ||
Line 70: | Line 70: | ||
*The writeThis and readThis methods allows user to read or write the instance. | *The writeThis and readThis methods allows user to read or write the instance. | ||
− | + | == Nothing Variable == | |
The primer demonstrates the use of the nothing type. 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. | The primer demonstrates the use of the nothing type. 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; | var nothingVar: nothing; |
Revision as of 22:27, 29 November 2022
Contents
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 primer covers procedures including overloading, argument intents and dynamic dispatch.
Overloading
The prime 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 prime 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
The primer demonstrates the use of the nothing type. 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;
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