Beispiel #1
0
def chunked_eval(blz_expr, chunk_size=32768):
    operands, code = blz_expr.gen_blir()
    total_size = _dimension(operands)
    temps = [_temp_for(i, chunk_size) for i in operands]
    temp_op = [
        i for i in zip(temps, operands) if isinstance(i[1], blaze.Array)
    ]
    offset = 0
    accum = 0.0
    _, env = blir.compile(code)
    ctx = blir.Context(env)
    while offset < total_size:
        curr_chunk_size = min(total_size - offset, chunk_size)
        slice_chunk = slice(0, curr_chunk_size)
        slice_src = slice(offset, offset + curr_chunk_size)
        for temp, op in temp_op:
            temp[slice_chunk] = op[slice_src]

        accum += blir.execute(ctx,
                              args=temps + [curr_chunk_size],
                              fname='main')
        offset = slice_src.stop

    ctx.destroy()
    return accum
Beispiel #2
0
def chunkwise_kernel():
    ast, env = compile(source)

    #Array = ca.carray(xrange(25000), rootdir='example1', mode='w',
                #dtype='int32', cparams=ca.cparams(clevel=0))
    Array = open('example1', mode='w')
    c = Array.data.ca
    ctx = Context(env)

    for i in range(c.nchunks):
        chunk = c.chunks[i]
        # read only access
        #x = c.chunks[0][:]
        # write access
        x = view(chunk)

        size = x.strides[0]
        args = (x, size)
        execute(ctx, args, fname='main')

        # this does a _save() behind the scenes
        c.chunks[i] = chunk

    ctx.destroy()

    rts = Runtime(1,2,3)
    rts.join()

    print Array
Beispiel #3
0
    def __init__(self, root_node, operands=None):
        assert root_node.op == "dot"

        if operands:
            root_node = deepcopy(root_node)
            self._ParameterBinder(operands).accept(root_node)

        terms = self._ExtractTerminals().accept(root_node)
        terms = {obj: "in%d" % i for i, obj in enumerate(terms)}

        str_signature = self._gen_blir_signature(terms)
        str_lhs = self._GenerateExpression(terms).accept(root_node.lhs)
        str_rhs = self._GenerateExpression(terms).accept(root_node.rhs)
        code = """
def main(%s, n: int) -> float {
    var float accum = 0.0;
    var int i = 0;
    for i in range(n) {
        accum = accum + (%s*%s);
    }
    return accum;
}
""" % (
            str_signature,
            str_lhs,
            str_rhs,
        )

        _, self.env = blir.compile(code)
        self.ctx = blir.Context(self.env)
        self.operands = list(terms)
        self.code = code
        self.time = 0.0
Beispiel #4
0
    def __init__(self, root_node, operands=None):
        assert (root_node.op == 'dot')

        if operands:
            root_node = deepcopy(root_node)
            self._ParameterBinder(operands).accept(root_node)

        terms = self._ExtractTerminals().accept(root_node)
        terms = {obj: 'in%d' % i for i, obj in enumerate(terms)}

        str_signature = self._gen_blir_signature(terms)
        str_lhs = self._GenerateExpression(terms).accept(root_node.lhs)
        str_rhs = self._GenerateExpression(terms).accept(root_node.rhs)
        code = '''
def main(%s, n: int) -> float {
    var float accum = 0.0;
    var int i = 0;
    for i in range(n) {
        accum = accum + (%s*%s);
    }
    return accum;
}
''' % (str_signature, str_lhs, str_rhs)

        _, self.env = blir.compile(code)
        self.ctx = blir.Context(self.env)
        self.operands = list(terms)
        self.code = code
        self.time = 0.0
Beispiel #5
0
def chunked_eval(blz_expr, chunk_size=32768):
    operands, code = blz_expr.gen_blir()
    total_size = _dimension(operands)
    temps = [_temp_for(i, chunk_size) for i in operands]
    temp_op = [i for i in zip(temps, operands) if isinstance(i[1], blaze.Array)]
    offset = 0
    accum = 0.0
    _, env = blir.compile(code)
    ctx = blir.Context(env)
    while offset < total_size:
        curr_chunk_size = min(total_size - offset, chunk_size)
        slice_chunk = slice(0, curr_chunk_size)
        slice_src = slice(offset, offset + curr_chunk_size)
        for temp, op in temp_op:
            temp[slice_chunk] = op[slice_src]

        accum += blir.execute(ctx, args=temps + [curr_chunk_size], fname="main")
        offset = slice_src.stop

    ctx.destroy()
    return accum
Beispiel #6
0
 def compile(self, **opts):
     """ Compile the kernel into native code. """
     from blaze.blir import compile
     return compile(str(self), **opts)
Beispiel #7
0
 def compile(self, **opts):
     """ Compile the kernel into native code. """
     from blaze.blir import compile
     return compile(str(self), **opts)
Beispiel #8
0
 def compile(self):
     import blaze.blir as bb
     return bb.compile(str(self.build_source()))
Beispiel #9
0
#------------------------------------------------------------------------

source = """
def main(x: array[int], n : int) -> void {
    var int i;
    var int j;
    for i in range(n) {
        for j in range(n) {
            x[i,j] = i+j;
        }
    }
}
"""

N = 15
ast, env = compile(source)

arr = np.eye(N, dtype='int32')
args = (arr, N)

ctx = Context(env)
execute(ctx, args, timing=True)
ctx.destroy()

print arr

#------------------------------------------------------------------------
# Vector Dot Product
#------------------------------------------------------------------------

N = 50000
Beispiel #10
0
#------------------------------------------------------------------------

source = """
def main(x: array[int], n : int) -> void {
    var int i;
    var int j;
    for i in range(n) {
        for j in range(n) {
            x[i,j] = i+j;
        }
    }
}
"""

N = 15
ast, env = compile(source)

arr = np.eye(N, dtype='int32')
args = (arr, N)

ctx = Context(env)
execute(ctx, args, timing=True)
ctx.destroy()

print arr

#------------------------------------------------------------------------
# Vector Dot Product
#------------------------------------------------------------------------

N = 50000