-
Notifications
You must be signed in to change notification settings - Fork 2
/
hello6-taskpar-dist.chpl
122 lines (99 loc) · 3.9 KB
/
hello6-taskpar-dist.chpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Distributed memory task parallel hello world
/*
usage on puma/ocelote:
chpl hello6-taskpar-dist.chpl
./hello6-taskpar-dist -nl 2
# something else to try
./hello6-taskpar-dist -nl 4 --tasksPerLocale=3
usage on laptop with docker:
docker pull docker.io/chapel/chapel-gasnet // only do this once, but it takes a few minutes
docker run --rm -v "$PWD":/myapp -w /myapp chapel/chapel-gasnet chpl hello6-taskpar-dist.chpl
docker run --rm -v "$PWD":/myapp -w /myapp chapel/chapel-gasnet ./hello6-taskpar-dist -nl 4
# something else to try
docker run --rm -v "$PWD":/myapp -w /myapp chapel/chapel-gasnet ./hello6-taskpar-dist -nl 4 --tasksPerLocale=3
See https://chapel-lang.org/docs/examples/index.html for more hello world variants.
1/25/23, downloaded from
https://github.com/chapel-lang/chapel/blob/main/test/release/examples/hello6-taskpar-dist.chpl
*/
/* This program shows how to use Chapel's task parallelism and
locality features to create a concurrent "Hello, world" program
that spans distributed memory `locales` (compute nodes) as well as
(optionally) the processing units (cores) within each locale. The
number of locales on which to run is specified on the executable's
command line using the `-nl` or `--numLocales` flag (e.g.,
``./hello -nl 64``).
*/
//
// The following `config const` specifies whether or not the locale
// name should be printed (for ease-of-testing purposes); the default
// can be overridden on the execution command line (e.g.,
// ``--printLocaleName=false``).
//
config const printLocaleName = true;
//
// This one specifies the number of tasks to use per locale:
//
config const tasksPerLocale = 1;
//
// Use a `coforall-loop` to create a distinct task per locale on which
// the program is executing. Here, we're iterating over the built-in
// `Locales` array that stores an array of locale values
// corresponding 1:1 with the system's compute nodes on which the
// program is executing. Thus, each iteration corresponds to one of
// the locales, represented by the loop index variable `loc`.
//
coforall loc in Locales {
//
// Migrate each task to its corresponding locale, `loc`. This is done
// using an `'on'-clause`, which moves execution of the current task
// to the locale corresponding to the expression following it.
//
on loc {
//
// Now use a second coforall-loop to create a number of tasks
// corresponding to the `tasksPerLocale` configuration constant.
// Since this loop body doesn't contain any on-clauses, all tasks will
// remain local to the current locale.
//
coforall tid in 0..#tasksPerLocale {
//
// Start building up the message to print using a string
// variable, `message`.
//
var message = "Hello, world! (from ";
//
// If we're running more than one task per locale, specialize the
// message based on the task ID.
//
if (tasksPerLocale > 1) then
message += "task " + tid:string + " of " + tasksPerLocale:string + " on ";
//
// Specialize the message based on the locale on which this task is
// running:
//
// * `here` refers to the locale on which this task is running (same as `loc`)
// * `.id` queries its unique ID in 0..`numLocales`-1
// * `.name` queries its name (similar to UNIX ``hostname``)
// * `numLocales` refers to the number of locales (as specified by -nl)
//
message += "locale " + here.id:string + " of " + numLocales:string;
if printLocaleName then message += " named " + loc.name;
//
// Terminate the message
//
message += ")";
//
// Print out the message. Since each message is being printed by a
// distinct task, they may appear in an arbitrary order.
//
writeln(message);
}
}
}
//
// For further examples of using task parallelism, refer to
// :ref:`examples/primers/taskParallel.chpl <primers-taskParallel>`.
//
// For further examples of using locales and on-clauses, refer to
// :ref:`examples/primers/locales.chpl <primers-locales>`.
//