-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.py
61 lines (48 loc) · 1.41 KB
/
alloc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import random
import cupy
import numpy
import util
sizes = [2 ** i for i in range(20)] * 100
random.seed(0)
random.shuffle(sizes)
def dummy_call():
pass
def mem_empty():
cupy.cuda.alloc(0)
def mem_1K():
cupy.cuda.alloc(1024)
def mem_1M():
cupy.cuda.alloc(1024 * 1024)
cnt = 1000
util.measure(dummy_call, "dummy_call", cnt)
util.measure(mem_empty, "mem_0B", cnt)
util.measure(mem_1K, "mem_1K", cnt)
util.measure(mem_1M, "mem_1M", cnt)
sizes = [(0, "0B"), (1024, "1K"), (1024 * 1024, "1M")]
for xp in [cupy, numpy]:
if xp is cupy:
str = "cp"
else:
str = "np"
if xp is cupy:
for size, s in sizes:
memptr = cupy.cuda.alloc(size)
def alloc():
xp.ndarray((size,), dtype='b', memptr=memptr)
util.measure(alloc, "%s_memptr_%s" % (str, s), cnt)
for size, s in sizes:
def alloc():
xp.ndarray((size,), dtype='b')
util.measure(alloc, "%s_alloc_%s" % (str, s), cnt)
for size, s in sizes:
def alloc():
xp.empty((size,), dtype='b')
util.measure(alloc, "%s_empty_%s" % (str, s), cnt)
for size, s in sizes:
def zeros():
xp.zeros((size,), dtype='b')
util.measure(zeros, "%s_zeros_%s" % (str, s), cnt)
for size, s in sizes:
def ones():
xp.ones((size,), dtype='b')
util.measure(ones, "%s_ones_%s" % (str, s), cnt)