Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Michael Krause
burnpy
Commits
6b0b82d3
Commit
6b0b82d3
authored
Apr 29, 2020
by
Michael Krause
🎉
Browse files
publish 1.0.0
parents
Pipeline
#5792
passed with stage
in 48 seconds
Changes
5
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
.gitignore
0 → 100644
View file @
6b0b82d3
__pycache__
.gitlab-ci.yml
0 → 100644
View file @
6b0b82d3
image
:
docker
services
:
-
docker:dind
variables
:
CONTAINER_RELEASE_IMAGE
:
registry.git.mpib-berlin.mpg.de/krause/burnpy:latest
stages
:
-
test
-
build
test
:
image
:
python:latest
script
:
-
python test.py
build
:
script
:
-
docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.git.mpib-berlin.mpg.de
-
docker build -t $CONTAINER_RELEASE_IMAGE .
-
docker push $CONTAINER_RELEASE_IMAGE
only
:
-
master
Dockerfile
0 → 100644
View file @
6b0b82d3
FROM
python:alpine
COPY
burn.py /bin/
ENTRYPOINT
["/bin/burn.py"]
burn.py
0 → 100755
View file @
6b0b82d3
#!/usr/bin/env python3
import
sys
import
os
from
socket
import
gethostname
from
math
import
floor
,
log10
from
concurrent.futures
import
ProcessPoolExecutor
,
as_completed
def
main
():
if
len
(
sys
.
argv
)
!=
2
and
len
(
sys
.
argv
)
!=
3
:
help
()
sys
.
exit
(
1
)
n
=
float
(
sys
.
argv
[
1
])
if
len
(
sys
.
argv
)
==
3
:
threads
=
int
(
sys
.
argv
[
2
])
else
:
threads
=
1
sysinfo
()
burn
(
n
,
threads
)
def
help
():
print
(
"Single core burner computing fib(n) recursively."
)
print
(
" Usage: {} <hours> [number of threads]"
.
format
(
sys
.
argv
[
0
]))
def
sysinfo
():
proc
=
'/proc/{}/status'
.
format
(
os
.
getpid
())
cpuset
=
''
with
open
(
proc
,
'r'
)
as
f
:
for
line
in
f
.
readlines
():
if
'Cpus_allowed_list'
in
line
:
cpuset
=
line
.
split
(
':'
,
1
)[
1
].
strip
()
print
(
'Sysinfo:'
)
print
(
' Hostname: {}'
.
format
(
gethostname
()))
print
(
' CPUSet: [{}]'
.
format
(
cpuset
))
def
burn
(
hours
,
threads
):
"""
Solve:
time(fib(36)) = 10s
time(fib(n)) = c * 1.618**n
10 = c ** 1.618**n
log10(10)/log10(1.618) = log10(c)/log10(1.618) + n
log10(10) - n*log10(1.618) = log10(c)
10**(1 - n*log10(1.618)) = c
"""
# c is the characteristic speed of this cpu
# and I measured time(fib(36)) = 10s
c
=
10
**
(
1
-
36
*
log10
(
1.618
))
n
=
log10
(
hours
*
3600
/
c
)
/
log10
(
1.618
)
n
=
int
(
floor
(
n
))
print
(
"Burning {} hours by stupidly calculating fib({}), using {} thread(s)"
.
format
(
hours
,
n
,
threads
))
with
ProcessPoolExecutor
(
max_workers
=
threads
)
as
pool
:
futures
=
{
pool
.
submit
(
fib
,
n
)
for
i
in
range
(
threads
)}
for
index
,
future
in
enumerate
(
as_completed
(
futures
)):
print
(
"(thread {}) fib({}) = "
"{} , congrats."
.
format
(
index
,
n
,
future
.
result
()))
def
fib
(
n
):
if
n
==
0
or
n
==
1
:
return
1
else
:
return
fib
(
n
-
1
)
+
fib
(
n
-
2
)
if
__name__
==
'__main__'
:
main
()
test.py
0 → 100644
View file @
6b0b82d3
import
unittest
import
time
from
burn
import
fib
class
TestFib
(
unittest
.
TestCase
):
def
test_fib
(
self
):
"""
Test fib()
"""
self
.
assertEqual
(
fib
(
0
),
1
)
self
.
assertEqual
(
fib
(
1
),
1
)
self
.
assertEqual
(
fib
(
5
),
8
)
def
test_slowness
(
self
):
"""
Verify fib() is super slow
"""
start
=
time
.
time
()
_
=
fib
(
25
)
end
=
time
.
time
()
self
.
assertGreater
(
end
-
start
,
1e-3
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment