提交 5147d5fc authored 作者: Olivier Breuleux's avatar Olivier Breuleux

more code in cc.py

上级 834c5aa3
from link import Linker
from copy import copy
class CodeBlock:
def __init__(self, declare, behavior, cleanup, sub):
self.declare = declare % sub
behavior_sub = copy(sub)
behavior_sub['fail'] = "{goto __label_%(id)i}" % sub
self.behavior = behavior % behavior_sub
self.cleanup = ("__label_%(id)i:\n" + cleanup) % sub
def code_gen(blocks):
decl = ""
head = ""
tail = ""
for block in blocks:
decl += block.declare
head = head + ("\n{\n%s" % block.behavior)
tail = ("%s\n}\n" % block.cleanup) + tail
return decl + head + tail
def struct_gen(args, struct_builders, blocks, sub):
struct_decl = ""
struct_init_head = ""
struct_init_tail = ""
struct_cleanup = ""
for block in blocks:
struct_decl += block.declare
struct_init_head = struct_init_head + ("\n{\n%s" % block.behavior)
struct_init_tail = ("%s\n}\n" % block.cleanup) + struct_init_tail
struct_cleanup += block.cleanup
behavior = code_gen(blocks)
args = ", ".join(["PyObject* %s" % arg for arg in args])
struct_code = """
struct __struct_%%(id)s {
%(struct_decl)s
__struct_%%(id)s(void) {}
~__struct_%%(id)s(void) {
cleanup();
}
void init(%(args)s) {
%(struct_init_head)s
return;
%(struct_init_tail)s
}
void cleanup(void) {
%(struct_cleanup)s
}
void run(void) {
%(behavior)s
}
};
""" % locals()
return struct_code
class CLinker(Linker):
......@@ -7,6 +72,9 @@ class CLinker(Linker):
def __init__(self, env):
self.env = env
def extract_sync(self, to_extract, to_sync, to_cleanup):
pass
def code_gen(self):
env = self.env
order = env.toposort()
......
......@@ -145,30 +145,6 @@ class ResultBase(object):
raise AbstractFunctionError()
# #
# # alloc
# #
# def alloc(self):
# """Create self.data from data_alloc, and set state to Allocated
# Graph routines like the linker will ask Ops to allocate outputs. The
# Ops, in turn, usually call this function. Results that are involved in
# destroy maps and view maps are exceptions to the usual case.
# """
# self.data = self.data_alloc() #might raise exception
# self.state = Allocated
# def data_alloc(self):
# """(abstract) Return an appropriate _data based on self.
# If a subclass overrides this function, then that overriding
# implementation will be used in alloc() to produce a data object.
# """
# raise AbstractFunctionError()
#
# C code generators
#
......
......@@ -23,7 +23,7 @@ class Mul(BinaryScalarOp):
def impl(self, x, y):
return x * y
def c_impl(self, (x, y), z):
return "%(z)s = %(x)s * %(y)s;"
return "%(z)s = %(x)s + %(y)s;"
def grad(self, (x, y), gz):
return mul(y, gz), mul(x, gz)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论