<commit_before>from __future__ import absolute_import, division, print_function

# More Python 3 vs 2 mojo
try:
    import queue
except ImportError:
    import Queue as queue

import pygraphviz as pgv
import targetc as genc

from grouping import *

from constructs import *
from expression import *
from codegen import *
from schedule import *
from poly import *
from bounds import *
from inline import *

# LOG CONFIG #
pipe_logger = logging.getLogger("pipe.py")
pipe_logger.setLevel(logging.INFO)
LOG = pipe_logger.log

def get_parents_from_comp_obj(comp):
    refs = comp.getObjects(Reference)
    # Filter out self and image references 
    refs = [ ref for ref in refs if not ref.objectRef == comp and \
                                 not isinstance(ref.objectRef, Image) ]
    return list(set([ref.objectRef for ref in refs]))

def get_comp_objs_and_dep_maps(outputs):
    """
    Find all the compute objects required for the outputs and
    also builds parent and children maps for the compute objects
    """
    comp_objs = []
    comp_objs_parents = {}
    comp_objs_children = {}
    q = queue.Queue()
    for comp_obj in outputs:
        q.put(comp_obj)
    while not q.empty():
        obj = q.get()
        parent_objs = get_parents_from_comp_obj(obj)
        if obj not in comp_objs:
            comp_objs.append(obj)
            comp_objs_parents[obj] = parent_objs
            for parobj in parent_objs:
                if parobj in comp_objs_children:
                    if obj not in comp_objs_children[parobj]:
                        comp_objs_children[parobj].append(obj)
                else:
                    comp_objs_children[parobj] = [obj]
            if len(parent_objs) != 0:
                for r in parent_objs:
                    q.put(r)

    for comp in comp_objs:
        if comp not in comp_objs_parents:
            comp_objs_parents[comp] = []
        if comp not in comp_objs_children:
            comp_objs_children[comp] = []

    return comp_objs, comp_objs_parents, comp_objs_children

class Group:
    """ 
        Group is a part of the pipeline which realizes a set of computation
        objects. Scheduling and storage allocation is done at the level of a 
        group. A group also maintains a polyhedral representation of the 
        computation objects when possible.
    """
    # Construct a group from a set of language functions / reductions
    def __init__(self, _ctx, _comp_objs, _param_constraints):
        # All the computation constructs in the language derive from the
        # Function class. Input images cannot be part of a group.
        for comp in _comp_objs:
            assert(isinstance(comp, Function))
            #assert(not isinstance(comp, Image))

        self._comp_objs  = _comp_objs
        self._polyrep = None
        refs = []

        for comp in self._comp_objs:
            refs += comp.getObjects(Reference)

        self._inputs = list(set([ref.objectRef for ref in refs \
                            if isinstance(ref.objectRef, Image)]))

        # Create a polyhedral representation if possible.
        # Currently doing extraction only when all the compute_objs
        # domains are affine. This can be revisited later.
        if self.isPolyhedral():
            self._polyrep = PolyRep(_ctx, self, [], _param_constraints)

    @property
    def compute_objs(self):
        return self._comp_objs
    @property
    def polyRep(self):
        return self._polyrep
    @property
    def inputs(self):
        return self._inputs
    @property
    def name(self):
        return [comp.name for comp in self.compute_objs]

    def is_fused(self):
        return len(self.compute_objs) > 1

    def getParameters(self):
        params = []
        for comp in self._comp_objs:
            params = params + comp.getObjects(Parameter)
        return list(set(params))

    def isPolyhedral(self):
        polyhedral = True
        for comp in self._comp_objs:
            if (not comp.hasBoundedIntegerDomain()):
                polyhedral = False
        return polyhedral

    def order_compute_objs(self):
        # Order stores the numbering of each compute object 
        # when topologically sorted.
        order = {}
        # Initialize all the initial numbering to zero for
        # all compute objects in the group
        for comp in self._comp_objs:
            order[comp] = 0
        # Doing a topological sort in an iterative fashion
        change = True
        while(change):
            change = False
            for comp in self._comp_objs:
                parentObjs = get_parents_from_comp_obj(comp)
                for pobj in parentObjs:
                    if (pobj in order  and (order[pobj] >= order[comp])):
                        order[comp] = order[pobj] + 1
                        change = True
        return order

    def __str__(self):
        comp_str  = '[' + \
                    ', '.join([comp.name \
                        for comp in self._comp_objs]) + \
                    ']'
        return comp_str

class Pipeline:
    def __init__(self, _ctx, _outputs,
                 _param_estimates, _param_constraints,
                 _grouping, _group_size, _inline_directives,
                 _tile_sizes, _size_threshold,
                 _options, _name = None):
        # Name of the pipleline is a concatenation of the names of the 
        # pipeline outputs, unless it is explicitly named.
        if _name is None:
            _name = ''.join([out.name for out in _outputs])

        self._name = _name<commit_msg>moving 'Group' member func order_compute_objs outside<commit_after>from __future__ import absolute_import, division, print_function

# More Python 3 vs 2 mojo
try:
    import queue
except ImportError:
    import Queue as queue

import pygraphviz as pgv
import targetc as genc

from grouping import *

from constructs import *
from expression import *
from codegen import *
from schedule import *
from poly import *
from bounds import *
from inline import *

# LOG CONFIG #
pipe_logger = logging.getLogger("pipe.py")
pipe_logger.setLevel(logging.INFO)
LOG = pipe_logger.log

def get_parents_from_comp_obj(comp):
    refs = comp.getObjects(Reference)
    # Filter out self and image references 
    refs = [ ref for ref in refs if not ref.objectRef == comp and \
                                 not isinstance(ref.objectRef, Image) ]
    return list(set([ref.objectRef for ref in refs]))

def get_comp_objs_and_dep_maps(outputs):
    """
    Find all the compute objects required for the outputs and
    also builds parent and children maps for the compute objects
    """
    comp_objs = []
    comp_objs_parents = {}
    comp_objs_children = {}
    q = queue.Queue()
    for comp_obj in outputs:
        q.put(comp_obj)
    while not q.empty():
        obj = q.get()
        parent_objs = get_parents_from_comp_obj(obj)
        if obj not in comp_objs:
            comp_objs.append(obj)
            comp_objs_parents[obj] = parent_objs
            for parobj in parent_objs:
                if parobj in comp_objs_children:
                    if obj not in comp_objs_children[parobj]:
                        comp_objs_children[parobj].append(obj)
                else:
                    comp_objs_children[parobj] = [obj]
            if len(parent_objs) != 0:
                for r in parent_objs:
                    q.put(r)

    for comp in comp_objs:
        if comp not in comp_objs_parents:
            comp_objs_parents[comp] = []
        if comp not in comp_objs_children:
            comp_objs_children[comp] = []

    return comp_objs, comp_objs_parents, comp_objs_children

def order_compute_objs(comp_objs):
    # Order stores the numbering of each compute object when topologically
    # sorted.
    order = {}
    # Initialize all the initial numbering to zero for all compute objects
    for comp in comp_objs:
        order[comp] = 0
    # Doing a topological sort in an iterative fashion
    change = True
    while(change):
        change = False
        for comp in comp_objs:
            parent_objs = get_parents_from_comp_obj(comp)
            for p_obj in parent_objs:
                if (p_obj in order and (order[p_obj] >= order[comp])):
                    order[comp] = order[pobj] + 1
                    change = True
    return order

class Group:
    """ 
        Group is a part of the pipeline which realizes a set of computation
        objects. Scheduling and storage allocation is done at the level of a 
        group. A group also maintains a polyhedral representation of the 
        computation objects when possible.
    """
    # Construct a group from a set of language functions / reductions
    def __init__(self, _ctx, _comp_objs, _param_constraints):
        # All the computation constructs in the language derive from the
        # Function class. Input images cannot be part of a group.
        for comp in _comp_objs:
            assert(isinstance(comp, Function))
            #assert(not isinstance(comp, Image))

        self._comp_objs  = _comp_objs
        self._polyrep = None
        refs = []

        for comp in self._comp_objs:
            refs += comp.getObjects(Reference)

        self._inputs = list(set([ref.objectRef for ref in refs \
                            if isinstance(ref.objectRef, Image)]))

        # Create a polyhedral representation if possible.
        # Currently doing extraction only when all the compute_objs
        # domains are affine. This can be revisited later.
        if self.isPolyhedral():
            self._polyrep = PolyRep(_ctx, self, [], _param_constraints)

    @property
    def compute_objs(self):
        return self._comp_objs
    @property
    def polyRep(self):
        return self._polyrep
    @property
    def inputs(self):
        return self._inputs
    @property
    def name(self):
        return [comp.name for comp in self.compute_objs]

    def is_fused(self):
        return len(self.compute_objs) > 1

    def getParameters(self):
        params = []
        for comp in self._comp_objs:
            params = params + comp.getObjects(Parameter)
        return list(set(params))

    def isPolyhedral(self):
        polyhedral = True
        for comp in self._comp_objs:
            if (not comp.hasBoundedIntegerDomain()):
                polyhedral = False
        return polyhedral

    def order_compute_objs(self):
        comp_objs = self._comp_objs
        order = order_compute_objs(comp_objs)
        return order

    def __str__(self):
        comp_str  = '[' + \
                    ', '.join([comp.name \
                        for comp in self._comp_objs]) + \
                    ']'
        return comp_str

class Pipeline:
    def __init__(self, _ctx, _outputs,
                 _param_estimates, _param_constraints,
                 _grouping, _group_size, _inline_directives,
                 _tile_sizes, _size_threshold,
                 _options, _name = None):
        # Name of the pipleline is a concatenation of the names of the 
        # pipeline outputs, unless it is explicitly named.
        if _name is None:
            _name = ''.join([out.name for out in _outputs])

        self._name = _name
{'fraction_non_alphanumeric': 0.06920815890264541, 'fraction_numerical': 0.0010688518749443305, 'mean_word_length': 2.071115973741794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7271254', 'n_tokens_mistral': 3197, 'n_tokens_neox': 3078, 'n_words': 1009}