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
21d19cbd
Commit
21d19cbd
authored
Feb 09, 2020
by
Neil Gershenfeld
Browse files
wip
parent
4135bd71
Pipeline
#4932
passed with stage
in 4 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Python/numbapig.py
View file @
21d19cbd
...
...
@@ -11,51 +11,92 @@ import time
# problem size
#
block_size
=
2
**
10
grid_size
=
2
**
2
0
grid_size
=
2
**
2
1
NPTS
=
grid_size
*
block_size
#
#
CUDA
kernels
# kernels
and functions
#
@
cuda
.
jit
def
init
(
arr
):
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
def
s
um_reduce
(
a
,
b
):
def
N
um
ba
_reduce
(
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
)
#
# compile kernels
#
init
[
grid_size
,
block_size
](
arr
)
pi
=
sum_reduce
(
arr
)
pi
=
Numba_reduce
(
arr
)
CUDA_reduce
(
arr
,
NPTS
)
#
# array calc
#
CUDA kernel
array calc
ulation
#
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
(
"
CUDA kernel
array calculation:"
)
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
#
# reduc
tion
#
Numba
reduc
e
#
init
[
grid_size
,
block_size
](
arr
)
start_time
=
time
.
time
()
pi
=
s
um_reduce
(
arr
)
pi
=
N
um
ba
_reduce
(
arr
)
end_time
=
time
.
time
()
mflops
=
NPTS
*
1.0
/
(
1.0e6
*
(
end_time
-
start_time
))
print
(
"Numba
CUDA
reduc
tion
:"
)
print
(
"Numba reduc
e
:"
)
print
(
" time = %f, estimated MFlops = %f"
%
(
end_time
-
start_time
,
mflops
))
#
# both
# both
with Numba reduce
#
start_time
=
time
.
time
()
init
[
grid_size
,
block_size
](
arr
)
pi
=
s
um_reduce
(
arr
)
pi
=
N
um
ba
_reduce
(
arr
)
end_time
=
time
.
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
(
" 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