Example #1
0
def run(args, options, param=-1, merge_opens=True, \
            reallocate=True, debug=False):
    """ Compile a file and output a Program object.
    
    If merge_opens is set to True, will attempt to merge any parallelisable open
    instructions. """

    prog = Program(args, options, param)
    instructions.program = prog
    instructions_base.program = prog
    types.program = prog
    comparison.program = prog
    prog.DEBUG = debug
    VARS['program'] = prog
    comparison.set_variant(options)

    print 'Compiling file', prog.infile

    if instructions_base.Instruction.count != 0:
        print 'instructions count', instructions_base.Instruction.count
        instructions_base.Instruction.count = 0
    prog.FIRST_PASS = False
    prog.reset_values()
    # make compiler modules directly accessible
    sys.path.insert(0, 'Compiler')
    # create the tapes
    execfile(prog.infile, VARS)

    # optimize the tapes
    for tape in prog.tapes:
        tape.optimize(options)

    if prog.main_thread_running:
        prog.update_req(prog.curr_tape)
    print 'Program requires:', repr(prog.req_num)
    print 'Memory size:', prog.allocated_mem

    # finalize the memory
    prog.finalize_memory()

    return prog
Example #2
0
def run(filename, options, param=-1, merge_opens=True, emulate=True, \
            reallocate=True, assemblymode=False, debug=False):
    """ Compile a file and output a Program object.
    
    If merge_opens is set to True, will attempt to merge any parallelisable open
    instructions. """
    
    prog = Program(filename, options, param, assemblymode)
    instructions.program = prog
    instructions_base.program = prog
    types.program = prog
    comparison.program = prog
    prog.EMULATE = emulate
    prog.DEBUG = debug
    VARS['program'] = prog
    comparison.set_variant(options)
    
    print 'Compiling file', prog.infile
    
    # no longer needed, but may want to support assembly in future (?)
    if assemblymode:
        prog.restart_main_thread()
        for i in xrange(INIT_REG_MAX):
            VARS['c%d'%i] = prog.curr_block.new_reg('c')
            VARS['s%d'%i] = prog.curr_block.new_reg('s')
            VARS['cg%d'%i] = prog.curr_block.new_reg('cg')
            VARS['sg%d'%i] = prog.curr_block.new_reg('sg')
            if i % 10000000 == 0 and i > 0:
                print "Initialized %d register variables at" % i, time.asctime()
        
        # first pass determines how many assembler registers are used
        prog.FIRST_PASS = True
        execfile(prog.infile, VARS)
    
    if instructions_base.Instruction.count != 0:
        print 'instructions count', instructions_base.Instruction.count
        instructions_base.Instruction.count = 0
    prog.FIRST_PASS = False
    prog.reset_values()
    # make compiler modules directly accessible
    sys.path.insert(0, 'Compiler')
    # create the tapes
    execfile(prog.infile, VARS)
    
    # optimize the tapes
    for tape in prog.tapes:
        tape.optimize(options)
    
    # check program still does the same thing after optimizations
    if emulate:
        clearmem = list(prog.mem_c)
        sharedmem = list(prog.mem_s)
        prog.emulate()
        if prog.mem_c != clearmem or prog.mem_s != sharedmem:
            print 'Warning: emulated memory values changed after compiler optimization'
        #    raise CompilerError('Compiler optimization caused incorrect memory write.')
    
    if prog.main_thread_running:
        prog.update_req(prog.curr_tape)
    print 'Program requires:', repr(prog.req_num)
    #print 'Cost:', prog.req_num.cost()
    print 'Memory size:', prog.allocated_mem

    # finalize the memory
    prog.finalize_memory()

    return prog
Example #3
0
def run_arithmetic(args, options, param=-1, merge_opens=True, \
                   reallocate=True, debug=False):

    from Compiler.program import Program
    from Compiler.config import *
    from Compiler.exceptions import *
    import instructions, instructions_base, types, comparison, library

    import interface
    from interface import ASTParser as ASTParser
    import inspect
    import copy
    interface.mpc_type = interface.SPDZ

    _interface = [t[1] for t in inspect.getmembers(interface, inspect.isclass)]
    for op in _interface:
        VARS[op.__name__] = op
    """ Compile a file and output a Program object.
    
    If merge_opens is set to True, will attempt to merge any parallelisable open
    instructions. """

    prog = Program(args, options, param)
    instructions.program = prog
    instructions_base.program = prog
    types.program = prog
    comparison.program = prog
    prog.DEBUG = debug

    VARS['program'] = prog
    comparison.set_variant(options)

    print 'Compiling file', prog.infile

    if instructions_base.Instruction.count != 0:
        print 'instructions count', instructions_base.Instruction.count
        instructions_base.Instruction.count = 0
    prog.FIRST_PASS = False
    prog.reset_values()
    # make compiler modules directly accessible
    sys.path.insert(0, 'Compiler')
    # create the tapes
    print 'Compiling file', prog.infile
    party = options.party

    a = ASTParser(prog.infile, party, debug=True)
    vectorized_calls, local_program = a.parse(options.split)

    if local_program:
        local_file = open(os.path.join(options.filename, "local.py"), "w")
        local_file.write(local_program)
        local_file.close()

    a.execute(VARS)

    # optimize the tapes
    for tape in prog.tapes:
        tape.optimize(options)

    if prog.main_thread_running:
        prog.update_req(prog.curr_tape)
    print 'Program requires:', repr(prog.req_num)
    print 'Memory size:', prog.allocated_mem
    print 'Program requires {0} rounds of communication in total.'.format(
        prog.rounds)
    print 'Program requires {0} invocations in total.'.format(prog.invocations)
    print 'Matmul calls: {0}'.format(vectorized_calls)
    for k in vectorized_calls.keys():
        prog.req_num[k] = vectorized_calls[k]

    num_vectorized_triples, num_vectorized_bits = sub_vectorized_triples(
        vectorized_calls, prog.req_num)
    print "Due to vectorized triples, Reduced triple count by: {0}. Reduced bit count by: {1}.".format(
        num_vectorized_triples, num_vectorized_bits)
    #prog.req_num[('modp', 'triple')] -= num_vectorized_triples
    #prog.req_num[('modp', 'bit')] -= num_vectorized_bits
    # Don't want negative triples/bits
    assert (prog.req_num[('modp', 'triple')] >= num_vectorized_triples)
    assert (prog.req_num[('modp', 'bit')] >= num_vectorized_bits)

    # finalize the memory
    prog.finalize_memory()

    # Write file to output
    prog.write_bytes(options.outfile)
    return prog