/* Determine whether an instruction has an internal storage. This
         * is used to determine if temporary storage has to be allocated in
         * various places. If an instruction already has storage by itself
         * (e.g. in the statetable) then temporary storage does not need to
         * be allocated.
         *
         * If the language supports first class arrays, then temporary storage
         * never needs to be allocated to store intermediate results. For other
         * languages however, temporary storage needs to be allocated for
         * anything that is multidimensional and does not have a representation
         * in the statetable. A special case is a InstructionVariable which
         * can optionally carry a slice. If there is such a slice and it is
         * not continuous, then the instruction does not have internal storage.
         */
        private bool InstructionHasStorage(Cdn.Instruction instruction, Context context)
        {
            if (context.SupportsFirstClassArrays)
            {
                return(true);
            }

            var v = instruction as Cdn.InstructionVariable;

            if (v != null)
            {
                // Check for slice
                if (!v.HasSlice)
                {
                    return(true);
                }

                // Only needs storage if indices are not continuous
                Cdn.Dimension dim;
                return(!Context.IndicesAreContinuous(v.GetSlice(out dim)));
            }

            var i = instruction as Cdn.InstructionIndex;

            if (i != null && i.IndexType == Cdn.InstructionIndexType.Offset)
            {
                return(true);
            }

            return(context.Program.StateTable.Contains(instruction));
        }
Beispiel #2
0
        public bool Equal(Cdn.Instruction i2, bool asstring)
        {
            bool raw_ret = cdn_instruction_equal(Handle, i2 == null ? IntPtr.Zero : i2.Handle, asstring);
            bool ret     = raw_ret;

            return(ret);
        }
Beispiel #3
0
        private void Resolve(Node node, Cdn.Instruction instruction, HashSet <Cdn.Expression> seen, Dictionary <object, State> mapping)
        {
            InstructionVariable       variable;
            InstructionRand           rand;
            InstructionCustomOperator cusop;
            InstructionCustomFunction cusfn;

            if (As(instruction, out variable))
            {
                Resolve(node, variable.Variable, seen, mapping);
            }
            else if (As(instruction, out rand))
            {
                AddDependency(node, rand, null);
            }
            else if (As(instruction, out cusfn))
            {
                Resolve(node, cusfn.Function.Expression, seen, mapping);
            }
            else if (As(instruction, out cusop))
            {
                var fn = cusop.Operator.PrimaryFunction;

                if (fn != null)
                {
                    Resolve(node, fn.Expression, seen, mapping);
                }

                AddDependency(node, cusop, null);
            }
        }
Beispiel #4
0
        public void Generate()
        {
            Profile.Do("load network", () => {
                LoadNetwork();
            });

            if (Options.Instance.Validate)
            {
                d_validator = new Validator(d_network);
            }

            if (Options.Instance.DelayTimeStep > 0)
            {
                Cdn.Expression    expr  = new Cdn.Expression(Options.Instance.DelayTimeStep.ToString("R"));
                Cdn.Instruction[] instr = new Cdn.Instruction[1];
                instr[0]          = new Cdn.InstructionNumber(Options.Instance.DelayTimeStep);
                expr.Instructions = instr;

                d_network.Integrator.AddVariable(new Cdn.Variable("delay_dt", expr, Cdn.VariableFlags.Out));
            }

            Profile.Do("initialize knowledge", () => {
                // Initialize the knowledge
                Knowledge.Initialize(d_network);
            });

            if (!Options.Instance.NoSparsity)
            {
                Profile.Do("sparsity", () => {
                    var sparsity = new Sparsity();
                    sparsity.Optimize();
                });
            }

            var t = Profile.Begin("collect");

            // Collect all the equations
            Tree.Collectors.Result collection = Collect();

            t.End();

            t = Profile.Begin("filter");

            // Filter conflicts and resolve final embeddings
            Tree.Embedding[] embeddings = Filter(collection);

            t.End();

            t = Profile.Begin("resolve equations");

            // Resolve final equations
            Dictionary <State, Tree.Node> equations = ResolveEquations(embeddings);

            t.End();

            // Create program
            t = Profile.Begin("create program");

            Programmer.Program program = new Programmer.Program(ProgrammerOptions(), embeddings, equations);

            t.End();

            bool outistemp = false;

            // Write program
            if (Options.Instance.Validate || Options.Instance.Compile)
            {
                // Create a new temporary directory for the output files
                string path = Path.GetTempFileName();
                File.Delete(path);

                Directory.CreateDirectory(path);
                program.Options.Output = path;

                outistemp = true;
            }
            else
            {
                Directory.CreateDirectory(program.Options.Output);
            }

            t = Profile.Begin("write program");

            d_writtenFiles = Options.Instance.Formatter.Write(program);

            t.End();

            if (Options.Instance.PrintCompileSource)
            {
                foreach (string filename in d_writtenFiles)
                {
                    Console.WriteLine("File: {0}", filename);
                    Console.WriteLine(File.ReadAllText(filename));
                }
            }

            if (Options.Instance.Validate && !Options.Instance.PrintCompileSource)
            {
                try
                {
                    d_validator.Validate(program, d_writtenFiles);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine(e.Message);

                    Directory.Delete(program.Options.Output, true);
                    Environment.Exit(1);
                }
            }
            else if (Options.Instance.Compile)
            {
                var files = Options.Instance.Formatter.Compile(Options.Instance.Verbose);

                if (Options.Instance.Verbose)
                {
                    Log.WriteLine("Compiled {0}...", String.Join(", ", Array.ConvertAll <string, string>(files, a => Path.GetFileName(a))));
                }

                if (!String.IsNullOrEmpty(Options.Instance.Output))
                {
                    try
                    {
                        Directory.CreateDirectory(Options.Instance.Output);
                    }
                    catch
                    {
                    }
                }

                foreach (var f in files)
                {
                    var dest = Path.GetFileName(f);

                    if (!String.IsNullOrEmpty(Options.Instance.Output))
                    {
                        dest = Path.Combine(Options.Instance.Output, dest);
                    }

                    try
                    {
                        File.Delete(dest);
                    } catch {}

                    File.Move(f, dest);
                }
            }

            if (outistemp)
            {
                try
                {
                    Directory.Delete(program.Options.Output, true);
                } catch {};
            }
        }
Beispiel #5
0
        public static ExpressionTreeIter NewFromInstructionTake(Cdn.Instruction instruction)
        {
            ExpressionTreeIter result = new ExpressionTreeIter(cdn_expression_tree_iter_new_from_instruction_take(instruction == null ? IntPtr.Zero : instruction.Handle));

            return(result);
        }
Beispiel #6
0
 public ExpressionTreeIter(Cdn.Instruction instruction)
 {
     Raw = cdn_expression_tree_iter_new_from_instruction(instruction == null ? IntPtr.Zero : instruction.Handle);
 }
Beispiel #7
0
 public static bool IsPlaceholder(Cdn.Instruction instruction)
 {
     return(InstructionCode(instruction)[0] == PlaceholderCode);
 }