Example #1
0
    def __init__(self,
                 dace_code,
                 fake_fname,
                 source_code=None,
                 sdfg=None,
                 headless=False):

        self.compiled = False
        self.dace_tmpfile = None
        self.dace_filename = os.path.basename(fake_fname)
        self.sdfg = sdfg  # This is the toplevel one
        self.sdfgs = []  # This is a collection of all the SDFGs
        self.generated_code = []
        self.generated_code_files = None
        self.matching_patterns = []
        self.headless = headless
        self.dace_code = dace_code
        self.source_code = source_code
        self.errors = [
        ]  # Any errors that arise from compilation are placed here to show
        # them once the sdfg is rendered

        self.has_multiple_eligible_sdfgs = False

        if self.sdfg is not None:
            self.compiled = True

            # Generate python stub to initialize inputs and load the SDFG
            self.dace_code = """
# THIS IS AN AUTOGENERATED LOADER FOR A SAVED SDFG

import dace
import numpy

{initializers}

sdfg = dace.SDFG.from_file("sdfg.out")
func = sdfg.compile()
func({args})

{prints}    """.format(
                initializers=self.get_arg_initializers(),
                args=", ".join([x + "=" + x for x in self.get_call_args()]),
                prints='\n'.join([
                    'print("{x} =", numpy.array2string({x}))'.format(x=x)
                    for x in self.get_call_args()
                ]))
            self.sdfgs = [('deserialized', self.sdfg)]

        tempdir = tempfile.mkdtemp()
        self.dace_tmpfile = os.path.join(tempdir, self.dace_filename)
        fh = open(self.dace_tmpfile, "w")
        fh.write(self.dace_code)
        fh.close()

        # Create SDFG unless we already have one
        if self.sdfg is None:
            saved_argv = sys.argv
            sys.argv = [self.dace_filename]
            gen_module = {}
            code = compile(self.dace_code, self.dace_tmpfile, 'exec')
            try:
                exec(code, gen_module)
            except Exception as ex:
                self.errors.append(ex)

            # Find dace programs
            self.sdfgs = [(name, parser.parse_from_function(obj))
                          for name, obj in gen_module.items()
                          if isinstance(obj, DaceProgram)]
            self.sdfgs += [(name, obj) for name, obj in gen_module.items()
                           if isinstance(obj, SDFG)]
            # TODO: detecting parents is broken, just take the first one for now
            self.sdfg = self.sdfgs[0][1]
            if len(self.sdfg) > 1:
                self.has_multiple_eligible_sdfgs = True
Example #2
0
    def __init__(self,
                 dace_code,
                 fake_fname,
                 source_code=None,
                 sdfg=None,
                 remote=False):

        self.compiled = False
        self.dace_tmpfile = None
        self.dace_filename = os.path.basename(fake_fname)
        self.sdfg = sdfg  # This is the toplevel one
        self.sdfgs = []  # This is a collection of all the SDFGs
        self.generated_code = []
        self.generated_code_files = None
        self.matching_patterns = []
        self.dace_code = dace_code
        self.source_code = source_code
        self.remote = remote
        self.repetitions = None
        self.errors = [
        ]  # Any errors that arise from compilation are placed here to show
        # them once the sdfg is rendered

        self.has_multiple_eligible_sdfgs = False

        if self.sdfg is not None:
            self.compiled = True
            self.sdfgs = [('deserialized', self.sdfg)]

        tempdir = tempfile.mkdtemp()
        self.dace_tmpfile = os.path.join(tempdir, self.dace_filename)
        fh = open(self.dace_tmpfile, "wb")
        fh.write(self.dace_code.encode('utf-8'))
        fh.close()

        # Create SDFG unless we already have one
        if self.sdfg is None and self.dace_code != "":
            saved_argv = sys.argv
            sys.argv = [self.dace_filename]
            gen_module = {}
            code = compile(self.dace_code, self.dace_tmpfile, 'exec')
            try:
                exec(code, gen_module)
            except Exception as ex:
                self.errors.append(ex)

            # Find dace programs
            self.sdfgs = [(name, parser.parse_from_function(obj))
                          for name, obj in gen_module.items()
                          if isinstance(obj, DaceProgram)]
            self.sdfgs += [(name, obj) for name, obj in gen_module.items()
                           if isinstance(obj, SDFG)]
            try:
                self.sdfg = self.sdfgs[0][1]
            except IndexError:
                if len(self.errors) > 0:
                    raise self.errors[-1]
                if len(self.sdfgs) == 0:
                    raise ValueError('No SDFGs found in file. SDFGs are only '
                                     'recognized when @dace.programs or SDFG '
                                     'objects are found in the global scope')
                raise
            if len(self.sdfg) > 1:
                self.has_multiple_eligible_sdfgs = True
Example #3
0
    def __init__(self,
                 dace_code,
                 fake_fname,
                 source_code=None,
                 sdfg=None,
                 headless=False):

        # TODO: Due to symbols, only one state per process is supported
        dace.symbolic.symbol.erase_all()

        self.compiled = False
        self.dace_tmpfile = None
        self.dace_filename = os.path.basename(fake_fname)
        self.sdfg = sdfg  # This is the toplevel one
        self.sdfgs = []  # This is a collection of all the SDFGs
        self.generated_code = []
        self.generated_code_files = None
        self.matching_patterns = []
        self.headless = headless
        self.dace_code = dace_code
        self.source_code = source_code
        self.repetitions = None
        self.errors = [
        ]  # Any errors that arise from compilation are placed here to show
        # them once the sdfg is rendered

        self.has_multiple_eligible_sdfgs = False

        if self.sdfg is not None:
            self.compiled = True

            # Generate python stub to initialize inputs and load the SDFG
            self.dace_code = """
# THIS IS AN AUTOGENERATED LOADER FOR A SAVED SDFG

import dace
import numpy

{initializers}

sdfg = dace.SDFG.from_file("sdfg.out")
func = sdfg.compile()
func({args})

{prints}    """.format(
                initializers=self.get_arg_initializers(),
                args=", ".join([x + "=" + x for x in self.get_call_args()]),
                prints='\n'.join([
                    'print("{x} =", numpy.array2string({x}))'.format(x=x)
                    for x in self.get_call_args()
                ]))
            self.sdfgs = [('deserialized', self.sdfg)]

        tempdir = tempfile.mkdtemp()
        self.dace_tmpfile = os.path.join(tempdir, self.dace_filename)
        fh = open(self.dace_tmpfile, "wb")
        fh.write(self.dace_code.encode('utf-8'))
        fh.close()

        # Create SDFG unless we already have one
        if self.sdfg is None and self.dace_code != "":
            saved_argv = sys.argv
            sys.argv = [self.dace_filename]
            gen_module = {}
            code = compile(self.dace_code, self.dace_tmpfile, 'exec')
            try:
                exec(code, gen_module)
            except Exception as ex:
                self.errors.append(ex)

            # Find dace programs
            self.sdfgs = [(name, parser.parse_from_function(obj))
                          for name, obj in gen_module.items()
                          if isinstance(obj, DaceProgram)]
            self.sdfgs += [(name, obj) for name, obj in gen_module.items()
                           if isinstance(obj, SDFG)]
            try:
                self.sdfg = self.sdfgs[0][1]
            except IndexError:
                if len(self.sdfgs) == 0:
                    raise ValueError('No SDFGs found in file. SDFGs are only '
                                     'recognized when @dace.programs or SDFG '
                                     'objects are found in the global scope')
                raise
            if len(self.sdfg) > 1:
                self.has_multiple_eligible_sdfgs = True