diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9ea0f13 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.* +!.gitignore diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..1eaf351 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,29 @@ +BSD 3-Clause License + +Copyright (c) 2022, Journal of Hamradio Informatics +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +* Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..2d40dc7 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +Tutorial: Chapel the Parallel Programming Language +==== + +![image](https://img.shields.io/badge/Chapel-1.28-red.svg) +![image](https://img.shields.io/badge/license-BSD%203--Clause-darkblue.svg) + +[うさぎさんでもわかる並列言語Chapel (PDF)](https://nextzlog.dev/chpl.pdf) [(HTML)](https://nextzlog.dev/chpl) + +## Contribution + +Feel free to make issues at [nextzlog/todo](https://github.com/nextzlog/todo). +Follow [@nextzlog](https://twitter.com/nextzlog) on Twitter. + +## License + +### Author + +[無線部開発班](https://nextzlog.dev) + +### Clauses + +[BSD 3-Clause License](LICENSE.md) diff --git a/chapter1/baz.chpl b/chapter1/baz.chpl new file mode 100644 index 0000000..bc5ed47 --- /dev/null +++ b/chapter1/baz.chpl @@ -0,0 +1,18 @@ +module Foo { + writeln("initilize Foo"); + proc main() { + writeln("This is Foo"); + } +} +module Bar { + writeln("initilize Bar"); + proc main() { + writeln("This is Bar"); + } +} +import baz.Foo; +import baz.Bar; +proc main() { + Foo.main(); + Bar.main(); +} diff --git a/chapter1/hello.chpl b/chapter1/hello.chpl new file mode 100644 index 0000000..30e60d1 --- /dev/null +++ b/chapter1/hello.chpl @@ -0,0 +1,5 @@ +/* +multiline comments + or block comments +*/ +writeln("Hello, world!"); // 1-line comments diff --git a/chapter2/cast.chpl b/chapter2/cast.chpl new file mode 100644 index 0000000..b6bae45 --- /dev/null +++ b/chapter2/cast.chpl @@ -0,0 +1,2 @@ +writeln(1 + 2: real); // 3.0 +writeln(int: string); // int(64) diff --git a/chapter2/reduce.chpl b/chapter2/reduce.chpl new file mode 100644 index 0000000..23f5374 --- /dev/null +++ b/chapter2/reduce.chpl @@ -0,0 +1,3 @@ +writeln(+ scan(1..10)); // 1 3 6 10 15 21 28 36 45 55 +writeln("value, index: ", minloc reduce zip([3, 2, 1], 0..2)); // value, index: (1, 2) +writeln("value, index: ", maxloc reduce zip([4, 5, 6], 0..2)); // value, index: (6, 2) diff --git a/chapter3/config.chpl b/chapter3/config.chpl new file mode 100644 index 0000000..0cc3276 --- /dev/null +++ b/chapter3/config.chpl @@ -0,0 +1,4 @@ +config param foo = 121; +config const bar = 121; +config type num = uint; +writeln(foo, bar, num: string); diff --git a/chapter3/const.chpl b/chapter3/const.chpl new file mode 100644 index 0000000..97971e6 --- /dev/null +++ b/chapter3/const.chpl @@ -0,0 +1,2 @@ +const foo: int = 12; +const bar = 12; diff --git a/chapter3/param.chpl b/chapter3/param.chpl new file mode 100644 index 0000000..1fbeceb --- /dev/null +++ b/chapter3/param.chpl @@ -0,0 +1,3 @@ +param foo: int = 12; +param bar = 12; +type num = int; diff --git a/chapter3/types.chpl b/chapter3/types.chpl new file mode 100644 index 0000000..9209272 --- /dev/null +++ b/chapter3/types.chpl @@ -0,0 +1,7 @@ +param a: bool = true; +param b: uint = 1919; +param c: real = 8.10; +param d: imag = 364i; +param e: string = 'ABC' + "DEF"; +param f: Gibier = Gibier.Rabbit; +enum Gibier {Deer, Boar, Rabbit}; diff --git a/chapter3/var.chpl b/chapter3/var.chpl new file mode 100644 index 0000000..d80bf0e --- /dev/null +++ b/chapter3/var.chpl @@ -0,0 +1,7 @@ +var foo: int = 12; +var bar: int; +foo = 889464; +var a = "golden axe."; +var b = "silver axe."; +a <=> b; +writeln(a, b, foo); diff --git a/chapter4/break.chpl b/chapter4/break.chpl new file mode 100644 index 0000000..0252d7d --- /dev/null +++ b/chapter4/break.chpl @@ -0,0 +1,3 @@ +while true do break; +for 1..100 do continue; +label outside for i in 1..10 do for j in 1..10 do break outside; diff --git a/chapter4/for.chpl b/chapter4/for.chpl new file mode 100644 index 0000000..4a65486 --- /dev/null +++ b/chapter4/for.chpl @@ -0,0 +1 @@ +for i in 1..100 do writeln(i); diff --git a/chapter4/if.chpl b/chapter4/if.chpl new file mode 100644 index 0000000..74b2096 --- /dev/null +++ b/chapter4/if.chpl @@ -0,0 +1,6 @@ +const age = 18; +if age < 18 then { + writeln("Adults Only"); +} else { + writeln("Yeah, Right"); +} diff --git a/chapter4/scope.chpl b/chapter4/scope.chpl new file mode 100644 index 0000000..6dcb298 --- /dev/null +++ b/chapter4/scope.chpl @@ -0,0 +1,6 @@ +{ + var foo = 12; + writeln(foo); +} +var foo = 13; +writeln(foo); diff --git a/chapter4/select.chpl b/chapter4/select.chpl new file mode 100644 index 0000000..c46008e --- /dev/null +++ b/chapter4/select.chpl @@ -0,0 +1,5 @@ +select "cat" { + when "cat" do writeln("meow"); + when "dog" do writeln("bowwow"); + otherwise writeln("gobblegobble"); +} diff --git a/chapter4/while.chpl b/chapter4/while.chpl new file mode 100644 index 0000000..0231a32 --- /dev/null +++ b/chapter4/while.chpl @@ -0,0 +1,4 @@ +var i = 1; +while i < 1024 do i *= 2; +do i >>= 1; while i >= 1; +writeln(i); // 0 diff --git a/chapter5/default.chpl b/chapter5/default.chpl new file mode 100644 index 0000000..57a0fe5 --- /dev/null +++ b/chapter5/default.chpl @@ -0,0 +1,4 @@ +proc foo(x: int = 0, y: int = 0): int return x + y; +writeln(foo(x = 1, y = 2)); // 3 +writeln(foo(y = 2, x = 1)); // 3 +writeln(foo(1)); // 1 diff --git a/chapter5/fact.chpl b/chapter5/fact.chpl new file mode 100644 index 0000000..6e5edb3 --- /dev/null +++ b/chapter5/fact.chpl @@ -0,0 +1,3 @@ +proc fact(param n: int) param: int where n >= 1 return n * fact(n-1); +proc fact(param n: int) param: int where n == 0 return 1; +if fact(8) != 5040 then compilerError("fact(7) == 5040"); diff --git a/chapter5/infer.chpl b/chapter5/infer.chpl new file mode 100644 index 0000000..fc4c885 --- /dev/null +++ b/chapter5/infer.chpl @@ -0,0 +1,3 @@ +proc foo(x, y) return x + y; +writeln(foo(1, 2i)); // 1 + 2i +writeln(foo(2i, 3)); // 3 + 2i diff --git a/chapter5/inline.chpl b/chapter5/inline.chpl new file mode 100644 index 0000000..46c0600 --- /dev/null +++ b/chapter5/inline.chpl @@ -0,0 +1,4 @@ +inline proc foo(x: int): int return 2 * x; +export proc bar(x: int): int return 2 * x; +writeln(foo(100)); // 200 +writeln(bar(300)); // 600 diff --git a/chapter5/intent.chpl b/chapter5/intent.chpl new file mode 100644 index 0000000..be63012 --- /dev/null +++ b/chapter5/intent.chpl @@ -0,0 +1,11 @@ +proc intents(inout x: int, in y: int, out z: int, ref v: int): void { + x += y; + z += y; + v += y; +} +var a: int = 1; +var b: int = 2; +var c: int = 3; +var d: int = 4; +intents(a, b, c, d); +writeln(a, b, c, d); // 3226 diff --git a/chapter5/iter.chpl b/chapter5/iter.chpl new file mode 100644 index 0000000..33aff3f --- /dev/null +++ b/chapter5/iter.chpl @@ -0,0 +1,11 @@ +iter iterator(): string { + yield "EMURATED"; + yield "EMURATED"; + yield "EMURATED"; +} +iter int.these() const ref: int { + for i in 1..this do yield i; +} +var repetition: int = 10; +for i in iterator() do writeln(i); +for i in repetition do writeln(i); diff --git a/chapter5/lambda.chpl b/chapter5/lambda.chpl new file mode 100644 index 0000000..75aa0ab --- /dev/null +++ b/chapter5/lambda.chpl @@ -0,0 +1,3 @@ +proc call(f, x: int, y: int): int return f(x, y); +const add = lambda(x: int, y: int) return x + y;; +writeln(call(add: func(int, int, int), 36, 514)); // 550 diff --git a/chapter5/lvalue.chpl b/chapter5/lvalue.chpl new file mode 100644 index 0000000..9b8bb33 --- /dev/null +++ b/chapter5/lvalue.chpl @@ -0,0 +1,4 @@ +var tuple = (0, 0, 0, 0, 0, 0, 0, 0, 0); +proc A(i: int) ref: int return tuple(i); +coforall i in tuple.indices do A(i) = i; +writeln(tuple); diff --git a/chapter5/name.chpl b/chapter5/name.chpl new file mode 100644 index 0000000..e0f7206 --- /dev/null +++ b/chapter5/name.chpl @@ -0,0 +1,2 @@ +proc foo return 100110; +writeln(foo); diff --git a/chapter5/nest.chpl b/chapter5/nest.chpl new file mode 100644 index 0000000..8b644b3 --- /dev/null +++ b/chapter5/nest.chpl @@ -0,0 +1,8 @@ +proc factorial(num: int): int { + proc tc(n, accum: int): int { + if n == 0 then return accum; + return tc(n - 1, n * accum); + } + return tc(num, 1); +} +writeln(factorial(10)); // 3628800 diff --git a/chapter5/operator.chpl b/chapter5/operator.chpl new file mode 100644 index 0000000..5475d23 --- /dev/null +++ b/chapter5/operator.chpl @@ -0,0 +1,2 @@ +operator *(text: string, num: int) return + reduce (for 1..num do text); +writeln("Lorem ipsum dolor sit amet, consectetur adipiscing elit" * 10); diff --git a/chapter5/param.chpl b/chapter5/param.chpl new file mode 100644 index 0000000..17a8c70 --- /dev/null +++ b/chapter5/param.chpl @@ -0,0 +1,2 @@ +proc tuplet(param dim: int, type eltType) type return dim * eltType; +const septet: tuplet(7, int) = (114, 514, 1919, 810, 364, 889, 464); diff --git a/chapter5/proc.chpl b/chapter5/proc.chpl new file mode 100644 index 0000000..0ef8139 --- /dev/null +++ b/chapter5/proc.chpl @@ -0,0 +1,4 @@ +proc foo(x: int, y: int): int { + return x + y; +} +writeln(foo(1, 2)); diff --git a/chapter5/query.chpl b/chapter5/query.chpl new file mode 100644 index 0000000..95fcd62 --- /dev/null +++ b/chapter5/query.chpl @@ -0,0 +1,6 @@ +proc foo(x: [?dom] ?el) return (dom, el: string); +proc bar(x) return (x.domain, x.eltType: string); +writeln(foo([1, 2, 3, 4, 5, 6, 7, 8])); // ({0..7}, int(64)) +writeln(bar([1, 2, 3, 4, 5, 6, 7, 8])); // ({0..7}, int(64)) +writeln(foo(["one" => 1, "two" => 2])); // ({one, two}, int(64)) +writeln(bar(["one" => 1, "two" => 2])); // ({one, two}, int(64)) diff --git a/chapter5/sched.chpl b/chapter5/sched.chpl new file mode 100644 index 0000000..63691cd --- /dev/null +++ b/chapter5/sched.chpl @@ -0,0 +1,3 @@ +require "sched.h"; +extern proc sched_getcpu(): int; +writeln("CPU:", sched_getcpu()); diff --git a/chapter5/sched.h b/chapter5/sched.h new file mode 100644 index 0000000..138759c --- /dev/null +++ b/chapter5/sched.h @@ -0,0 +1 @@ +int sched_getcpu(); diff --git a/chapter5/throw.chpl b/chapter5/throw.chpl new file mode 100644 index 0000000..50fb78e --- /dev/null +++ b/chapter5/throw.chpl @@ -0,0 +1,10 @@ +proc foo(message: string) throws { + defer writeln("See you"); + throw new Error(message); +} +try { + foo("Hello,"); + foo("world!"); +} catch e { + writeln(e); +} diff --git a/chapter5/varargs.chpl b/chapter5/varargs.chpl new file mode 100644 index 0000000..3a17e30 --- /dev/null +++ b/chapter5/varargs.chpl @@ -0,0 +1,2 @@ +proc sum(x: int ...): int return + reduce(x); +writeln(sum(1, 2, 3) + sum((...(100, 200)))); // 306 diff --git a/chapter5/where.chpl b/chapter5/where.chpl new file mode 100644 index 0000000..7d96903 --- /dev/null +++ b/chapter5/where.chpl @@ -0,0 +1,6 @@ +proc whichType(x: ?xt): string where xt == real return "x is real"; +proc whichType(x: ?xt): string where xt == imag return "x is imag"; +proc whichType(x: ?xt): string return "x is neither real nor imag"; +writeln(whichType(114.514)); +writeln(whichType(364364i)); +writeln(whichType(1919810)); diff --git a/chapter6/accessor.chpl b/chapter6/accessor.chpl new file mode 100644 index 0000000..600654c --- /dev/null +++ b/chapter6/accessor.chpl @@ -0,0 +1,10 @@ +record User { + var name: string; +} +proc User.name ref { + writeln(".name"); + return this.name; +} +var user: User; +user.name = "Jane"; +writeln(user.name); diff --git a/chapter6/deinit.chpl b/chapter6/deinit.chpl new file mode 100644 index 0000000..73cd314 --- /dev/null +++ b/chapter6/deinit.chpl @@ -0,0 +1,9 @@ +class Sub { + const x: real = 0; + const y: real = 0; + proc deinit() { + writeln("deleted"); + } +} +const sub = new Sub(x = 1, y = 2); +writeln("x, y: ", (sub.x, sub.y)); diff --git a/chapter6/duck.chpl b/chapter6/duck.chpl new file mode 100644 index 0000000..053398d --- /dev/null +++ b/chapter6/duck.chpl @@ -0,0 +1,9 @@ +class Duck { + proc quack() return "quack!"; +} +class Kamo { + proc quack() return "quack!"; +} +proc voice(x) return x.quack(); +writeln(voice(new Duck())); // quack! +writeln(voice(new Kamo())); // quack! diff --git a/chapter6/generics.chpl b/chapter6/generics.chpl new file mode 100644 index 0000000..94d9d6a --- /dev/null +++ b/chapter6/generics.chpl @@ -0,0 +1,8 @@ +record Stack { + type eltType; + param size: int; +} +var a: Stack(eltType = uint, size = 12); +var b: Stack(eltType = real, size = 24); +writeln("a is ", a.type: string); // a is Stack(uint(64), 12) +writeln("b is ", b.type: string); // b is Stack(real(64), 24) diff --git a/chapter6/inherit.chpl b/chapter6/inherit.chpl new file mode 100644 index 0000000..1ace22f --- /dev/null +++ b/chapter6/inherit.chpl @@ -0,0 +1,10 @@ +class Foo { + proc foo() return "foo!"; +} +class Bar: Foo { + override proc foo() return super.foo() + "bar!"; +} +const foo = new Foo(); +const bar = new Bar(); +writeln(foo.foo()); // foo! +writeln(bar.foo()); // foo!bar! diff --git a/chapter6/init.chpl b/chapter6/init.chpl new file mode 100644 index 0000000..31b0bc9 --- /dev/null +++ b/chapter6/init.chpl @@ -0,0 +1,10 @@ +class Add { + const x: real; + const y: real; + proc init(x, y) { + this.x = x; + this.y = y; + } +} +const add = new Add(x = 1, y = 2); +writeln("x, y: ", (add.x, add.y)); diff --git a/chapter6/method.chpl b/chapter6/method.chpl new file mode 100644 index 0000000..8dda098 --- /dev/null +++ b/chapter6/method.chpl @@ -0,0 +1,9 @@ +record User { + var name: string; + proc set(name) { + this.name = name; + } +} +var user: User; +user.set("Alicia"); +writeln(user.name); diff --git a/chapter6/num1.chpl b/chapter6/num1.chpl new file mode 100644 index 0000000..48477db --- /dev/null +++ b/chapter6/num1.chpl @@ -0,0 +1,8 @@ +class Num { + var r: real; + var i: imag; +} +var num: Num = new Num(); +num.r = 816.07; +num.i = 14.22i; +writeln(num.r); diff --git a/chapter6/num2.chpl b/chapter6/num2.chpl new file mode 100644 index 0000000..3651475 --- /dev/null +++ b/chapter6/num2.chpl @@ -0,0 +1,8 @@ +record Num { + var r: real; + var i: imag; +} +var num: Num; +num.r = 816.07; +num.i = 14.22i; +writeln(num.r); diff --git a/chapter6/num3.chpl b/chapter6/num3.chpl new file mode 100644 index 0000000..c96f639 --- /dev/null +++ b/chapter6/num3.chpl @@ -0,0 +1,8 @@ +union Num { + var r: real; + var i: imag; +} +var num: Num; +num.r = 816.07; +num.i = 14.22i; +writeln(num.i); diff --git a/chapter6/owned.chpl b/chapter6/owned.chpl new file mode 100644 index 0000000..182d5a1 --- /dev/null +++ b/chapter6/owned.chpl @@ -0,0 +1,7 @@ +class Hello { + proc deinit() { + writeln("See you"); + } +} +var hello: owned Hello = new owned Hello(); +var hallo: borrowed Hello = hello.borrow(); diff --git a/chapter6/shared.chpl b/chapter6/shared.chpl new file mode 100644 index 0000000..8afe15c --- /dev/null +++ b/chapter6/shared.chpl @@ -0,0 +1,7 @@ +class Hello { + proc deinit() { + writeln("See you"); + } +} +var hello = new shared Hello(); +var hallo = new unmanaged Hello(); diff --git a/chapter6/this.chpl b/chapter6/this.chpl new file mode 100644 index 0000000..87c59bd --- /dev/null +++ b/chapter6/this.chpl @@ -0,0 +1,5 @@ +class Add { + proc this(a: int, b: int): int return a + b; +} +const add = new Add(); +writeln(add(123, 45)); // 168 diff --git a/chapter7/array.chpl b/chapter7/array.chpl new file mode 100644 index 0000000..0d34baf --- /dev/null +++ b/chapter7/array.chpl @@ -0,0 +1,4 @@ +const rectangular = [1, 2, 3, 4, 5, 6, 7, 8]; +const associative = [1 => "one", 2 => "two"]; +writeln(rectangular, rectangular.domain); // 1 2 3 4 5 6 7 8{0..7} +writeln(associative, associative.domain); // one two{1, 2} diff --git a/chapter7/by.chpl b/chapter7/by.chpl new file mode 100644 index 0000000..36bbaf2 --- /dev/null +++ b/chapter7/by.chpl @@ -0,0 +1,3 @@ +writeln(10..30 by -7); // 30 23 16 +writeln(10..30 by -7 align 13); // 27 20 13 +writeln(10..30 by -7 align 13 # 2); // 27 20 diff --git a/chapter7/domain.chpl b/chapter7/domain.chpl new file mode 100644 index 0000000..abe58aa --- /dev/null +++ b/chapter7/domain.chpl @@ -0,0 +1,6 @@ +const rectangular: domain = {0..10, -1..2}; +const associative: domain = {"foo", "bar"}; +writeln(rectangular.rank); +writeln(rectangular.dims()); +writeln(rectangular.idxType: string); // int(64) +writeln(associative.idxType: string); // string diff --git a/chapter7/for.chpl b/chapter7/for.chpl new file mode 100644 index 0000000..e4cba51 --- /dev/null +++ b/chapter7/for.chpl @@ -0,0 +1,3 @@ +for i in 1..100 do writeln(i); +forall i in 1..100 do writeln(i); +coforall i in 1..100 do writeln(i); diff --git a/chapter7/loops.chpl b/chapter7/loops.chpl new file mode 100644 index 0000000..3345db7 --- /dev/null +++ b/chapter7/loops.chpl @@ -0,0 +1,2 @@ +for xyz in {1..10, 1..10, 1..10} do writeln(xyz); +for boy in {"Tom", "Ken", "Bob"} do writeln(boy); diff --git a/chapter7/pass.chpl b/chapter7/pass.chpl new file mode 100644 index 0000000..dca6297 --- /dev/null +++ b/chapter7/pass.chpl @@ -0,0 +1,8 @@ +proc update(arr: [] int) { + arr = [2, 3, 4]; +} +var A = [1, 2, 3]; +var B = A; +update(A); +writeln(A); // 2 3 4 +writeln(B); // 1 2 3 diff --git a/chapter7/pattern.chpl b/chapter7/pattern.chpl new file mode 100644 index 0000000..10cdae0 --- /dev/null +++ b/chapter7/pattern.chpl @@ -0,0 +1,2 @@ +proc foo(x: int, (y, z): (int, int)): int return x * (y + z); +writeln(foo(2, (3, 4))); // 14 diff --git a/chapter7/range.chpl b/chapter7/range.chpl new file mode 100644 index 0000000..9e11a7a --- /dev/null +++ b/chapter7/range.chpl @@ -0,0 +1,3 @@ +const from100To200: range(int, boundedType = BoundedRangeType.bounded) = 100..200; +const from100ToInf: range(int, boundedType = BoundedRangeType.boundedLow) = 100..; +const fromInfTo200: range(int, boundedType = BoundedRangeType.boundedHigh) = ..20; diff --git a/chapter7/ref.chpl b/chapter7/ref.chpl new file mode 100644 index 0000000..806cb8f --- /dev/null +++ b/chapter7/ref.chpl @@ -0,0 +1,3 @@ +var boys = ['Tom', 'Ken', 'Bob']; +for boy in boys do boy += '-san'; +writeln(boys); // Tom-san Ken-san Bob-san diff --git a/chapter7/size.chpl b/chapter7/size.chpl new file mode 100644 index 0000000..0869803 --- /dev/null +++ b/chapter7/size.chpl @@ -0,0 +1,5 @@ +writeln((100..200).size); // 101 +writeln((100..200).low); // 100 +writeln((100..200).high); // 200 +writeln((100..200).stride); // 1 +writeln((100..200).alignment); // 100 diff --git a/chapter7/sparse.chpl b/chapter7/sparse.chpl new file mode 100644 index 0000000..f5aa8ad --- /dev/null +++ b/chapter7/sparse.chpl @@ -0,0 +1,5 @@ +var D: sparse subdomain({1..16, 1..64}); +var A: [D] real; +D += (8, 10); +D += (3, 64); +A[8, 10] = 114.514; diff --git a/chapter7/subarray.chpl b/chapter7/subarray.chpl new file mode 100644 index 0000000..83a963b --- /dev/null +++ b/chapter7/subarray.chpl @@ -0,0 +1,5 @@ +var A: [{1..10, 1..10}] real; +var B: [{'foo', 'bar'}] real; +A[1, 2] = 1.2; +A(3, 4) = 3.4; +writeln(A(1..2, 1..3)); diff --git a/chapter7/tuple.chpl b/chapter7/tuple.chpl new file mode 100644 index 0000000..edabbcb --- /dev/null +++ b/chapter7/tuple.chpl @@ -0,0 +1,3 @@ +const nums: 9 * int = (60, 45, 53, 163, 90, 53, 165, 75, 60); +const boys: (string, string, string) = ("Tom", "Ken", "Bob"); +writeln(boys(0), boys(1), boys(2), for name in boys do name); // TomKenBobTom Ken Bob diff --git a/chapter7/unpack.chpl b/chapter7/unpack.chpl new file mode 100644 index 0000000..0b734e7 --- /dev/null +++ b/chapter7/unpack.chpl @@ -0,0 +1,2 @@ +var (a, b): (string, int) = ("student", 24); +writeln(a, b); diff --git a/chapter8/atomic.chpl b/chapter8/atomic.chpl new file mode 100644 index 0000000..38d8167 --- /dev/null +++ b/chapter8/atomic.chpl @@ -0,0 +1,7 @@ +config const N = 80; +var sum: atomic int; +do { + coforall n in 1..N do sum.sub(n * n); + coforall n in 1..N do sum.add(n * n); +} while sum.read() == 0; // infinite loop +writeln(sum); diff --git a/chapter8/begin.chpl b/chapter8/begin.chpl new file mode 100644 index 0000000..904142a --- /dev/null +++ b/chapter8/begin.chpl @@ -0,0 +1,6 @@ +sync { + begin writeln("1st parallel task"); + begin writeln("2nd parallel task"); + begin writeln("3rd parallel task"); +} +writeln("task 1, 2, 3 are finished"); diff --git a/chapter8/block.chpl b/chapter8/block.chpl new file mode 100644 index 0000000..5868294 --- /dev/null +++ b/chapter8/block.chpl @@ -0,0 +1,3 @@ +use BlockDist; +var A: [{1..10,1..10} dmapped Block(boundingBox={1..10,1..10})] real; +for l in Locales do on l do for (i, j) in A.localSubdomain() do writeln(A(i, j).locale); diff --git a/chapter8/cobegin.chpl b/chapter8/cobegin.chpl new file mode 100644 index 0000000..eceb15a --- /dev/null +++ b/chapter8/cobegin.chpl @@ -0,0 +1,6 @@ +cobegin { + writeln("1st parallel task"); + writeln("2nd parallel task"); + writeln("3rd parallel task"); +} +writeln("task 1, 2, 3 are finished"); diff --git a/chapter8/cyclic.chpl b/chapter8/cyclic.chpl new file mode 100644 index 0000000..03b74b6 --- /dev/null +++ b/chapter8/cyclic.chpl @@ -0,0 +1,3 @@ +use CyclicDist; +var B: [{1..10,1..10} dmapped Cyclic(startIdx=(1,1))] real; +for l in Locales do on l do for (i, j) in B.localSubdomain() do writeln(B(i, j).locale); diff --git a/chapter8/forall.chpl b/chapter8/forall.chpl new file mode 100644 index 0000000..f2ecf2f --- /dev/null +++ b/chapter8/forall.chpl @@ -0,0 +1,3 @@ +forall i in 1..100 do writeln(i); +coforall i in 1..100 do writeln(i); +sync for i in 1..100 do begin writeln(i); diff --git a/chapter8/iter.chpl b/chapter8/iter.chpl new file mode 100644 index 0000000..76ea845 --- /dev/null +++ b/chapter8/iter.chpl @@ -0,0 +1,16 @@ +iter foo(rng): int { + for i in rng do yield i; +} +iter foo(param tag, rng): range where tag == iterKind.leader { + if rng.size > 16 { + const mid = (rng.high + rng.low) / 2; + cobegin { + for i in foo(tag, rng(..mid+0)) do yield i; + for i in foo(tag, rng(mid+1..)) do yield i; + } + } else yield rng; +} +iter foo(param tag, rng, followThis): int where tag == iterKind.follower { + for i in followThis do yield i; +} +forall i in foo(1..100) do writeln(i); diff --git a/chapter8/locale.chpl b/chapter8/locale.chpl new file mode 100644 index 0000000..86ffa41 --- /dev/null +++ b/chapter8/locale.chpl @@ -0,0 +1,5 @@ +coforall l in Locales do on l { + writeln(here == l); + writeln(here.name); + writeln(here.numPUs()); +} diff --git a/chapter8/on.chpl b/chapter8/on.chpl new file mode 100644 index 0000000..0b5e158 --- /dev/null +++ b/chapter8/on.chpl @@ -0,0 +1,4 @@ +import BigInteger; +var x: BigInteger.bigint; +on Locales(0) do x = new BigInteger.bigint(12); +on x.locale do writeln(x, " is on ", x.locale); diff --git a/chapter8/race.chpl b/chapter8/race.chpl new file mode 100644 index 0000000..4709597 --- /dev/null +++ b/chapter8/race.chpl @@ -0,0 +1,7 @@ +config const N = 80; +var sum: int; +do { + coforall n in 1..N with(ref sum) do sum -= n * n; + coforall n in 1..N with(ref sum) do sum += n * n; +} while sum == 0; +writeln(sum); diff --git a/chapter8/serial.chpl b/chapter8/serial.chpl new file mode 100644 index 0000000..c3383b5 --- /dev/null +++ b/chapter8/serial.chpl @@ -0,0 +1,4 @@ +serial true { + begin writeln("1st serial task"); + begin writeln("2nd serial task"); +} diff --git a/chapter8/shamble.chpl b/chapter8/shamble.chpl new file mode 100644 index 0000000..de1f213 --- /dev/null +++ b/chapter8/shamble.chpl @@ -0,0 +1,10 @@ +inline proc string.shambles: void { + proc traverse(a: int, b: int) { + if b > a { + const mid = (a + b) / 2; + begin traverse(a, 0 + mid); + begin traverse(1 + mid, b); + } else writeln(this(a)); + } + sync traverse(0, this.size - 1); +} diff --git a/chapter8/sync.chpl b/chapter8/sync.chpl new file mode 100644 index 0000000..62dd19b --- /dev/null +++ b/chapter8/sync.chpl @@ -0,0 +1,13 @@ +config const N = 80; +var sum: sync int = 0; +do { + coforall n in 1..N do { + const v = sum.readFE(); + sum.writeEF(v + n * n); + } + coforall n in 1..N do { + const v = sum.readFE(); + sum.writeEF(v - n * n); + } +} while sum.readFF() == 0; // infinite loop +writeln(sum.readFF()); diff --git a/chapter8/with.chpl b/chapter8/with.chpl new file mode 100644 index 0000000..5dde7a2 --- /dev/null +++ b/chapter8/with.chpl @@ -0,0 +1,8 @@ +var a: string; +var b: string; +sync { + begin with(ref a) a = "1st parallel task"; + begin with(ref b) b = "2nd parallel task"; +} +writeln(a); +writeln(b);