From 56171326c725f70cfdec99d8086d9c5dc8d1a211 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 12 Jul 2015 16:17:23 -0700 Subject: added chapel.html.markdown, performed a first pass at the tutorial, and the beginning readme of the tutorial. Currently missing topics [ reduction, scal, non-loop tuple expansion, whole array/partial array assignment] --- chapel.html.markdown | 722 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 722 insertions(+) create mode 100644 chapel.html.markdown (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown new file mode 100644 index 00000000..a17222b9 --- /dev/null +++ b/chapel.html.markdown @@ -0,0 +1,722 @@ +--- +language: chapel +filename: learnchapel.chpl +contributors: + - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola.com/"] +lang: en +--- +What is Chapel? +=============== +You can read all about chapel at [Cray's official Chapel website](chapel.cray.com). +In short, Chapel is an open-source, high-productivity, parallel-programming language in development +at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. + +Chapel is currently in-development so there are occasional hiccups with +performance and language features, which is why you should write as much Chapel + +Your input, questions, and discoveries are important to us! +----------------------------------------------------------- +The more information you give the Chapel development team about issues you encounter with the language, +the better the language gets. +Feel free to email the team and other developers through the sourceforge email lists at [sourceforge](https://sourceforge.net/p/chapel/mailman) +There is also a #chapel-developers hosted at chat.freenode.net. + +If you're really interested in the cutting edge compiler or contributing to the project, +the git repository for Chapel is open-source at [github](https://github.com/chapel-lang/chapel) +under the Apache v2.0 license + +Installing the Compiler +----------------------- +Chapel can be built and installed on your average 'nix machine (and cygwin). +Download the latest release version from https://github.com/chapel-lang/chapel/releases/ +and its as easy as +1. ```tar -xvf chapel-1.11.0.tar.gz``` +2. ```cd chapel-1.11.0``` +3. ```make``` +4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` + +You will need to ```source util/setchplenv.*``` from the chapel directory 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 +1. ```brew update``` +2. ```brew install chapel``` + +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. +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. + +Occasionally check here back to see if more topics have been added. + +```chapel +// Comments are C-family style +// one line comment +/* + multi-line comment +*/ + +// Basic printing +write( "Hello, " ); +writeln( "World!" ); +// write and writeln can take a list of things to print. +// each thing is printed right next to each other, so include your spacing! +writeln( "There are ", 3, " commas (\",\") in this line of code" ); +// Different output channels +stdout.writeln( "This goes to standard output (just like plain writeln() does)"); +stderr.writeln( "This goes to standard error" ); + +// Variables +// Variables dont have to be explicitly as long as the compiler can figure +// out the type that it will hold. +var myVar = 10; // 10 is an int, so myVar is implicitly an int +myVar = -10; +// var anError; // compile time error, dont know what type anError should be. + +// We can (and should) explicitly type things +var mySecondVar: real; // define mySecondVar as a real +var myThirdVar: real = -1.234; +mySecondVar = myThirdVar; + +// There are a number of basic types. +var myInt: int = -1000; // signed ints +var myUint: uint = 1234; // unsigned ints +var myReal: real = 9.876; // floating point numbers +var myImag: imag = 5.0i; // imaginary numbers +var myCplx: complex = 10 + 9i; // complex numbers +myCplx = myInt + myImag ; // another way to form complex numbers +var myBool: bool = false; // booleans +var myStr: string = "Some string..."; // strings + +// Some types can have sizes +var my8Int: int(8) = 10; // 8 bit (one byte) sized int; +var my64Real: real(64) = 1.516; // 64 bit (8 bytes) sized real + +// Typecasting +var intFromReal = myReal : int; +// could also explicitly type intFromReal +// var intFromReal: int = myReal : int; + +// Operators +// Math operators +var a: int, thisInt = 1234, thatInt = 5678; +a = thisInt + thatInt; // Addition +a = thisInt * thatInt; // Multiplication +a = thisInt - thatInt; // Subtraction +a = thisInt / thatInt; // division +a = thisInt ** thatInt; // exponentiation +a = thisInt % thatInt; // remainder (modulo) + +// Logical Operators +var b: bool, thisBool = false, thatBool = true; +b = thisBool && thatBool; // logical and +b = thisBool || thatBool; // logical or +b = !thisBool; // logical negation + +// Relational Operators +b = thisInt > thatInt; // greater-than +b = thisInt >= thatInt; // greater-than-or-equal-to +b = thisInt < a && a <= thatInt; // less-than, and, less-than-or-equal-to +b = thisInt != thatInt; // not-equal-to +b = thisInt == thatInt; // equal-to + +// Bitwise operations +a = thisInt << 10; // left-bit-shift by 10 bits; +a = thatInt >> 5; // right-bit-shift by 5 bits; +a = ~thisInt; // bitwise-negation +a = thisInt ^ thatInt; // bitwise exclusive-or + +// Compound assignment operations +a += thisInt; // addition-equals ( a = a + thisInt;) +a *= thatInt; // times-equals ( a = a * thatInt; ) +b &&= thatBool; // logical-and-equals ( b = b && thatBool; ) +a <<= 3; // left-bit-shift-equals ( a = a << 10; ) +// and so on... +// Unlike other C family languages there are no +// pre/post-increment/decrement operators like +// ++j, --j, j++, j-- + + +// Swap operator +var temp_this = thisInt; +var temp_that = thatInt; +thisInt <=> thatInt; // Swap the values of thisInt and thatInt +writeln( (temp_this == thatInt) && (temp_that == thisInt) ); + +// We can also define operator overloads, +// which we'll cover with procedures. + +// Tuples +// tuples can be of the same type +var sameTup: 2*int = (10,-1); +// or different types +var diffTup: (int,real,complex) = (5, 1.928, myCplx); +// Accessed using array bracket notation +// However, tuples are all 1-indexed +writeln( "(", sameTup[1], ",", sameTup[2], ")" ); +writeln( diffTup ); +// Tuples can also be written into. +diffTup[1] = -1; +// Can also be used to easily write a collection of variables +// as is common in debugging +writeln( (a,b,thisInt,thatInt,thisBool,thatBool) ); + +// Type aliasing +type chroma = int; // type of a single hue +type RGBColor = 3*chroma; // type representing a full color +var black: RGBColor = ( 0,0,0 ); +var white: RGBColor = ( 255, 255, 255 ); + + + +// If-Then statements +// if-thens dont require parentheses around the condition +// as they do in C (however, we will use them) +// and a single line body can use the 'then' keyword instead of braces +// and else statements can be written similarly +// (but we're only going to show it once). +if 10 < 100 then + writeln( "All is well" ); + +if -1 < 1 then + writeln( "Continuing to believe reality" ); +else + writeln( "Send mathematician, something's wrong" ); + + +if ( 10 > 100 ) { + writeln( "Universe broken. Please reboot universe." ); +} + +if ( a % 2 == 0 ) { + writeln( a, " is even." ); +} else { + writeln( a, " is odd." ); +} + +if ( a % 3 == 0 ) { + writeln( a, " is even divisible by 3." ); +} else if ( a % 3 == 1 ){ + writeln( a, " is divided by 3 with a remainder of 1." ); +} else { + writeln( b, " is divided by 3 with a remainder of 2." ); +} + +// Ternary: if-then-else in a statement +var maximum = if ( thisInt < thatInt ) then thatInt else thisInt; + +// Select statements +// Select statements are much like switch statements in other languages +// However, Select statements dont cascade like in C or Java +var inputOption = "anOption"; +select( inputOption ){ + when "anOption" do writeln( "Chose 'anOption'" ); + when "otherOption" { + writeln( "Chose 'otherOption'" ); + writeln( "Which has a body" ); + } + otherwise { + writeln( "Any other Input" ); + writeln( "the otherwise case doesn't need a do if the body is one line" ); + writeln( "Oh, and when statements dont cascade like the case statements" ); + writeln( "of other languages" ); + } +} + +// Loops +// While Loops +// While loops and Do-While loops are basically the same in every language. + +var j: int = 1; +var jSum: int = 0; +while( j <= 1000 ){ + jSum += j; + j += 1; // there are no ++j, --j, j++, j--, operators +} +writeln( jSum ); + +// basic Do-While loop +do{ + jSum += j; + j += 1; +}while( j <= 10000 ); +writeln( jSum ); + +// For loops +// For loops are much like those in python in that they iterate over a range. +// ranges themselves are types, and can be stuffed into variables +// (more about that later) + +for i in 1..10 do write( i , ", ") ; +writeln(); + +var iSum: int = 0; +for i in 1..1000 { + iSum += i; +} +writeln( iSum ); + +for x in 1..10 { + for y in 1..10 { + write( (x,y), "\t" ); + } + writeln(); +} + +// Ranges and Domains +// For-loops and arrays both use ranges and domains to +// define an index set that can be iterated over. +// Ranges are single dimensional +// Domains can be multi-dimensional and represent indicies +// of different types as well. +// They are types, and can be assigned into variables; +var range1to10: range = 1..10; // // 1, 2, 3, ... , 10 + +// Ranges can be strided using the 'by' operator. +// Note: the stridable=true is only necessary if we type the variable +var range2to10by2: range(stridable=true) = 2..10 by 2; // 2, 4, 6, 8, 10 + +// The end point of a range can be determined using the count (#) operator +var rangeCount: range = -5..#12; // range from -5 to 6 + +// Can mix operators +var rangeCountBy: range(stridable=true) = -5..#12 by 2; // -5, -3, -1, 1, 3, 5 +writeln( rangeCountBy ); + +// Can query properties of the range +// Print the first index, last index, number of indices, +// stride, and ask if 2 is include in the range +writeln( ( rangeCountBy.first, rangeCountBy.last, rangeCountBy.length, + rangeCountBy.stride, rangeCountBy.member( 2 ) ) ); + +for i in rangeCountBy{ + write( i, if i == rangeCountBy.last then "\n" else ", " ); +} + +// domains are similarly defined using range notation +var domain1to10: domain(1) = {1..10}; // domain from 1..10; +var twoDimensions: domain(2) = {-2..2,0..2}; // domain over two dimensions + +// Can iterate over the indices as tuples +for idx in twoDimensions do + write( idx , ", "); +writeln(); + +// Or can deconstruct the tuple +for (x,y) in twoDimensions { + write( (x,y), ", " ); +} +writeln(); + +// Associative domains act like sets +var intSet: domain(int); // empty set of ints +intSet += 1; +intSet += 2; +intSet += 3; +intSet += 1; // redundant add 1 +intSet -= 3; // remove 3 +writeln( intSet ); + + +// Arrays +// Array are similar to those of other languages. +// Their sizes are defined using ranges and domains. +// that represent their indices, but we'll touch more on those later +var intArray: [1..10] int; // array of integers defined using range literal + +// Accessed using bracket notation +for i in 1..10 do + intArray[i] = -i; +writeln( intArray ); +// we cannot access intArray[0] because it exists outside +// of the index set we defined (1..10) +// intArray[11] is illegal for the same reason. + +var realDomain: domain(2) = {1..5,1..7}; +var realArray: [realDomain] real; +// similarly we could have done: +// var realArray: [1..5,1..7] real; + +for i in 1..5 { + // use the range from 2nd dimension of the domain + for j in realDomain.dim(2) { + realArray[i,j] = -1.61803 * i + 0.5 * j; // access using index list + var idx: 2*int = (i,j); // note: 'index' is a keyword + realArray[idx] = - realArray[(i,j)]; // index using tuples + } +} + +// arrays have domains as members that we can iterate over +for idx in realArray.domain { // idx is, again, a 2*int tuple + realArray[idx] = 1 / realArray[idx[1],idx[2]]; // access by tuple and list +} + +writeln( realArray ); + +// can also iterate over the values of an array +var rSum: real = 0; +for value in realArray { + rSum += value; // read a value + value = rSum; // write a value +} +writeln( rSum, "\n", realArray ); + +// Using associative domains we can create associative arrays (dictionaries) +var dictDomain: domain(string) = { "one", "two" }; +var dict: [dictDomain] int = [ "one" => 1, "two" => 2 ]; +dict["three"] = 3; +writeln( dict ); + + +// Procedures +// Chapel procedures have similar syntax to other languages functions. + +proc fibonacci( n : int ) : int { + if ( n == 0 || n == 1 ) then return n; + return fibonacci( n-1 ) + fibonacci( n-2 ); +} + +// input parameters can be untyped +proc doublePrint( thing ): void { + write( thing, " ", thing, "\n"); +} + +// return type can be inferred (as long as the compiler can figure it out) +proc addThree( n ) { + return n + 3; +} + +doublePrint( addThree( fibonacci( 20 ) ) ); + +// Can also take unlimited number of parameters +proc maxOf( x ...?k ) { + // x refers to a tuple of one type, with k elements + var maximum = x[1]; + for i in 2..k do maximum = if (maximum < x[i]) then x[i] else maximum; + return maximum; +} +writeln( maxOf( 1, -10, 189, -9071982, 5, 17, 20001, 42 ) ); + +// the ? operator is called the query operator, and is used to take +// undetermined values (like tuple and array sizes, and generic types). + +// Taking arrays as parameters. +// The query operator is used to determine the domain of A. +// this is important to define the return type (if you wanted to) +proc invertArray( A: [?D] int ): [D] int{ + for a in A do a = -a; + return A; +} + +writeln( invertArray( intArray ) ); + +// Procedures can have default parameter values, and +// the parameters can be named in the call, even out of order +proc defaultsProc( x: int, y: real = 1.2634 ): (int,real){ + return (x,y); +} + +writeln( defaultsProc( 10 ) ); +writeln( defaultsProc( x=11 ) ); +writeln( defaultsProc( x=12, y=5.432 ) ); +writeln( defaultsProc( y=9.876, x=13 ) ); + +// Generic procedures can still retain type +// Here we define a procedure that takes two arguments +// of the same type, yet we dont define what that type is. +proc genericProc( arg1 : ?valueType, arg2 : valueType ): void { + select( valueType ){ + when int do writeln( arg1, " and ", arg2, " are ints" ); + when real do writeln( arg1, " and ", arg2, " are reals" ); + otherwise writeln( arg1, " and ", arg2, " are somethings!" ); + } +} + +genericProc( 1, 2 ); +genericProc( 1.2, 2.3 ); +genericProc( 1.0+2.0i, 3.0+4.0i ); + +// We can also enforce a form of polymorphism with the 'where' clause +// This allows the compiler to decide which function to use. +// Note: that means that all information needs to be known at compile +// time. Hence, we use params here to assert that the arguments must +// be known at compile time. +proc whereProc( param N : int ): void + where ( N > 0 ) { + writeln( "N is greater than 0" ); +} + +proc whereProc( param N : int ): void + where ( N < 0 ) { + writeln( "N is less than 0" ); +} + +whereProc( 10 ); +whereProc( -1 ); +// whereProc( 0 ) would result in a compiler error because there +// are no functions that satisfy the where clause's condition. +// We could have defined a whereProc without a where clause that would +// then have been called. + +// Operator definitions are through procedures as well +// we can define the unary operators: +// + - ! ~ +// and the binary operators: +// + - * / % ** == <= >= < > << >> & | ˆ by +// += -= *= /= %= **= &= |= ˆ= <<= >>= <=> + +// boolean exclusive or operator +proc ^( left : bool, right : bool ): bool { + return (left || right) && !( left && right ); +} + +writeln( true ^ true ); +writeln( false ^ true ); +writeln( true ^ false ); +writeln( false ^ false ); + +// Define a * operator on any two types. +proc *( left : ?ltype, right : ?rtype): ( ltype, rtype ){ + return (left, right ); +} + +writeln( 1 * "a" ); // uses our * operator +writeln( 1 * 2 ); // uses the original * operator + +/* +Note: You could break everything if you + get careless with your overloads. +This here will break everything. Dont do it. +proc +( left: int, right: int ): int{ + return left - right; +} +*/ + +// Classes +class MyClass { + // Member variables + var memberInt : int; + var memberBool : bool = true; + + // Classes have default constructors that dont need to be coded (see below) + // Our explicitly defined constructor + proc MyClass( val : real ){ + this.memberInt = ceil( val ): int; + } + + // Our explicitly defined destructor + proc ~MyClass( ){ + writeln( "MyClass Destructor called ", (this.memberInt, this.memberBool) ); + } + + // Class methods + proc setMemberInt( val: int ){ + this.memberInt = val; + } + + proc setMemberBool( val: bool ){ + this.memberBool = val; + } + + proc getMemberInt( ): int{ + return this.memberInt; + } + + proc getMemberBool(): bool { + return this.memberBool; + } + +} + +// Construct using default constructor, using default values +var myObject = new MyClass( 10 ); + myObject = new MyClass( memberInt = 10 ); // equivalent +writeln( myObject.getMemberInt() ); +// ... using our values +var myDiffObject = new MyClass( -1, true ); + myDiffObject = new MyClass( memberInt = -1, + memberBool = false ); // equivalent +writeln( (myDiffObject.getMemberInt(), myDiffObject.getMemberBool() )); + +// Construct using written constructor +var myOtherObject = new MyClass( 1.95 ); + myOtherObject = new MyClass( val = 1.95 ); // equivalent +writeln( myOtherObject.getMemberInt() ); + +// We can define an operator on our class as well but +// the definition has to be outside the class definition +proc +( A : MyClass, B : MyClass) : MyClass { + return new MyClass( memberInt = A.getMemberInt() + B.getMemberInt(), + memberBool = A.getMemberBool() || B.getMemberBool() ); +} + +var plusObject = myObject + myDiffObject; +writeln( (plusObject.getMemberInt(), plusObject.getMemberBool() ) ); + +// destruction +delete myObject; +delete myDiffObject; +delete myOtherObject; +delete plusObject; + +// Classes can inherit from one or more parent classes +class MyChildClass : MyClass { + var memberComplex: complex; +} + +// Generic Classes +class GenericClass { + type classType; + var classDomain: domain(1); + var classArray: [classDomain] classType; + + // Explicit constructor + proc GenericClass( type classType, elements : int ){ + this.classDomain = {1..#elements}; + } + + // Copy constructor + // Note: We still have to put the the type as an argument, but we can + // default to the type of the other object using the query (?) operator + // Further, we can take advantage of this to allow our copy constructor + // to copy classes of different types + proc GenericClass( other : GenericClass(?otherType), + type classType = otherType ) { + this.classDomain = other.classDomain; + // Copy and cast + [ idx in this.classDomain ] this[ idx ] = other[ idx ] : classType; + } + + // Define bracket notation on a GenericClass object + // i.e. objVar[ i ] or objVar( i ) + proc this( i : int ) ref : classType { + return this.classArray[ i ]; + } + + // Define an iterator for the class. + // i.e. for i in objVar do .... + iter these() ref : classType { + for i in this.classDomain do + yield this[i]; + } + +} + +var realList = new GenericClass( real, 10 ); +// We can assign to the array in the object using the bracket notation +for i in realList.classDomain do realList[i] = i + 1.0; +// We can iterate over a +for value in realList do write( value, ", " ); +writeln(); + +// Make a copy of realList using the copy constructor +var copyList = new GenericClass( realList ); +for value in copyList do write( value, ", " ); +writeln(); + +// make a copy of realList and change the type, also using the copy constructor +var copyNewTypeList = new GenericClass( realList, int ); +for value in copyNewTypeList do write( value, ", " ); +writeln(); + + +// Tasks +// A task is some work that will be done separately from +// the current task, and (if there are any available) in its own thread. + +// a synch statement will ensure that the progress of the +// main task will not progress until the children have synced back up. +sync { +// a begin statement will spin the body off into one new task + begin { + var a = 0; + for i in 1..1000 do a += 1; + writeln( "Done: ", a); + } + writeln( "spun off a task!"); +} +writeln( "Back together" ); + +proc printFibb( n: int ){ + writeln( "fibonacci(",n,") = ", fibonacci( n ) ); +} + +// a cobegin statement will spin each +// statement of the body into one new task +cobegin { + printFibb( 20 ); + printFibb( 10 ); + printFibb( 5 ); + { + // this is a nested statement body and thus is a single statement + // to the parent statement and is executed by a single task + writeln( "this gets" ); + writeln( "executed as" ); + writeln( "a whole" ); + } +} +// Notice here that the prints may happen in any order. + +// Coforall loop will create a new task for EACH iteration +// NOTE! coforall should be used only for creating tasks! +// Using it to iterating over an array or something like that is very a bad idea! + +var num_tasks = 10; // Number of tasks we want +coforall taskID in 1..#num_tasks { + writeln( "Hello from task# ", taskID ); +} +// Again we see that prints happen in any order. + +// forall loops are another parallel loop, but only create a smaller number +// of tasks, specifically dataParTasksPerLocale number of task (more later) +forall i in 1..100 { + write( i, ", "); +} +writeln(); +// Here we see that there are sections that are in order, followed by +// a section that would not follow ( e.g. 1, 2, 3, 7, 8, 9, 4, 5, 6, ) +// this is because each task is taking on a chunk of the range 1..10 +// (1..3, 4..6, or 7..9) doing that chunk serially, but each task happens +// in parallel. +// Your results may depend on your machine and configuration + +// For both the forall and coforall loops, the execution of the parent task +// will not continue until all the children sync up. + +// forall loops are particularly useful for parallel iteration over arrays +// Lets run an experiment to see how much faster a parallel loop is +use Time; // Import the Time module to use Timer objects +var timer: Timer; +var myBigArray: [{1..4000,1..4000}] real; // large array we will write into +// Serial Experiment +timer.start(); // start timer +for (x,y) in myBigArray.domain { // serial iteration + myBigArray[x,y] = (x:real) / (y:real); +} +timer.stop(); // stop timer +writeln( "Serial: ", timer.elapsed() ); // print elapsed time +timer.clear(); // clear timer for parallel loop + +// Parallel Experiment +timer.start(); // start timer +forall (x,y) in myBigArray.domain { // parallel iteration + myBigArray[x,y] = (x:real) / (y:real); +} +timer.stop(); // stop timer +writeln( "Parallel: ", timer.elapsed() ); // print elapsed time +timer.clear(); +// you may have noticed that (depending on how many cores you have) that +// the parallel loop went faster than the serial loop + +// A succinct way of writing a forall loop over an array: +[ val in myBigArray ] val = 1 / val; // iterate over values +// or +[ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; // iterate over indicies + +``` \ No newline at end of file -- cgit v1.2.3 From 3ad75a4fb79e819a1ea5b2ab74bbfbb2c59c17fd Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 12 Jul 2015 16:40:32 -0700 Subject: Minor fix removed .com from my (ian bertolacci) website link --- chapel.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index a17222b9..df9c1a44 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -2,7 +2,7 @@ language: chapel filename: learnchapel.chpl contributors: - - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola.com/"] + - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"] lang: en --- What is Chapel? @@ -719,4 +719,4 @@ timer.clear(); // or [ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; // iterate over indicies -``` \ No newline at end of file +``` -- cgit v1.2.3 From 2ba9af4b85de0a2214d3c4de0f1509440a41adef Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 12 Jul 2015 16:43:42 -0700 Subject: Header modified Found an unfinished sentence, fixed it. --- chapel.html.markdown | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index df9c1a44..01826a9c 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -11,11 +11,10 @@ You can read all about chapel at [Cray's official Chapel website](chapel.cray.co In short, Chapel is an open-source, high-productivity, parallel-programming language in development at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. -Chapel is currently in-development so there are occasional hiccups with -performance and language features, which is why you should write as much Chapel - Your input, questions, and discoveries are important to us! ----------------------------------------------------------- +Chapel is currently in-development so there are occasional hiccups with +performance and language features. The more information you give the Chapel development team about issues you encounter with the language, the better the language gets. Feel free to email the team and other developers through the sourceforge email lists at [sourceforge](https://sourceforge.net/p/chapel/mailman) @@ -39,7 +38,6 @@ You will need to ```source util/setchplenv.*``` from the chapel directory 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 1. ```brew update``` 2. ```brew install chapel``` -- cgit v1.2.3 From cbbb833445e0e2b6223ce1c30943ed92ebc87ad7 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 12 Jul 2015 16:45:18 -0700 Subject: List formatting Lists needed a space before numbers to actually become html list formatted. --- chapel.html.markdown | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 01826a9c..9005b05f 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -29,18 +29,18 @@ Installing the Compiler Chapel can be built and installed on your average 'nix machine (and cygwin). Download the latest release version from https://github.com/chapel-lang/chapel/releases/ and its as easy as -1. ```tar -xvf chapel-1.11.0.tar.gz``` -2. ```cd chapel-1.11.0``` -3. ```make``` -4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` + 1. ```tar -xvf chapel-1.11.0.tar.gz``` + 2. ```cd chapel-1.11.0``` + 3. ```make``` + 4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` You will need to ```source util/setchplenv.*``` from the chapel directory 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 -1. ```brew update``` -2. ```brew install chapel``` + 1. ```brew update``` + 2. ```brew install chapel``` Who is this tutorial for? ------------------------- -- cgit v1.2.3 From 7ff3bd31d86009aec4d14292b2cbd02a3352e70f Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 12 Jul 2015 16:48:34 -0700 Subject: Link and header fix 1. put latest release url under the text [download the latest ....] 2. changed '... important to us!' to '... important to the developers!' so not to confuse the us (chapel dev team) with the tutorial people (learnxinyminutes) --- chapel.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 9005b05f..db566454 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -11,8 +11,8 @@ You can read all about chapel at [Cray's official Chapel website](chapel.cray.co In short, Chapel is an open-source, high-productivity, parallel-programming language in development at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. -Your input, questions, and discoveries are important to us! ------------------------------------------------------------ +Your input, questions, and discoveries are important to the developers! +----------------------------------------------------------------------- Chapel is currently in-development so there are occasional hiccups with performance and language features. The more information you give the Chapel development team about issues you encounter with the language, @@ -27,7 +27,7 @@ under the Apache v2.0 license Installing the Compiler ----------------------- Chapel can be built and installed on your average 'nix machine (and cygwin). -Download the latest release version from https://github.com/chapel-lang/chapel/releases/ +[Download the latest release version](https://github.com/chapel-lang/chapel/releases/) and its as easy as 1. ```tar -xvf chapel-1.11.0.tar.gz``` 2. ```cd chapel-1.11.0``` -- cgit v1.2.3 From 70a5440d4c4575dfe07495cadc9aa48f1d85d016 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 12 Jul 2015 16:49:49 -0700 Subject: Bad Link fix fixed a link that was suppose to go to the chapel.cray.com site, but did not. --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index db566454..90b64854 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -7,7 +7,7 @@ lang: en --- What is Chapel? =============== -You can read all about chapel at [Cray's official Chapel website](chapel.cray.com). +You can read all about chapel at [Cray's official Chapel website](http://chapel.cray.com). In short, Chapel is an open-source, high-productivity, parallel-programming language in development at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. -- cgit v1.2.3 From 28f2817ac36e6fd85bff29e8dcda71f452d4b97e Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 12 Jul 2015 16:50:55 -0700 Subject: removed hyphen removed hyphen from library-documentation --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 90b64854..1c5bcc15 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -49,7 +49,7 @@ hear about what fiber mixture the ropes are, or how they were braided, or how th differ between one another. 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. +and the [library documentation](http://chapel.cray.com/docs/latest/) for more details. Occasionally check here back to see if more topics have been added. -- cgit v1.2.3 From 7bdcbc834e25c89a4ae59ac6282e82e75f6f7fb6 Mon Sep 17 00:00:00 2001 From: "ian.bertolacci" Date: Mon, 13 Jul 2015 16:42:46 -0700 Subject: removed IRC node. That node refers to people developing in the compiler, no developers using the language --- chapel.html.markdown | 1 - 1 file changed, 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 1c5bcc15..3e6c7bb6 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -18,7 +18,6 @@ performance and language features. The more information you give the Chapel development team about issues you encounter with the language, the better the language gets. Feel free to email the team and other developers through the sourceforge email lists at [sourceforge](https://sourceforge.net/p/chapel/mailman) -There is also a #chapel-developers hosted at chat.freenode.net. If you're really interested in the cutting edge compiler or contributing to the project, the git repository for Chapel is open-source at [github](https://github.com/chapel-lang/chapel) -- cgit v1.2.3 From 2a41844229c841894b58f64b1841802e24483803 Mon Sep 17 00:00:00 2001 From: "ian.bertolacci" Date: Tue, 14 Jul 2015 07:40:53 -0700 Subject: changed some wording in header. added tuple expansion --- chapel.html.markdown | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 3e6c7bb6..3563e4c1 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -17,11 +17,10 @@ Chapel is currently in-development so there are occasional hiccups with performance and language features. The more information you give the Chapel development team about issues you encounter with the language, the better the language gets. -Feel free to email the team and other developers through the sourceforge email lists at [sourceforge](https://sourceforge.net/p/chapel/mailman) +Feel free to email the team and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman). -If you're really interested in the cutting edge compiler or contributing to the project, -the git repository for Chapel is open-source at [github](https://github.com/chapel-lang/chapel) -under the Apache v2.0 license +If you're really interested in the development of the compiler or contributing to the project, +[check out the master Github repository](https://github.com/chapel-lang/chapel). Installing the Compiler ----------------------- @@ -33,7 +32,7 @@ and its as easy as 3. ```make``` 4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` -You will need to ```source util/setchplenv.*``` from the chapel directory every +You will need to ```source util/setchplenv.EXT``` from the chapel directory every time your terminal starts so its suggested that you drop that command in a script that will get executed on startup (like .bashrc). @@ -160,10 +159,14 @@ writeln( "(", sameTup[1], ",", sameTup[2], ")" ); writeln( diffTup ); // Tuples can also be written into. diffTup[1] = -1; -// Can also be used to easily write a collection of variables -// as is common in debugging +// you can expand tuples as well +var (tupInt, tupReal, tupCplx) = diffTup; +writeln( diffTup == (tupInt, tupReal, tupCplx) ); +// Can also be used to easily write a collection of +// variables as a list (common in debugging) writeln( (a,b,thisInt,thatInt,thisBool,thatBool) ); + // Type aliasing type chroma = int; // type of a single hue type RGBColor = 3*chroma; // type representing a full color -- cgit v1.2.3 From df0cd1395db5178aabc68bf17d48a8278f16d749 Mon Sep 17 00:00:00 2001 From: "ian.bertolacci" Date: Tue, 14 Jul 2015 17:30:09 -0700 Subject: numerous slight modifications, and additions. Notable: added const, param, config --- chapel.html.markdown | 197 +++++++++++++++++++++++++++------------------------ 1 file changed, 103 insertions(+), 94 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 3563e4c1..6382f75a 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -8,15 +8,12 @@ lang: en What is Chapel? =============== You can read all about chapel at [Cray's official Chapel website](http://chapel.cray.com). -In short, Chapel is an open-source, high-productivity, parallel-programming language in development -at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. +In short, Chapel is an open-source, high-productivity, parallel-programming language in development at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. Your input, questions, and discoveries are important to the developers! ----------------------------------------------------------------------- -Chapel is currently in-development so there are occasional hiccups with -performance and language features. -The more information you give the Chapel development team about issues you encounter with the language, -the better the language gets. +Chapel is currently in-development so there are occasional hiccups with performance and language features. +The more information you give the Chapel development team about issues you encounter with the language, the better the language gets. Feel free to email the team and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman). If you're really interested in the development of the compiler or contributing to the project, @@ -32,9 +29,7 @@ and its as easy as 3. ```make``` 4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` -You will need to ```source util/setchplenv.EXT``` from the chapel directory 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 the chapel directory 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 1. ```brew update``` @@ -42,12 +37,9 @@ Chapel is easily installed with Brew for OS X 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. -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 [library documentation](http://chapel.cray.com/docs/latest/) for more details. Occasionally check here back to see if more topics have been added. @@ -69,11 +61,11 @@ stdout.writeln( "This goes to standard output (just like plain writeln() does)") stderr.writeln( "This goes to standard error" ); // Variables -// Variables dont have to be explicitly as long as the compiler can figure -// out the type that it will hold. +// Variables dont have to be explicitly typed as long as +// the compiler can figure out the type that it will hold. var myVar = 10; // 10 is an int, so myVar is implicitly an int myVar = -10; -// var anError; // compile time error, dont know what type anError should be. +// var anError; // this would be a compile time error. // We can (and should) explicitly type things var mySecondVar: real; // define mySecondVar as a real @@ -96,10 +88,26 @@ var my64Real: real(64) = 1.516; // 64 bit (8 bytes) sized real // Typecasting var intFromReal = myReal : int; -// could also explicitly type intFromReal -// var intFromReal: int = myReal : int; +var intFromReal2: int = myReal : int; + +// consts are constants, they cannot be changed after set in runtime +const almostPi: real = 11.0/7.0; +// params are constants whose value must be known statically at compile time +// Like consts, they cannot be changed during runtime +param compileTimeConst: int = 16; + +// The config modifier allows values to be set at the command line +// and is much easier that the usual getOpts debacle +// config vars and consts can be changed through the command line at run time +config var varCmdLineArg: int = -123; +config const constCmdLineArg: int = 777; +// set with --VarName=Value or --VarName Value at run time + +// config params can be set at compile time +config param paramCmdLineArg: bool = false; +writeln( varCmdLineArg, ", ", constCmdLineArg, ", ", paramCmdLineArg ); +//set config with --set paramCmdLineArg=value at compile time -// Operators // Math operators var a: int, thisInt = 1234, thatInt = 5678; a = thisInt + thatInt; // Addition @@ -138,12 +146,11 @@ a <<= 3; // left-bit-shift-equals ( a = a << 10; ) // pre/post-increment/decrement operators like // ++j, --j, j++, j-- - // Swap operator -var temp_this = thisInt; -var temp_that = thatInt; +var old_this = thisInt; +var old_that = thatInt; thisInt <=> thatInt; // Swap the values of thisInt and thatInt -writeln( (temp_this == thatInt) && (temp_that == thisInt) ); +writeln( (old_this == thatInt) && (old_that == thisInt) ); // We can also define operator overloads, // which we'll cover with procedures. @@ -162,13 +169,13 @@ diffTup[1] = -1; // you can expand tuples as well var (tupInt, tupReal, tupCplx) = diffTup; writeln( diffTup == (tupInt, tupReal, tupCplx) ); -// Can also be used to easily write a collection of -// variables as a list (common in debugging) +// Can also be used to easily write a collection +// of variables as a list (common in debugging) writeln( (a,b,thisInt,thatInt,thisBool,thatBool) ); // Type aliasing -type chroma = int; // type of a single hue +type chroma = int; // type of a single hue type RGBColor = 3*chroma; // type representing a full color var black: RGBColor = ( 0,0,0 ); var white: RGBColor = ( 255, 255, 255 ); @@ -176,11 +183,11 @@ var white: RGBColor = ( 255, 255, 255 ); // If-Then statements -// if-thens dont require parentheses around the condition +// if-thens dont require parentheses around the condition // as they do in C (however, we will use them) -// and a single line body can use the 'then' keyword instead of braces -// and else statements can be written similarly -// (but we're only going to show it once). +// A single line body can use the 'then' keyword instead of +// braces and else statements can be written similarly + if 10 < 100 then writeln( "All is well" ); @@ -211,7 +218,6 @@ if ( a % 3 == 0 ) { // Ternary: if-then-else in a statement var maximum = if ( thisInt < thatInt ) then thatInt else thisInt; -// Select statements // Select statements are much like switch statements in other languages // However, Select statements dont cascade like in C or Java var inputOption = "anOption"; @@ -229,10 +235,7 @@ select( inputOption ){ } } -// Loops -// While Loops // While loops and Do-While loops are basically the same in every language. - var j: int = 1; var jSum: int = 0; while( j <= 1000 ){ @@ -248,11 +251,10 @@ do{ }while( j <= 10000 ); writeln( jSum ); -// For loops + // For loops are much like those in python in that they iterate over a range. -// ranges themselves are types, and can be stuffed into variables +// Ranges themselves are types, and can be stuffed into variables // (more about that later) - for i in 1..10 do write( i , ", ") ; writeln(); @@ -273,14 +275,23 @@ for x in 1..10 { // For-loops and arrays both use ranges and domains to // define an index set that can be iterated over. // Ranges are single dimensional -// Domains can be multi-dimensional and represent indicies -// of different types as well. +// Domains can be multi-dimensional and can +// represent indicies of different types as well. // They are types, and can be assigned into variables; var range1to10: range = 1..10; // // 1, 2, 3, ... , 10 +var range2to11 = 2..11; // 2, 3, 4, ..., 11 + +//ranges can be unbounded +var range1toInf: range(boundedType=BoundedRangeType.boundedLow) = 1.. ; // 1, 2, 3, 4, 5, ... +var rangeNegInfto1 = ..1; // ..., -4, -3, -2, -1, 0, 1 +// Note: the range(boundedType= ... ) is only +// necessary if we explicitly type the variable // Ranges can be strided using the 'by' operator. -// Note: the stridable=true is only necessary if we type the variable var range2to10by2: range(stridable=true) = 2..10 by 2; // 2, 4, 6, 8, 10 +var reverse2to10by2 = 10..2 by -2; // 10, 8, 6, 4, 2 +// Note: the range(stridable=true) is only +// necessary if we explicitly type the variable // The end point of a range can be determined using the count (#) operator var rangeCount: range = -5..#12; // range from -5 to 6 @@ -299,9 +310,11 @@ for i in rangeCountBy{ write( i, if i == rangeCountBy.last then "\n" else ", " ); } -// domains are similarly defined using range notation -var domain1to10: domain(1) = {1..10}; // domain from 1..10; -var twoDimensions: domain(2) = {-2..2,0..2}; // domain over two dimensions +// Rectangular domains are similarly defined using range notation +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 thirdDim: range = 1..16; +var threeDims: domain(3) = {thirdDim, 1..10, 5..10}; // using a range variable // Can iterate over the indices as tuples for idx in twoDimensions do @@ -310,38 +323,37 @@ writeln(); // Or can deconstruct the tuple for (x,y) in twoDimensions { - write( (x,y), ", " ); + write( "(", x, ", ", y, ")", ", " ); } writeln(); // Associative domains act like sets -var intSet: domain(int); // empty set of ints -intSet += 1; -intSet += 2; -intSet += 3; -intSet += 1; // redundant add 1 -intSet -= 3; // remove 3 -writeln( intSet ); +var stringSet: domain(string); // empty set of strings +stringSet += "a"; +stringSet += "b"; +stringSet += "c"; +stringSet += "a"; // redundant add "a" +stringSet -= "c"; // remove "c" +writeln( stringSet ); -// Arrays // Array are similar to those of other languages. -// Their sizes are defined using ranges and domains. -// that represent their indices, but we'll touch more on those later -var intArray: [1..10] int; // array of integers defined using range literal +// Their sizes are defined using domains that represent their indices +var intArray: [1..10] int; +var intArray2: [{1..10}] int; //equivalent // Accessed using bracket notation for i in 1..10 do intArray[i] = -i; writeln( intArray ); // we cannot access intArray[0] because it exists outside -// of the index set we defined (1..10) +// of the index set, {1..10}, we defined it to have // intArray[11] is illegal for the same reason. var realDomain: domain(2) = {1..5,1..7}; var realArray: [realDomain] real; -// similarly we could have done: -// var realArray: [1..5,1..7] real; +var realArray2: [1..5,1..7] real; // equivalent +var realArray3: [{1..5,1..7}] real; // equivalent for i in 1..5 { // use the range from 2nd dimension of the domain @@ -353,7 +365,7 @@ for i in 1..5 { } // arrays have domains as members that we can iterate over -for idx in realArray.domain { // idx is, again, a 2*int tuple +for idx in realArray.domain { // again, idx is a 2*int tuple realArray[idx] = 1 / realArray[idx[1],idx[2]]; // access by tuple and list } @@ -373,16 +385,13 @@ var dict: [dictDomain] int = [ "one" => 1, "two" => 2 ]; dict["three"] = 3; writeln( dict ); - -// Procedures // Chapel procedures have similar syntax to other languages functions. - proc fibonacci( n : int ) : int { - if ( n == 0 || n == 1 ) then return n; + if ( n <= 1 ) then return n; return fibonacci( n-1 ) + fibonacci( n-2 ); } -// input parameters can be untyped +// input parameters can be untyped (a generic procedure) proc doublePrint( thing ): void { write( thing, " ", thing, "\n"); } @@ -394,7 +403,7 @@ proc addThree( n ) { doublePrint( addThree( fibonacci( 20 ) ) ); -// Can also take unlimited number of parameters +// Can also take 'unlimited' number of parameters proc maxOf( x ...?k ) { // x refers to a tuple of one type, with k elements var maximum = x[1]; @@ -404,7 +413,7 @@ proc maxOf( x ...?k ) { writeln( maxOf( 1, -10, 189, -9071982, 5, 17, 20001, 42 ) ); // the ? operator is called the query operator, and is used to take -// undetermined values (like tuple and array sizes, and generic types). +// undetermined values (like tuple or array sizes, and generic types). // Taking arrays as parameters. // The query operator is used to determine the domain of A. @@ -427,9 +436,9 @@ writeln( defaultsProc( x=11 ) ); writeln( defaultsProc( x=12, y=5.432 ) ); writeln( defaultsProc( y=9.876, x=13 ) ); -// Generic procedures can still retain type -// Here we define a procedure that takes two arguments -// of the same type, yet we dont define what that type is. +// We can query the type of arguments to make safer generic procedures +// Here we define a procedure that takes two arguments of +// the same type, yet we dont define what that type is. proc genericProc( arg1 : ?valueType, arg2 : valueType ): void { select( valueType ){ when int do writeln( arg1, " and ", arg2, " are ints" ); @@ -481,24 +490,24 @@ writeln( false ^ true ); writeln( true ^ false ); writeln( false ^ false ); -// Define a * operator on any two types. +// Define a * operator on any two types that returns a tupe of those types proc *( left : ?ltype, right : ?rtype): ( ltype, rtype ){ return (left, right ); } writeln( 1 * "a" ); // uses our * operator -writeln( 1 * 2 ); // uses the original * operator +writeln( 1 * 2 ); // uses the original * operator /* -Note: You could break everything if you - get careless with your overloads. -This here will break everything. Dont do it. +Note: You could break everything if you get careless with your overloads. +This here will break everything. Don't do it. proc +( left: int, right: int ): int{ return left - right; } */ -// Classes +// Classes are similar to those in C++ and Java. +// They currently lack privatization class MyClass { // Member variables var memberInt : int; @@ -541,8 +550,8 @@ writeln( myObject.getMemberInt() ); // ... using our values var myDiffObject = new MyClass( -1, true ); myDiffObject = new MyClass( memberInt = -1, - memberBool = false ); // equivalent -writeln( (myDiffObject.getMemberInt(), myDiffObject.getMemberBool() )); + memberBool = true ); // equivalent +writeln( myDiffObject ); // Construct using written constructor var myOtherObject = new MyClass( 1.95 ); @@ -557,7 +566,7 @@ proc +( A : MyClass, B : MyClass) : MyClass { } var plusObject = myObject + myDiffObject; -writeln( (plusObject.getMemberInt(), plusObject.getMemberBool() ) ); +writeln( plusObject ); // destruction delete myObject; @@ -609,9 +618,11 @@ class GenericClass { } var realList = new GenericClass( real, 10 ); -// We can assign to the array in the object using the bracket notation +// We can assign to the member array of the object using the bracket +// notation that we defined ( proc this( i: int ){ ... } ) for i in realList.classDomain do realList[i] = i + 1.0; -// We can iterate over a +// We can iterate over the values in our list with the iterator +// we defined ( iter these(){ ... } ) for value in realList do write( value, ", " ); writeln(); @@ -626,11 +637,10 @@ for value in copyNewTypeList do write( value, ", " ); writeln(); -// Tasks -// A task is some work that will be done separately from -// the current task, and (if there are any available) in its own thread. +// A task is some work that will be done separately from the current +// task, and (if there are any available) in its own thread. -// a synch statement will ensure that the progress of the +// a sync statement will ensure that the progress of the // main task will not progress until the children have synced back up. sync { // a begin statement will spin the body off into one new task @@ -661,20 +671,19 @@ cobegin { writeln( "a whole" ); } } -// Notice here that the prints may happen in any order. +// Notice here that the prints from each statement may happen in any order. // Coforall loop will create a new task for EACH iteration -// NOTE! coforall should be used only for creating tasks! -// Using it to iterating over an array or something like that is very a bad idea! - var num_tasks = 10; // Number of tasks we want coforall taskID in 1..#num_tasks { writeln( "Hello from task# ", taskID ); } // Again we see that prints happen in any order. +// NOTE! coforall should be used only for creating tasks! +// Using it to iterating over a structure is very a bad idea! // forall loops are another parallel loop, but only create a smaller number -// of tasks, specifically dataParTasksPerLocale number of task (more later) +// of tasks, specifically --dataParTasksPerLocale=number of task forall i in 1..100 { write( i, ", "); } @@ -715,8 +724,8 @@ timer.clear(); // the parallel loop went faster than the serial loop // A succinct way of writing a forall loop over an array: -[ val in myBigArray ] val = 1 / val; // iterate over values -// or -[ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; // iterate over indicies - +// iterate over values +[ val in myBigArray ] val = 1 / val; +// or iterate over indicies +[ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; ``` -- cgit v1.2.3 From 6a6673b1540eda903981bdaccc844a5fc802a216 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Tue, 14 Jul 2015 17:32:00 -0700 Subject: Fixed almost pi 22/7ths is closer to pi than 11/7ths. Almost twice as close! --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 6382f75a..fd01b0f2 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -91,7 +91,7 @@ var intFromReal = myReal : int; var intFromReal2: int = myReal : int; // consts are constants, they cannot be changed after set in runtime -const almostPi: real = 11.0/7.0; +const almostPi: real = 22.0/7.0; // params are constants whose value must be known statically at compile time // Like consts, they cannot be changed during runtime param compileTimeConst: int = 16; -- cgit v1.2.3 From e8b259f4c6fa71cea3ba9d0f65469b2a5eb56da6 Mon Sep 17 00:00:00 2001 From: "ian.bertolacci" Date: Tue, 14 Jul 2015 17:44:55 -0700 Subject: Moved majority of header to bottom. Less preachy? --- chapel.html.markdown | 77 +++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 34 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index fd01b0f2..799d32c3 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -5,43 +5,11 @@ contributors: - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"] lang: en --- -What is Chapel? -=============== + You can read all about chapel at [Cray's official Chapel website](http://chapel.cray.com). In short, Chapel is an open-source, high-productivity, parallel-programming language in development at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. -Your input, questions, and discoveries are important to the developers! ------------------------------------------------------------------------ -Chapel is currently in-development so there are occasional hiccups with performance and language features. -The more information you give the Chapel development team about issues you encounter with the language, the better the language gets. -Feel free to email the team and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman). - -If you're really interested in the development of the compiler or contributing to the project, -[check out the master Github repository](https://github.com/chapel-lang/chapel). - -Installing the Compiler ------------------------ -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/) -and its as easy as - 1. ```tar -xvf chapel-1.11.0.tar.gz``` - 2. ```cd chapel-1.11.0``` - 3. ```make``` - 4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` - -You will need to ```source util/setchplenv.EXT``` from the chapel directory 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 - 1. ```brew update``` - 2. ```brew install chapel``` - -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. -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. - -Occasionally check here back to see if more topics have been added. +More information and support can be found at the bottom of this document. ```chapel // Comments are C-family style @@ -729,3 +697,44 @@ timer.clear(); // or iterate over indicies [ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; ``` +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. +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. + +Occasionally check back here and on the Chapel site to see if more topics have been added or more tutorials created. + +Your input, questions, and discoveries are important to the developers! +----------------------------------------------------------------------- +The Chapel language is still in-development (version 1.11.0), so there are occasional hiccups with performance and language features. +The more information you give the Chapel development team about issues you encounter or features you would like to see, the better the language becomes. +Feel free to email the team and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman). + +If you're really interested in the development of the compiler or contributing to the project, +[check out the master Github repository](https://github.com/chapel-lang/chapel). +It is under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0). + +Installing the Compiler +----------------------- +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/) +and its as easy as + 1. ```tar -xvf chapel-1.11.0.tar.gz``` + 2. ```cd chapel-1.11.0``` + 3. ```make``` + 4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` + +You will need to ```source util/setchplenv.EXT``` from the chapel directory 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 + 1. ```brew update``` + 2. ```brew install chapel``` + +Compiling Code +-------------- +Builds like other compilers +```chpl myFile.chpl -o myExe`` + +A notable argument: + * ``--fast``: enables a number of optimizations and disables array bounds checks. Only enable when application is stable. -- cgit v1.2.3 From 343ae6ca1bd908aedfabe1dad948e4efff449628 Mon Sep 17 00:00:00 2001 From: "ian.bertolacci" Date: Tue, 14 Jul 2015 17:49:59 -0700 Subject: Almost last update. Small changes to wording --- chapel.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 799d32c3..5633214a 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -703,7 +703,7 @@ This tutorial is for people who want to learn the ropes of chapel without having 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. -Occasionally check back here and on the Chapel site 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. Your input, questions, and discoveries are important to the developers! ----------------------------------------------------------------------- @@ -734,7 +734,7 @@ Chapel is easily installed with Brew for OS X Compiling Code -------------- Builds like other compilers -```chpl myFile.chpl -o myExe`` +```chpl myFile.chpl -o myExe``` A notable argument: - * ``--fast``: enables a number of optimizations and disables array bounds checks. Only enable when application is stable. + * ``--fast``: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. -- cgit v1.2.3 From 2e7ebaa3b940ec9de04f2266138abca58becaf30 Mon Sep 17 00:00:00 2001 From: "ian.bertolacci" Date: Tue, 14 Jul 2015 18:40:53 -0700 Subject: A number of edits. Some cosmetic. Added iterators, argument/return intents --- chapel.html.markdown | 129 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 87 insertions(+), 42 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 5633214a..ecbab2a2 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -28,7 +28,6 @@ writeln( "There are ", 3, " commas (\",\") in this line of code" ); stdout.writeln( "This goes to standard output (just like plain writeln() does)"); stderr.writeln( "This goes to standard error" ); -// Variables // Variables dont have to be explicitly typed as long as // the compiler can figure out the type that it will hold. var myVar = 10; // 10 is an int, so myVar is implicitly an int @@ -123,39 +122,31 @@ writeln( (old_this == thatInt) && (old_that == thisInt) ); // We can also define operator overloads, // which we'll cover with procedures. -// Tuples -// tuples can be of the same type +// Tuples can be of the same type var sameTup: 2*int = (10,-1); +var sameTup2 = (11, -6); // or different types var diffTup: (int,real,complex) = (5, 1.928, myCplx); +var diffTupe2 = ( 7, 5.64, 6.0+1.5i ); // Accessed using array bracket notation // However, tuples are all 1-indexed writeln( "(", sameTup[1], ",", sameTup[2], ")" ); writeln( diffTup ); // Tuples can also be written into. diffTup[1] = -1; -// you can expand tuples as well +// Can expand tuple values into their own variables var (tupInt, tupReal, tupCplx) = diffTup; writeln( diffTup == (tupInt, tupReal, tupCplx) ); -// Can also be used to easily write a collection -// of variables as a list (common in debugging) +// Useful for writing a list of variables ( as is common in debugging) writeln( (a,b,thisInt,thatInt,thisBool,thatBool) ); - // Type aliasing type chroma = int; // type of a single hue type RGBColor = 3*chroma; // type representing a full color var black: RGBColor = ( 0,0,0 ); var white: RGBColor = ( 255, 255, 255 ); - - -// If-Then statements -// if-thens dont require parentheses around the condition -// as they do in C (however, we will use them) -// A single line body can use the 'then' keyword instead of -// braces and else statements can be written similarly - +// if-then-else works just like any other C-family language if 10 < 100 then writeln( "All is well" ); @@ -164,7 +155,6 @@ if -1 < 1 then else writeln( "Send mathematician, something's wrong" ); - if ( 10 > 100 ) { writeln( "Universe broken. Please reboot universe." ); } @@ -219,7 +209,6 @@ do{ }while( j <= 10000 ); writeln( jSum ); - // For loops are much like those in python in that they iterate over a range. // Ranges themselves are types, and can be stuffed into variables // (more about that later) @@ -245,9 +234,11 @@ for x in 1..10 { // Ranges are single dimensional // Domains can be multi-dimensional and can // represent indicies of different types as well. -// They are types, and can be assigned into variables; -var range1to10: range = 1..10; // // 1, 2, 3, ... , 10 +// They are first-class citizen types, and can be assigned into variables +var range1to10: range = 1..10; // 1, 2, 3, ..., 10 var range2to11 = 2..11; // 2, 3, 4, ..., 11 +var rangeThistoThat: range = thisInt..thatInt; // using variables +var rangeEmpty: range = 100..-100 ; // this is a valid, but empty range //ranges can be unbounded var range1toInf: range(boundedType=BoundedRangeType.boundedLow) = 1.. ; // 1, 2, 3, 4, 5, ... @@ -257,10 +248,12 @@ var rangeNegInfto1 = ..1; // ..., -4, -3, -2, -1, 0, 1 // Ranges can be strided using the 'by' operator. var range2to10by2: range(stridable=true) = 2..10 by 2; // 2, 4, 6, 8, 10 -var reverse2to10by2 = 10..2 by -2; // 10, 8, 6, 4, 2 // Note: the range(stridable=true) is only // necessary if we explicitly type the variable +// Use by to create a reverse range +var reverse2to10by2 = 10..2 by -2; // 10, 8, 6, 4, 2 + // The end point of a range can be determined using the count (#) operator var rangeCount: range = -5..#12; // range from -5 to 6 @@ -278,7 +271,7 @@ for i in rangeCountBy{ write( i, if i == rangeCountBy.last then "\n" else ", " ); } -// Rectangular domains are similarly defined using range notation +// Rectangular domains are defined using the same range syntax 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 thirdDim: range = 1..16; @@ -324,8 +317,7 @@ var realArray2: [1..5,1..7] real; // equivalent var realArray3: [{1..5,1..7}] real; // equivalent for i in 1..5 { - // use the range from 2nd dimension of the domain - for j in realDomain.dim(2) { + for j in realDomain.dim(2) { // only use the 2nd dimension of the domain realArray[i,j] = -1.61803 * i + 0.5 * j; // access using index list var idx: 2*int = (i,j); // note: 'index' is a keyword realArray[idx] = - realArray[(i,j)]; // index using tuples @@ -351,7 +343,7 @@ writeln( rSum, "\n", realArray ); var dictDomain: domain(string) = { "one", "two" }; var dict: [dictDomain] int = [ "one" => 1, "two" => 2 ]; dict["three"] = 3; -writeln( dict ); +for key in dictDomain do writeln( dict[key] ); // Chapel procedures have similar syntax to other languages functions. proc fibonacci( n : int ) : int { @@ -404,7 +396,40 @@ writeln( defaultsProc( x=11 ) ); writeln( defaultsProc( x=12, y=5.432 ) ); writeln( defaultsProc( y=9.876, x=13 ) ); -// We can query the type of arguments to make safer generic procedures +// intent modifiers on the arguments convey how +// those arguments are passed to the procedure +// in: copy arg in, but not out +// out: copy arg out, but not in +// inout: copy arg in, copy arg out +// ref: pass arg by reference +proc intentsProc( in inarg, out outarg, inout inoutarg, ref refarg ){ + writeln( "Inside Before: ", (inarg, outarg, inoutarg, refarg) ); + inarg = inarg + 100; + outarg = outarg + 100; + inoutarg = inoutarg + 100; + refarg = refarg + 100; + writeln( "Inside After: ", (inarg, outarg, inoutarg, refarg) ); +} + +var inVar: int = 1; +var outVar: int = 2; +var inoutVar: int = 3; +var refVar: int = 4; +writeln( "Outside Before: ", (inVar, outVar, inoutVar, refVar) ); +intentsProc( inVar, outVar, inoutVar, refVar ); +writeln( "Outside After: ", (inVar, outVar, inoutVar, refVar) ); + +// Similarly we can define intents on the return type +proc makeArray( elems: int, startNumber: int ) ref : [1..#elems] int { + var array: [1..#elems] int; + for idx in array.domain do array[idx] = startNumber + idx; + return array; +} +writeln( makeArray( 10, -1 ) ); +// this makes more practical sense for class methods where references to +// elements in a data-structure are returned via a method or iterator + +// We can query the type of arguments to generic procedures // Here we define a procedure that takes two arguments of // the same type, yet we dont define what that type is. proc genericProc( arg1 : ?valueType, arg2 : valueType ): void { @@ -421,9 +446,8 @@ genericProc( 1.0+2.0i, 3.0+4.0i ); // We can also enforce a form of polymorphism with the 'where' clause // This allows the compiler to decide which function to use. -// Note: that means that all information needs to be known at compile -// time. Hence, we use params here to assert that the arguments must -// be known at compile time. +// Note: that means that all information needs to be known at compile time. +// The param modifier on the arg is used to enforce this constraint. proc whereProc( param N : int ): void where ( N > 0 ) { writeln( "N is greater than 0" ); @@ -474,6 +498,25 @@ proc +( left: int, right: int ): int{ } */ +// iterators are a close cousin to the procedure. +// However, instead of returning a single value, +// iterators yield many values to a loop. +// This is useful when a complicated set or order of iterations is needed but +// allows the code defining the iterations to be separate from the loop body. +iter oddsThenEvens( N: int ): int { + for i in 1..N by 2 { + yield i; // yield values instead of returning. + } + + for i in 2..N by 2 { + yield i; + } +} + +for i in oddsThenEvens( 10 ) do write( i, ", " ); +writeln(); + + // Classes are similar to those in C++ and Java. // They currently lack privatization class MyClass { @@ -567,16 +610,18 @@ class GenericClass { type classType = otherType ) { this.classDomain = other.classDomain; // Copy and cast - [ idx in this.classDomain ] this[ idx ] = other[ idx ] : classType; + for idx in this.classDomain do this[ idx ] = other[ idx ] : classType; } - // Define bracket notation on a GenericClass object + // Define bracket notation on a GenericClass + // object so it can behave like a normal array // i.e. objVar[ i ] or objVar( i ) proc this( i : int ) ref : classType { return this.classArray[ i ]; } - // Define an iterator for the class. + // Define an iterator for the class to + // yield values from the array to a loop // i.e. for i in objVar do .... iter these() ref : classType { for i in this.classDomain do @@ -604,19 +649,20 @@ var copyNewTypeList = new GenericClass( realList, int ); for value in copyNewTypeList do write( value, ", " ); writeln(); +// Parallelism +// In other languages, parallelism is typically this is done with +// complicated libraries and strange class structure hierarchies. +// Chapel has it baked right into the language. -// A task is some work that will be done separately from the current -// task, and (if there are any available) in its own thread. - +// 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 task will not progress until the children have synced back up. sync { -// a begin statement will spin the body off into one new task - begin { + begin { // start of new task's body var a = 0; for i in 1..1000 do a += 1; writeln( "Done: ", a); - } + } // end of new tasks body writeln( "spun off a task!"); } writeln( "Back together" ); @@ -625,12 +671,11 @@ proc printFibb( n: int ){ writeln( "fibonacci(",n,") = ", fibonacci( n ) ); } -// a cobegin statement will spin each -// statement of the body into one new task +// a cobegin statement will spin each statement of the body into one new task cobegin { - printFibb( 20 ); - printFibb( 10 ); - printFibb( 5 ); + printFibb( 20 ); // new task + printFibb( 10 ); // new task + printFibb( 5 ); // new task { // this is a nested statement body and thus is a single statement // to the parent statement and is executed by a single task -- cgit v1.2.3 From 7640ea4a306b979912d63d646864da824103c082 Mon Sep 17 00:00:00 2001 From: "ian.bertolacci" Date: Tue, 14 Jul 2015 19:07:59 -0700 Subject: Some rewriting, hopefully the last commit before pull request --- chapel.html.markdown | 276 +++++++++++++++++++++++++++------------------------ 1 file changed, 145 insertions(+), 131 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index ecbab2a2..8b41dc32 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -25,29 +25,30 @@ writeln( "World!" ); // each thing is printed right next to each other, so include your spacing! writeln( "There are ", 3, " commas (\",\") in this line of code" ); // 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" ); // Variables dont have to be explicitly typed as long as // the compiler can figure out the type that it will hold. var myVar = 10; // 10 is an int, so myVar is implicitly an int myVar = -10; +var mySecondVar = myVar; // var anError; // this would be a compile time error. // We can (and should) explicitly type things -var mySecondVar: real; // define mySecondVar as a real -var myThirdVar: real = -1.234; -mySecondVar = myThirdVar; +var myThirdVar: real; // define mySecondVar as a real +var myFourthVar: real = -1.234; +myThirdVar = myFourthVar; // There are a number of basic types. -var myInt: int = -1000; // signed ints -var myUint: uint = 1234; // unsigned ints -var myReal: real = 9.876; // floating point numbers -var myImag: imag = 5.0i; // imaginary numbers -var myCplx: complex = 10 + 9i; // complex numbers -myCplx = myInt + myImag ; // another way to form complex numbers -var myBool: bool = false; // booleans -var myStr: string = "Some string..."; // strings +var myInt: int = -1000; // Signed ints +var myUint: uint = 1234; // Unsigned ints +var myReal: real = 9.876; // Floating point numbers +var myImag: imag = 5.0i; // Imaginary numbers +var myCplx: complex = 10 + 9i; // Complex numbers +myCplx = myInt + myImag ; // Another way to form complex numbers +var myBool: bool = false; // Booleans +var myStr: string = "Some string..."; // Strings // Some types can have sizes var my8Int: int(8) = 10; // 8 bit (one byte) sized int; @@ -59,6 +60,7 @@ var intFromReal2: int = myReal : int; // consts are constants, they cannot be changed after set in runtime const almostPi: real = 22.0/7.0; + // params are constants whose value must be known statically at compile time // Like consts, they cannot be changed during runtime param compileTimeConst: int = 16; @@ -68,47 +70,47 @@ param compileTimeConst: int = 16; // config vars and consts can be changed through the command line at run time config var varCmdLineArg: int = -123; config const constCmdLineArg: int = 777; -// set with --VarName=Value or --VarName Value at run time +// Set with --VarName=Value or --VarName Value at run time // config params can be set at compile time config param paramCmdLineArg: bool = false; writeln( varCmdLineArg, ", ", constCmdLineArg, ", ", paramCmdLineArg ); -//set config with --set paramCmdLineArg=value at compile time +// Set config with --set paramCmdLineArg=value at compile time // Math operators var a: int, thisInt = 1234, thatInt = 5678; a = thisInt + thatInt; // Addition a = thisInt * thatInt; // Multiplication a = thisInt - thatInt; // Subtraction -a = thisInt / thatInt; // division -a = thisInt ** thatInt; // exponentiation -a = thisInt % thatInt; // remainder (modulo) +a = thisInt / thatInt; // Division +a = thisInt ** thatInt; // Exponentiation +a = thisInt % thatInt; // Remainder (modulo) // Logical Operators var b: bool, thisBool = false, thatBool = true; -b = thisBool && thatBool; // logical and -b = thisBool || thatBool; // logical or -b = !thisBool; // logical negation +b = thisBool && thatBool; // Logical and +b = thisBool || thatBool; // Logical or +b = !thisBool; // Logical negation // Relational Operators -b = thisInt > thatInt; // greater-than -b = thisInt >= thatInt; // greater-than-or-equal-to -b = thisInt < a && a <= thatInt; // less-than, and, less-than-or-equal-to -b = thisInt != thatInt; // not-equal-to -b = thisInt == thatInt; // equal-to +b = thisInt > thatInt; // Greater-than +b = thisInt >= thatInt; // Greater-than-or-equal-to +b = thisInt < a && a <= thatInt; // Less-than, and, less-than-or-equal-to +b = thisInt != thatInt; // Not-equal-to +b = thisInt == thatInt; // Equal-to // Bitwise operations -a = thisInt << 10; // left-bit-shift by 10 bits; -a = thatInt >> 5; // right-bit-shift by 5 bits; -a = ~thisInt; // bitwise-negation -a = thisInt ^ thatInt; // bitwise exclusive-or +a = thisInt << 10; // Left-bit-shift by 10 bits; +a = thatInt >> 5; // Right-bit-shift by 5 bits; +a = ~thisInt; // Bitwise-negation +a = thisInt ^ thatInt; // Bitwise exclusive-or // Compound assignment operations -a += thisInt; // addition-equals ( a = a + thisInt;) -a *= thatInt; // times-equals ( a = a * thatInt; ) -b &&= thatBool; // logical-and-equals ( b = b && thatBool; ) -a <<= 3; // left-bit-shift-equals ( a = a << 10; ) -// and so on... +a += thisInt; // Addition-equals ( a = a + thisInt;) +a *= thatInt; // Times-equals ( a = a * thatInt; ) +b &&= thatBool; // Logical-and-equals ( b = b && thatBool; ) +a <<= 3; // LEft-bit-shift-equals ( a = a << 10; ) +// and many, many more. // Unlike other C family languages there are no // pre/post-increment/decrement operators like // ++j, --j, j++, j-- @@ -119,8 +121,7 @@ var old_that = thatInt; thisInt <=> thatInt; // Swap the values of thisInt and thatInt writeln( (old_this == thatInt) && (old_that == thisInt) ); -// We can also define operator overloads, -// which we'll cover with procedures. +// Operator overloads can also be defined, as we'll see with procedures // Tuples can be of the same type var sameTup: 2*int = (10,-1); @@ -128,25 +129,29 @@ var sameTup2 = (11, -6); // or different types var diffTup: (int,real,complex) = (5, 1.928, myCplx); var diffTupe2 = ( 7, 5.64, 6.0+1.5i ); + // Accessed using array bracket notation // However, tuples are all 1-indexed writeln( "(", sameTup[1], ",", sameTup[2], ")" ); writeln( diffTup ); + // Tuples can also be written into. diffTup[1] = -1; + // Can expand tuple values into their own variables var (tupInt, tupReal, tupCplx) = diffTup; writeln( diffTup == (tupInt, tupReal, tupCplx) ); + // Useful for writing a list of variables ( as is common in debugging) writeln( (a,b,thisInt,thatInt,thisBool,thatBool) ); // Type aliasing -type chroma = int; // type of a single hue -type RGBColor = 3*chroma; // type representing a full color +type chroma = int; // Type of a single hue +type RGBColor = 3*chroma; // Type representing a full color var black: RGBColor = ( 0,0,0 ); var white: RGBColor = ( 255, 255, 255 ); -// if-then-else works just like any other C-family language +// If-then-else works just like any other C-family language if 10 < 100 then writeln( "All is well" ); @@ -193,16 +198,16 @@ select( inputOption ){ } } -// While loops and Do-While loops are basically the same in every language. +// While and Do-While loops are basically the same in every language. var j: int = 1; var jSum: int = 0; while( j <= 1000 ){ jSum += j; - j += 1; // there are no ++j, --j, j++, j--, operators + j += 1; } writeln( jSum ); -// basic Do-While loop +// Do-While loop do{ jSum += j; j += 1; @@ -213,7 +218,7 @@ writeln( jSum ); // Ranges themselves are types, and can be stuffed into variables // (more about that later) for i in 1..10 do write( i , ", ") ; -writeln(); +writeln( ); var iSum: int = 0; for i in 1..1000 { @@ -225,7 +230,7 @@ for x in 1..10 { for y in 1..10 { write( (x,y), "\t" ); } - writeln(); + writeln( ); } // Ranges and Domains @@ -238,14 +243,16 @@ for x in 1..10 { var range1to10: range = 1..10; // 1, 2, 3, ..., 10 var range2to11 = 2..11; // 2, 3, 4, ..., 11 var rangeThistoThat: range = thisInt..thatInt; // using variables -var rangeEmpty: range = 100..-100 ; // this is a valid, but empty range +var rangeEmpty: range = 100..-100 ; // this is valid but contains no indices -//ranges can be unbounded -var range1toInf: range(boundedType=BoundedRangeType.boundedLow) = 1.. ; // 1, 2, 3, 4, 5, ... -var rangeNegInfto1 = ..1; // ..., -4, -3, -2, -1, 0, 1 +// Ranges can be unbounded +var range1toInf: range(boundedType=BoundedRangeType.boundedLow) = 1.. ; +// 1, 2, 3, 4, 5, ... // Note: the range(boundedType= ... ) is only // necessary if we explicitly type the variable +var rangeNegInfto1 = ..1; // ..., -4, -3, -2, -1, 0, 1 + // Ranges can be strided using the 'by' operator. var range2to10by2: range(stridable=true) = 2..10 by 2; // 2, 4, 6, 8, 10 // Note: the range(stridable=true) is only @@ -280,24 +287,23 @@ var threeDims: domain(3) = {thirdDim, 1..10, 5..10}; // using a range variable // Can iterate over the indices as tuples for idx in twoDimensions do write( idx , ", "); -writeln(); +writeln( ); -// Or can deconstruct the tuple +// or can deconstruct the tuple for (x,y) in twoDimensions { write( "(", x, ", ", y, ")", ", " ); } -writeln(); +writeln( ); // Associative domains act like sets var stringSet: domain(string); // empty set of strings stringSet += "a"; stringSet += "b"; stringSet += "c"; -stringSet += "a"; // redundant add "a" -stringSet -= "c"; // remove "c" +stringSet += "a"; // Redundant add "a" +stringSet -= "c"; // Remove "c" writeln( stringSet ); - // Array are similar to those of other languages. // Their sizes are defined using domains that represent their indices var intArray: [1..10] int; @@ -307,35 +313,35 @@ var intArray2: [{1..10}] int; //equivalent for i in 1..10 do intArray[i] = -i; writeln( intArray ); -// we cannot access intArray[0] because it exists outside -// of the index set, {1..10}, we defined it to have +// We cannot access intArray[0] because it exists outside +// of the index set, {1..10}, we defined it to have. // intArray[11] is illegal for the same reason. var realDomain: domain(2) = {1..5,1..7}; var realArray: [realDomain] real; -var realArray2: [1..5,1..7] real; // equivalent -var realArray3: [{1..5,1..7}] real; // equivalent +var realArray2: [1..5,1..7] real; // Equivalent +var realArray3: [{1..5,1..7}] real; // Equivalent for i in 1..5 { - for j in realDomain.dim(2) { // only use the 2nd dimension of the domain - realArray[i,j] = -1.61803 * i + 0.5 * j; // access using index list - var idx: 2*int = (i,j); // note: 'index' is a keyword - realArray[idx] = - realArray[(i,j)]; // index using tuples + for j in realDomain.dim(2) { // Only use the 2nd dimension of the domain + realArray[i,j] = -1.61803 * i + 0.5 * j; // Access using index list + var idx: 2*int = (i,j); // Note: 'index' is a keyword + realArray[idx] = - realArray[(i,j)]; // Index using tuples } } -// arrays have domains as members that we can iterate over -for idx in realArray.domain { // again, idx is a 2*int tuple - realArray[idx] = 1 / realArray[idx[1],idx[2]]; // access by tuple and list +// Arrays have domains as members that we can iterate over +for idx in realArray.domain { // Again, idx is a 2*int tuple + realArray[idx] = 1 / realArray[idx[1],idx[2]]; // Access by tuple and list } writeln( realArray ); -// can also iterate over the values of an array +// Can also iterate over the values of an array var rSum: real = 0; for value in realArray { - rSum += value; // read a value - value = rSum; // write a value + rSum += value; // Read a value + value = rSum; // Write a value } writeln( rSum, "\n", realArray ); @@ -351,12 +357,12 @@ proc fibonacci( n : int ) : int { return fibonacci( n-1 ) + fibonacci( n-2 ); } -// input parameters can be untyped (a generic procedure) +// Input parameters can be untyped (a generic procedure) proc doublePrint( thing ): void { write( thing, " ", thing, "\n"); } -// return type can be inferred (as long as the compiler can figure it out) +// Return type can be inferred (as long as the compiler can figure it out) proc addThree( n ) { return n + 3; } @@ -372,12 +378,12 @@ proc maxOf( x ...?k ) { } writeln( maxOf( 1, -10, 189, -9071982, 5, 17, 20001, 42 ) ); -// the ? operator is called the query operator, and is used to take +// The ? operator is called the query operator, and is used to take // undetermined values (like tuple or array sizes, and generic types). // Taking arrays as parameters. // The query operator is used to determine the domain of A. -// this is important to define the return type (if you wanted to) +// This is important to define the return type (if you wanted to) proc invertArray( A: [?D] int ): [D] int{ for a in A do a = -a; return A; @@ -396,7 +402,7 @@ writeln( defaultsProc( x=11 ) ); writeln( defaultsProc( x=12, y=5.432 ) ); writeln( defaultsProc( y=9.876, x=13 ) ); -// intent modifiers on the arguments convey how +// Intent modifiers on the arguments convey how // those arguments are passed to the procedure // in: copy arg in, but not out // out: copy arg out, but not in @@ -426,7 +432,7 @@ proc makeArray( elems: int, startNumber: int ) ref : [1..#elems] int { return array; } writeln( makeArray( 10, -1 ) ); -// this makes more practical sense for class methods where references to +// This makes more practical sense for class methods where references to // elements in a data-structure are returned via a method or iterator // We can query the type of arguments to generic procedures @@ -462,17 +468,17 @@ whereProc( 10 ); whereProc( -1 ); // whereProc( 0 ) would result in a compiler error because there // are no functions that satisfy the where clause's condition. -// We could have defined a whereProc without a where clause that would -// then have been called. +// We could have defined a whereProc without a where clause that would then have +// served as a catch all for all the other cases (of which there is only one). -// Operator definitions are through procedures as well -// we can define the unary operators: +// Operator definitions are through procedures as well. +// We can define the unary operators: // + - ! ~ // and the binary operators: // + - * / % ** == <= >= < > << >> & | ˆ by // += -= *= /= %= **= &= |= ˆ= <<= >>= <=> -// boolean exclusive or operator +// Boolean exclusive or operator proc ^( left : bool, right : bool ): bool { return (left || right) && !( left && right ); } @@ -487,8 +493,8 @@ proc *( left : ?ltype, right : ?rtype): ( ltype, rtype ){ return (left, right ); } -writeln( 1 * "a" ); // uses our * operator -writeln( 1 * 2 ); // uses the original * operator +writeln( 1 * "a" ); // Uses our * operator +writeln( 1 * 2 ); // Uses the default * operator /* Note: You could break everything if you get careless with your overloads. @@ -498,24 +504,21 @@ proc +( left: int, right: int ): int{ } */ -// iterators are a close cousin to the procedure. +// Iterators are a sisters to the procedure, and almost +// everything about procedures also applies to iterators // However, instead of returning a single value, // iterators yield many values to a loop. // This is useful when a complicated set or order of iterations is needed but // allows the code defining the iterations to be separate from the loop body. iter oddsThenEvens( N: int ): int { - for i in 1..N by 2 { + for i in 1..N by 2 do yield i; // yield values instead of returning. - } - - for i in 2..N by 2 { + for i in 2..N by 2 do yield i; - } } for i in oddsThenEvens( 10 ) do write( i, ", " ); -writeln(); - +writeln( ); // Classes are similar to those in C++ and Java. // They currently lack privatization @@ -548,7 +551,7 @@ class MyClass { return this.memberInt; } - proc getMemberBool(): bool { + proc getMemberBool( ): bool { return this.memberBool; } @@ -556,30 +559,30 @@ class MyClass { // Construct using default constructor, using default values var myObject = new MyClass( 10 ); - myObject = new MyClass( memberInt = 10 ); // equivalent -writeln( myObject.getMemberInt() ); + myObject = new MyClass( memberInt = 10 ); // Equivalent +writeln( myObject.getMemberInt( ) ); // ... using our values var myDiffObject = new MyClass( -1, true ); myDiffObject = new MyClass( memberInt = -1, - memberBool = true ); // equivalent + memberBool = true ); // Equivalent writeln( myDiffObject ); // Construct using written constructor var myOtherObject = new MyClass( 1.95 ); - myOtherObject = new MyClass( val = 1.95 ); // equivalent -writeln( myOtherObject.getMemberInt() ); + myOtherObject = new MyClass( val = 1.95 ); // Equivalent +writeln( myOtherObject.getMemberInt( ) ); // We can define an operator on our class as well but // the definition has to be outside the class definition proc +( A : MyClass, B : MyClass) : MyClass { - return new MyClass( memberInt = A.getMemberInt() + B.getMemberInt(), - memberBool = A.getMemberBool() || B.getMemberBool() ); + return new MyClass( memberInt = A.getMemberInt( ) + B.getMemberInt( ), + memberBool = A.getMemberBool( ) || B.getMemberBool( ) ); } var plusObject = myObject + myDiffObject; writeln( plusObject ); -// destruction +// Destruction delete myObject; delete myDiffObject; delete myOtherObject; @@ -620,10 +623,10 @@ class GenericClass { return this.classArray[ i ]; } - // Define an iterator for the class to - // yield values from the array to a loop + // Define an implicit iterator for the class + // to yield values from the array to a loop // i.e. for i in objVar do .... - iter these() ref : classType { + iter these( ) ref : classType { for i in this.classDomain do yield this[i]; } @@ -635,19 +638,19 @@ var realList = new GenericClass( real, 10 ); // notation that we defined ( proc this( i: int ){ ... } ) for i in realList.classDomain do realList[i] = i + 1.0; // We can iterate over the values in our list with the iterator -// we defined ( iter these(){ ... } ) +// we defined ( iter these( ){ ... } ) for value in realList do write( value, ", " ); -writeln(); +writeln( ); // Make a copy of realList using the copy constructor var copyList = new GenericClass( realList ); for value in copyList do write( value, ", " ); -writeln(); +writeln( ); -// make a copy of realList and change the type, also using the copy constructor +// Make a copy of realList and change the type, also using the copy constructor var copyNewTypeList = new GenericClass( realList, int ); for value in copyNewTypeList do write( value, ", " ); -writeln(); +writeln( ); // Parallelism // In other languages, parallelism is typically this is done with @@ -655,14 +658,14 @@ writeln(); // Chapel has it baked right into the language. // 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 task will not progress until the children have synced back up. +// A sync statement will ensure that the progress of the main +// task will not progress until the children have synced back up. sync { - begin { // start of new task's body + begin { // Start of new task's body var a = 0; for i in 1..1000 do a += 1; writeln( "Done: ", a); - } // end of new tasks body + } // End of new tasks body writeln( "spun off a task!"); } writeln( "Back together" ); @@ -671,13 +674,13 @@ proc printFibb( n: int ){ writeln( "fibonacci(",n,") = ", fibonacci( n ) ); } -// a cobegin statement will spin each statement of the body into one new task +// A cobegin statement will spin each statement of the body into one new task cobegin { printFibb( 20 ); // new task printFibb( 10 ); // new task printFibb( 5 ); // new task { - // this is a nested statement body and thus is a single statement + // This is a nested statement body and thus is a single statement // to the parent statement and is executed by a single task writeln( "this gets" ); writeln( "executed as" ); @@ -700,41 +703,41 @@ coforall taskID in 1..#num_tasks { forall i in 1..100 { write( i, ", "); } -writeln(); +writeln( ); // Here we see that there are sections that are in order, followed by -// a section that would not follow ( e.g. 1, 2, 3, 7, 8, 9, 4, 5, 6, ) -// this is because each task is taking on a chunk of the range 1..10 +// a section that would not follow ( e.g. 1, 2, 3, 7, 8, 9, 4, 5, 6, ). +// This is because each task is taking on a chunk of the range 1..10 // (1..3, 4..6, or 7..9) doing that chunk serially, but each task happens // in parallel. // Your results may depend on your machine and configuration -// For both the forall and coforall loops, the execution of the parent task -// will not continue until all the children sync up. +// For both the forall and coforall loops, the execution of the +// parent task will not continue until all the children sync up. -// forall loops are particularly useful for parallel iteration over arrays +// forall loops are particularly useful for parallel iteration over arrays. // Lets run an experiment to see how much faster a parallel loop is use Time; // Import the Time module to use Timer objects var timer: Timer; -var myBigArray: [{1..4000,1..4000}] real; // large array we will write into +var myBigArray: [{1..4000,1..4000}] real; // Large array we will write into // Serial Experiment -timer.start(); // start timer -for (x,y) in myBigArray.domain { // serial iteration +timer.start( ); // Start timer +for (x,y) in myBigArray.domain { // Serial iteration myBigArray[x,y] = (x:real) / (y:real); } -timer.stop(); // stop timer -writeln( "Serial: ", timer.elapsed() ); // print elapsed time -timer.clear(); // clear timer for parallel loop +timer.stop( ); // Stop timer +writeln( "Serial: ", timer.elapsed( ) ); // Print elapsed time +timer.clear( ); // Clear timer for parallel loop // Parallel Experiment -timer.start(); // start timer -forall (x,y) in myBigArray.domain { // parallel iteration +timer.start( ); // start timer +forall (x,y) in myBigArray.domain { // Parallel iteration myBigArray[x,y] = (x:real) / (y:real); } -timer.stop(); // stop timer -writeln( "Parallel: ", timer.elapsed() ); // print elapsed time -timer.clear(); -// you may have noticed that (depending on how many cores you have) that -// the parallel loop went faster than the serial loop +timer.stop( ); // Stop timer +writeln( "Parallel: ", timer.elapsed( ) ); // Print elapsed time +timer.clear( ); +// You may have noticed that (depending on how many cores you have) +// that the parallel loop went faster than the serial loop // A succinct way of writing a forall loop over an array: // iterate over values @@ -750,6 +753,17 @@ Refer to the [language specification](http://chapel.cray.com/language.html) and 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: + * Modules and standard modules + * Synchronize and atomic variables + * Multiple Locales (distributed memory system) + * proc main(){ ... } + * Records + * Whole/sliced array assignment + * Reductions and scans + * Range and domain slicing + * Parallel iterators + Your input, questions, and discoveries are important to the developers! ----------------------------------------------------------------------- The Chapel language is still in-development (version 1.11.0), so there are occasional hiccups with performance and language features. -- cgit v1.2.3 From d6f8b8e629b0eb043710864566f894c0ce5f871c Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Tue, 14 Jul 2015 19:09:10 -0700 Subject: Codify proc() main lacking note --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 8b41dc32..2cd55a66 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -757,7 +757,7 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to * Modules and standard modules * Synchronize and atomic variables * Multiple Locales (distributed memory system) - * proc main(){ ... } + * ```proc main(){ ... }``` * Records * Whole/sliced array assignment * Reductions and scans -- cgit v1.2.3 From 37257b592275a5ddb41d7e1d6545aa039b1a35c3 Mon Sep 17 00:00:00 2001 From: "ian.bertolacci" Date: Wed, 15 Jul 2015 12:08:15 -0700 Subject: epiphany on ref returns. lead to better example of ref returns and then also a description of ref vars --- chapel.html.markdown | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 8b41dc32..77efbf5a 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -77,6 +77,15 @@ config param paramCmdLineArg: bool = false; writeln( varCmdLineArg, ", ", constCmdLineArg, ", ", paramCmdLineArg ); // Set config with --set paramCmdLineArg=value at compile time +// refs operate much like a reference in C++ +var actual = 10; +ref refToActual = actual; // refToActual refers to actual +writeln( actual, " == ", refToActual ); // prints the same value +actual = -123; // modify actual (which refToActual refers to) +writeln( actual, " == ", refToActual ); // prints the same value +refToActual = 99999999; // modify what refToActual refers to (which is actual) +writeln( actual, " == ", refToActual ); // prints the same value + // Math operators var a: int, thisInt = 1234, thatInt = 5678; a = thisInt + thatInt; // Addition @@ -426,12 +435,19 @@ intentsProc( inVar, outVar, inoutVar, refVar ); writeln( "Outside After: ", (inVar, outVar, inoutVar, refVar) ); // Similarly we can define intents on the return type -proc makeArray( elems: int, startNumber: int ) ref : [1..#elems] int { - var array: [1..#elems] int; - for idx in array.domain do array[idx] = startNumber + idx; - return array; -} -writeln( makeArray( 10, -1 ) ); +// refElement returns a reference to an element of array +proc refElement( array : [?D] ?T, idx ) ref : T { + return array[ idx ]; // returns a reference to +} + +var myChangingArray : [1..5] int = [1,2,3,4,5]; +writeln( myChangingArray ); +// Store reference to element in ref variable +ref refToElem = refElement( myChangingArray, 5 ); +writeln( refToElem ); +refToElem = -2; // modify reference which modifies actual value in array +writeln( refToElem ); +writeln( myChangingArray ); // This makes more practical sense for class methods where references to // elements in a data-structure are returned via a method or iterator -- cgit v1.2.3 From c22acdf8bd8750b664393422e1e00d4055f69e31 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Fri, 17 Jul 2015 14:45:18 +0900 Subject: Update chapel.html.markdown Make sure nothing breaks (too badly) --- chapel.html.markdown | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index d4d6d648..d4ce4dfa 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -3,7 +3,6 @@ language: chapel filename: learnchapel.chpl contributors: - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"] -lang: en --- You can read all about chapel at [Cray's official Chapel website](http://chapel.cray.com). @@ -11,7 +10,7 @@ In short, Chapel is an open-source, high-productivity, parallel-programming lang More information and support can be found at the bottom of this document. -```chapel +```c // Comments are C-family style // one line comment /* -- cgit v1.2.3 From 4edb920f518327325d092f76a99f901821f35c76 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Fri, 17 Jul 2015 10:09:21 -0700 Subject: capitalization and attempt to fix lists --- chapel.html.markdown | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index d4ce4dfa..f308d1d8 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -1,11 +1,11 @@ --- -language: chapel +language: Chapel filename: learnchapel.chpl contributors: - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"] --- -You can read all about chapel at [Cray's official Chapel website](http://chapel.cray.com). +You can read all about Chapel at [Cray's official Chapel website](http://chapel.cray.com). In short, Chapel is an open-source, high-productivity, parallel-programming language in development at Cray Inc., and is designed to run on multi-core PCs as well as multi-kilocore supercomputers. More information and support can be found at the bottom of this document. @@ -762,7 +762,7 @@ timer.clear( ); ``` 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. Refer to the [language specification](http://chapel.cray.com/language.html) and the [library documentation](http://chapel.cray.com/docs/latest/) for more details. @@ -793,22 +793,27 @@ Installing the Compiler ----------------------- 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/) -and its as easy as - 1. ```tar -xvf chapel-1.11.0.tar.gz``` - 2. ```cd chapel-1.11.0``` - 3. ```make``` - 4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` +and its as easy as + +1. ```tar -xvf chapel-1.11.0.tar.gz``` + +2. ```cd chapel-1.11.0``` + +3. ```make``` + +4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` You will need to ```source util/setchplenv.EXT``` from the chapel directory 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 - 1. ```brew update``` - 2. ```brew install chapel``` + +1. ```brew update``` + +2. ```brew install chapel``` Compiling Code -------------- Builds like other compilers ```chpl myFile.chpl -o myExe``` -A notable argument: - * ``--fast``: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. +A notable argument, ``--fast`` enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. -- cgit v1.2.3 From 4ae7b4921979998152c828a2b70576258a1f1468 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Fri, 17 Jul 2015 10:13:51 -0700 Subject: Changed bash examples from block code Bash examples were in block code form (triple tick), should have been in inline mode (single tic) --- chapel.html.markdown | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index f308d1d8..5a0c3920 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -795,25 +795,25 @@ 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/) and its as easy as -1. ```tar -xvf chapel-1.11.0.tar.gz``` +`tar -xvf chapel-1.11.0.tar.gz` -2. ```cd chapel-1.11.0``` +`cd chapel-1.11.0` -3. ```make``` +`make` -4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` +`source util/setchplenv.bash # or .sh or .csh or .fish` -You will need to ```source util/setchplenv.EXT``` from the chapel directory 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 the chapel directory 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 -1. ```brew update``` +`brew update` -2. ```brew install chapel``` +`brew install chapel` Compiling Code -------------- Builds like other compilers -```chpl myFile.chpl -o myExe``` +`chpl myFile.chpl -o myExe` -A notable argument, ``--fast`` enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. +A notable argument, `--fast` enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. -- cgit v1.2.3 From 77c4ddb522881b4975bff0e6b34e46566336e266 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Fri, 17 Jul 2015 10:15:29 -0700 Subject: Put lists back in Pretty sure what was causing the 'back end' error was my bad markdown skills --- chapel.html.markdown | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 5a0c3920..c6c4a191 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -794,26 +794,21 @@ Installing the Compiler 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/) and its as easy as - -`tar -xvf chapel-1.11.0.tar.gz` - -`cd chapel-1.11.0` - -`make` - -`source util/setchplenv.bash # or .sh or .csh or .fish` + 1. `tar -xvf chapel-1.11.0.tar.gz` + 2. `cd chapel-1.11.0` + 3. `make` + 4. `source util/setchplenv.bash # or .sh or .csh or .fish` You will need to `source util/setchplenv.EXT` from the chapel directory 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 - -`brew update` - -`brew install chapel` + 1. `brew update` + 2. `brew install chapel` Compiling Code -------------- Builds like other compilers + `chpl myFile.chpl -o myExe` A notable argument, `--fast` enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. -- cgit v1.2.3 From e7fe0b267a4c11136f4703493206d5862fa0fb76 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Fri, 17 Jul 2015 10:24:16 -0700 Subject: Spellcheck --- chapel.html.markdown | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index c6c4a191..b03aee7b 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -117,7 +117,7 @@ a = thisInt ^ thatInt; // Bitwise exclusive-or a += thisInt; // Addition-equals ( a = a + thisInt;) a *= thatInt; // Times-equals ( a = a * thatInt; ) b &&= thatBool; // Logical-and-equals ( b = b && thatBool; ) -a <<= 3; // LEft-bit-shift-equals ( a = a << 10; ) +a <<= 3; // Left-bit-shift-equals ( a = a << 10; ) // and many, many more. // Unlike other C family languages there are no // pre/post-increment/decrement operators like @@ -190,7 +190,7 @@ if ( a % 3 == 0 ) { var maximum = if ( thisInt < thatInt ) then thatInt else thisInt; // Select statements are much like switch statements in other languages -// However, Select statements dont cascade like in C or Java +// However, Select statements don't cascade like in C or Java var inputOption = "anOption"; select( inputOption ){ when "anOption" do writeln( "Chose 'anOption'" ); @@ -201,8 +201,6 @@ select( inputOption ){ otherwise { writeln( "Any other Input" ); writeln( "the otherwise case doesn't need a do if the body is one line" ); - writeln( "Oh, and when statements dont cascade like the case statements" ); - writeln( "of other languages" ); } } @@ -246,7 +244,7 @@ for x in 1..10 { // define an index set that can be iterated over. // Ranges are single dimensional // Domains can be multi-dimensional and can -// represent indicies of different types as well. +// represent indices of different types as well. // They are first-class citizen types, and can be assigned into variables var range1to10: range = 1..10; // 1, 2, 3, ..., 10 var range2to11 = 2..11; // 2, 3, 4, ..., 11 @@ -452,7 +450,7 @@ writeln( myChangingArray ); // We can query the type of arguments to generic procedures // Here we define a procedure that takes two arguments of -// the same type, yet we dont define what that type is. +// the same type, yet we don't define what that type is. proc genericProc( arg1 : ?valueType, arg2 : valueType ): void { select( valueType ){ when int do writeln( arg1, " and ", arg2, " are ints" ); @@ -620,10 +618,10 @@ class GenericClass { } // Copy constructor - // Note: We still have to put the the type as an argument, but we can + // Note: We still have to put the type as an argument, but we can // default to the type of the other object using the query (?) operator // Further, we can take advantage of this to allow our copy constructor - // to copy classes of different types + // to copy classes of different types and cast on the fly proc GenericClass( other : GenericClass(?otherType), type classType = otherType ) { this.classDomain = other.classDomain; -- cgit v1.2.3 From 6a54bca6ee74486b6eddadec17b6fc7f0c8bf43b Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Fri, 17 Jul 2015 10:31:32 -0700 Subject: Capitalization, Chapel Directory. --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index b03aee7b..fb54d5a1 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -797,7 +797,7 @@ and its as easy as 3. `make` 4. `source util/setchplenv.bash # or .sh or .csh or .fish` -You will need to `source util/setchplenv.EXT` from the chapel directory 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 1. `brew update` -- cgit v1.2.3 From 71f87d44fd4aae061e0c9d1b52a202a1be4fc332 Mon Sep 17 00:00:00 2001 From: Adam Bard Date: Sat, 18 Jul 2015 09:21:14 +0900 Subject: More line breaks Fix formatting in places --- chapel.html.markdown | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index d4ce4dfa..ec46a083 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -760,8 +760,10 @@ timer.clear( ); // or iterate over indicies [ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; ``` + 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. 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. @@ -769,6 +771,7 @@ Refer to the [language specification](http://chapel.cray.com/language.html) and 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: + * Modules and standard modules * Synchronize and atomic variables * Multiple Locales (distributed memory system) @@ -781,6 +784,7 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to Your input, questions, and discoveries are important to the developers! ----------------------------------------------------------------------- + The Chapel language is still in-development (version 1.11.0), so there are occasional hiccups with performance and language features. The more information you give the Chapel development team about issues you encounter or features you would like to see, the better the language becomes. Feel free to email the team and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman). @@ -791,9 +795,11 @@ It is under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0) Installing the Compiler ----------------------- + 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/) and its as easy as + 1. ```tar -xvf chapel-1.11.0.tar.gz``` 2. ```cd chapel-1.11.0``` 3. ```make``` @@ -802,13 +808,17 @@ and its as easy as You will need to ```source util/setchplenv.EXT``` from the chapel directory 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 + 1. ```brew update``` 2. ```brew install chapel``` Compiling Code -------------- -Builds like other compilers + +Builds like other compilers: + ```chpl myFile.chpl -o myExe``` A notable argument: + * ``--fast``: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. -- cgit v1.2.3 From bad5266d05485cd61ab1aa0884180ab78738cd9b Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sat, 18 Jul 2015 14:16:08 -0700 Subject: Slight modifications. --- chapel.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index c97a5f2b..7ac73bbb 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -1,6 +1,6 @@ --- language: Chapel -filename: learnchapel.chpl +filename: learnChapel.chpl contributors: - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"] --- @@ -27,7 +27,7 @@ writeln( "There are ", 3, " commas (\",\") in this line of code" ); stdout.writeln( "This goes to standard output (just like plain writeln( ) does)"); stderr.writeln( "This goes to standard error" ); -// Variables dont have to be explicitly typed as long as +// Variables don't have to be explicitly typed as long as // the compiler can figure out the type that it will hold. var myVar = 10; // 10 is an int, so myVar is implicitly an int myVar = -10; @@ -540,7 +540,7 @@ class MyClass { var memberInt : int; var memberBool : bool = true; - // Classes have default constructors that dont need to be coded (see below) + // Classes have default constructors that don't need to be coded (see below) // Our explicitly defined constructor proc MyClass( val : real ){ this.memberInt = ceil( val ): int; @@ -771,7 +771,7 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to ### What this tutorial is lacking: * Modules and standard modules - * Synchronize and atomic variables + * Synchronize variables and atomic operations * Multiple Locales (distributed memory system) * ```proc main(){ ... }``` * Records -- cgit v1.2.3 From fe0c14b8ad0f04b1d493fa6612a14d6cc7a48650 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 19 Jul 2015 16:01:04 -0700 Subject: De-capitalization Changed language name and filename back to lower case 'chapel' in keeping with previous name to avoid possible site breakage. --- chapel.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 7ac73bbb..53e9747c 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -1,6 +1,6 @@ --- -language: Chapel -filename: learnChapel.chpl +language: chapel +filename: learnchapel.chpl contributors: - ["Ian J. Bertolacci", "http://www.cs.colostate.edu/~ibertola/"] --- -- cgit v1.2.3 From 155bf5d0a285de2c4dec4033c8e9c20f6a42072b Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 19 Jul 2015 19:59:30 -0700 Subject: Added sync, single, and atomic vars. --- chapel.html.markdown | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 7ac73bbb..4f857775 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -757,6 +757,83 @@ timer.clear( ); [ val in myBigArray ] val = 1 / val; // 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 ); + } +} + +// sync vars have two states: empty and full. +// If you read an empty variable or write a full variable, you are waited +// until the variable is full or empty again +var someSyncVar$: sync int; // varName$ is a convention not a law. +sync { + begin { + writeln( "Waiting to read" ); + var read_sync = someSyncVar$; + writeln( "value is ", read_sync ); + } + + begin { + writeln( "Writing in..." ); + countdown( 3 ); + someSyncVar$ = 123; + } +} + +// single vars can only be written once. A read on an unwritten single results +// in a wait, but when the variable has a value it can be read indefinitely +var someSingleVar$: single int; // varName$ is a convention not a law. +sync { + begin { + writeln( "Waiting to read" ); + for i in 1..5 { + var read_single = someSingleVar$; + writeln( i,"th time around an the value is ", read_single ); + } + } + + begin { + writeln( "Writing in..." ); + countdown( 3 ); + someSingleVar$ = 5; // first and only write ever. + } +} + +// atomic variables can be of type bool, int, uint, and real of any size. +var uranium: atomic int; +uranium.write( 238 ); // atomically write a variable +writeln( uranium.read() ); // atomically read a variable +// operations are described as functions, you could define your own operators. +uranium.sub( 3 ); // atomically subtract a variable +writeln( uranium.read() ); +var replaceWith = 239; +var was = uranium.exchange( replaceWith ); +writeln( "uranium was ", was, " but is now ", replaceWith ); +var isEqualTo = 235; +if uranium.compareExchange( isEqualTo, replaceWith ) { + writeln( "uranium was equal to ", isEqualTo, + " so replaced value with ", replaceWith ); +} else { + writeln( "uranium was not equal to ", isEqualTo, + " value stays the same... whatever it was" ); +} + +sync { + begin { + writeln( "Waiting to for uranium to be ", isEqualTo ); + uranium.waitFor( isEqualTo ); + writeln( "Uranium was set (by someone) to ", isEqualTo ); + } + + begin { + writeln( "Waiting to write uranium to ", isEqualTo ); + countdown( 3 ); + uranium.write( isEqualTo ); + } +} ``` Who is this tutorial for? @@ -771,7 +848,6 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to ### What this tutorial is lacking: * Modules and standard modules - * Synchronize variables and atomic operations * Multiple Locales (distributed memory system) * ```proc main(){ ... }``` * Records -- cgit v1.2.3 From bcc84f9c768041b7f260907d4432e12658b76626 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Mon, 20 Jul 2015 08:09:28 -0700 Subject: Explained atomics, moved atomics first Felt the need to add in an explanation about atomics, then felt that (because atomics are common in language and in theory) it should go before sync and single vars. Should I explain sync and single? --- chapel.html.markdown | 69 +++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 33 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 6fe7c1b0..bbbf50e8 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -765,6 +765,42 @@ proc countdown( seconds: int ){ } } +// Atomic variables, common to many languages, are ones whose operations +// occur uninterupted. Multiple threads can both modify atomic variables +// and can know that their values are safe. +// Chapel atomic variables can be of type bool, int, uint, and real. +var uranium: atomic int; +uranium.write( 238 ); // atomically write a variable +writeln( uranium.read() ); // atomically read a variable +// operations are described as functions, you could define your own operators. +uranium.sub( 3 ); // atomically subtract a variable +writeln( uranium.read() ); +var replaceWith = 239; +var was = uranium.exchange( replaceWith ); +writeln( "uranium was ", was, " but is now ", replaceWith ); +var isEqualTo = 235; +if uranium.compareExchange( isEqualTo, replaceWith ) { + writeln( "uranium was equal to ", isEqualTo, + " so replaced value with ", replaceWith ); +} else { + writeln( "uranium was not equal to ", isEqualTo, + " value stays the same... whatever it was" ); +} + +sync { + begin { + writeln( "Waiting to for uranium to be ", isEqualTo ); + uranium.waitFor( isEqualTo ); + writeln( "Uranium was set (by someone) to ", isEqualTo ); + } + + begin { + writeln( "Waiting to write uranium to ", isEqualTo ); + countdown( 3 ); + uranium.write( isEqualTo ); + } +} + // sync vars have two states: empty and full. // If you read an empty variable or write a full variable, you are waited // until the variable is full or empty again @@ -801,39 +837,6 @@ sync { someSingleVar$ = 5; // first and only write ever. } } - -// atomic variables can be of type bool, int, uint, and real of any size. -var uranium: atomic int; -uranium.write( 238 ); // atomically write a variable -writeln( uranium.read() ); // atomically read a variable -// operations are described as functions, you could define your own operators. -uranium.sub( 3 ); // atomically subtract a variable -writeln( uranium.read() ); -var replaceWith = 239; -var was = uranium.exchange( replaceWith ); -writeln( "uranium was ", was, " but is now ", replaceWith ); -var isEqualTo = 235; -if uranium.compareExchange( isEqualTo, replaceWith ) { - writeln( "uranium was equal to ", isEqualTo, - " so replaced value with ", replaceWith ); -} else { - writeln( "uranium was not equal to ", isEqualTo, - " value stays the same... whatever it was" ); -} - -sync { - begin { - writeln( "Waiting to for uranium to be ", isEqualTo ); - uranium.waitFor( isEqualTo ); - writeln( "Uranium was set (by someone) to ", isEqualTo ); - } - - begin { - writeln( "Waiting to write uranium to ", isEqualTo ); - countdown( 3 ); - uranium.write( isEqualTo ); - } -} ``` Who is this tutorial for? -- cgit v1.2.3 From d2d98a381047d30e0355a8dacd0fdbe1188345c0 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Mon, 20 Jul 2015 08:17:01 -0700 Subject: Spelling error Tupe -> Tuple --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index bbbf50e8..9ce184c0 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -501,7 +501,7 @@ writeln( false ^ true ); writeln( true ^ false ); writeln( false ^ false ); -// Define a * operator on any two types that returns a tupe of those types +// Define a * operator on any two types that returns a tuple of those types proc *( left : ?ltype, right : ?rtype): ( ltype, rtype ){ return (left, right ); } -- cgit v1.2.3 From 2936ca4c69f7b96dfbe7f67de0ebcad910395ba1 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Mon, 20 Jul 2015 12:59:35 -0700 Subject: Erroneous comment removed "define mySecondVar as a real" unnecessary and incorrect. --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 9ce184c0..4cbc5a4b 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -35,7 +35,7 @@ var mySecondVar = myVar; // var anError; // this would be a compile time error. // We can (and should) explicitly type things -var myThirdVar: real; // define mySecondVar as a real +var myThirdVar: real; var myFourthVar: real = -1.234; myThirdVar = myFourthVar; -- cgit v1.2.3 From d35d9d213c3ce3ba1bb53aef896898d87e213d58 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Mon, 20 Jul 2015 13:01:25 -0700 Subject: Word error that -> than --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 4cbc5a4b..b124f54a 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -65,7 +65,7 @@ const almostPi: real = 22.0/7.0; param compileTimeConst: int = 16; // The config modifier allows values to be set at the command line -// and is much easier that the usual getOpts debacle +// and is much easier than the usual getOpts debacle // config vars and consts can be changed through the command line at run time config var varCmdLineArg: int = -123; config const constCmdLineArg: int = 777; -- cgit v1.2.3 From 77daaef8ed6d2fed88405cf038d4e5f0b82dc1ef Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Mon, 20 Jul 2015 13:07:23 -0700 Subject: config param changeup changed some of the wording of the param config example. Namely put the comment immediately below the example. Also added --set to the noteable arguments list. Also changed "compile(?: )?time" to "compile-time" --- chapel.html.markdown | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index b124f54a..520f959d 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -32,7 +32,7 @@ stderr.writeln( "This goes to standard error" ); var myVar = 10; // 10 is an int, so myVar is implicitly an int myVar = -10; var mySecondVar = myVar; -// var anError; // this would be a compile time error. +// var anError; // this would be a compile-time error. // We can (and should) explicitly type things var myThirdVar: real; @@ -57,11 +57,11 @@ var my64Real: real(64) = 1.516; // 64 bit (8 bytes) sized real var intFromReal = myReal : int; var intFromReal2: int = myReal : int; -// consts are constants, they cannot be changed after set in runtime +// consts are constants, they cannot be changed after set in runtime. const almostPi: real = 22.0/7.0; -// params are constants whose value must be known statically at compile time -// Like consts, they cannot be changed during runtime +// params are constants whose value must be known statically at compile-time +// Their value cannot be changed. param compileTimeConst: int = 16; // The config modifier allows values to be set at the command line @@ -71,10 +71,10 @@ config var varCmdLineArg: int = -123; config const constCmdLineArg: int = 777; // Set with --VarName=Value or --VarName Value at run time -// config params can be set at compile time +// config params can be set/changed at compile-time config param paramCmdLineArg: bool = false; +// Set config with --set paramCmdLineArg=value at compile-time writeln( varCmdLineArg, ", ", constCmdLineArg, ", ", paramCmdLineArg ); -// Set config with --set paramCmdLineArg=value at compile time // refs operate much like a reference in C++ var actual = 10; @@ -465,7 +465,7 @@ genericProc( 1.0+2.0i, 3.0+4.0i ); // We can also enforce a form of polymorphism with the 'where' clause // This allows the compiler to decide which function to use. -// Note: that means that all information needs to be known at compile time. +// Note: that means that all information needs to be known at compile-time. // The param modifier on the arg is used to enforce this constraint. proc whereProc( param N : int ): void where ( N > 0 ) { @@ -896,6 +896,7 @@ Builds like other compilers: ```chpl myFile.chpl -o myExe``` -A notable argument: +Notable arguments: * ``--fast``: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. + * ```--set =```: set config param to at compile-time -- cgit v1.2.3 From 3332ce43bb73dcb850250847c6cffdc396338d5e Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Wed, 22 Jul 2015 19:08:22 -0700 Subject: added scans and reductions --- chapel.html.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 520f959d..e058bde9 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -837,6 +837,25 @@ sync { someSingleVar$ = 5; // first and only write ever. } } + +// we can define the operations + * & | ^ && || min max minloc maxloc +// over an entire array using scans and reductions +// Reductions apply the operation over the entire array and +// result in a single value +var listOfValues: [1..10] int = [456,354,15,57,36,45,15,8,678,2]; +var sumOfValues = + reduce listOfValues; +var maxValue = max reduce listOfValues; // give just max value +// gives max value and index of the max value +var (theMaxValue, idxOfMax) = maxloc reduce zip(listOfValues, listOfValues.domain); +writeln( (sumOfValues, maxValue, idxOfMax, listOfValues[ idxOfMax ] ) ); + +// Scans apply the operation incrementally and return an array of the +// value of the operation at that index as it progressed through the +// array from array.domain.low to array.domain.high +var runningSumOfValues = + scan listOfValues; +var maxScan = max scan listOfValues; +writeln( runningSumOfValues ); +writeln( maxScan ); ``` Who is this tutorial for? -- cgit v1.2.3 From c2f85b27b5620054bb0306ee4730cefff60ad87c Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sat, 1 Aug 2015 17:02:51 -0700 Subject: added count-down mutex example. Cleaned up some of the reductions and scans prints --- chapel.html.markdown | 88 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 65 insertions(+), 23 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index e058bde9..0d1d38dc 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -533,6 +533,12 @@ iter oddsThenEvens( N: int ): int { for i in oddsThenEvens( 10 ) do write( i, ", " ); writeln( ); +// The 'zippered' iterator is an iterator that takes two or more iterators that +// have the same number of iterations and zips them together into one stream + // Ranges have implicit iterators +for (odd, even) in zip( 1..#10 by 2, 2..#10 by 2 ) do + writeln( (odd, even) ); + // Classes are similar to those in C++ and Java. // They currently lack privatization class MyClass { @@ -569,7 +575,7 @@ class MyClass { } } - + // Construct using default constructor, using default values var myObject = new MyClass( 10 ); myObject = new MyClass( memberInt = 10 ); // Equivalent @@ -732,6 +738,7 @@ writeln( ); use Time; // Import the Time module to use Timer objects var timer: Timer; var myBigArray: [{1..4000,1..4000}] real; // Large array we will write into + // Serial Experiment timer.start( ); // Start timer for (x,y) in myBigArray.domain { // Serial iteration @@ -754,7 +761,8 @@ timer.clear( ); // A succinct way of writing a forall loop over an array: // iterate over values -[ val in myBigArray ] val = 1 / val; +[ val in myBigArray ] val = 1 / val; + // or iterate over indicies [ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; @@ -770,32 +778,35 @@ proc countdown( seconds: int ){ // and can know that their values are safe. // Chapel atomic variables can be of type bool, int, uint, and real. var uranium: atomic int; -uranium.write( 238 ); // atomically write a variable +uranium.write( 238 ); // atomically write a variable writeln( uranium.read() ); // atomically read a variable + // operations are described as functions, you could define your own operators. uranium.sub( 3 ); // atomically subtract a variable writeln( uranium.read() ); + var replaceWith = 239; var was = uranium.exchange( replaceWith ); writeln( "uranium was ", was, " but is now ", replaceWith ); + var isEqualTo = 235; if uranium.compareExchange( isEqualTo, replaceWith ) { writeln( "uranium was equal to ", isEqualTo, " so replaced value with ", replaceWith ); } else { writeln( "uranium was not equal to ", isEqualTo, - " value stays the same... whatever it was" ); + " so value stays the same... whatever it was" ); } sync { - begin { - writeln( "Waiting to for uranium to be ", isEqualTo ); + begin { // Reader task + writeln( "Reader: waiting for uranium to be ", isEqualTo ); uranium.waitFor( isEqualTo ); - writeln( "Uranium was set (by someone) to ", isEqualTo ); + writeln( "Reader: uranium was set (by someone) to ", isEqualTo ); } - begin { - writeln( "Waiting to write uranium to ", isEqualTo ); + begin { // Writer task + writeln( "Writer: will set uranium to the value ", isEqualTo, " in..." ); countdown( 3 ); uranium.write( isEqualTo ); } @@ -806,14 +817,14 @@ sync { // until the variable is full or empty again var someSyncVar$: sync int; // varName$ is a convention not a law. sync { - begin { - writeln( "Waiting to read" ); + begin { // Reader task + writeln( "Reader: waiting to read." ); var read_sync = someSyncVar$; writeln( "value is ", read_sync ); } - begin { - writeln( "Writing in..." ); + begin { // Writer task + writeln( "Writer: will write in..." ); countdown( 3 ); someSyncVar$ = 123; } @@ -823,30 +834,62 @@ sync { // in a wait, but when the variable has a value it can be read indefinitely var someSingleVar$: single int; // varName$ is a convention not a law. sync { - begin { - writeln( "Waiting to read" ); + begin { // Reader task + writeln( "Reader: waiting to read." ); for i in 1..5 { var read_single = someSingleVar$; - writeln( i,"th time around an the value is ", read_single ); + writeln( "Reader: iteration ", i,", and the value is ", read_single ); } } - begin { - writeln( "Writing in..." ); + begin { // Writer task + writeln( "Writer: will write in..." ); countdown( 3 ); someSingleVar$ = 5; // first and only write ever. } } +// Heres an example of using atomics and a synch variable to create a +// count-down mutex (also known as a multiplexer) +var count: atomic int; // our counter +var lock$: sync bool; // the mutex lock + +count.write( 2 ); // Only let two tasks in at a time. +lock$.writeXF( true ); // Set lock$ to full (unlocked) +// Note: The value doesnt actually matter, just the state +// (full:unlocked / empty:locked) +// Also, writeXF() fills (F) the sync var regardless of its state (X) + +coforall task in 1..#5 { // Generate tasks + // Create a barrier + do{ + lock$; // Read lock$ (wait) + }while count.read() < 1; // Keep waiting until a spot opens up + + count.sub(1); // decrement the counter + lock$.writeXF( true ); // Set lock$ to full (signal) + + // Actual 'work' + writeln( "Task #", task, " doing work." ); + sleep( 2 ); + + count.add( 1 ); // Increment the counter + lock$.writeXF( true ); // Set lock$ to full (signal) +} + // we can define the operations + * & | ^ && || min max minloc maxloc // over an entire array using scans and reductions // Reductions apply the operation over the entire array and // result in a single value -var listOfValues: [1..10] int = [456,354,15,57,36,45,15,8,678,2]; +var listOfValues: [1..10] int = [15,57,354,36,45,15,456,8,678,2]; var sumOfValues = + reduce listOfValues; -var maxValue = max reduce listOfValues; // give just max value -// gives max value and index of the max value -var (theMaxValue, idxOfMax) = maxloc reduce zip(listOfValues, listOfValues.domain); +var maxValue = max reduce listOfValues; // 'max' give just max value + +// 'maxloc' gives max value and index of the max value +// Note: We have to zip the array and domain together with the zip iterator +var (theMaxValue, idxOfMax) = maxloc reduce zip(listOfValues, + listOfValues.domain); + writeln( (sumOfValues, maxValue, idxOfMax, listOfValues[ idxOfMax ] ) ); // Scans apply the operation incrementally and return an array of the @@ -874,7 +917,6 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to * ```proc main(){ ... }``` * Records * Whole/sliced array assignment - * Reductions and scans * Range and domain slicing * Parallel iterators -- cgit v1.2.3 From 11ca1012e402bbc51f70283c4e20ee8bf31deb7f Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 2 Aug 2015 14:35:41 -0700 Subject: Clarified some wording on the zipper iterator. Changed the example to something more simple and informative. --- chapel.html.markdown | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 0d1d38dc..cfde0454 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -533,11 +533,13 @@ iter oddsThenEvens( N: int ): int { for i in oddsThenEvens( 10 ) do write( i, ", " ); writeln( ); -// The 'zippered' iterator is an iterator that takes two or more iterators that -// have the same number of iterations and zips them together into one stream - // Ranges have implicit iterators -for (odd, even) in zip( 1..#10 by 2, 2..#10 by 2 ) do - writeln( (odd, even) ); +// 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 +// iteration of the zipped iterator yields a tuple of one value yielded +// from each iterator. + // Ranges have implicit iterators +for (positive, negative) in zip( 1..5, -5..-1) do + writeln( (positive, negative) ); // Classes are similar to those in C++ and Java. // They currently lack privatization -- cgit v1.2.3 From b27d5268227f8812c894a217a08fd8c6221d1797 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 2 Aug 2015 15:37:01 -0700 Subject: Added modules and main() Worth noting that this will change the diff of the parallel section quite a bit, since they became the body of the main procedure. Thus each line in intented. --- chapel.html.markdown | 462 ++++++++++++++++++++++++++++----------------------- 1 file changed, 258 insertions(+), 204 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index cfde0454..d8783728 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -673,234 +673,288 @@ var copyNewTypeList = new GenericClass( realList, int ); for value in copyNewTypeList do write( value, ", " ); writeln( ); -// Parallelism -// In other languages, parallelism is typically this is done with -// complicated libraries and strange class structure hierarchies. -// Chapel has it baked right into the language. - -// 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 -// task will not progress until the children have synced back up. -sync { - begin { // Start of new task's body - var a = 0; - for i in 1..1000 do a += 1; - writeln( "Done: ", a); - } // End of new tasks body - writeln( "spun off a task!"); -} -writeln( "Back together" ); -proc printFibb( n: int ){ - writeln( "fibonacci(",n,") = ", fibonacci( n ) ); -} +// Modules are Chapel's way of managing name spaces. +// The files containing these modules do not need to be named after the modules +// (as is with Java), but files implicitly name modules. +// In this case, this file implicitly names the 'learnchapel' module -// A cobegin statement will spin each statement of the body into one new task -cobegin { - printFibb( 20 ); // new task - printFibb( 10 ); // new task - printFibb( 5 ); // new task - { - // This is a nested statement body and thus is a single statement - // to the parent statement and is executed by a single task - writeln( "this gets" ); - writeln( "executed as" ); - writeln( "a whole" ); +module OurModule { + // We can use modules inside of other modules. + use Time; + + // We'll use this a procedure in the parallelism section. + proc countdown( seconds: int ){ + for i in 1..seconds by -1 { + writeln( i ); + sleep( 1 ); + } + } + + // Submodule of Ourmodule + // It is possible to create arbitrarily deep module nests. + module ChildModule { + proc foo(){ + writeln( "ChildModule.foo()"); + } } -} -// Notice here that the prints from each statement may happen in any order. - -// Coforall loop will create a new task for EACH iteration -var num_tasks = 10; // Number of tasks we want -coforall taskID in 1..#num_tasks { - writeln( "Hello from task# ", taskID ); -} -// Again we see that prints happen in any order. -// NOTE! coforall should be used only for creating tasks! -// Using it to iterating over a structure is very a bad idea! - -// forall loops are another parallel loop, but only create a smaller number -// of tasks, specifically --dataParTasksPerLocale=number of task -forall i in 1..100 { - write( i, ", "); -} -writeln( ); -// Here we see that there are sections that are in order, followed by -// a section that would not follow ( e.g. 1, 2, 3, 7, 8, 9, 4, 5, 6, ). -// This is because each task is taking on a chunk of the range 1..10 -// (1..3, 4..6, or 7..9) doing that chunk serially, but each task happens -// in parallel. -// Your results may depend on your machine and configuration - -// For both the forall and coforall loops, the execution of the -// parent task will not continue until all the children sync up. - -// forall loops are particularly useful for parallel iteration over arrays. -// Lets run an experiment to see how much faster a parallel loop is -use Time; // Import the Time module to use Timer objects -var timer: Timer; -var myBigArray: [{1..4000,1..4000}] real; // Large array we will write into - -// Serial Experiment -timer.start( ); // Start timer -for (x,y) in myBigArray.domain { // Serial iteration - myBigArray[x,y] = (x:real) / (y:real); -} -timer.stop( ); // Stop timer -writeln( "Serial: ", timer.elapsed( ) ); // Print elapsed time -timer.clear( ); // Clear timer for parallel loop - -// Parallel Experiment -timer.start( ); // start timer -forall (x,y) in myBigArray.domain { // Parallel iteration - myBigArray[x,y] = (x:real) / (y:real); -} -timer.stop( ); // Stop timer -writeln( "Parallel: ", timer.elapsed( ) ); // Print elapsed time -timer.clear( ); -// You may have noticed that (depending on how many cores you have) -// that the parallel loop went faster than the serial loop - -// A succinct way of writing a forall loop over an array: -// iterate over values -[ val in myBigArray ] val = 1 / val; - -// 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 ); + + 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 + // In other languages, parallelism is typically this is done with + // complicated libraries and strange class structure hierarchies. + // Chapel has it baked right into the language. + + // 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 + // task will not progress until the children have synced back up. + sync { + begin { // Start of new task's body + var a = 0; + for i in 1..1000 do a += 1; + writeln( "Done: ", a); + } // End of new tasks body + writeln( "spun off a task!"); + } + writeln( "Back together" ); -// Atomic variables, common to many languages, are ones whose operations -// occur uninterupted. Multiple threads can both modify atomic variables -// and can know that their values are safe. -// Chapel atomic variables can be of type bool, int, uint, and real. -var uranium: atomic int; -uranium.write( 238 ); // atomically write a variable -writeln( uranium.read() ); // atomically read a variable - -// operations are described as functions, you could define your own operators. -uranium.sub( 3 ); // atomically subtract a variable -writeln( uranium.read() ); - -var replaceWith = 239; -var was = uranium.exchange( replaceWith ); -writeln( "uranium was ", was, " but is now ", replaceWith ); - -var isEqualTo = 235; -if uranium.compareExchange( isEqualTo, replaceWith ) { - writeln( "uranium was equal to ", isEqualTo, - " so replaced value with ", replaceWith ); -} else { - writeln( "uranium was not equal to ", isEqualTo, - " so value stays the same... whatever it was" ); -} + proc printFibb( n: int ){ + writeln( "fibonacci(",n,") = ", fibonacci( n ) ); + } -sync { - begin { // Reader task - writeln( "Reader: waiting for uranium to be ", isEqualTo ); - uranium.waitFor( isEqualTo ); - writeln( "Reader: uranium was set (by someone) to ", isEqualTo ); + // A cobegin statement will spin each statement of the body into one new task + cobegin { + printFibb( 20 ); // new task + printFibb( 10 ); // new task + printFibb( 5 ); // new task + { + // This is a nested statement body and thus is a single statement + // to the parent statement and is executed by a single task + writeln( "this gets" ); + writeln( "executed as" ); + writeln( "a whole" ); + } } + // Notice here that the prints from each statement may happen in any order. - begin { // Writer task - writeln( "Writer: will set uranium to the value ", isEqualTo, " in..." ); - countdown( 3 ); - uranium.write( isEqualTo ); + // Coforall loop will create a new task for EACH iteration + var num_tasks = 10; // Number of tasks we want + coforall taskID in 1..#num_tasks { + writeln( "Hello from task# ", taskID ); + } + // Again we see that prints happen in any order. + // NOTE! coforall should be used only for creating tasks! + // Using it to iterating over a structure is very a bad idea! + + // forall loops are another parallel loop, but only create a smaller number + // of tasks, specifically --dataParTasksPerLocale=number of task + forall i in 1..100 { + write( i, ", "); + } + writeln( ); + // Here we see that there are sections that are in order, followed by + // a section that would not follow ( e.g. 1, 2, 3, 7, 8, 9, 4, 5, 6, ). + // This is because each task is taking on a chunk of the range 1..10 + // (1..3, 4..6, or 7..9) doing that chunk serially, but each task happens + // in parallel. + // Your results may depend on your machine and configuration + + // For both the forall and coforall loops, the execution of the + // parent task will not continue until all the children sync up. + + // forall loops are particularly useful for parallel iteration over arrays. + // Lets run an experiment to see how much faster a parallel loop is + use Time; // Import the Time module to use Timer objects + var timer: Timer; + var myBigArray: [{1..4000,1..4000}] real; // Large array we will write into + + // Serial Experiment + timer.start( ); // Start timer + for (x,y) in myBigArray.domain { // Serial iteration + myBigArray[x,y] = (x:real) / (y:real); + } + timer.stop( ); // Stop timer + writeln( "Serial: ", timer.elapsed( ) ); // Print elapsed time + timer.clear( ); // Clear timer for parallel loop + + // Parallel Experiment + timer.start( ); // start timer + forall (x,y) in myBigArray.domain { // Parallel iteration + myBigArray[x,y] = (x:real) / (y:real); + } + timer.stop( ); // Stop timer + writeln( "Parallel: ", timer.elapsed( ) ); // Print elapsed time + timer.clear( ); + // You may have noticed that (depending on how many cores you have) + // that the parallel loop went faster than the serial loop + + // A succinct way of writing a forall loop over an array: + // iterate over values + [ val in myBigArray ] val = 1 / val; + + // 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 ); + } } -} -// sync vars have two states: empty and full. -// If you read an empty variable or write a full variable, you are waited -// until the variable is full or empty again -var someSyncVar$: sync int; // varName$ is a convention not a law. -sync { - begin { // Reader task - writeln( "Reader: waiting to read." ); - var read_sync = someSyncVar$; - writeln( "value is ", read_sync ); + // Atomic variables, common to many languages, are ones whose operations + // occur uninterupted. Multiple threads can both modify atomic variables + // and can know that their values are safe. + // Chapel atomic variables can be of type bool, int, uint, and real. + var uranium: atomic int; + uranium.write( 238 ); // atomically write a variable + writeln( uranium.read() ); // atomically read a variable + + // operations are described as functions, you could define your own operators. + uranium.sub( 3 ); // atomically subtract a variable + writeln( uranium.read() ); + + var replaceWith = 239; + var was = uranium.exchange( replaceWith ); + writeln( "uranium was ", was, " but is now ", replaceWith ); + + var isEqualTo = 235; + if uranium.compareExchange( isEqualTo, replaceWith ) { + writeln( "uranium was equal to ", isEqualTo, + " so replaced value with ", replaceWith ); + } else { + writeln( "uranium was not equal to ", isEqualTo, + " so value stays the same... whatever it was" ); } - begin { // Writer task - writeln( "Writer: will write in..." ); - countdown( 3 ); - someSyncVar$ = 123; + sync { + begin { // Reader task + writeln( "Reader: waiting for uranium to be ", isEqualTo ); + uranium.waitFor( isEqualTo ); + writeln( "Reader: uranium was set (by someone) to ", isEqualTo ); + } + + begin { // Writer task + writeln( "Writer: will set uranium to the value ", isEqualTo, " in..." ); + countdown( 3 ); + uranium.write( isEqualTo ); + } } -} -// single vars can only be written once. A read on an unwritten single results -// in a wait, but when the variable has a value it can be read indefinitely -var someSingleVar$: single int; // varName$ is a convention not a law. -sync { - begin { // Reader task - writeln( "Reader: waiting to read." ); - for i in 1..5 { - var read_single = someSingleVar$; - writeln( "Reader: iteration ", i,", and the value is ", read_single ); + // sync vars have two states: empty and full. + // If you read an empty variable or write a full variable, you are waited + // until the variable is full or empty again + var someSyncVar$: sync int; // varName$ is a convention not a law. + sync { + begin { // Reader task + writeln( "Reader: waiting to read." ); + var read_sync = someSyncVar$; + writeln( "value is ", read_sync ); + } + + begin { // Writer task + writeln( "Writer: will write in..." ); + countdown( 3 ); + someSyncVar$ = 123; } } - begin { // Writer task - writeln( "Writer: will write in..." ); - countdown( 3 ); - someSingleVar$ = 5; // first and only write ever. + // single vars can only be written once. A read on an unwritten single results + // in a wait, but when the variable has a value it can be read indefinitely + var someSingleVar$: single int; // varName$ is a convention not a law. + sync { + begin { // Reader task + writeln( "Reader: waiting to read." ); + for i in 1..5 { + var read_single = someSingleVar$; + writeln( "Reader: iteration ", i,", and the value is ", read_single ); + } + } + + begin { // Writer task + writeln( "Writer: will write in..." ); + countdown( 3 ); + someSingleVar$ = 5; // first and only write ever. + } } -} -// Heres an example of using atomics and a synch variable to create a -// count-down mutex (also known as a multiplexer) -var count: atomic int; // our counter -var lock$: sync bool; // the mutex lock - -count.write( 2 ); // Only let two tasks in at a time. -lock$.writeXF( true ); // Set lock$ to full (unlocked) -// Note: The value doesnt actually matter, just the state -// (full:unlocked / empty:locked) -// Also, writeXF() fills (F) the sync var regardless of its state (X) - -coforall task in 1..#5 { // Generate tasks - // Create a barrier - do{ - lock$; // Read lock$ (wait) - }while count.read() < 1; // Keep waiting until a spot opens up + // Heres an example of using atomics and a synch variable to create a + // count-down mutex (also known as a multiplexer) + var count: atomic int; // our counter + var lock$: sync bool; // the mutex lock + + count.write( 2 ); // Only let two tasks in at a time. + lock$.writeXF( true ); // Set lock$ to full (unlocked) + // Note: The value doesnt actually matter, just the state + // (full:unlocked / empty:locked) + // Also, writeXF() fills (F) the sync var regardless of its state (X) + + coforall task in 1..#5 { // Generate tasks + // Create a barrier + do{ + lock$; // Read lock$ (wait) + }while count.read() < 1; // Keep waiting until a spot opens up - count.sub(1); // decrement the counter - lock$.writeXF( true ); // Set lock$ to full (signal) + count.sub(1); // decrement the counter + lock$.writeXF( true ); // Set lock$ to full (signal) - // Actual 'work' - writeln( "Task #", task, " doing work." ); - sleep( 2 ); + // Actual 'work' + writeln( "Task #", task, " doing work." ); + sleep( 2 ); - count.add( 1 ); // Increment the counter - lock$.writeXF( true ); // Set lock$ to full (signal) -} + count.add( 1 ); // Increment the counter + lock$.writeXF( true ); // Set lock$ to full (signal) + } -// we can define the operations + * & | ^ && || min max minloc maxloc -// over an entire array using scans and reductions -// Reductions apply the operation over the entire array and -// result in a single value -var listOfValues: [1..10] int = [15,57,354,36,45,15,456,8,678,2]; -var sumOfValues = + reduce listOfValues; -var maxValue = max reduce listOfValues; // 'max' give just max value - -// 'maxloc' gives max value and index of the max value -// Note: We have to zip the array and domain together with the zip iterator -var (theMaxValue, idxOfMax) = maxloc reduce zip(listOfValues, - listOfValues.domain); + // we can define the operations + * & | ^ && || min max minloc maxloc + // over an entire array using scans and reductions + // Reductions apply the operation over the entire array and + // result in a single value + var listOfValues: [1..10] int = [15,57,354,36,45,15,456,8,678,2]; + var sumOfValues = + reduce listOfValues; + var maxValue = max reduce listOfValues; // 'max' give just max value + + // 'maxloc' gives max value and index of the max value + // Note: We have to zip the array and domain together with the zip iterator + var (theMaxValue, idxOfMax) = maxloc reduce zip(listOfValues, + listOfValues.domain); -writeln( (sumOfValues, maxValue, idxOfMax, listOfValues[ idxOfMax ] ) ); - -// Scans apply the operation incrementally and return an array of the -// value of the operation at that index as it progressed through the -// array from array.domain.low to array.domain.high -var runningSumOfValues = + scan listOfValues; -var maxScan = max scan listOfValues; -writeln( runningSumOfValues ); -writeln( maxScan ); + writeln( (sumOfValues, maxValue, idxOfMax, listOfValues[ idxOfMax ] ) ); + + // Scans apply the operation incrementally and return an array of the + // value of the operation at that index as it progressed through the + // array from array.domain.low to array.domain.high + var runningSumOfValues = + scan listOfValues; + var maxScan = max scan listOfValues; + writeln( runningSumOfValues ); + writeln( maxScan ); +} ``` Who is this tutorial for? -- cgit v1.2.3 From 27cc82076214e68d9d93c90aafe5ae567fe78892 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 2 Aug 2015 15:44:16 -0700 Subject: Uniform tics, noteable arguments addition. Changed all N-tics for preformatted/code text into single tics if that code was sinle line (i.e. bash commands, compiler arguments) Added --main-module, --module-dir to Notable arguments section. Realized that the text was disappering, so changed it to ``. Need to confirm it success. --- chapel.html.markdown | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index d8783728..14bbe9f5 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -968,9 +968,8 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to ### What this tutorial is lacking: - * Modules and standard modules + * Exposition of the standard modules * Multiple Locales (distributed memory system) - * ```proc main(){ ... }``` * Records * Whole/sliced array assignment * Range and domain slicing @@ -994,26 +993,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/) and its as easy as - 1. ```tar -xvf chapel-1.11.0.tar.gz``` - 2. ```cd chapel-1.11.0``` - 3. ```make``` - 4. ```source util/setchplenv.bash # or .sh or .csh or .fish``` + 1. `tar -xvf chapel-1.11.0.tar.gz` + 2. `cd chapel-1.11.0` + 3. `make` + 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). Chapel is easily installed with Brew for OS X - 1. ```brew update``` - 2. ```brew install chapel``` + 1. `brew update` + 2. `brew install chapel` Compiling Code -------------- Builds like other compilers: -```chpl myFile.chpl -o myExe``` +`chpl myFile.chpl -o myExe` Notable arguments: - * ``--fast``: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. - * ```--set =```: set config param to at compile-time + * `--fast`: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. + * `--set =`: set config param `` to `` at compile-time. + * `--main-module `: use the main() procedure found in the module `` as the executable's main. + * `--module-dir `: includes ` Date: Sun, 2 Aug 2015 15:53:19 -0700 Subject: Small corrections. Removed duplicate countDown. Weird leftover words from parallelism statemet General corrections so spelling capitalization --- chapel.html.markdown | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 14bbe9f5..a4bcfb9f 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -683,7 +683,7 @@ module OurModule { // We can use modules inside of other modules. use Time; - // We'll use this a procedure in the parallelism section. + // We'll use this procedure in the parallelism section. proc countdown( seconds: int ){ for i in 1..seconds by -1 { writeln( i ); @@ -691,7 +691,7 @@ module OurModule { } } - // Submodule of Ourmodule + // Submodules of OurModule // It is possible to create arbitrarily deep module nests. module ChildModule { proc foo(){ @@ -726,7 +726,7 @@ foo(); // Less explicit call on ChildModule.foo() proc main(){ // 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. // Chapel has it baked right into the language. @@ -821,13 +821,6 @@ proc main(){ // 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 // occur uninterupted. Multiple threads can both modify atomic variables // and can know that their values are safe. -- cgit v1.2.3 From 4f6ae05392504f63b5ddd5c23d81700bf6aa1b7a Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Sun, 2 Aug 2015 16:05:19 -0700 Subject: Small changes no.2 --- chapel.html.markdown | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index a4bcfb9f..33bdc21d 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -673,15 +673,14 @@ var copyNewTypeList = new GenericClass( realList, int ); for value in copyNewTypeList do write( value, ", " ); 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 is with Java), but files implicitly name 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; + use Time; // Time is one of the standard modules. // We'll use this procedure in the parallelism section. proc countdown( seconds: int ){ @@ -947,7 +946,7 @@ proc main(){ var maxScan = max scan listOfValues; writeln( runningSumOfValues ); writeln( maxScan ); -} +} // end main() ``` Who is this tutorial for? @@ -1010,4 +1009,4 @@ Notable arguments: * `--fast`: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. * `--set =`: set config param `` to `` at compile-time. * `--main-module `: use the main() procedure found in the module `` as the executable's main. - * `--module-dir `: includes ``: includes `` in the module search path. \ No newline at end of file -- cgit v1.2.3 From b1a544d1f04fc6c9769037af5fd165e7032a2985 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Wed, 5 Aug 2015 18:22:56 -0700 Subject: main(), modules, array/range/domain slicing, loop-expressions, array expressions, in depth zippered iterators Quite the addition. Modules required main() because of the begin statement issue. array/range/domain slicing was more intricate than I though. And loop/array expressions had some very very intersting behavior that necessitated a dive into zippered iterators. Some back-of-the-head anxiety about the tutorial being to in depth? Nawww. --- chapel.html.markdown | 115 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 103 insertions(+), 12 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 33bdc21d..c8489371 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -24,7 +24,7 @@ writeln( "World!" ); // each thing is printed right next to each other, so include your spacing! writeln( "There are ", 3, " commas (\",\") in this line of code" ); // 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" ); // 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 +// However they are required to be bounded (unlike ranges) 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 thirdDim: range = 1..16; @@ -310,6 +311,18 @@ stringSet += "a"; // Redundant add "a" stringSet -= "c"; // Remove "c" 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. // Their sizes are defined using domains that represent their indices var intArray: [1..10] int; @@ -357,6 +370,48 @@ var dict: [dictDomain] int = [ "one" => 1, "two" => 2 ]; dict["three"] = 3; 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. proc fibonacci( n : int ) : int { if ( n <= 1 ) then return n; @@ -533,6 +588,19 @@ iter oddsThenEvens( N: int ): int { for i in oddsThenEvens( 10 ) do write( i, ", " ); 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 // of iterations) using zip() to create a single zipped iterator, where each // 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 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. // They currently lack privatization class MyClass { @@ -813,12 +909,9 @@ proc main(){ // You may have noticed that (depending on how many cores you have) // that the parallel loop went faster than the serial loop - // A succinct way of writing a forall loop over an array: - // iterate over values - [ val in myBigArray ] val = 1 / val; - - // or iterate over indicies - [ idx in myBigArray.domain ] myBigArray[idx] = -myBigArray[idx]; + // The bracket style loop-expression described + // much earlier implicitly uses a forall loop. + [ val in myBigArray ] val = 1 / val; // Parallel operation // Atomic variables, common to many languages, are ones whose operations // occur uninterupted. Multiple threads can both modify atomic variables @@ -837,7 +930,7 @@ proc main(){ writeln( "uranium was ", was, " but is now ", replaceWith ); var isEqualTo = 235; - if uranium.compareExchange( isEqualTo, replaceWith ) { + if ( uranium.compareExchange( isEqualTo, replaceWith ) ) { writeln( "uranium was equal to ", isEqualTo, " so replaced value with ", replaceWith ); } else { @@ -911,7 +1004,7 @@ proc main(){ // Create a barrier do{ 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 lock$.writeXF( true ); // Set lock$ to full (signal) @@ -954,7 +1047,7 @@ 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. 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. @@ -963,8 +1056,6 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to * Exposition of the standard modules * Multiple Locales (distributed memory system) * Records - * Whole/sliced array assignment - * Range and domain slicing * Parallel iterators Your input, questions, and discoveries are important to the developers! -- cgit v1.2.3 From 938720074b8b18a9ada93fb8a040b9ca1a813747 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Mon, 17 Aug 2015 14:19:41 -0700 Subject: Update to Chapel A reader/writer sync example was missing a "Reader: " notem --- chapel.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index c8489371..8a88a652 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -960,7 +960,7 @@ proc main(){ begin { // Reader task writeln( "Reader: waiting to read." ); var read_sync = someSyncVar$; - writeln( "value is ", read_sync ); + writeln( "Reader: value is ", read_sync ); } begin { // Writer task @@ -1100,4 +1100,4 @@ Notable arguments: * `--fast`: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. * `--set =`: set config param `` to `` at compile-time. * `--main-module `: use the main() procedure found in the module `` as the executable's main. - * `--module-dir `: includes `` in the module search path. \ No newline at end of file + * `--module-dir `: includes `` in the module search path. -- cgit v1.2.3 From 57ea4af17c7ab17e9c32096c2579e0a985e44781 Mon Sep 17 00:00:00 2001 From: Ian Bertolacci Date: Fri, 2 Oct 2015 10:31:25 -0600 Subject: Updated info for Chapel 1.12.0 --- chapel.html.markdown | 196 +++++++++++++++++++++++++-------------------------- 1 file changed, 98 insertions(+), 98 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 8a88a652..1d0abe6e 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -27,7 +27,7 @@ writeln( "There are ", 3, " commas (\",\") in this line of code" ); stdout.writeln( "This goes to standard output, just like plain writeln() does"); 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 // the compiler can figure out the type that it will hold. var myVar = 10; // 10 is an int, so myVar is implicitly an int myVar = -10; @@ -65,9 +65,9 @@ const almostPi: real = 22.0/7.0; param compileTimeConst: int = 16; // The config modifier allows values to be set at the command line -// and is much easier than the usual getOpts debacle +// and is much easier than the usual getOpts debacle // config vars and consts can be changed through the command line at run time -config var varCmdLineArg: int = -123; +config var varCmdLineArg: int = -123; config const constCmdLineArg: int = 777; // Set with --VarName=Value or --VarName Value at run time @@ -119,9 +119,9 @@ a *= thatInt; // Times-equals ( a = a * thatInt; ) b &&= thatBool; // Logical-and-equals ( b = b && thatBool; ) a <<= 3; // Left-bit-shift-equals ( a = a << 10; ) // and many, many more. -// Unlike other C family languages there are no +// Unlike other C family languages there are no // pre/post-increment/decrement operators like -// ++j, --j, j++, j-- +// ++j, --j, j++, j-- // Swap operator var old_this = thisInt; @@ -155,7 +155,7 @@ writeln( (a,b,thisInt,thatInt,thisBool,thatBool) ); // Type aliasing type chroma = int; // Type of a single hue -type RGBColor = 3*chroma; // Type representing a full color +type RGBColor = 3*chroma; // Type representing a full color var black: RGBColor = ( 0,0,0 ); var white: RGBColor = ( 255, 255, 255 ); @@ -198,7 +198,7 @@ select( inputOption ){ writeln( "Chose 'otherOption'" ); writeln( "Which has a body" ); } - otherwise { + otherwise { writeln( "Any other Input" ); writeln( "the otherwise case doesn't need a do if the body is one line" ); } @@ -221,7 +221,7 @@ do{ writeln( jSum ); // For loops are much like those in python in that they iterate over a range. -// Ranges themselves are types, and can be stuffed into variables +// Ranges themselves are types, and can be stuffed into variables // (more about that later) for i in 1..10 do write( i , ", ") ; writeln( ); @@ -240,28 +240,28 @@ for x in 1..10 { } // Ranges and Domains -// For-loops and arrays both use ranges and domains to +// For-loops and arrays both use ranges and domains to // define an index set that can be iterated over. // Ranges are single dimensional -// Domains can be multi-dimensional and can +// Domains can be multi-dimensional and can // represent indices of different types as well. // They are first-class citizen types, and can be assigned into variables var range1to10: range = 1..10; // 1, 2, 3, ..., 10 var range2to11 = 2..11; // 2, 3, 4, ..., 11 var rangeThistoThat: range = thisInt..thatInt; // using variables -var rangeEmpty: range = 100..-100 ; // this is valid but contains no indices +var rangeEmpty: range = 100..-100 ; // this is valid but contains no indices -// Ranges can be unbounded -var range1toInf: range(boundedType=BoundedRangeType.boundedLow) = 1.. ; +// Ranges can be unbounded +var range1toInf: range(boundedType=BoundedRangeType.boundedLow) = 1.. ; // 1, 2, 3, 4, 5, ... -// Note: the range(boundedType= ... ) is only +// Note: the range(boundedType= ... ) is only // necessary if we explicitly type the variable var rangeNegInfto1 = ..1; // ..., -4, -3, -2, -1, 0, 1 // Ranges can be strided using the 'by' operator. var range2to10by2: range(stridable=true) = 2..10 by 2; // 2, 4, 6, 8, 10 -// Note: the range(stridable=true) is only +// Note: the range(stridable=true) is only // necessary if we explicitly type the variable // Use by to create a reverse range @@ -275,9 +275,9 @@ var rangeCountBy: range(stridable=true) = -5..#12 by 2; // -5, -3, -1, 1, 3, 5 writeln( rangeCountBy ); // Can query properties of the range -// Print the first index, last index, number of indices, +// Print the first index, last index, number of indices, // stride, and ask if 2 is include in the range -writeln( ( rangeCountBy.first, rangeCountBy.last, rangeCountBy.length, +writeln( ( rangeCountBy.first, rangeCountBy.last, rangeCountBy.length, rangeCountBy.stride, rangeCountBy.member( 2 ) ) ); for i in rangeCountBy{ @@ -309,7 +309,7 @@ stringSet += "b"; stringSet += "c"; stringSet += "a"; // Redundant add "a" 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 @@ -332,13 +332,13 @@ var intArray2: [{1..10}] int; //equivalent for i in 1..10 do intArray[i] = -i; writeln( intArray ); -// We cannot access intArray[0] because it exists outside +// We cannot access intArray[0] because it exists outside // of the index set, {1..10}, we defined it to have. // intArray[11] is illegal for the same reason. var realDomain: domain(2) = {1..5,1..7}; var realArray: [realDomain] real; -var realArray2: [1..5,1..7] real; // Equivalent +var realArray2: [1..5,1..7] real; // Equivalent var realArray3: [{1..5,1..7}] real; // Equivalent for i in 1..5 { @@ -350,7 +350,7 @@ for i in 1..5 { } // Arrays have domains as members that we can iterate over -for idx in realArray.domain { // Again, idx is a 2*int tuple +for idx in realArray.domain { // Again, idx is a 2*int tuple realArray[idx] = 1 / realArray[idx[1],idx[2]]; // Access by tuple and list } @@ -377,7 +377,7 @@ 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 = thisArray; thatArray[1] = -1; writeln( (thisArray, thatArray) ); @@ -389,12 +389,12 @@ writeln( (thisArray, thatArray) ); var thisPlusThat = thisArray + thatArray; writeln( thisPlusThat ); -// Arrays and loops can also be expressions, where loop +// 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, +// 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; @@ -407,7 +407,7 @@ 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: +// 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. @@ -431,7 +431,7 @@ proc addThree( n ) { doublePrint( addThree( fibonacci( 20 ) ) ); // Can also take 'unlimited' number of parameters -proc maxOf( x ...?k ) { +proc maxOf( x ...?k ) { // x refers to a tuple of one type, with k elements var maximum = x[1]; for i in 2..k do maximum = if (maximum < x[i]) then x[i] else maximum; @@ -439,7 +439,7 @@ proc maxOf( x ...?k ) { } writeln( maxOf( 1, -10, 189, -9071982, 5, 17, 20001, 42 ) ); -// The ? operator is called the query operator, and is used to take +// The ? operator is called the query operator, and is used to take // undetermined values (like tuple or array sizes, and generic types). // Taking arrays as parameters. @@ -463,7 +463,7 @@ writeln( defaultsProc( x=11 ) ); writeln( defaultsProc( x=12, y=5.432 ) ); writeln( defaultsProc( y=9.876, x=13 ) ); -// Intent modifiers on the arguments convey how +// Intent modifiers on the arguments convey how // those arguments are passed to the procedure // in: copy arg in, but not out // out: copy arg out, but not in @@ -489,18 +489,18 @@ writeln( "Outside After: ", (inVar, outVar, inoutVar, refVar) ); // Similarly we can define intents on the return type // refElement returns a reference to an element of array proc refElement( array : [?D] ?T, idx ) ref : T { - return array[ idx ]; // returns a reference to + return array[ idx ]; // returns a reference to } var myChangingArray : [1..5] int = [1,2,3,4,5]; writeln( myChangingArray ); -// Store reference to element in ref variable -ref refToElem = refElement( myChangingArray, 5 ); +// Store reference to element in ref variable +ref refToElem = refElement( myChangingArray, 5 ); writeln( refToElem ); refToElem = -2; // modify reference which modifies actual value in array writeln( refToElem ); writeln( myChangingArray ); -// This makes more practical sense for class methods where references to +// This makes more practical sense for class methods where references to // elements in a data-structure are returned via a method or iterator // We can query the type of arguments to generic procedures @@ -520,7 +520,7 @@ genericProc( 1.0+2.0i, 3.0+4.0i ); // We can also enforce a form of polymorphism with the 'where' clause // This allows the compiler to decide which function to use. -// Note: that means that all information needs to be known at compile-time. +// Note: that means that all information needs to be known at compile-time. // The param modifier on the arg is used to enforce this constraint. proc whereProc( param N : int ): void where ( N > 0 ) { @@ -534,7 +534,7 @@ proc whereProc( param N : int ): void whereProc( 10 ); whereProc( -1 ); -// whereProc( 0 ) would result in a compiler error because there +// whereProc( 0 ) would result in a compiler error because there // are no functions that satisfy the where clause's condition. // We could have defined a whereProc without a where clause that would then have // served as a catch all for all the other cases (of which there is only one). @@ -543,7 +543,7 @@ whereProc( -1 ); // We can define the unary operators: // + - ! ~ // and the binary operators: -// + - * / % ** == <= >= < > << >> & | ˆ by +// + - * / % ** == <= >= < > << >> & | ˆ by // += -= *= /= %= **= &= |= ˆ= <<= >>= <=> // Boolean exclusive or operator @@ -569,14 +569,14 @@ Note: You could break everything if you get careless with your overloads. This here will break everything. Don't do it. proc +( left: int, right: int ): int{ return left - right; -} +} */ -// Iterators are a sisters to the procedure, and almost +// Iterators are a sisters to the procedure, and almost // everything about procedures also applies to iterators -// However, instead of returning a single value, +// However, instead of returning a single value, // iterators yield many values to a loop. -// This is useful when a complicated set or order of iterations is needed but +// This is useful when a complicated set or order of iterations is needed but // allows the code defining the iterations to be separate from the loop body. iter oddsThenEvens( N: int ): int { for i in 1..N by 2 do @@ -601,15 +601,15 @@ for i in absolutelyNothing( 10 ){ writeln( "Woa there! absolutelyNothing yielded ", i ); } -// 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 -// iteration of the zipped iterator yields a tuple of one value yielded +// 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 +// iteration of the zipped iterator yields a tuple of one value yielded // from each iterator. // Ranges have implicit iterators -for (positive, negative) in zip( 1..5, -5..-1) do +for (positive, negative) in zip( 1..5, -5..-1) do writeln( (positive, negative) ); -// Zipper iteration is quite important in the assignment of arrays, +// 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; @@ -629,10 +629,10 @@ for (i, j) in zip( toThisArray.domain, -100..#5 ){ } writeln( toThisArray ); -// This is all very important in undestanding why the statement +// 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 +// 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. @@ -641,8 +641,8 @@ writeln( toThisArray ); // They currently lack privatization class MyClass { // Member variables - var memberInt : int; - var memberBool : bool = true; + var memberInt : int; + var memberBool : bool = true; // Classes have default constructors that don't need to be coded (see below) // Our explicitly defined constructor @@ -659,28 +659,28 @@ class MyClass { proc setMemberInt( val: int ){ this.memberInt = val; } - + proc setMemberBool( val: bool ){ this.memberBool = val; } - proc getMemberInt( ): int{ + proc getMemberInt( ): int{ return this.memberInt; } proc getMemberBool( ): bool { return this.memberBool; } - + } - + // Construct using default constructor, using default values var myObject = new MyClass( 10 ); myObject = new MyClass( memberInt = 10 ); // Equivalent writeln( myObject.getMemberInt( ) ); // ... using our values var myDiffObject = new MyClass( -1, true ); - myDiffObject = new MyClass( memberInt = -1, + myDiffObject = new MyClass( memberInt = -1, memberBool = true ); // Equivalent writeln( myDiffObject ); @@ -689,7 +689,7 @@ var myOtherObject = new MyClass( 1.95 ); myOtherObject = new MyClass( val = 1.95 ); // Equivalent writeln( myOtherObject.getMemberInt( ) ); -// We can define an operator on our class as well but +// We can define an operator on our class as well but // the definition has to be outside the class definition proc +( A : MyClass, B : MyClass) : MyClass { return new MyClass( memberInt = A.getMemberInt( ) + B.getMemberInt( ), @@ -715,46 +715,46 @@ class GenericClass { type classType; var classDomain: domain(1); var classArray: [classDomain] classType; - + // Explicit constructor proc GenericClass( type classType, elements : int ){ this.classDomain = {1..#elements}; } - + // Copy constructor - // Note: We still have to put the type as an argument, but we can + // Note: We still have to put the type as an argument, but we can // default to the type of the other object using the query (?) operator // Further, we can take advantage of this to allow our copy constructor // to copy classes of different types and cast on the fly - proc GenericClass( other : GenericClass(?otherType), + proc GenericClass( other : GenericClass(?otherType), type classType = otherType ) { this.classDomain = other.classDomain; // Copy and cast - for idx in this.classDomain do this[ idx ] = other[ idx ] : classType; + for idx in this.classDomain do this[ idx ] = other[ idx ] : classType; } - - // Define bracket notation on a GenericClass + + // Define bracket notation on a GenericClass // object so it can behave like a normal array // i.e. objVar[ i ] or objVar( i ) proc this( i : int ) ref : classType { return this.classArray[ i ]; } - - // Define an implicit iterator for the class + + // Define an implicit iterator for the class // to yield values from the array to a loop // i.e. for i in objVar do .... iter these( ) ref : classType { for i in this.classDomain do yield this[i]; } - + } var realList = new GenericClass( real, 10 ); -// We can assign to the member array of the object using the bracket +// We can assign to the member array of the object using the bracket // notation that we defined ( proc this( i: int ){ ... } ) for i in realList.classDomain do realList[i] = i + 1.0; -// We can iterate over the values in our list with the iterator +// We can iterate over the values in our list with the iterator // we defined ( iter these( ){ ... } ) for value in realList do write( value, ", " ); writeln( ); @@ -777,23 +777,23 @@ writeln( ); 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 + } + + // 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()" ); @@ -806,7 +806,7 @@ module OurModule { use OurModule; // At this point we have not used ChildModule or SiblingModule so their symbols -// (i.e. foo ) are not available to us. +// (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() @@ -821,13 +821,13 @@ foo(); // Less explicit call on ChildModule.foo() proc main(){ // Parallelism - // In other languages, parallelism is typically done with + // In other languages, parallelism is typically done with // complicated libraries and strange class structure hierarchies. // Chapel has it baked right into the language. - // A begin statement will spin the body of that statement off + // 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. sync { begin { // Start of new task's body @@ -848,7 +848,7 @@ proc main(){ printFibb( 20 ); // new task printFibb( 10 ); // new task printFibb( 5 ); // new task - { + { // This is a nested statement body and thus is a single statement // to the parent statement and is executed by a single task writeln( "this gets" ); @@ -867,26 +867,26 @@ proc main(){ // NOTE! coforall should be used only for creating tasks! // Using it to iterating over a structure is very a bad idea! - // forall loops are another parallel loop, but only create a smaller number + // forall loops are another parallel loop, but only create a smaller number // of tasks, specifically --dataParTasksPerLocale=number of task forall i in 1..100 { write( i, ", "); } writeln( ); - // Here we see that there are sections that are in order, followed by + // Here we see that there are sections that are in order, followed by // a section that would not follow ( e.g. 1, 2, 3, 7, 8, 9, 4, 5, 6, ). // This is because each task is taking on a chunk of the range 1..10 // (1..3, 4..6, or 7..9) doing that chunk serially, but each task happens // in parallel. // Your results may depend on your machine and configuration - // For both the forall and coforall loops, the execution of the + // For both the forall and coforall loops, the execution of the // parent task will not continue until all the children sync up. // forall loops are particularly useful for parallel iteration over arrays. // Lets run an experiment to see how much faster a parallel loop is use Time; // Import the Time module to use Timer objects - var timer: Timer; + var timer: Timer; var myBigArray: [{1..4000,1..4000}] real; // Large array we will write into // Serial Experiment @@ -906,7 +906,7 @@ proc main(){ timer.stop( ); // Stop timer writeln( "Parallel: ", timer.elapsed( ) ); // Print elapsed time 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 // The bracket style loop-expression described @@ -926,15 +926,15 @@ proc main(){ writeln( uranium.read() ); var replaceWith = 239; - var was = uranium.exchange( replaceWith ); + var was = uranium.exchange( replaceWith ); writeln( "uranium was ", was, " but is now ", replaceWith ); var isEqualTo = 235; if ( uranium.compareExchange( isEqualTo, replaceWith ) ) { - writeln( "uranium was equal to ", isEqualTo, + writeln( "uranium was equal to ", isEqualTo, " so replaced value with ", replaceWith ); } else { - writeln( "uranium was not equal to ", isEqualTo, + writeln( "uranium was not equal to ", isEqualTo, " so value stays the same... whatever it was" ); } @@ -989,14 +989,14 @@ proc main(){ } } - // Heres an example of using atomics and a synch variable to create a + // Heres an example of using atomics and a synch variable to create a // count-down mutex (also known as a multiplexer) var count: atomic int; // our counter var lock$: sync bool; // the mutex lock count.write( 2 ); // Only let two tasks in at a time. lock$.writeXF( true ); // Set lock$ to full (unlocked) - // Note: The value doesnt actually matter, just the state + // Note: The value doesnt actually matter, just the state // (full:unlocked / empty:locked) // Also, writeXF() fills (F) the sync var regardless of its state (X) @@ -1005,10 +1005,10 @@ proc main(){ do{ lock$; // Read lock$ (wait) }while ( count.read() < 1 ); // Keep waiting until a spot opens up - + count.sub(1); // decrement the counter lock$.writeXF( true ); // Set lock$ to full (signal) - + // Actual 'work' writeln( "Task #", task, " doing work." ); sleep( 2 ); @@ -1027,13 +1027,13 @@ proc main(){ // 'maxloc' gives max value and index of the max value // Note: We have to zip the array and domain together with the zip iterator - var (theMaxValue, idxOfMax) = maxloc reduce zip(listOfValues, + var (theMaxValue, idxOfMax) = maxloc reduce zip(listOfValues, listOfValues.domain); - + writeln( (sumOfValues, maxValue, idxOfMax, listOfValues[ idxOfMax ] ) ); // Scans apply the operation incrementally and return an array of the - // value of the operation at that index as it progressed through the + // value of the operation at that index as it progressed through the // array from array.domain.low to array.domain.high var runningSumOfValues = + scan listOfValues; var maxScan = max scan listOfValues; @@ -1046,14 +1046,14 @@ 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. -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 [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. ### What this tutorial is lacking: - * Exposition of the standard modules + * Exposition of the [standard modules](http://chapel.cray.com/docs/latest/modules/modules.html) * Multiple Locales (distributed memory system) * Records * Parallel iterators @@ -1061,11 +1061,11 @@ Occasionally check back here and on the [Chapel site](http://chapel.cray.com) to Your input, questions, and discoveries are important to the developers! ----------------------------------------------------------------------- -The Chapel language is still in-development (version 1.11.0), so there are occasional hiccups with performance and language features. +The Chapel language is still in-development (version 1.12.0), so there are occasional hiccups with performance and language features. The more information you give the Chapel development team about issues you encounter or features you would like to see, the better the language becomes. Feel free to email the team and other developers through the [sourceforge email lists](https://sourceforge.net/p/chapel/mailman). -If you're really interested in the development of the compiler or contributing to the project, +If you're really interested in the development of the compiler or contributing to the project, [check out the master Github repository](https://github.com/chapel-lang/chapel). It is under the [Apache 2.0 License](http://www.apache.org/licenses/LICENSE-2.0). @@ -1074,10 +1074,10 @@ Installing the Compiler 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/) -and its as easy as +and its as easy as - 1. `tar -xvf chapel-1.11.0.tar.gz` - 2. `cd chapel-1.11.0` + 1. `tar -xvf chapel-1.12.0.tar.gz` + 2. `cd chapel-1.12.0` 3. `make` 4. `source util/setchplenv.bash # or .sh or .csh or .fish` -- cgit v1.2.3 From eb35f7748a11e04f15cb9901a86702f881155b14 Mon Sep 17 00:00:00 2001 From: David Stockton Date: Sat, 3 Oct 2015 19:40:02 -0600 Subject: Fix usage of "it's" in example comment for chapel --- chapel.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index c8489371..05e5b867 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -1074,14 +1074,14 @@ Installing the Compiler 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/) -and its as easy as +and it's as easy as 1. `tar -xvf chapel-1.11.0.tar.gz` 2. `cd chapel-1.11.0` 3. `make` 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 it's 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 @@ -1100,4 +1100,4 @@ Notable arguments: * `--fast`: enables a number of optimizations and disables array bounds checks. Should only enable when application is stable. * `--set =`: set config param `` to `` at compile-time. * `--main-module `: use the main() procedure found in the module `` as the executable's main. - * `--module-dir `: includes `` in the module search path. \ No newline at end of file + * `--module-dir `: includes `` in the module search path. -- cgit v1.2.3 From 960ee4a1856db8eadb96277bb2422edfa8f2a81c Mon Sep 17 00:00:00 2001 From: Gabriel Halley Date: Wed, 7 Oct 2015 23:11:24 -0400 Subject: removing whitespace all over --- chapel.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index 02a96b04..e20be998 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -307,7 +307,7 @@ var stringSet: domain(string); // empty set of strings stringSet += "a"; stringSet += "b"; stringSet += "c"; -stringSet += "a"; // Redundant add "a" +stringSet += "a"; // Redundant add "a" stringSet -= "c"; // Remove "c" writeln( stringSet ); @@ -524,12 +524,12 @@ genericProc( 1.0+2.0i, 3.0+4.0i ); // The param modifier on the arg is used to enforce this constraint. proc whereProc( param N : int ): void where ( N > 0 ) { - writeln( "N is greater than 0" ); + writeln( "N is greater than 0" ); } proc whereProc( param N : int ): void where ( N < 0 ) { - writeln( "N is less than 0" ); + writeln( "N is less than 0" ); } whereProc( 10 ); -- cgit v1.2.3 From 6f2d38155930911159bfb4e169b4a4430fed2e72 Mon Sep 17 00:00:00 2001 From: Tim Heaney Date: Thu, 8 Oct 2015 07:14:24 -0400 Subject: Typo: "thought of" not "though of" --- chapel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'chapel.html.markdown') diff --git a/chapel.html.markdown b/chapel.html.markdown index e20be998..7252a3e4 100644 --- a/chapel.html.markdown +++ b/chapel.html.markdown @@ -633,7 +633,7 @@ writeln( toThisArray ); // 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. +// the same size, the body of the expression can be thought 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. -- cgit v1.2.3