Skip to content

Commit

Permalink
implemented copy_to_buffer to copy data to existing buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mpottinger committed Sep 27, 2022
1 parent fd8c5df commit 187db08
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
6 changes: 3 additions & 3 deletions dcz.metal
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ inline ComplexFloat sinh(ComplexFloat z) { return z.sinh(); }
inline ComplexFloat cosh(ComplexFloat z) { return z.cosh(); }
inline ComplexFloat pow(ComplexFloat x, ComplexFloat y) { return x.pow(y); }

// the complex func sin(z)*sin(c(1)/z)
// sed s/sin(z)*sin(c(1)/z)/"z*z"/g dc.metal > dcz.metal
ComplexFloat z_func(ComplexFloat z) { return sin(z)*sin(c(1)/z);}
// the complex func cos(z)/(sin(z*z*z*z-1))
// sed s/cos(z)/(sin(z*z*z*z-1))/"z*z"/g dc.metal > dcz.metal
ComplexFloat z_func(ComplexFloat z) { return cos(z)/(sin(z*z*z*z-1));}


color HSV2int(float h, const float s, const float v);
Expand Down
13 changes: 11 additions & 2 deletions fractal.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,23 @@
print(f'compiled in {t0:.2} run fractal on size {w, h}={w * h} pix', end='')

# create i/o buffers to match kernel func. params
c = np.array([0.5, 0], dtype=np.float32)
r = np.array([-2, 2], dtype=np.float32)
bsize = m.int_buf([w, h]) # input: size(w,h), center(x,y), range(x,y)
bcenter = m.float_buf([0.5, 0])
brange = m.float_buf([-2, 2])
bcenter = m.float_buf(c)
brange = m.float_buf(r)

bpix = m.empty_int(w * h) # output


while True:
# random center and range
c = np.random.uniform(-1, 1, 2).astype(np.float32)
r = np.random.uniform(-2, 2, 2).astype(np.float32)

print(f' center={c}, range={r}', end='')
m.copy_to_buffer(bcenter, c)
m.copy_to_buffer(brange, r)
m.set_buffers(buffers=(bpix, bsize, bcenter, brange), threads=(w, h))

t = lap()
Expand Down
21 changes: 19 additions & 2 deletions pymetal.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import Metal as mtl




log = logging.getLogger(__name__)

class Metal():
def __init__(self, source, func_name=None):
self.pm = PyMetal()
self.pm.opendevice()
print("Metal shader initializing: ", source, "func_name: ", func_name)
print('devices:', self.pm.lsdev())
if source.find('.metallib') != -1:
self.pm.openlibrary_compiled(source)
elif source.find('.metal') != -1:
Expand Down Expand Up @@ -87,6 +87,11 @@ def run(self):
def get_buffer(self, buf, dtype):
return self.pm.buf2numpy(buf, dtype)

# methods to copy data to existing buffers
def copy_to_buffer(self, buf, data):
self.pm.copynumpy2buf(buf, data)





Expand Down Expand Up @@ -248,13 +253,25 @@ def buf2byte(self, buf):
def buf2numpy(self, buf, dtype):
return numpy.frombuffer(buf.contents().as_buffer(buf.length()), dtype=dtype)

# methods to copy numpy data to existing buffers
def copynumpy2buf(self,buf,data):
# make sure data is a numpy array
if not isinstance(data, numpy.ndarray):
raise Exception("data is not a numpy array")
# make sure data is the right size
if data.nbytes != buf.length():
raise Exception("data is wrong size")
self.buf2byte(buf)[:] = data.tobytes()


def getqueue(self, **kwargs):
cqueue = self.dev.newCommandQueue()
self.setopt(cqueue, kwargs)
cbuffer = cqueue.commandBuffer()
self.setopt(cbuffer, kwargs)
return cqueue, cbuffer


def getmtlsize(self, arg):
if isinstance(arg, int):
return mtl.MTLSize(width=arg, height=1, depth=1)
Expand Down
6 changes: 3 additions & 3 deletions voronoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def randpc(): # random point(x,y), color, n_points
# create i/o buffers to match kernel func. params
# input: points(x,y,color, n_points)


bpointcolor = m.buffer(randpc())
bpix = m.empty_int(w * h) # output
while True:
bpointcolor = m.buffer(randpc())
bpix = m.empty_int(w * h) # output
m.copy_to_buffer(bpointcolor, randpc())
m.set_buffers(buffers=(bpix, bpointcolor), threads=(w, h))

t = lap()
Expand Down

0 comments on commit 187db08

Please sign in to comment.