From d6731755de22ab46158d0418c44ee15a70d58522 Mon Sep 17 00:00:00 2001 From: Jean-Francois Baffier Date: Sun, 24 Jan 2021 14:21:47 +0900 Subject: [PATCH] v0.1.4: Multithreading (#26) * doc up * Added multihreading in optimize! (#25) * Added multihreading in optimize! * Updated CI for multithreading * Bump new version for multithreading --- .github/workflows/ci.yml | 4 ++++ Project.toml | 4 ++-- src/CompositionalNetworks.jl | 3 ++- src/genetic.jl | 11 +++++++---- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 088c407..f16fd0e 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -18,6 +18,8 @@ jobs: arch: - x64 - x86 + threads: + - "2" exclude: - os: macOS-latest arch: x86 @@ -41,6 +43,8 @@ jobs: ${{ runner.os }}- - uses: julia-actions/julia-buildpkg@v1 - uses: julia-actions/julia-runtest@v1 + env: + JULIA_NUM_THREADS: ${{ matrix.threads }} - uses: julia-actions/julia-processcoverage@v1 - uses: codecov/codecov-action@v1 with: diff --git a/Project.toml b/Project.toml index 8196929..b3275bd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "CompositionalNetworks" uuid = "4b67e4b5-442d-4ef5-b760-3f5df3a57537" authors = ["Jean-François Baffier"] -version = "0.1.3" +version = "0.1.4" [deps] ConstraintDomains = "5800fd60-8556-4464-8d61-84ebf7a0bedb" @@ -11,7 +11,7 @@ OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" [compat] -ConstraintDomains = "0.1.2" +ConstraintDomains = "0.1" Dictionaries = "0.3" Evolutionary = "0.7, 0.8" OrderedCollections = "1.3" diff --git a/src/CompositionalNetworks.jl b/src/CompositionalNetworks.jl index f743106..3fae0be 100644 --- a/src/CompositionalNetworks.jl +++ b/src/CompositionalNetworks.jl @@ -5,8 +5,9 @@ import Evolutionary: GA, tournament, singlepoint, flip, optimize, minimizer, Opt import Random: bitrand, falses import OrderedCollections: LittleDict import Dictionaries: Dictionary, set! -import Base.Iterators: product +import Base.Iterators: product, flatten import ConstraintDomains: _get_domain, _length +import Base.Threads: @threads, threadid, nthreads # Exports utilities export lazy, lazy_param, csv2space diff --git a/src/genetic.jl b/src/genetic.jl index 8da0038..584374c 100644 --- a/src/genetic.jl +++ b/src/genetic.jl @@ -46,14 +46,17 @@ Optimize and set the weigths of an ICN with a given set of configuration `X` and function optimize!(icn, X, X_sols, global_iter, local_iter, dom_size, param=nothing; metric=hamming, popSize=100) results = Dictionary{BitVector,Int}() - @info "Starting optimization of weights" - for i in 1:global_iter + aux_results = Vector{BitVector}(undef, global_iter) + @info """Starting optimization of weights$(nthreads() > 1 ? " (multithreaded)" : "")""" + @threads for i in 1:global_iter @info "Iteration $i" - _optimize!(icn, X, X_sols, dom_size, param; + aux_icn = deepcopy(icn) + _optimize!(aux_icn, X, X_sols, dom_size, param; iter=local_iter, metric=metric, pop_size=popSize ) - _incsert!(results, _weigths(icn)) + aux_results[i] = _weigths(aux_icn) end + foreach(bv -> _incsert!(results, bv), aux_results) best = rand(findall(x -> x == maximum(results), results)) _weigths!(icn, best) @info show_composition(icn) best results