Skip to content

Instantly share code, notes, and snippets.

@dutta-alankar
Created June 24, 2025 08:19
Show Gist options
  • Select an option

  • Save dutta-alankar/ae44303dd577d43f202e2198012c3fa3 to your computer and use it in GitHub Desktop.

Select an option

Save dutta-alankar/ae44303dd577d43f202e2198012c3fa3 to your computer and use it in GitHub Desktop.
Demonstration of stencil distribution on Chapel
import StencilDist.stencilDist;
import StencilDist.boundaries;
const side_low = 1;
const side_high = -1;
config const n = 10;
config const nghost = 2;
config const periodic = false;
const Space = {1..n};
const AllSpace = Space.expand((nghost,));
const Dist = Space dmapped new stencilDist(Space, fluff=(nghost,), periodic=periodic);
var A: [Dist] real;
// Initialize interior values
sync forall i in A.domain do
A[i] = i:real;
sync A.updateFluff();
// Print full array including ghost cells
writeln("Full array (no ghosts):");
for i in A.domain do // replace this with forall to see print order randomized
writeln("inner: A[", i, "] = ", A[i]);
// Access and print the ghost boundaries
writeln("\nGhost boundaries (before update):");
if periodic {
for (arr_val, edge) in A.boundaries() {
// writeln(edge, " ", edge.type:string);
// writeln(arr_val, " ", arr_val.type:string);
for edge_dir in edge {
if edge_dir==side_low {
writeln("Left ghost: ", arr_val);
}
if edge_dir==side_high {
writeln("Right ghost: ", arr_val);
}
}
}
}
else {
for i in AllSpace {
if !Space.contains(i) {
if i<Space.low then
writeln("Left ghost: ", A[i]);
else
writeln("Right ghost: ", A[i]);
}
}
}
writeln("\nUpdating...");
sync forall i in AllSpace {
if Space.contains(i) then
var pass = 1; // anything dummy; here keeping it unchanged
else {
if i<Space.low then
A[i] = A[i+n];
else
A[i] = A[i%n]; // periodic
}
}
sync A.updateFluff();
for i in AllSpace { // replace this with forall to see print order randomized
if Space.contains(i) then
writeln("inner: A[", i, "] = ", A[i]);
else {
if i<Space.low then
writeln("left: A[", i, "] = ", A[i]);
else
writeln("right: A[", i, "] = ", A[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment