Skip to content

Add mt_out_of_order #6

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

Merged
merged 6 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 0 additions & 41 deletions .appveyor.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
steps:
- uses: julia-actions/setup-julia@latest
with:
version: 1.3
version: 1.4
- name: Pkg.add("CompatHelper")
run: julia -e 'using Pkg; Pkg.add("CompatHelper")'
- name: CompatHelper.main()
Expand Down
33 changes: 25 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ language: julia
os:
- linux
- osx
- windows

julia:
- 1.0
- 1.2
- 1.3
- 1.4
- nightly

env:
- JULIA_NUM_THREADS=1
- JULIA_NUM_THREADS=4

## uncomment the following lines to allow failures on nightly julia
## (tests will run but not make your overall status red)
matrix:
allow_failures:
- julia: nightly
fast_finish: true
arch:
- amd64
- x86

branches:
only:
- master
- dev
- /^release-.*$/
- /^v\d+\.\d+(\.\d+)?(-\S*)?$/

notifications:
email: false
Expand All @@ -32,10 +38,21 @@ after_success:
- julia -e 'import Pkg; Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())'

jobs:
allow_failures:
# - julia: nightly
- os: windows
julia: 1.0
env: JULIA_NUM_THREADS=4
fast_finish: true
exclude:
# To recuce number of tests:
- arch: x86
env: JULIA_NUM_THREADS=1
include:
- stage: "Documentation"
julia: 1.0
julia: 1.4
os: linux
arch: amd64
script:
- julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path=pwd())); Pkg.instantiate()'
- julia --project=docs/ docs/make.jl
Expand Down
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.4.1"

[deps]
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"

[compat]
julia = "1"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[![Documentation for development version](https://img.shields.io/badge/docs-dev-blue.svg)](https://oschulz.github.io/ParallelProcessingTools.jl/dev)
[![License](http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](LICENSE.md)
[![Travis Build Status](https://travis-ci.com/oschulz/ParallelProcessingTools.jl.svg?branch=master)](https://travis-ci.com/oschulz/ParallelProcessingTools.jl)
[![Appveyor Build Status](https://ci.appveyor.com/api/projects/status/github/oschulz/ParallelProcessingTools.jl?branch=master&svg=true)](https://ci.appveyor.com/project/oschulz/ParallelProcessingTools-jl)
[![Codecov](https://codecov.io/gh/oschulz/ParallelProcessingTools.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/oschulz/ParallelProcessingTools.jl)

This Julia package provides some tools to ease multithreaded and distributed programming, especially for more complex use cases and when using multiple processes with multiple threads on each process. It also provides functions and macros designed to ease the transition to the new multi-threading model introduced in Julia v1.3.
Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"

[compat]
Documenter = "~0.23"
Documenter = "~0.24"
5 changes: 3 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ makedocs(
prettyurls = !("local" in ARGS),
canonical = "https://oschulz.github.io/ParallelProcessingTools.jl/stable/"
),
pages=[
pages = [
"Home" => "index.md",
"API" => "api.md",
"LICENSE" => "LICENSE.md",
Expand All @@ -26,5 +26,6 @@ makedocs(

deploydocs(
repo = "github.com/oschulz/ParallelProcessingTools.jl.git",
forcepush = true
forcepush = true,
push_preview = true,
)
2 changes: 2 additions & 0 deletions src/ParallelProcessingTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ module ParallelProcessingTools
using Base.Threads
using Distributed

using MacroTools

include("threadsafe.jl")
include("threadlocal.jl")
include("onthreads.jl")
Expand Down
56 changes: 55 additions & 1 deletion src/onthreads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export @onthreads

macro onallthreads(expr)
quote
Base.depwarn("`@onallthreads expr` is deprecated, use `@onallthreads allthreads() expr` instead.", nothing)
Base.depwarn("`@onallthreads expr` is deprecated, use `@onthreads allthreads() expr` instead.", nothing)
$(_thread_exec_func(:(ParallelProcessingTools.allthreads()), expr))
end
end
Expand Down Expand Up @@ -223,3 +223,57 @@ macro mt_async(expr)
end
end
export @mt_async


using MacroTools
"""
@mt_out_of_order begin expr... end

Runs all top-level expressions in `begin expr... end` on parallel tasks.
On Julia >= v1.3, the tasks will run multi-threaded.

Example:

```
@mt_out_of_order begin
a = foo()
bar()
c = baz()
end

will run `a = foo()`, `bar()` and `c = baz()` in parallel and in arbitrary
order, results of assignments will appear in the outside scope.
"""
macro mt_out_of_order(ex)
if !(ex isa Expr && ex.head == :block)
throw(ErrorException("@mt_out_of_order expects a code block as it's argument"))
end

exprs = ex.args
idxs = eachindex(ex.args)
tasks = gensym(:tasks)
handle_results = Vector{Expr}()
for i in idxs
trg = nothing; val = nothing;
if @capture(exprs[i], trg_ = val_)
if val isa Expr
exprs[i] = :(push!($tasks, @mt_async($(esc(val)))))
push!(handle_results, :($(esc(trg)) = fetch(popfirst!($tasks))))
else
exprs[i] = esc(exprs[i])
end
elseif exprs[i] isa Expr
ftvar = gensym()
exprs[i] = :(push!($tasks, @mt_async($(esc(exprs[i])))))
push!(handle_results, :(wait(popfirst!($tasks))))
else
exprs[i] = esc(exprs[i])
end
end
pushfirst!(exprs, :($tasks = Vector{Task}()))
append!(exprs, handle_results)
push!(exprs, :(@assert isempty($tasks)))
push!(exprs, :nothing)
ex
end
export @mt_out_of_order
17 changes: 17 additions & 0 deletions test/test_onthreads.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ using Base.Threads
end
end

@testset "macro mt_out_of_order" begin
@test begin
b = 0
foo() = b = 9
bar() = 33

@mt_out_of_order begin
a = (sleep(0.05); 42)
foo()
c::Int = bar()

d = :trivial
end
(a, b, c, d) == (42, 9, 33, :trivial)
end
end

@testset "Examples" begin
@testset "Example 1" begin
tlsum = ThreadLocal(0.0)
Expand Down