Exemple #1
0
        /// <summary>
        /// <inheritDoc/>
        ///
        /// </summary>
        /// <exception cref="System.Exception"/>
        public int Run(string[] args)
        {
            //parse arguments
            if (args.Length != DistSum.Parameters.Count + 2)
            {
                return(Org.Apache.Hadoop.Examples.PI.Util.PrintUsage(args, GetType().FullName + " <name> <sigma> "
                                                                     + DistSum.Parameters.List + "\n  <name> The name." + "\n  <sigma> The summation."
                                                                     + DistSum.Parameters.Description));
            }
            int       i     = 0;
            string    name  = args[i++];
            Summation sigma = Summation.ValueOf(args[i++]);

            SetParameters(DistSum.Parameters.Parse(args, i));
            [email protected]();
            [email protected]("name  = " + name);
            [email protected]("sigma = " + sigma);
            [email protected](parameters);
            [email protected]();
            //run jobs
            Summation result = Execute(name, sigma);

            if (result.Equals(sigma))
            {
                sigma.SetValue(result.GetValue());
                timer.Tick("\n\nDONE\n\nsigma=" + sigma);
                return(0);
            }
            else
            {
                timer.Tick("\n\nDONE WITH ERROR\n\nresult=" + result);
                return(1);
            }
        }
Exemple #2
0
 internal Computation(DistSum _enclosing, int index, string name, Summation sigma)
 {
     this._enclosing = _enclosing;
     this.index      = index;
     this.name       = name;
     this.sigma      = sigma;
 }
        public void BindDualSelfProperty()
        {
            var summation = new Summation();

            AssertChange(summation, x => x.Sum, () => summation.A = 1);
            AssertChange(summation, x => x.Sum, () => summation.B = 1);
        }
Exemple #4
0
        /// <summary>Start a job to compute sigma</summary>
        /// <exception cref="System.IO.IOException"/>
        private void Compute(string name, Summation sigma)
        {
            if (sigma.GetValue() != null)
            {
                throw new IOException("sigma.getValue() != null, sigma=" + sigma);
            }
            //setup remote directory
            FileSystem fs  = FileSystem.Get(GetConf());
            Path       dir = fs.MakeQualified(new Path(parameters.remoteDir, name));

            if (!Org.Apache.Hadoop.Examples.PI.Util.CreateNonexistingDirectory(fs, dir))
            {
                return;
            }
            //setup a job
            Job  job    = CreateJob(name, sigma);
            Path outdir = new Path(dir, "out");

            FileOutputFormat.SetOutputPath(job, outdir);
            //start a map/reduce job
            string startmessage = "steps/parts = " + sigma.E.GetSteps() + "/" + parameters.nParts
                                  + " = " + Org.Apache.Hadoop.Examples.PI.Util.Long2string(sigma.E.GetSteps() / parameters
                                                                                           .nParts);

            Org.Apache.Hadoop.Examples.PI.Util.RunJob(name, job, parameters.machine, startmessage
                                                      , timer);
            IList <TaskResult> results = Org.Apache.Hadoop.Examples.PI.Util.ReadJobOutputs(fs,
                                                                                           outdir);

            Org.Apache.Hadoop.Examples.PI.Util.WriteResults(name, results, fs, parameters.remoteDir
                                                            );
            fs.Delete(dir, true);
            //combine results
            IList <TaskResult> combined = Org.Apache.Hadoop.Examples.PI.Util.Combine(results);
            PrintWriter        @out     = Org.Apache.Hadoop.Examples.PI.Util.CreateWriter(parameters.localDir
                                                                                          , name);

            try
            {
                foreach (TaskResult r in combined)
                {
                    string s = TaskResult2string(name, r);
                    @out.WriteLine(s);
                    @out.Flush();
                    [email protected](s);
                }
            }
            finally
            {
                @out.Close();
            }
            if (combined.Count == 1)
            {
                Summation s = combined[0].GetElement();
                if (sigma.Contains(s) && s.Contains(sigma))
                {
                    sigma.SetValue(s.GetValue());
                }
            }
        }
        public void BaseFunctionTest()
        {
            Summation sum      = new Summation();
            double    expected = 5;
            double    actual   = sum.Execute(2, 3);

            Assert.AreEqual(expected, actual);
        }
        public void PropertiesTest()
        {
            Summation    sum = new Summation();
            Priority     expectedPriority     = Priority.LOW;
            NumberOfArgs expectedNumberOfArgs = NumberOfArgs.TWO;

            Assert.AreEqual(expectedPriority, sum.GetPriority());
            Assert.AreEqual(expectedNumberOfArgs, sum.GetNumberOfArgs());
        }
Exemple #7
0
                /// <returns>a list containing a single split of summation</returns>
                public override IList <InputSplit> GetSplits(JobContext context)
                {
                    //read sigma from conf
                    Configuration conf  = context.GetConfiguration();
                    Summation     sigma = SummationWritable.Read(typeof(DistSum), conf);
                    //create splits
                    IList <InputSplit> splits = new AList <InputSplit>(1);

                    splits.AddItem(new DistSum.Machine.SummationSplit(sigma));
                    return(splits);
                }
        /// <summary>
        /// <inheritDoc/>
        ///
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        public void ReadFields(DataInput @in)
        {
            ArithmeticProgression N = SummationWritable.ArithmeticProgressionWritable.Read(@in
                                                                                           );
            ArithmeticProgression E = SummationWritable.ArithmeticProgressionWritable.Read(@in
                                                                                           );

            sigma = new Summation(N, E);
            if (@in.ReadBoolean())
            {
                sigma.SetValue(@in.ReadDouble());
            }
        }
        static void Main(string[] args)
        {
            /// Expression: 1 + 5 * (4 - 3) * (2 + 1 / 2)
            /// Composite 1: 1 + 5 * (4 - 3) * (2 + 1 / 2)
            ///     Number 1: 1
            ///     Operator 1: +
            ///     Composite 2: 5 * (4 - 3) * (2 + 1 / 2)
            ///         Number 2: 5
            ///         Operator 2: *
            ///         Composite 3: (4 - 3) * (2 + 1 / 2)
            ///             Composite 4: 4 - 3
            ///                 Number 3: 4
            ///                 Operator 3: -
            ///                 Number 4: 3
            ///             Operator 4: *
            ///             Composite 5: 2 + 1 / 2
            ///                 Number 5: 2
            ///                 Operator 5: +
            ///                 Composite 6: 1 / 2
            ///                     Number 6: 1
            ///                     Operator 6: /
            ///                     Number 7: 2

            NumbericOperand  number7    = new NumbericOperand(2);
            Operator         operator6  = new Division();
            NumbericOperand  number6    = new NumbericOperand(1);
            CompositeOperand composite6 = new CompositeOperand(number6, number7, operator6);

            NumbericOperand  number5    = new NumbericOperand(2);
            Operator         operator5  = new Summation();
            CompositeOperand composite5 = new CompositeOperand(number5, composite6, operator5);

            NumbericOperand  number3    = new NumbericOperand(4);
            Operator         operator3  = new Subtraction();
            NumbericOperand  number4    = new NumbericOperand(3);
            CompositeOperand composite4 = new CompositeOperand(number3, number4, operator3);

            Operator         operator4  = new Multiplication();
            CompositeOperand composite3 = new CompositeOperand(composite4, composite5, operator4);

            NumbericOperand  number2    = new NumbericOperand(5);
            Operator         operator2  = new Multiplication();
            CompositeOperand composite2 = new CompositeOperand(number2, composite3, operator2);

            NumbericOperand  number1    = new NumbericOperand(1);
            Operator         operator1  = new Summation();
            CompositeOperand composite1 = new CompositeOperand(number1, composite2, operator1);

            Console.WriteLine("1 + 5 * (4 - 3) * (2 + 1 / 2) = " + composite1.Calculate());
        }
Exemple #10
0
        /// <summary>Create a job</summary>
        /// <exception cref="System.IO.IOException"/>
        private Job CreateJob(string name, Summation sigma)
        {
            Job           job     = Job.GetInstance(GetConf(), parameters.remoteDir + "/" + name);
            Configuration jobconf = job.GetConfiguration();

            job.SetJarByClass(typeof(DistSum));
            jobconf.SetInt(NParts, parameters.nParts);
            SummationWritable.Write(sigma, typeof(DistSum), jobconf);
            // disable task timeout
            jobconf.SetLong(MRJobConfig.TaskTimeout, 0);
            // do not use speculative execution
            jobconf.SetBoolean(MRJobConfig.MapSpeculative, false);
            jobconf.SetBoolean(MRJobConfig.ReduceSpeculative, false);
            return(job);
        }
        /// <summary>Write sigma to DataOutput</summary>
        /// <exception cref="System.IO.IOException"/>
        public static void Write(Summation sigma, DataOutput @out)
        {
            SummationWritable.ArithmeticProgressionWritable.Write(sigma.N, @out);
            SummationWritable.ArithmeticProgressionWritable.Write(sigma.E, @out);
            double v = sigma.GetValue();

            if (v == null)
            {
                @out.WriteBoolean(false);
            }
            else
            {
                @out.WriteBoolean(true);
                @out.WriteDouble(v);
            }
        }
Exemple #12
0
                /// <summary>Partitions the summation into parts and then return them as splits</summary>
                public override IList <InputSplit> GetSplits(JobContext context)
                {
                    //read sigma from conf
                    Configuration conf   = context.GetConfiguration();
                    Summation     sigma  = SummationWritable.Read(typeof(DistSum), conf);
                    int           nParts = conf.GetInt(NParts, 0);
                    //create splits
                    IList <InputSplit> splits = new AList <InputSplit>(nParts);

                    Summation[] parts = sigma.Partition(nParts);
                    for (int i = 0; i < parts.Length; ++i)
                    {
                        splits.AddItem(new DistSum.Machine.SummationSplit(parts[i]));
                    }
                    //LOG.info("parts[" + i + "] = " + parts[i]);
                    return(splits);
                }
Exemple #13
0
            /// <summary>Compute sigma</summary>
            /// <exception cref="System.IO.IOException"/>
            /// <exception cref="System.Exception"/>
            internal static void Compute <_T0>(Summation sigma, TaskInputOutputContext <_T0> context
                                               )
            {
                string s;

                Log.Info(s = "sigma=" + sigma);
                context.SetStatus(s);
                long start = Runtime.CurrentTimeMillis();

                sigma.Compute();
                long       duration = Runtime.CurrentTimeMillis() - start;
                TaskResult result   = new TaskResult(sigma, duration);

                Log.Info(s = "result=" + result);
                context.SetStatus(s);
                context.Write(NullWritable.Get(), result);
            }
Exemple #14
0
    /*
     * public void UpdateCellPositions()
     * {
     * for (int i = 0; i != mCells.Count; ++i)
     * {
     *  SmCell cell = mCells[i];
     *  cell.UpdateVertexPositions();
     * }
     * }
     */

    // Fast summation
    public void SumParticlesToRegions()
    {
        for (int i = 0; i != mSummations[0].Count; ++i)
        {
            Summation bar = mSummations[0][i];
            bar.SumFromChildren();
        }

        for (int i = 0; i != mSummations[1].Count; ++i)
        {
            Summation plate = mSummations[1][i];
            plate.SumFromChildren();
        }

        for (int i = 0; i != mRegions.Count; ++i)
        {
            SmRegion region = mRegions[i];
            region.SumFromChildren();
        }
    }
Exemple #15
0
    public void SumRegionsToParticles()
    {
        for (int i = 0; i != mSummations[1].Count; ++i)
        {
            Summation plate = mSummations[1][i];
            plate.SumFromParents();
        }

        for (int i = 0; i != mSummations[0].Count; ++i)
        {
            Summation bar = mSummations[0][i];
            bar.SumFromParents();
        }

        for (int i = 0; i != mParticles.Count; ++i)
        {
            SmParticle particle = mParticles[i];
            particle.SumFromParents();
        }
    }
Exemple #16
0
        //Выражения с низким приоритетом (сложение, вычитание)
        private Operation parseLow()
        {
            Operation result = parseMedium();

            while (true)
            {
                if (Match('+'))
                {
                    result = new Summation(result, parseMedium());
                }
                else
                if (Match('-'))
                {
                    result = new Subtraction(result, parseMedium());
                }
                else
                {
                    return(result);
                }
            }
        }
Exemple #17
0
        /// <summary>Partition sigma and execute the computations.</summary>
        private Summation Execute(string name, Summation sigma)
        {
            Summation[] summations = sigma.Partition(parameters.nJobs);
            IList <DistSum.Computation> computations = new AList <DistSum.Computation>();

            for (int i = 0; i < summations.Length; i++)
            {
                computations.AddItem(new DistSum.Computation(this, i, name, summations[i]));
            }
            try
            {
                Org.Apache.Hadoop.Examples.PI.Util.Execute(parameters.nThreads, computations);
            }
            catch (Exception e)
            {
                throw new RuntimeException(e);
            }
            IList <Summation> combined = Org.Apache.Hadoop.Examples.PI.Util.Combine(Arrays.AsList
                                                                                        (summations));

            return(combined.Count == 1 ? combined[0] : null);
        }
Exemple #18
0
            public (Token, string) Parse(string input)
            {
                var prefix = new Summation().Parse(input);
                var suffix = new Maybe(
                    new Create(TokenType.Node)
                {
                    new SkipWhitespace(),
                    new SelectFrom {
                        new Char('*'), new Char('/'), new Char('%')
                    }, new SkipWhitespace(), new Summation()
                }
                    ).Parse(prefix.Item2);

                var token = prefix.Item1;

                if (suffix.Item1.type != TokenType.Failure)
                {
                    token     += suffix.Item1;
                    token.type = TokenType.Product;
                }

                return(token, suffix.Item2);
            }
 internal SummationWritable(Summation sigma)
 {
     this.sigma = sigma;
 }
 /// <summary>Write sigma to conf</summary>
 public static void Write(Summation sigma, Type clazz, Configuration conf)
 {
     conf.Set(clazz.Name + ".sigma", sigma.ToString());
 }
Exemple #21
0
    public List <Summation> GenerateChildSums(int childLevel)
    {
        int splitDimension = childLevel;

        FindParticleRange(splitDimension, ref mMinDim, ref mMaxDim);

        List <Summation> newSums = new List <Summation>();

        Summation[] childArray = new Summation[mMaxDim - mMinDim + 1];
        for (int i = 0; i != mMaxDim - mMinDim + 1; ++i)
        {
            childArray[i] = new Summation();
        }

        for (int i = 0; i != mParticles.Count; ++i)
        {
            SmParticle p = mParticles[i];
            childArray[p.LP.mIndex[splitDimension] - mMinDim].mParticles.Add(p);
        }

        for (int i = mMinDim; i <= mMaxDim; ++i)
        {
            Summation child = childArray[i - mMinDim];

            if (child.mParticles.Count == 0)
            {
                child = null;
                childArray[i - mMinDim] = null;
            }
            else
            {
                child.mParticles.Sort(SmParticle.CompareSmParticle);
                child.LP = child.mParticles[0].LP;

                Summation identical = FindIdenticalSummation(ref child.mParticles, childLevel);

                if (null != identical)
                {
                    child = null;
                    childArray[i - mMinDim] = null;

                    mChildren.Add(identical);
                    identical.mParents.Add(this);
                }
                else
                {
                    newSums.Add(child);
                    mChildren.Add(child);
                    child.mParents.Add(this);
                    child.LP.mSummations[childLevel].Add(child);
                    LP.Body.mSummations[childLevel].Add(child);

                    if (childLevel > 0)
                    {
                        child.GenerateChildSums(childLevel - 1);
                    }
                    else
                    {
                        for (int m = 0; m != child.mParticles.Count; ++m)
                        {
                            SmParticle p = child.mParticles[m];
                            child.mChildren.Add(p);
                            p.mParents.Add(child);
                        }
                    }
                }
            }
        }

        System.Array.Clear(childArray, 0, childArray.Length);
        System.Array.Resize(ref childArray, 0);
        return(newSums);
    }
 /// <summary>Read sigma from conf</summary>
 public static Summation Read(Type clazz, Configuration conf)
 {
     return(Summation.ValueOf(conf.Get(clazz.Name + ".sigma")));
 }
 public void Init()
 {
     sum = new Summation();
 }
        public void NullArgsTest()
        {
            Summation sum = new Summation();

            Assert.Catch <NotEnoughArgsException>(() => sum.Execute());
        }