Esempio n. 1
0
        private void Constructed()
        {
            InstructionCustomFunction icfunc = d_instruction as InstructionCustomFunction;
            InstructionCustomOperator icop   = d_instruction as InstructionCustomOperator;

            Cdn.Function f = null;

            if (icfunc != null)
            {
                f = icfunc.Function;
            }
            else if (icop != null)
            {
                f = icop.Operator.PrimaryFunction;
            }

            if (f == null)
            {
                return;
            }

            var args   = f.Arguments;
            int offset = 0;

            for (int i = 0; i < args.Length; ++i)
            {
                if (args[i].Unused)
                {
                    d_children.RemoveAt(i - offset);
                    ++offset;
                }
            }
        }
Esempio n. 2
0
        public DelayedState(InstructionCustomOperator delayed, double delay, Cdn.Expression expr, Flags type) : base(delayed, expr, type)
        {
            d_delayed = delayed;
            d_delay   = delay;

            d_size = new Size((uint)System.Math.Round(d_delay / Options.Instance.DelayTimeStep));
        }
Esempio n. 3
0
        private void ExtractDelayedState(State st, HashSet <DelayedState.Key> same)
        {
            if (st.Instructions == null)
            {
                return;
            }

            foreach (Cdn.Instruction instruction in st.Instructions)
            {
                InstructionCustomOperator op = instruction as InstructionCustomOperator;

                if (op == null || !(op.Operator is OperatorDelayed))
                {
                    continue;
                }

                if (Options.Instance.DelayTimeStep <= 0)
                {
                    throw new Exception("The network uses the `delayed' operator but no delay time step was specified (--delay-time-step)...");
                }

                double delay = ComputeDelayedDelay(st.Instructions, st.Expression, op);

                OperatorDelayed  opdel = (OperatorDelayed)op.Operator;
                DelayedState.Key key   = new DelayedState.Key(opdel, delay);

                if (!same.Add(key))
                {
                    d_delays.Add(op, delay);
                    continue;
                }

                double size;

                size = delay / Options.Instance.DelayTimeStep;

                if (size % 1 > double.Epsilon)
                {
                    throw new Exception(String.Format("Time delay `{0}' is not a multiple of the delay time step `{1}'",
                                                      delay, Options.Instance.DelayTimeStep));
                }

                d_delays.Add(op, delay);

                DelayedState s = new DelayedState(op, delay, opdel.Expression, Cdn.RawC.State.Flags.None);
                AddState(null, s);

                // Create a new expression for the initial value of this state
                AddInitialize(new DelayedState(op, delay, opdel.InitialValue, Cdn.RawC.State.Flags.Initialization));
                d_delayedStates.Add(s);

                // Recurse into state
                ExtractDelayedState(s, same);
            }
        }
Esempio n. 4
0
        private int[] InstructionSparsity(InstructionCustomOperator instr, SparsityInfo[] children, Dictionary <Variable, SparsityInfo> mapping)
        {
            var f = instr.Operator.PrimaryFunction;

            if (f != null)
            {
                return(FunctionSparsity(f, children, mapping));
            }

            return(new int[0]);
        }
Esempio n. 5
0
        /*
         * Support for custom operators which do not generate functions.
         * Currently, the only operator supported here is the delay operator.
         * All other operators which currently exist generate functions which
         * are handled just like other custom functions.
         */
        protected virtual string Translate(InstructionCustomOperator instruction, Context context)
        {
            var op = instruction.Operator;

            if (op is OperatorDelayed)
            {
                return(TranslateDelayed(instruction, context));
            }

            throw new NotSupportedException(String.Format("The custom operator `{0}' is not yet supported.",
                                                          op.ToString()));
        }
Esempio n. 6
0
        /*
         * Delay operator instructions are translated to a simple lookup in
         * the statetable where they delayed expressions are stored.
         */
        protected virtual string TranslateDelayed(InstructionCustomOperator instruction, Context context)
        {
            OperatorDelayed delayed = (OperatorDelayed)instruction.Operator;
            double          delay;

            if (!Knowledge.Instance.LookupDelay(instruction, out delay))
            {
                throw new NotSupportedException("Unable to determine delay of delayed operator");
            }

            DataTable.DataItem item = context.Program.StateTable[new DelayedState.Key(delayed, delay)];

            return(String.Format("{0}[{1}]",
                                 context.This(context.Program.StateTable),
                                 item.AliasOrIndex));
        }
Esempio n. 7
0
        private double ComputeDelayedDelay(Instruction[] instructions, Expression e, InstructionCustomOperator instr)
        {
            Cdn.Stack stack = new Cdn.Stack(e.StackSize);

            foreach (Cdn.Instruction i in instructions)
            {
                if (i == instr)
                {
                    return(stack.Pop());
                }

                i.Execute(stack);
            }

            return(0);
        }
Esempio n. 8
0
 public DelayedState(InstructionCustomOperator delayed, double delay, Flags type) : this(delayed, delay, null, type)
 {
 }
Esempio n. 9
0
 public DelayedState(InstructionCustomOperator delayed, double delay) : this(delayed, delay, Flags.None)
 {
 }