Esempio n. 1
0
        /// <summary>
        /// Use the low-level macro code generator and optimizer to generate low-level assignments
        /// </summary>
        /// <returns>The optimized low-level assignments output</returns>
        public GMacCodeBlock CreateOptimizedCodeBlock()
        {
            //Create the low-level generator object
            var gen = new LlGenerator(BaseMacro.AssociatedMacro);

            //Define bindings for low-level primitive macro input and output parameters
            foreach (var binding in Bindings)
            {
                if (binding.IsConstant)
                {
                    gen.DefineParameter(
                        binding.ValueAccess.AssociatedValueAccess,
                        binding.ConstantValue.AssociatedScalarValue
                        );
                }

                else
                {
                    gen.DefineParameter(binding.ValueAccess.AssociatedValueAccess);
                }
            }

            //Generate un-optimized low-level macro computations from the base macro and its parameters bindings
            gen.GenerateLowLevelItems();

            //Optimize low-level macro computations
            return(TcbOptimizer.Process(gen, FixOutputComputationsOrder, Progress));
        }
Esempio n. 2
0
        public string TestGenerationByConstantInputs()
        {
            _randomSource = new Random(DateTime.Now.Millisecond);

            var gen1 = new LlGenerator(BaseMacro);

            //Fill input parameters by random values
            foreach (var param in BaseMacro.Parameters)
            {
                if (param.DirectionIn)
                {
                    var valueAccessList = param.ExpandAll();

                    foreach (var valueAccess in valueAccessList)
                    {
                        var assignedValue = GetRandomValue(valueAccess.ExpressionType);

                        gen1.DataTable.DefineConstantInputParameter(valueAccess, assignedValue);
                    }
                }
                else
                {
                    gen1.DataTable.DefineOutputParameter(param);
                }
            }

            //Evaluate (generate) the outputs using the constant inputs
            gen1.GenerateLowLevelItems();

            Log.AppendLine();
            Log.AppendLineAtNewLine("First Generator Result");
            Log.AppendAtNewLine(gen1.ToString());

            Log.AppendLine();
            Log.AppendAtNewLine("Begin full low level symbolic generation ... ");
            _timer.Reset();
            _timer.Start();

            //Use another generator with all input parameters set as variable
            var gen2 = new LlGenerator(BaseMacro);

            gen2.DefineAllParameters();

            //Generate the symbolic expressions without optimizations
            gen2.GenerateLowLevelItems();

            //Optimize the generated symbolic expressions
            var genOpt = TcbOptimizer.Process(gen2);

            //genOpt.OptimizeLowLevelItems();

            _timer.Stop();
            Log.AppendLine(" Finished after " + _timer.Elapsed);
            Log.AppendLine();

            Log.AppendLine();
            Log.AppendLineAtNewLine("Second Generator Result");
            Log.AppendAtNewLine(genOpt.ToString());

            //Evaluate all symbolic expressions in order using the constant values for inputs from the first generator
            var valuesDict =
                new Dictionary <string, ValuePrimitive <MathematicaScalar> >();

            foreach (var inputItem in genOpt.InputVariables)
            {
                var inputItemName  = inputItem.LowLevelName;
                var inputItemValue = gen1.DataTable.GetItemByName(inputItem.LowLevelName).AssignedRhsValue;

                valuesDict.Add(inputItemName, inputItemValue);

                Log.AppendAtNewLine("Input ");
                Log.Append(inputItem.ValueAccessName);
                Log.Append(" = ");
                Log.AppendLine(inputItemValue.ToString());
            }

            foreach (var item in genOpt.ComputedVariables)
            {
                ExecuteAssignment(item, valuesDict);
            }

            //Report the differences between the output parameters of the two methods of computation
            foreach (var outputItem1 in gen1.DataTable.Outputs)
            {
                var outputItem1Value = outputItem1.AssignedRhsValue;
                var outputItem2Value = valuesDict[outputItem1.ItemName];

                Log.AppendAtNewLine("Input ");
                Log.Append(outputItem1.HlItemName);
                Log.AppendLine(" = ");
                Log.IncreaseIndentation();
                Log.AppendLineAtNewLine(outputItem1Value.ToString());
                Log.AppendLineAtNewLine(outputItem2Value.ToString());
                Log.DecreaseIndentation();
            }

            return(Log.ToString());
        }