mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-05 14:28:31 +00:00
Merge pull request #1183 from ian-bertolacci/master
[Chapel] Modules, main(), range/domain/array slicing and array assignment, loop expressions, zipped iterators
This commit is contained in:
commit
d1874637c7
@ -24,7 +24,7 @@ writeln( "World!" );
|
|||||||
// each thing is printed right next to each other, so include your spacing!
|
// each thing is printed right next to each other, so include your spacing!
|
||||||
writeln( "There are ", 3, " commas (\",\") in this line of code" );
|
writeln( "There are ", 3, " commas (\",\") in this line of code" );
|
||||||
// Different output channels
|
// Different output channels
|
||||||
stdout.writeln( "This goes to standard output (just like plain writeln( ) does)");
|
stdout.writeln( "This goes to standard output, just like plain writeln() does");
|
||||||
stderr.writeln( "This goes to standard error" );
|
stderr.writeln( "This goes to standard error" );
|
||||||
|
|
||||||
// Variables don't have to be explicitly typed as long as
|
// Variables don't have to be explicitly typed as long as
|
||||||
@ -285,6 +285,7 @@ for i in rangeCountBy{
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Rectangular domains are defined using the same range syntax
|
// Rectangular domains are defined using the same range syntax
|
||||||
|
// However they are required to be bounded (unlike ranges)
|
||||||
var domain1to10: domain(1) = {1..10}; // 1D domain from 1..10;
|
var domain1to10: domain(1) = {1..10}; // 1D domain from 1..10;
|
||||||
var twoDimensions: domain(2) = {-2..2,0..2}; // 2D domain over product of ranges
|
var twoDimensions: domain(2) = {-2..2,0..2}; // 2D domain over product of ranges
|
||||||
var thirdDim: range = 1..16;
|
var thirdDim: range = 1..16;
|
||||||
@ -310,6 +311,18 @@ stringSet += "a"; // Redundant add "a"
|
|||||||
stringSet -= "c"; // Remove "c"
|
stringSet -= "c"; // Remove "c"
|
||||||
writeln( stringSet );
|
writeln( stringSet );
|
||||||
|
|
||||||
|
// Both ranges and domains can be sliced to produce a range or domain with the
|
||||||
|
// intersection of indices
|
||||||
|
var rangeA = 1.. ; // range from 1 to infinity
|
||||||
|
var rangeB = ..5; // range from negative infinity to 5
|
||||||
|
var rangeC = rangeA[rangeB]; // resulting range is 1..5
|
||||||
|
writeln( (rangeA, rangeB, rangeC ) );
|
||||||
|
|
||||||
|
var domainA = {1..10, 5..20};
|
||||||
|
var domainB = {-5..5, 1..10};
|
||||||
|
var domainC = domainA[domainB];
|
||||||
|
writeln( (domainA, domainB, domainC) );
|
||||||
|
|
||||||
// Array are similar to those of other languages.
|
// Array are similar to those of other languages.
|
||||||
// Their sizes are defined using domains that represent their indices
|
// Their sizes are defined using domains that represent their indices
|
||||||
var intArray: [1..10] int;
|
var intArray: [1..10] int;
|
||||||
@ -357,6 +370,48 @@ var dict: [dictDomain] int = [ "one" => 1, "two" => 2 ];
|
|||||||
dict["three"] = 3;
|
dict["three"] = 3;
|
||||||
for key in dictDomain do writeln( dict[key] );
|
for key in dictDomain do writeln( dict[key] );
|
||||||
|
|
||||||
|
// Arrays can be assigned to each other in different ways
|
||||||
|
var thisArray : [{0..5}] int = [0,1,2,3,4,5];
|
||||||
|
var thatArray : [{0..5}] int;
|
||||||
|
|
||||||
|
// Simply assign one to the other.
|
||||||
|
// This copies thisArray into thatArray, instead of just creating a reference.
|
||||||
|
// Modifying thisArray does not also modify thatArray.
|
||||||
|
thatArray = thisArray;
|
||||||
|
thatArray[1] = -1;
|
||||||
|
writeln( (thisArray, thatArray) );
|
||||||
|
|
||||||
|
// Assign a slice one array to a slice (of the same size) of the other.
|
||||||
|
thatArray[{4..5}] = thisArray[{1..2}];
|
||||||
|
writeln( (thisArray, thatArray) );
|
||||||
|
|
||||||
|
// Operation can also be promoted to work on arrays.
|
||||||
|
var thisPlusThat = thisArray + thatArray;
|
||||||
|
writeln( thisPlusThat );
|
||||||
|
|
||||||
|
// Arrays and loops can also be expressions, where loop
|
||||||
|
// body's expression is the result of each iteration.
|
||||||
|
var arrayFromLoop = for i in 1..10 do i;
|
||||||
|
writeln( arrayFromLoop );
|
||||||
|
|
||||||
|
// An expression can result in nothing,
|
||||||
|
// such as when filtering with an if-expression
|
||||||
|
var evensOrFives = for i in 1..10 do if (i % 2 == 0 || i % 5 == 0) then i;
|
||||||
|
|
||||||
|
writeln( arrayFromLoop );
|
||||||
|
|
||||||
|
// Or could be written with a bracket notation
|
||||||
|
// Note: this syntax uses the 'forall' parallel concept discussed later.
|
||||||
|
var evensOrFivesAgain = [ i in 1..10 ] if (i % 2 == 0 || i % 5 == 0) then i;
|
||||||
|
|
||||||
|
// Or over the values of the array
|
||||||
|
arrayFromLoop = [ value in arrayFromLoop ] value + 1;
|
||||||
|
|
||||||
|
// Note: this notation can get somewhat tricky. For example:
|
||||||
|
// evensOrFives = [ i in 1..10 ] if (i % 2 == 0 || i % 5 == 0) then i;
|
||||||
|
// would break.
|
||||||
|
// The reasons for this are explained in depth when discussing zipped iterators.
|
||||||
|
|
||||||
// Chapel procedures have similar syntax to other languages functions.
|
// Chapel procedures have similar syntax to other languages functions.
|
||||||
proc fibonacci( n : int ) : int {
|
proc fibonacci( n : int ) : int {
|
||||||
if ( n <= 1 ) then return n;
|
if ( n <= 1 ) then return n;
|
||||||
@ -533,6 +588,19 @@ iter oddsThenEvens( N: int ): int {
|
|||||||
for i in oddsThenEvens( 10 ) do write( i, ", " );
|
for i in oddsThenEvens( 10 ) do write( i, ", " );
|
||||||
writeln( );
|
writeln( );
|
||||||
|
|
||||||
|
// Iterators can also yield conditionally, the result of which can be nothing
|
||||||
|
iter absolutelyNothing( N ): int {
|
||||||
|
for i in 1..N {
|
||||||
|
if ( N < i ) { // Always false
|
||||||
|
yield i; // Yield statement never happens
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in absolutelyNothing( 10 ){
|
||||||
|
writeln( "Woa there! absolutelyNothing yielded ", i );
|
||||||
|
}
|
||||||
|
|
||||||
// We can zipper together two or more iterators (who have the same number
|
// We can zipper together two or more iterators (who have the same number
|
||||||
// of iterations) using zip() to create a single zipped iterator, where each
|
// of iterations) using zip() to create a single zipped iterator, where each
|
||||||
// iteration of the zipped iterator yields a tuple of one value yielded
|
// iteration of the zipped iterator yields a tuple of one value yielded
|
||||||
@ -541,6 +609,34 @@ writeln( );
|
|||||||
for (positive, negative) in zip( 1..5, -5..-1) do
|
for (positive, negative) in zip( 1..5, -5..-1) do
|
||||||
writeln( (positive, negative) );
|
writeln( (positive, negative) );
|
||||||
|
|
||||||
|
// Zipper iteration is quite important in the assignment of arrays,
|
||||||
|
// slices of arrays, and array/loop expressions.
|
||||||
|
var fromThatArray : [1..#5] int = [1,2,3,4,5];
|
||||||
|
var toThisArray : [100..#5] int;
|
||||||
|
|
||||||
|
// The operation
|
||||||
|
toThisArray = fromThatArray;
|
||||||
|
// is produced through
|
||||||
|
for (i,j) in zip( toThisArray.domain, fromThatArray.domain) {
|
||||||
|
toThisArray[ i ] = fromThatArray[ j ];
|
||||||
|
}
|
||||||
|
|
||||||
|
toThisArray = [ j in -100..#5 ] j;
|
||||||
|
writeln( toThisArray );
|
||||||
|
// is produced through
|
||||||
|
for (i, j) in zip( toThisArray.domain, -100..#5 ){
|
||||||
|
toThisArray[i] = j;
|
||||||
|
}
|
||||||
|
writeln( toThisArray );
|
||||||
|
|
||||||
|
// This is all very important in undestanding why the statement
|
||||||
|
// var iterArray : [1..10] int = [ i in 1..10 ] if ( i % 2 == 1 ) then j;
|
||||||
|
// exhibits a runtime error.
|
||||||
|
// Even though the domain of the array and the loop-expression are
|
||||||
|
// the same size, the body of the expression can be though of as an iterator.
|
||||||
|
// Because iterators can yield nothing, that iterator yields a different number
|
||||||
|
// of things than the domain of the array or loop, which is not allowed.
|
||||||
|
|
||||||
// Classes are similar to those in C++ and Java.
|
// Classes are similar to those in C++ and Java.
|
||||||
// They currently lack privatization
|
// They currently lack privatization
|
||||||
class MyClass {
|
class MyClass {
|
||||||
@ -673,12 +769,64 @@ var copyNewTypeList = new GenericClass( realList, int );
|
|||||||
for value in copyNewTypeList do write( value, ", " );
|
for value in copyNewTypeList do write( value, ", " );
|
||||||
writeln( );
|
writeln( );
|
||||||
|
|
||||||
|
// Modules are Chapel's way of managing name spaces.
|
||||||
|
// The files containing these modules do not need to be named after the modules
|
||||||
|
// (as in Java), but files implicitly name modules.
|
||||||
|
// In this case, this file implicitly names the 'learnchapel' module
|
||||||
|
|
||||||
|
module OurModule {
|
||||||
|
// We can use modules inside of other modules.
|
||||||
|
use Time; // Time is one of the standard modules.
|
||||||
|
|
||||||
|
// We'll use this procedure in the parallelism section.
|
||||||
|
proc countdown( seconds: int ){
|
||||||
|
for i in 1..seconds by -1 {
|
||||||
|
writeln( i );
|
||||||
|
sleep( 1 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Submodules of OurModule
|
||||||
|
// It is possible to create arbitrarily deep module nests.
|
||||||
|
module ChildModule {
|
||||||
|
proc foo(){
|
||||||
|
writeln( "ChildModule.foo()");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module SiblingModule {
|
||||||
|
proc foo(){
|
||||||
|
writeln( "SiblingModule.foo()" );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} // end OurModule
|
||||||
|
|
||||||
|
// Using OurModule also uses all the modules it uses.
|
||||||
|
// Since OurModule uses Time, we also use time.
|
||||||
|
use OurModule;
|
||||||
|
|
||||||
|
// At this point we have not used ChildModule or SiblingModule so their symbols
|
||||||
|
// (i.e. foo ) are not available to us.
|
||||||
|
// However, the module names are, and we can explicitly call foo() through them.
|
||||||
|
SiblingModule.foo(); // Calls SiblingModule.foo()
|
||||||
|
|
||||||
|
// Super explicit naming.
|
||||||
|
OurModule.ChildModule.foo(); // Calls ChildModule.foo()
|
||||||
|
|
||||||
|
use ChildModule;
|
||||||
|
foo(); // Less explicit call on ChildModule.foo()
|
||||||
|
|
||||||
|
// We can declare a main procedure
|
||||||
|
// Note: all the code above main still gets executed.
|
||||||
|
proc main(){
|
||||||
|
|
||||||
// Parallelism
|
// Parallelism
|
||||||
// In other languages, parallelism is typically this is done with
|
// In other languages, parallelism is typically done with
|
||||||
// complicated libraries and strange class structure hierarchies.
|
// complicated libraries and strange class structure hierarchies.
|
||||||
// Chapel has it baked right into the language.
|
// Chapel has it baked right into the language.
|
||||||
|
|
||||||
// A begin statement will spin the body of that statement off into one new task.
|
// A begin statement will spin the body of that statement off
|
||||||
|
// into one new task.
|
||||||
// A sync statement will ensure that the progress of the main
|
// A sync statement will ensure that the progress of the main
|
||||||
// task will not progress until the children have synced back up.
|
// task will not progress until the children have synced back up.
|
||||||
sync {
|
sync {
|
||||||
@ -761,19 +909,9 @@ timer.clear( );
|
|||||||
// You may have noticed that (depending on how many cores you have)
|
// You may have noticed that (depending on how many cores you have)
|
||||||
// that the parallel loop went faster than the serial loop
|
// that the parallel loop went faster than the serial loop
|
||||||
|
|
||||||
// A succinct way of writing a forall loop over an array:
|
// The bracket style loop-expression described
|
||||||
// iterate over values
|
// much earlier implicitly uses a forall loop.
|
||||||
[ val in myBigArray ] val = 1 / val;
|
[ val in myBigArray ] val = 1 / val; // Parallel operation
|
||||||
|
|
||||||
// or iterate over indicies
|
|
||||||
[ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx];
|
|
||||||
|
|
||||||
proc countdown( seconds: int ){
|
|
||||||
for i in 1..seconds by -1 {
|
|
||||||
writeln( i );
|
|
||||||
sleep( 1 );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Atomic variables, common to many languages, are ones whose operations
|
// Atomic variables, common to many languages, are ones whose operations
|
||||||
// occur uninterupted. Multiple threads can both modify atomic variables
|
// occur uninterupted. Multiple threads can both modify atomic variables
|
||||||
@ -792,7 +930,7 @@ var was = uranium.exchange( replaceWith );
|
|||||||
writeln( "uranium was ", was, " but is now ", replaceWith );
|
writeln( "uranium was ", was, " but is now ", replaceWith );
|
||||||
|
|
||||||
var isEqualTo = 235;
|
var isEqualTo = 235;
|
||||||
if uranium.compareExchange( isEqualTo, replaceWith ) {
|
if ( uranium.compareExchange( isEqualTo, replaceWith ) ) {
|
||||||
writeln( "uranium was equal to ", isEqualTo,
|
writeln( "uranium was equal to ", isEqualTo,
|
||||||
" so replaced value with ", replaceWith );
|
" so replaced value with ", replaceWith );
|
||||||
} else {
|
} else {
|
||||||
@ -866,7 +1004,7 @@ coforall task in 1..#5 { // Generate tasks
|
|||||||
// Create a barrier
|
// Create a barrier
|
||||||
do{
|
do{
|
||||||
lock$; // Read lock$ (wait)
|
lock$; // Read lock$ (wait)
|
||||||
}while count.read() < 1; // Keep waiting until a spot opens up
|
}while ( count.read() < 1 ); // Keep waiting until a spot opens up
|
||||||
|
|
||||||
count.sub(1); // decrement the counter
|
count.sub(1); // decrement the counter
|
||||||
lock$.writeXF( true ); // Set lock$ to full (signal)
|
lock$.writeXF( true ); // Set lock$ to full (signal)
|
||||||
@ -901,6 +1039,7 @@ var runningSumOfValues = + scan listOfValues;
|
|||||||
var maxScan = max scan listOfValues;
|
var maxScan = max scan listOfValues;
|
||||||
writeln( runningSumOfValues );
|
writeln( runningSumOfValues );
|
||||||
writeln( maxScan );
|
writeln( maxScan );
|
||||||
|
} // end main()
|
||||||
```
|
```
|
||||||
|
|
||||||
Who is this tutorial for?
|
Who is this tutorial for?
|
||||||
@ -908,18 +1047,15 @@ Who is this tutorial for?
|
|||||||
|
|
||||||
This tutorial is for people who want to learn the ropes of chapel without having to hear about what fiber mixture the ropes are, or how they were braided, or how the braid configurations differ between one another.
|
This tutorial is for people who want to learn the ropes of chapel without having to hear about what fiber mixture the ropes are, or how they were braided, or how the braid configurations differ between one another.
|
||||||
It won't teach you how to develop amazingly performant code, and it's not exhaustive.
|
It won't teach you how to develop amazingly performant code, and it's not exhaustive.
|
||||||
Refer to the [language specification](http://chapel.cray.com/language.html) and the [library documentation](http://chapel.cray.com/docs/latest/) for more details.
|
Refer to the [language specification](http://chapel.cray.com/language.html) and the [module documentation](http://chapel.cray.com/docs/latest/) for more details.
|
||||||
|
|
||||||
Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to see if more topics have been added or more tutorials created.
|
Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to see if more topics have been added or more tutorials created.
|
||||||
|
|
||||||
### What this tutorial is lacking:
|
### What this tutorial is lacking:
|
||||||
|
|
||||||
* Modules and standard modules
|
* Exposition of the standard modules
|
||||||
* Multiple Locales (distributed memory system)
|
* Multiple Locales (distributed memory system)
|
||||||
* ```proc main(){ ... }```
|
|
||||||
* Records
|
* Records
|
||||||
* Whole/sliced array assignment
|
|
||||||
* Range and domain slicing
|
|
||||||
* Parallel iterators
|
* Parallel iterators
|
||||||
|
|
||||||
Your input, questions, and discoveries are important to the developers!
|
Your input, questions, and discoveries are important to the developers!
|
||||||
@ -940,26 +1076,28 @@ Chapel can be built and installed on your average 'nix machine (and cygwin).
|
|||||||
[Download the latest release version](https://github.com/chapel-lang/chapel/releases/)
|
[Download the latest release version](https://github.com/chapel-lang/chapel/releases/)
|
||||||
and its as easy as
|
and its as easy as
|
||||||
|
|
||||||
1. ```tar -xvf chapel-1.11.0.tar.gz```
|
1. `tar -xvf chapel-1.11.0.tar.gz`
|
||||||
2. ```cd chapel-1.11.0```
|
2. `cd chapel-1.11.0`
|
||||||
3. ```make```
|
3. `make`
|
||||||
4. ```source util/setchplenv.bash # or .sh or .csh or .fish```
|
4. `source util/setchplenv.bash # or .sh or .csh or .fish`
|
||||||
|
|
||||||
You will need to `source util/setchplenv.EXT` from within the Chapel directory (`$CHPL_HOME`) every time your terminal starts so its suggested that you drop that command in a script that will get executed on startup (like .bashrc).
|
You will need to `source util/setchplenv.EXT` from within the Chapel directory (`$CHPL_HOME`) every time your terminal starts so its suggested that you drop that command in a script that will get executed on startup (like .bashrc).
|
||||||
|
|
||||||
Chapel is easily installed with Brew for OS X
|
Chapel is easily installed with Brew for OS X
|
||||||
|
|
||||||
1. ```brew update```
|
1. `brew update`
|
||||||
2. ```brew install chapel```
|
2. `brew install chapel`
|
||||||
|
|
||||||
Compiling Code
|
Compiling Code
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
Builds like other compilers:
|
Builds like other compilers:
|
||||||
|
|
||||||
```chpl myFile.chpl -o myExe```
|
`chpl myFile.chpl -o myExe`
|
||||||
|
|
||||||
Notable arguments:
|
Notable arguments:
|
||||||
|
|
||||||
* ``--fast``: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable.
|
* `--fast`: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable.
|
||||||
* ```--set <Symbol Name>=<Value>```: set config param <Symbol Name> to <Value> at compile-time
|
* `--set <Symbol Name>=<Value>`: set config param `<Symbol Name>` to `<Value>` at compile-time.
|
||||||
|
* `--main-module <Module Name>`: use the main() procedure found in the module `<Module Name>` as the executable's main.
|
||||||
|
* `--module-dir <Directory>`: includes `<Directory>` in the module search path.
|
Loading…
Reference in New Issue
Block a user