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
21d19cbd
Commit
21d19cbd
authored
Feb 09, 2020
by
Neil Gershenfeld
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
wip
parent
4135bd71
Pipeline
#4932
passed with stage
in 4 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
15 deletions
+56
-15
Python/numbapig.py
Python/numbapig.py
+56
-15
No files found.
Python/numbapig.py
View file @
21d19cbd
...
@@ -11,51 +11,92 @@ import time
...
@@ -11,51 +11,92 @@ import time
# problem size
# problem size
#
#
block_size
=
2
**
10
block_size
=
2
**
10
grid_size
=
2
**
2
0
grid_size
=
2
**
2
1
NPTS
=
grid_size
*
block_size
NPTS
=
grid_size
*
block_size
#
#
#
CUDA kernel
s
#
kernels and function
s
#
#
@
cuda
.
jit
@
cuda
.
jit
def
init
(
arr
):
def
init
(
arr
):
i
=
1
+
cuda
.
grid
(
1
)
i
=
1
+
cuda
.
grid
(
1
)
arr
[
i
]
=
0.5
/
((
i
-
0.75
)
*
(
i
-
0.25
))
arr
[
i
-
1
]
=
0.5
/
((
i
-
0.75
)
*
(
i
-
0.25
))
#
@
cuda
.
reduce
@
cuda
.
reduce
def
sum
_reduce
(
a
,
b
):
def
Numba
_reduce
(
a
,
b
):
return
a
+
b
return
a
+
b
#
#
# compile kernels
@
cuda
.
jit
def
CUDA_sum
(
arr
,
len
):
i
=
cuda
.
grid
(
1
)
if
(
i
<
len
):
arr
[
i
]
+=
arr
[
i
+
len
]
#
def
CUDA_reduce
(
arr
,
NPTS
):
len
=
NPTS
>>
1
while
(
1
):
CUDA_sum
[
grid_size
,
block_size
](
arr
,
len
)
len
=
len
>>
1
if
(
len
==
0
):
return
#
# device array
#
#
arr
=
cuda
.
device_array
(
NPTS
,
np
.
float32
)
arr
=
cuda
.
device_array
(
NPTS
,
np
.
float32
)
#
# compile kernels
#
init
[
grid_size
,
block_size
](
arr
)
init
[
grid_size
,
block_size
](
arr
)
pi
=
sum_reduce
(
arr
)
pi
=
Numba_reduce
(
arr
)
CUDA_reduce
(
arr
,
NPTS
)
#
#
#
array calc
#
CUDA kernel array calculation
#
#
start_time
=
time
.
time
()
start_time
=
time
.
time
()
init
[
grid_size
,
block_size
](
arr
)
init
[
grid_size
,
block_size
](
arr
)
end_time
=
time
.
time
()
end_time
=
time
.
time
()
mflops
=
NPTS
*
4.0
/
(
1.0e6
*
(
end_time
-
start_time
))
mflops
=
NPTS
*
4.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"
Numba CUDA
array calculation:"
)
print
(
"
CUDA kernel
array calculation:"
)
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
#
#
#
reduction
#
Numba reduce
#
#
init
[
grid_size
,
block_size
](
arr
)
start_time
=
time
.
time
()
start_time
=
time
.
time
()
pi
=
sum
_reduce
(
arr
)
pi
=
Numba
_reduce
(
arr
)
end_time
=
time
.
time
()
end_time
=
time
.
time
()
mflops
=
NPTS
*
1.0
/
(
1.0e6
*
(
end_time
-
start_time
))
mflops
=
NPTS
*
1.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"Numba
CUDA reduction
:"
)
print
(
"Numba
reduce
:"
)
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
#
#
# both
# both
with Numba reduce
#
#
start_time
=
time
.
time
()
start_time
=
time
.
time
()
init
[
grid_size
,
block_size
](
arr
)
init
[
grid_size
,
block_size
](
arr
)
pi
=
sum
_reduce
(
arr
)
pi
=
Numba
_reduce
(
arr
)
end_time
=
time
.
time
()
end_time
=
time
.
time
()
mflops
=
NPTS
*
5.0
/
(
1.0e6
*
(
end_time
-
start_time
))
mflops
=
NPTS
*
5.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"
Numba CUDA both
:"
)
print
(
"
both with Numba reduce
:"
)
print
(
" NPTS = %d, pi = %f"
%
(
NPTS
,
pi
))
print
(
" NPTS = %d, pi = %f"
%
(
NPTS
,
pi
))
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
#
# CUDA kernel reduction
#
init
[
grid_size
,
block_size
](
arr
)
start_time
=
time
.
time
()
CUDA_reduce
(
arr
,
NPTS
)
end_time
=
time
.
time
()
mflops
=
NPTS
*
1.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"CUDA kernel reduction:"
)
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
#
# both with CUDA kernel reduction
#
start_time
=
time
.
time
()
init
[
grid_size
,
block_size
](
arr
)
CUDA_reduce
(
arr
,
NPTS
)
end_time
=
time
.
time
()
darr
=
arr
.
copy_to_host
()
mflops
=
NPTS
*
5.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"both with CUDA kernel reduction:"
)
print
(
" NPTS = %d, pi = %f"
%
(
NPTS
,
darr
[
0
]))
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