Created
January 25, 2022 20:27
-
-
Save felipenoris/b69a2e27b644807ffd9a67faa23c95f6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function create_heavy_work_tasks(parallel_jobs::Integer, secs::Integer) :: Vector{Task} | |
jobs = Vector{Task}() | |
for i in 1:parallel_jobs | |
push!(jobs, create_single_heavy_work_task(secs)) | |
end | |
return jobs | |
end | |
function create_single_heavy_work_task(secs::Integer) :: Task | |
return @task begin | |
heavy_work(secs) | |
end | |
end | |
# consumes CPU for `est_duration_secs` secs | |
function heavy_work(est_duration_secs::Integer) | |
num_iterations = 20_000_000 * est_duration_secs | |
acum = 0.0 | |
for i in 1:num_iterations | |
acum += exp(log(i*0.01)) | |
end | |
return acum | |
end | |
function schedule_jobs_with_spawn(jobs::Vector{Task}) | |
@sync for job in jobs | |
Threads.@spawn begin | |
schedule(job) | |
fetch(job) | |
end | |
end | |
nothing | |
end | |
function schedule_jobs_with_threads(jobs::Vector{Task}) | |
Threads.@threads for job in jobs | |
schedule(job) | |
fetch(job) | |
end | |
nothing | |
end | |
function run_heavy_work(parallel_jobs::Integer, secs::Integer; scheduler::Symbol=:spawn) | |
el = @elapsed begin | |
jobs = create_heavy_work_tasks(parallel_jobs, secs) | |
if scheduler === :spawn | |
schedule_jobs_with_spawn(jobs) | |
elseif scheduler === :threads | |
schedule_jobs_with_threads(jobs) | |
else | |
error("Scheduler não suportado: $scheduler") | |
end | |
end | |
@info("heavy_work with $parallel_jobs jobs de $secs secs took $el secs.") | |
nothing | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment