Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pi
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pub
pi
Commits
4135bd71
Commit
4135bd71
authored
Feb 09, 2020
by
Neil Gershenfeld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wip
parent
d30cf1e9
Pipeline
#4931
passed with stage
in 3 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
61 additions
and
0 deletions
+61
-0
Python/numbapig.py
Python/numbapig.py
+61
-0
No files found.
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