-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpmt1.jl
76 lines (64 loc) · 1.5 KB
/
mpmt1.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env julia
# -*- coding: utf-8 -*-
#
# mpmt1.jl: Julia version of mpmt1.py
#
# License:
# Apache License, Version 2.0
# History:
# * 2022/03/21 v0.1 Initial version
# Author:
# Masanori Itoh <masanori.itoh@gmail.com>
# TODO:
# * Add multiprocessing mode?
#
using Dates
using Printf
using Getopt
using Base.Threads
#using Benchmarktools
function busy_worker(identity, duration)
msg = @sprintf "busy_worker: %d started" identity
println(msg)
ts_start = time()
while true
ts_now = time()
if (ts_now - ts_start) > duration
msg = @sprintf "busy_worker: %d duration %d expired" identity duration
println(msg)
break
end
end
end
#
# main routine
#
num_context = 4
duration = 5
use_thread = 1
# https://juliapackages.com/p/getopt
for (opt, arg) in Getopt.getopt(ARGS, "n:d:m:")
#@printf "%s %s\n" opt arg
if opt == "-n"
global num_context = parse(Int, arg)
end
if opt == "-d"
global duration = parse(Int, arg)
end
if opt == "-m"
if arg == "p" || arg == "P"
# currently ignored.
global use_thread = 0
end
end
end
@printf "num_context = %d duration = %d\n" num_context duration
#@printf "DEBUG: nthreads = %d" Threads.nthreads()
if num_context > Threads.nthreads()
@printf "ERROR: Increase nthrads(%d to %d) via JULIA_NUM_THREADS or -t.\n" Threads.nthreads() num_context
exit()
end
#@printf "nthreads = %d\n" Threads.nthreads()
@threads for t in 1:num_context
busy_worker(t, duration)
end