Why did I get this behaviour?

77 Views Asked by At

I was going through the specification of Chapel and was reading on Task Level Parallelism, in particular the synchronization variables (sync and single) and the logical state of them and how they go about. I came across this example given in the specification on this Link

var count$: sync int=0;
cobegin{
    count$+=1
    count$+=1
    count$+=1
}

On running the above code, I get an error but the specification does not talk about it and expects the program to run properly. Why do I get this behaviour?

2

There are 2 best solutions below

0
On

Chapel is a language that uses semicolons to terminate statements in order to avoid ambiguities. It looks to me like you dropped the semicolons from the three statements within the cobegin when transcribing the example from the language specification and that it should work once they are restored (TIO):

var count$: sync int=0;
cobegin{
    count$+=1;
    count$+=1;
    count$+=1;
}
writeln(count$.readFE());

Note that most of the examples in the language specification are available within the Chapel release in the $CHPL_HOME/examples/spec/ directory, organized by chapter so that they need not be re-typed or cut-and-pasted. For example, that example (whose name is indicated as syncCounter.chpl) can be found in $CHPL_HOME/examples/spec/Task_Parallelism_and_Synchronization/syncCounter.chpl.

If you're working from the development branch on GitHub rather than an official release, they can be found in $CHPL_HOME/test/release/examples/spec after you have executed a make spectests command from the $CHPL_HOME directory.

2
On

In the specification, the count$ += 1 lines each have a semicolon at the end:

var count$: sync int = 0;
cobegin {
  count$ += 1;
  count$ += 1;
  count$ += 1;
}

which works for me.

Without the semicolons, the syntax error is expected.

However, the internal error: assertion error [AST/build.cpp:2374] (or internal error: AST-BUI-2374 ... is unexpected. As its discoverer, would you like to open an issue in the issue tracker: https://github.com/chapel-lang/chapel/issues ?