-
-
Notifications
You must be signed in to change notification settings - Fork 888
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import cupy | ||
import time | ||
|
||
device = cupy.cuda.Device() | ||
memory_pool = cupy.cuda.MemoryPool() | ||
cupy.cuda.set_allocator(memory_pool.malloc) | ||
rand = cupy.random.generator.RandomState(seed=1) | ||
|
||
n = 10 | ||
streams = [] | ||
events = [] | ||
zs = [] | ||
|
||
start_time = time.time() | ||
for i in range(n): | ||
stream = cupy.cuda.stream.Stream() | ||
streams.append(stream) | ||
with stream: | ||
x = rand.normal(size=(1, 1024 * 256)) | ||
y = rand.normal(size=(1024 * 256, 1)) | ||
z = cupy.matmul(x, y) | ||
zs.append(z) | ||
events.append(stream.record()) | ||
|
||
reduction_stream = cupy.cuda.stream.Stream() | ||
# Block the reduction_stream until all event occurs. This does not block host. | ||
# This is not required when reduction is performed in the default (Stream.null) | ||
# stream unless streams are created with `non_blocking=True` flag. | ||
for i in range(n): | ||
reduction_stream.wait_event(events[i]) | ||
with reduction_stream: | ||
z = sum(zs) | ||
|
||
device.synchronize() | ||
elapsed_time = time.time() - start_time | ||
print('elapsed time', elapsed_time) | ||
print('total bytes', memory_pool.total_bytes()) |