Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarifies experimental / parallel example on manual.rst #12472

Merged
merged 3 commits into from Oct 24, 2019

Conversation

UNIcodeX
Copy link
Contributor

Details:
Calling useParallel() in example fails with compiler error
Error: 'parallel' section without 'spawn'

Adding spawn causes error:
Error: internal error: (filename: "ccgexprs.nim", line: 1032, column: 17)
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp c

Therefore a separate proc, threadedEcho, is added for the echo'ing
of the string, which allows the example to build, however, sync()
must be added so that the "echo in parallel" strings will actually
be shown on the terminal. Otherwise, the program will spawn of the
threads and exit before they can return to the main thread.

Details:
Calling `useParallel()` in example fails with compiler error
  Error: 'parallel' section without 'spawn'

Adding `spawn` causes error:
  Error: internal error: (filename: "ccgexprs.nim", line: 1032, column: 17)
  No stack traceback available
  To create a stacktrace, rerun compilation with ./koch temp c <file>

Therefore a separate proc, `threadedEcho`, is added for the echo'ing
of the string, which allows the example to build, however, `sync()`
must be added so that the "echo in parallel" strings will actually
be shown on the terminal. Otherwise, the program will spawn of the
threads and exit before they can return to the main thread.
@UNIcodeX UNIcodeX changed the title Clarifies experimental / parallel on manual.rst Clarifies experimental / parallel example on manual.rst Oct 21, 2019
@UNIcodeX
Copy link
Contributor Author

UNIcodeX commented Oct 21, 2019

Observation: Sometimes without sync() all threads echo as expected, and sometimes they don't. I'm unsure if this is an issue with my machine(s).

It may be good, for illustrative purposes of the example, to actually expand a bit further and output the thread number, illustrating that threads spawned later may return before threads spawned earlier.

import threadpool, strformat
{.experimental: "parallel".}

proc threadedEcho(s: string, i: int) =
  echo(fmt"'{s}' for thread # {i}")

proc useParallel() =
  parallel:
    for i in 0..4:
      spawn threadedEcho("echo in parallel", i)
  sync()

useParallel()

Which, in my testing, yields something like this:

'echo in parallel' for thread # 0
'echo in parallel' for thread # 2
'echo in parallel' for thread # 1
'echo in parallel' for thread # 3
'echo in parallel' for thread # 4

@Araq
Copy link
Member

Araq commented Oct 21, 2019

parallel does not require sync! If it doesn't work, that's a bug, sync is wrong here.

@UNIcodeX
Copy link
Contributor Author

UNIcodeX commented Oct 21, 2019

I've ran it several more times without the sync() and did not have the issue. Chalking that one up to some weirdness.

What about the rest??

Merge remote-tracking branch 'upstream/devel' into devel
Issue:
Calling useParallel() in example failed with compiler error
`Error: 'parallel' section without 'spawn'`

Adding spawn yielded compiler error:
```bash
Error: internal error: (filename: "ccgexprs.nim", line: 1032, column: 17)
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp c
```

Proposed Solution:
- Separate proc, threadedEcho, is added for the echo'ing
  of the string, which allows the example to build
- Added the thread number so that it can demonstrate that sometimes
  threads which were started sooner, come back after threads which
  were started later.
@UNIcodeX
Copy link
Contributor Author

UNIcodeX commented Oct 23, 2019

Removed sync().

Added output of thread number.

import threadpool
{.experimental: "parallel".}

proc threadedEcho(s: string, i: int) =
  echo(s, " ", $i)

proc useParallel() =
  parallel:
    for i in 0..4:
      spawn threadedEcho("echo in parallel", i)

useParallel()

example output:

echo in parallel 0
echo in parallel 3
echo in parallel 4
echo in parallel 1
echo in parallel 2

@narimiran narimiran merged commit d731646 into nim-lang:devel Oct 24, 2019
narimiran pushed a commit that referenced this pull request Oct 24, 2019
…12472)

* Clarifies experimental / parallel on manual.rst

Details:
Calling `useParallel()` in example fails with compiler error
  Error: 'parallel' section without 'spawn'

Adding `spawn` causes error:
  Error: internal error: (filename: "ccgexprs.nim", line: 1032, column: 17)
  No stack traceback available
  To create a stacktrace, rerun compilation with ./koch temp c <file>

Therefore a separate proc, `threadedEcho`, is added for the echo'ing
of the string, which allows the example to build, however, `sync()`
must be added so that the "echo in parallel" strings will actually
be shown on the terminal. Otherwise, the program will spawn of the
threads and exit before they can return to the main thread.

* Fixes and clarifies example for threading in manual.rst

Issue:
Calling useParallel() in example failed with compiler error
`Error: 'parallel' section without 'spawn'`

Adding spawn yielded compiler error:
```bash
Error: internal error: (filename: "ccgexprs.nim", line: 1032, column: 17)
No stack traceback available
To create a stacktrace, rerun compilation with ./koch temp c
```

Proposed Solution:
- Separate proc, threadedEcho, is added for the echo'ing
  of the string, which allows the example to build
- Added the thread number so that it can demonstrate that sometimes
  threads which were started sooner, come back after threads which
  were started later.

(cherry picked from commit d731646)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants