Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
pub
pi
Commits
4135bd71
Commit
4135bd71
authored
Feb 09, 2020
by
Neil Gershenfeld
Browse files
wip
parent
d30cf1e9
Pipeline
#4931
passed with stage
in 3 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Python/numbapig.py
0 → 100644
View file @
4135bd71
#
# numbapig.py
# Neil Gershenfeld 2/9/20
# calculation of pi by a Numba CUDA sum
# pi = 3.14159265358979323846
#
from
numba
import
cuda
import
numpy
as
np
import
time
#
# problem size
#
block_size
=
2
**
10
grid_size
=
2
**
20
NPTS
=
grid_size
*
block_size
#
# CUDA kernels
#
@
cuda
.
jit
def
init
(
arr
):
i
=
1
+
cuda
.
grid
(
1
)
arr
[
i
]
=
0.5
/
((
i
-
0.75
)
*
(
i
-
0.25
))
@
cuda
.
reduce
def
sum_reduce
(
a
,
b
):
return
a
+
b
#
# compile kernels
#
arr
=
cuda
.
device_array
(
NPTS
,
np
.
float32
)
init
[
grid_size
,
block_size
](
arr
)
pi
=
sum_reduce
(
arr
)
#
# array calc
#
start_time
=
time
.
time
()
init
[
grid_size
,
block_size
](
arr
)
end_time
=
time
.
time
()
mflops
=
NPTS
*
4.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"Numba CUDA array calculation:"
)
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
#
# reduction
#
start_time
=
time
.
time
()
pi
=
sum_reduce
(
arr
)
end_time
=
time
.
time
()
mflops
=
NPTS
*
1.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"Numba CUDA reduction:"
)
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
#
# both
#
start_time
=
time
.
time
()
init
[
grid_size
,
block_size
](
arr
)
pi
=
sum_reduce
(
arr
)
end_time
=
time
.
time
()
mflops
=
NPTS
*
5.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"Numba CUDA both:"
)
print
(
" NPTS = %d, pi = %f"
%
(
NPTS
,
pi
))
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a 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