-
Notifications
You must be signed in to change notification settings - Fork 1
/
testSimplePyCuda.py
executable file
·144 lines (133 loc) · 3.89 KB
/
testSimplePyCuda.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python
# basic example from SimplePyCuda library
# MIT License - Igor Machado Coelho (2017)
import ctypes
from simplepycuda import SimplePyCuda, SimpleSourceModule, Grid, Block
import numpy
def simpleLoadTest(cuda):
lib = ctypes.cdll.LoadLibrary('./__simplepycuda_kernel_doublify.so')
lib.kernel_loader.argtypes = [ctypes.c_void_p, Grid, Block, ctypes.c_ulong, ctypes.c_ulong]
a = numpy.random.randn(4,4)
a_gpu = cuda.mem_alloc(a.nbytes)
lib.kernel_loader(a_gpu, Grid(1, 1), Block(4, 4, 1), 0, 0)
print "Kernel OK"
# finish
def classicExample(cuda):
a = numpy.random.randn(4,4)
a = a.astype(numpy.float32)
print a
a_gpu = cuda.mem_alloc(a.nbytes)
cuda.memcpy_htod(a_gpu, a)
mod = SimpleSourceModule("""
#include<stdio.h>
__global__ void doublify ( float* a )
{
int idx = threadIdx.x + threadIdx.y*4;
a[idx] *= 2;
printf("oi=%d\\n",idx);
}
""","nvcc", ["--ptxas-options=-v","--compiler-options -O3","--compiler-options -Wall"])
func = mod.get_function("doublify")
# TODO: this next line will be made automatically in get_function method... just need a few more time :)
func.argtypes = [ctypes.c_void_p, Grid, Block, ctypes.c_ulong, ctypes.c_ulong]
func(a_gpu, Grid(1, 1), Block(4, 4, 1), 0, 0)
cuda.memcpy_dtoh(a, a_gpu)
cuda.deviceSynchronize()
print a
cuda.free(a_gpu) # this is not necessary in PyCUDA
print "Finished"
def main():
cuda = SimplePyCuda()
classicExample(cuda)
return 0
print '============ SimplePyCuda ============'
p = cuda.malloc(10)
print "malloc'd 10 bytes. GPU pointer:", p
cuda.free(p)
print "free'd 10 bytes at", p
#
print "will reset device"
cuda.deviceReset()
#
print "number of GPU devices:",cuda.getDeviceCount()
print "selecting GPU device 0"
cuda.setDevice(0)
#
print "creating float matrix"
a = numpy.random.randn(4,3)
print(a)
a = a.astype(numpy.float32)
print a.nbytes, "bytes"
a_gpu = cuda.mem_alloc(a.nbytes)
print "GPU pointer=",a_gpu
cuda.memcpy_htod(a_gpu, a)
cuda.memcpy_dtoh(a, a_gpu)
print(a)
#
cuda.free(a_gpu)
#
print "creating integer matrix (as vector)"
b = numpy.random.randn(4,3)*10
b = b.astype(numpy.int32)
print(b)
b_gpu = cuda.mem_alloc(b.nbytes)
cuda.memcpy_htod(b_gpu, b)
#
print "filling first 4 bytes (first integer) with 0x00, 0 in decimal"
cuda.memset(b_gpu, 0x00, 4)
cuda.memcpy_dtoh(b, b_gpu)
print(b)
#
print "filling first 10 bytes (first two integers and half) with 0x12, 303174162 in decimal"
cuda.memset(b_gpu, 0x12, 10)
cuda.memcpy_dtoh(b, b_gpu)
print(b)
#
print "filling all bytes with 0x12 (303174162 in decimal)"
cuda.memset(b_gpu, 0x12, b.nbytes)
cuda.memcpy_dtoh(b, b_gpu)
print(b)
#
print "synchronizing device"
cuda.deviceSynchronize()
#
c = numpy.random.randn(1000,1000)
c = c.astype(numpy.float32)
t1 = cuda.eventCreate()
t2 = cuda.eventCreate()
cuda.eventRecord(t1)
c_gpu = cuda.mem_alloc(c.nbytes)
cuda.memcpy_htod(c_gpu, c)
cuda.memcpy_dtoh(c, c_gpu)
cuda.eventRecord(t2)
cuda.eventSynchronize(t2) # if not used, will issue a 'cudaErrorNotReady' error.
time = cuda.eventElapsedTime(t1, t2)
print "took",time,"ms to copy",c.nbytes,"bytes"
cuda.eventDestroy(t1)
cuda.eventDestroy(t2)
#
print ""
print "will test doublify kernel"
cuda.memcpy_htod(a_gpu, a)
print a
mod = SimpleSourceModule(""" __global__ void doublify ( float* a )
{
int idx = threadIdx.x + threadIdx.y*4;
a[idx] *= 2;
//printf("oi=%d\\n",idx);
}
""")
func = mod.get_function("doublify")
# TODO: this will enter automatically in get_function method... just need a few more time :)
func.argtypes = [ctypes.c_void_p, Grid, Block, ctypes.c_ulong, ctypes.c_ulong]
func(a_gpu, Grid(1, 1), Block(4, 4, 1), 0, 0)
cuda.deviceSynchronize()
print "kernel executed"
cuda.memcpy_dtoh(a, a_gpu)
print a
#
print "will reset device"
cuda.deviceReset()
print '============ Finish SimplePyCuda example ============'
if __name__ == '__main__':
main()