-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod2.py
More file actions
215 lines (161 loc) · 5.8 KB
/
method2.py
File metadata and controls
215 lines (161 loc) · 5.8 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
###################
## This is a simple PyOpenCL example
##
## The purpose is to evaluate Bicubic Bezier surfaces on the GPU,
## AUTHOR: Shayan Javed
##
import sys
import pyopencl as cl
from numpy import *
import numpy
import time
# Global variables
degreeU = 0
degreeV = 0
detail = 0.0
# class definition begins here
# degree = the degree of the bezier curve
# detail = how finely rendererd the curve should be (increments
class BezierCurve:
def __init__(self, degree=4, detail=0.1, vertices=[]):
self.degree = degree
self.detail = detail
# vertices is a 2d-list [ [x0 y0 z0] [x1 y1 z1] ... [xN yN zN] ]
self.vertices = vertices
# the function to read a bezier file - returns list of vertices
def readBezierFile(fileName):
# open the file first
file = open(fileName)
# read all the text
str = file.read()
# split it up line-by-line
lines = str.split('\n')
# first line - degree + detail
line1 = lines[0].split()
global degreeU
global degreeV
global detail
degreeU = int(line1[0])
degreeV = int(line1[1])
detail = float(line1[2])
order = int(degreeU) + 1
# create list
# numpy array - make sure to specify astype as float32 to avoid /128 errors
vertices = empty( (order * order, 4)).astype(numpy.float32)
# go through the rest of the lines, read in the curve
index = 1
while index <= (len(lines) - 1):
# read line
line = lines[index].split()
# create a new list
newList = [line[0], line[1], line[2], 0]
# assign values
vertices[index-1] = newList
# increment
index = index + 1
return vertices
# reads a file and returns all the text as a string
def readFile(fileName):
# open the file first
file = open(fileName, 'r')
# read all the text
str = "".join(file.readlines())
return str
# the main function
def main(argv=None):
if argv is None:
argv = sys.argv
# try to read from a file here - returns the array
vertices = readBezierFile("patch1")#"curve1")# * 10.0
print vertices
print "Vertices:"
print vertices.size
# read the kernel file
kernelString = readFile("bezier.cl")
# the bezier curve TODO: fix to surface instead
#curve1 = BezierCurve(len(vertices), 0.1, vertices)
# Array of UV values - 36 in total (detail by 0.2)
uvValues = empty( (36, 2)).astype(numpy.float32)
index = 0
for u in range(0, 12, 2): # step = 2
for v in range(0, 12, 2):
# conver the ints to floats
fU = float(u)
fV = float(v)
uvValues[index] = [ fU/10.0, fV/10.0]
index = index + 1
print "\nUV Values: "
print uvValues
print uvValues.size
##########################
### OPENCL SETUP
#########################
# Platform test
for found_platform in cl.get_platforms():
if found_platform.name == 'NVIDIA CUDA':
my_platform = found_platform
print "Selected platform:", my_platform.name
for device in my_platform.get_devices():
dev_type = cl.device_type.to_string(device.type)
if dev_type == 'GPU':
dev = device
print "Selected device: ", dev_type
# context
# 2 ways of creating it. First directly specifying the dev_type and the 2nd just does it some way I guess
ctx = cl.Context([dev])
#ctx = cl.create_some_context()
# command queue
cq = cl.CommandQueue(ctx, properties=cl.command_queue_properties.PROFILING_ENABLE)
# memory flags
mf = cl.mem_flags
# create the Buffers
# input buffers
# the control point vertices
vertex_buffer = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=vertices)
# the uv buffer
uv_buffer = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=uvValues)
# final output
output_buffer = cl.Buffer(ctx, mf.WRITE_ONLY, uvValues.nbytes * 2) # double the amount of value
# the OpenCL program
prg = cl.Program(ctx, kernelString).build()
##########################
# DIFFERENT STARTS HERE
#########################
# the global size (number of uv-values): 36
globalSize = 36
localSize = 36 # 36 threads per work-group? Then only 1 work-group
# timer start
start = time.time()
# evaluate
exec_evt = prg.bezierEval2(cq, (globalSize,), (localSize,), vertex_buffer, uv_buffer, output_buffer)# cl.LocalMemory(9 * numpy.dtype('float32').itemsize * 4), cl.LocalMemory(4 * numpy.dtype('float32').itemsize * 4)) # local_buffer)#numpy.int32(degreeU), numpy.float32(u), vertex_buffer, inter_buf)
exec_evt.wait()
# elapsed time
elapsed = exec_evt.profile.end - exec_evt.profile.start
print("Execution time of test: %g " % elapsed)
# read back the result - works!
#eval = empty((36, 4)).astype(numpy.float32);
eval = empty( 36*4).astype(numpy.float32)#numpy.empty_like(vertices)
cl.enqueue_read_buffer(cq, output_buffer, eval).wait()
print "It took: ", time.time() - start,"seconds."
# Output to a file
f = open('output2', 'w')
index = 0
j = 6
while index < 36:
val1 = eval[index * 4]
val2 = eval[index * 4 + 1]
val3 = eval[index * 4 + 2]
val4 = eval[index * 4 + 3]
f.write(repr(round(val1, 2)).rjust(j))
f.write(" ")
f.write(repr(round(val2, 2)).rjust(j))
f.write(" ")
f.write(repr(round(val3, 2)).rjust(j))
f.write(" ")
f.write(repr(round(val4, 2)).rjust(j))
f.write("\n")
index = index + 1
#print eval
# main invocation
if __name__ == "__main__":
sys.exit(main())