Example #1
0
        public override void Run(ModuleInfo info, CancellationToken token = default(CancellationToken))
        {
            var problem       = new KnapsackDeserializer().Deserialize(_moduleOptions.FilePath);
            var pointContexts = new KnapsackParallelizer()
                                .ParallelizeKnapsack(problem.Items, problem.Capacity, problem.Items.Length - 1, _moduleOptions.PointCount);

            var channelsWithContexts = pointContexts.Select(pointContext =>
            {
                IPoint point     = info.CreatePoint();
                IChannel channel = point.CreateChannel();
                point.ExecuteClass(typeof(KnapsackWorkerModule).ToString());
                return(new { Channel = channel, Context = pointContext });
            }).ToArray();

            foreach (var pair in channelsWithContexts)
            {
                pair.Channel.WriteObject(pair.Context.Parameters);
            }

            Console.WriteLine($"Starting Knapsack Module on {_moduleOptions.PointCount} points");
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            double result = channelsWithContexts.Select(pair => pair.Context.Transform(pair.Channel.ReadDouble())).Max();

            Console.WriteLine($"Result: {result}. Elapsed: {stopwatch.ElapsedMilliseconds} ms");
        }
        public override void Run(ModuleInfo info, CancellationToken token = default)
        {
            int       pointsNum = options.PointsCount;
            Stopwatch sw        = new Stopwatch();

            matrix = GetMatrix(options.InputFile);

            if (matrix.Length % pointsNum != 0)
            {
                throw new Exception($"Matrix size (now {matrix.Length}) should be divided by {pointsNum}!");
            }

            channels = new IChannel[pointsNum];
            points   = new IPoint[pointsNum];

            for (int i = 0; i < pointsNum; ++i)
            {
                points[i]   = info.CreatePoint();
                channels[i] = points[i].CreateChannel();
                points[i].ExecuteClass("FloydWarshallParcs.ModuleFloydWarshall");
            }

            DistributeAllData();

            sw.Start();
            RunParallelFloyd();
            sw.Stop();

            int[][] result = GatherAllData();

            SaveMatrix(options.OutputFile, result);
            Console.WriteLine("Done");
            Console.WriteLine($"Total time {sw.ElapsedMilliseconds} ms ({sw.ElapsedTicks} ticks)");
            Console.ReadLine();
        }
Example #3
0
        public void Run(ModuleInfo info, CancellationToken token = default(CancellationToken))
        {
            string text = @"Auroras are occasionally seen in latitudes below the auroral zone, 
                            when a geomagnetic storm temporarily enlarges the auroral oval.Red: At the highest altitudes, 
                           excited atomic oxygen emits at 630 nm (red); low concentration of atoms and lower sensitivity of 
                           eyes at this wavelength make this color visible only under more intense solar activity. The 
                           low number of oxygen atoms and their gradually diminishing concentration is responsible for 
                           the faint appearance of the top parts of the curtains. 
                           Scarlet, crimson, and carmine are the most often-seen hues of red for the auroras.";

            string[] texts = new string[]
            {
                text.Substring(0, text.Length / 2),
                text.Substring(text.Length / 2, text.Length / 2 - 1)
            };
            RSAProvider rsa = new RSAProvider(Constants.KeySize.Bit64);

            const int pointsNum = 2;
            var       points    = new IPoint[pointsNum];
            var       channels  = new IChannel[pointsNum];

            for (int i = 0; i < pointsNum; ++i)
            {
                points[i]   = info.CreatePoint();
                channels[i] = points[i].CreateChannel();
                points[i].ExecuteClass("CustomModule.RSAModule");
            }

            for (int i = 0; i < pointsNum; ++i)
            {
                channels[i].WriteData(rsa.PublicKey.KeyOne.ToString());
                channels[i].WriteData(rsa.PublicKey.KeyTwo.ToString());
                channels[i].WriteData(texts[i]);
            }
            DateTime time = DateTime.Now;

            Console.WriteLine("Waiting for result...");

            string res = "";

            for (int i = pointsNum - 1; i >= 0; --i)
            {
                res += channels[i].ReadString();
            }

            Console.WriteLine("Result found: res = {0}, time = {1}", res, System.Math.Round((DateTime.Now - time).TotalSeconds, 3));
        }
Example #4
0
        public void Run(ModuleInfo info, CancellationToken token = default(CancellationToken))
        {
            Console.WriteLine("Enter left border a:");
            int a = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter right border b:");
            int       b         = Convert.ToInt32(Console.ReadLine());
            const int pointsNum = 2;
            var       points    = new IPoint[pointsNum];
            var       channels  = new IChannel[pointsNum];

            for (int i = 0; i < pointsNum; ++i)
            {
                points[i]   = info.CreatePoint();
                channels[i] = points[i].CreateChannel();
                points[i].ExecuteClass("MyMainPalindromModule.PalindromModule");
            }

            int y = a;
            int c = a;

            for (int i = 0; i < pointsNum; ++i)
            {
                channels[i].WriteData(c);
                channels[i].WriteData(Math.Min(y + (int)Math.Floor((double)(b - a) / pointsNum), b));
                y += (int)Math.Floor((double)(b - a) / pointsNum);
                c  = y + 1;
            }
            DateTime time = DateTime.Now;

            Console.WriteLine("Waiting for result...");

            List <int> res = new List <int>();

            for (int i = 0; i < pointsNum; ++i)
            {
                res.AddRange(channels[i].ReadObject <List <int> >());
            }

            foreach (var number in res)
            {
                Console.Write(number + " ");
            }
            Console.WriteLine();
            Console.WriteLine("Number of simple palindroms: {0}, time = {1}", res.Count, Math.Round((DateTime.Now - time).TotalSeconds, 3));
        }
Example #5
0
        public void Run(ModuleInfo info, CancellationToken token = default(CancellationToken))
        {
            double    a         = 0;
            double    b         = Math.PI / 2;
            double    h         = 0.00000001;
            const int pointsNum = 2;
            var       points    = new IPoint[pointsNum];
            var       channels  = new IChannel[pointsNum];

            for (int i = 0; i < pointsNum; ++i)
            {
                points[i]   = info.CreatePoint();
                channels[i] = points[i].CreateChannel();
                points[i].ExecuteClass("FirstModule.IntegralModule");
            }

            double y = a;

            for (int i = 0; i < pointsNum; ++i)
            {
                channels[i].WriteData(y);
                channels[i].WriteData(y + (b - a) / pointsNum);
                channels[i].WriteData(h);
                y += (b - a) / pointsNum;
            }
            DateTime time = DateTime.Now;

            Console.WriteLine("Waiting for result...");

            double res = 0;

            for (int i = pointsNum - 1; i >= 0; --i)
            {
                res += channels[i].ReadDouble();
            }

            Console.WriteLine("Result found: res = {0}, time = {1}", res, Math.Round((DateTime.Now - time).TotalSeconds, 3));
        }
Example #6
0
        public override void Run(ModuleInfo info, CancellationToken token = default(CancellationToken))
        {
            string file1 = options.File1;
            string file2 = options.File2;
            Matrix a, b;

            try
            {
                a = Matrix.LoadFromFile(file1);
                b = Matrix.LoadFromFile(file2);
            }

            catch (FileNotFoundException ex)
            {
                _log.Error("File with a given fileName not found, stopping the application...", ex);
                return;
            }

            int[] possibleValues = { 1, 2, 4, 8, 16, 32 };

            int pointsNum = options.PointsNum;

            if (!possibleValues.Contains(pointsNum))
            {
                _log.ErrorFormat("Cannot start module with given number of points. Possible usages: {0}", string.Join(" ", possibleValues));
                return;
            }

            _log.InfoFormat("Starting Matrixes Module on {0} points", pointsNum);

            var points   = new IPoint[pointsNum];
            var channels = new IChannel[pointsNum];

            for (int i = 0; i < pointsNum; ++i)
            {
                points[i]   = info.CreatePoint();
                channels[i] = points[i].CreateChannel();
                points[i].ExecuteClass("NewMatrixModule.MultMatrix");
            }

            var      resMatrix = new Matrix(a.Height, b.Width);
            DateTime time      = DateTime.Now;

            _log.Info("Waiting for a result...");

            switch (pointsNum)
            {
            case 1:
                channels[0].WriteObject(a);
                channels[0].WriteObject(b);
                resMatrix = (Matrix)channels[0].ReadObject(typeof(Matrix));
                break;

            case 2:
            {
                var matrixPairs = Divide2(a, b).ToArray();
                channels[0].WriteObject(matrixPairs[0].Item1);
                channels[0].WriteObject(matrixPairs[0].Item2);
                channels[1].WriteObject(matrixPairs[1].Item1);
                channels[1].WriteObject(matrixPairs[1].Item2);

                LogSendingTime(time);

                Join2(resMatrix, channels.Select(c => new Lazy <Matrix>(c.ReadObject <Matrix>)).ToArray());
            }
            break;

            case 4:
            {
                var matrixPairs = Divide4(a, b).ToArray();
                for (int i = 0; i < matrixPairs.Length; i++)
                {
                    channels[i].WriteObject(matrixPairs[i].Item1);
                    channels[i].WriteObject(matrixPairs[i].Item2);
                }

                LogSendingTime(time);

                Join4(resMatrix, channels.Select(c => new Lazy <Matrix>(c.ReadObject <Matrix>)).ToArray());
            }
            break;

            case 8:
            {
                var matrixPairs = Divide8(a, b).ToArray();
                for (int i = 0; i < matrixPairs.Length; i++)
                {
                    channels[i].WriteObject(matrixPairs[i].Item1);
                    channels[i].WriteObject(matrixPairs[i].Item2);
                }

                LogSendingTime(time);

                Join8(resMatrix, channels.Select(c => new Lazy <Matrix>(c.ReadObject <Matrix>)).ToArray());
            }
            break;

            case 16:
            {
                var matrixPairs8 = Divide8(a, b).ToArray();
                for (int i = 0; i < 8; i++)
                {
                    var m2 = Divide2(matrixPairs8[i].Item1, matrixPairs8[i].Item2).ToArray();
                    channels[i * 2].WriteObject(m2[0].Item1);
                    channels[i * 2].WriteObject(m2[0].Item2);
                    channels[i * 2 + 1].WriteObject(m2[1].Item1);
                    channels[i * 2 + 1].WriteObject(m2[1].Item2);
                }

                LogSendingTime(time);

                var resMatrix8 =
                    Enumerable.Range(0, 8)
                    .Select(i => new Matrix(matrixPairs8[i].Item1.Height, matrixPairs8[i].Item2.Width))
                    .ToArray();
                for (int i = 0; i < 8; i++)
                {
                    Join2(
                        resMatrix8[i],
                        new[] { channels[2 * i], channels[2 * i + 1] }.Select(
                            c => new Lazy <Matrix>(c.ReadObject <Matrix>)).ToArray());
                }

                Join8(resMatrix, resMatrix8.Select(m => new Lazy <Matrix>(() => m)).ToArray());
                //not nice, probably create overload
            }
            break;

            case 32:
            {
                var matrixPairs8 = Divide8(a, b).ToArray();
                for (int i = 0; i < 8; i++)
                {
                    var m4 = Divide4(matrixPairs8[i].Item1, matrixPairs8[i].Item2).ToArray();

                    for (int j = 0; j < 4; j++)
                    {
                        channels[i * 4 + j].WriteObject(m4[j].Item1);
                        channels[i * 4 + j].WriteObject(m4[j].Item2);
                    }
                }

                LogSendingTime(time);

                var resMatrix8 =
                    Enumerable.Range(0, 8)
                    .Select(i => new Matrix(matrixPairs8[i].Item1.Height, matrixPairs8[i].Item2.Width))
                    .ToArray();
                for (int i = 0; i < 8; i++)
                {
                    Join4(
                        resMatrix8[i],
                        Enumerable.Range(0, 4)
                        .Select(j => channels[4 * i + j])
                        .Select(c => new Lazy <Matrix>(c.ReadObject <Matrix>))
                        .ToArray());
                }

                Join8(resMatrix, resMatrix8.Select(m => new Lazy <Matrix>(() => m)).ToArray());
                //not nice, probably create overload
            }
            break;

            default:
                _log.Error("Unexpected error.");
                return;
            }

            LogResultFoundTime(time);
            SaveMatrix(resMatrix);
        }
Example #7
0
        public override void Run(ModuleInfo info, CancellationToken token = default)
        {
            var    textFilePath = options.FilePath;
            string text;

            try
            {
                text = File.ReadAllText(textFilePath);
            }

            catch (FileNotFoundException)
            {
                return;
            }

            int[] possibleValues = { 1, 2, 4, 8, 16, 32 };

            int pointsNum = options.PointsNum;

            if (!possibleValues.Contains(pointsNum))
            {
                return;
            }
            Console.WriteLine("Point2");


            var points   = new IPoint[pointsNum];
            var channels = new IChannel[pointsNum];

            for (int i = 0; i < pointsNum; ++i)
            {
                points[i]   = info.CreatePoint();
                channels[i] = points[i].CreateChannel();
                points[i].ExecuteClass("ParcsNet.StringHasher");
            }

            BigInteger hash = 0;
            DateTime   time = DateTime.Now;

            var textOptions = new HashStringOptions
            {
                Text          = text,
                StartPosition = 0
            };

            Console.WriteLine("Point3");
            var watch = Stopwatch.StartNew();

            switch (pointsNum)
            {
            case 1:
                channels[0].WriteObject(textOptions);
                hash = channels[0].ReadObject <BigInteger>();
                break;

            case 2:
            case 4:
            case 8:
            case 16:
            case 32:
            {
                var splitedText = DivideByParts(textOptions, pointsNum).ToArray();
                for (int i = 0; i < splitedText.Length; i++)
                {
                    channels[i].WriteObject(splitedText[i]);
                }

                hash = Join(channels.Select(c => c.ReadObject <BigInteger>()).ToArray());

                Console.WriteLine("Point4 " + hash);
            }
            break;

            default:
                return;
            }
            watch.Stop();

            Console.WriteLine("Point5");
            SaveHash(hash, watch.ElapsedMilliseconds);
        }
Example #8
0
        public void Run(ModuleInfo info, CancellationToken token = default(CancellationToken))
        {
            string key1;
            string key2;

            string[] texts;

            string text = @"Auroras are occasionally seen in latitudes below the auroral zone, 
                            when a geomagnetic storm temporarily enlarges the auroral oval.Red: At the highest altitudes, 
                           excited atomic oxygen emits at 630 nm (red); low concentration of atoms and lower sensitivity of 
                           eyes at this wavelength make this color visible only under more intense solar activity. The 
                           low number of oxygen atoms and their gradually diminishing concentration is responsible for 
                           the faint appearance of the top parts of the curtains. 
                           Scarlet, crimson, and carmine are the most often-seen hues of red for the auroras.";

            if (mode)
            {
                text = File.ReadAllText("Encoded.txt");

                key1 = File.ReadAllText("Key1.txt");
                key2 = File.ReadAllText("Key2.txt");
                Console.WriteLine("encoded text :{0}", text);
                Console.WriteLine("key1: {0}", key1);
                Console.WriteLine("key2: {0}", key2);

                texts = text.Split(new char[] { '|' });
                if (texts.Length != pointsNum)
                {
                    Console.WriteLine($"Source was encodden on {texts.Length} numper of points while you set {pointsNum}, use same amount");
                    throw new Exception($"Source was encodden on {texts.Length} numper of points while you set {pointsNum}, use same amount");
                }
            }
            else
            {
                try
                {
                    text = File.ReadAllText("TextSample.txt");
                }
                catch (Exception e)
                {
                    Console.WriteLine("File missing, using sample");
                }



                int len = text.Length / pointsNum;
                int ost = text.Length % pointsNum;
                texts = new string[pointsNum];

                for (int i = 0; i < pointsNum - 1; i++)
                {
                    texts[i] = text.Substring(len * i, len);
                }
                texts[pointsNum - 1] = text.Substring(len * (pointsNum - 1), len + ost);

                RSAProvider rsa = new RSAProvider(Constants.KeySize.Bit64);

                key1 = rsa.PublicKey.KeyOne.ToString();
                key2 = rsa.PublicKey.KeyTwo.ToString();

                string prkey1 = rsa.PrivateKey.KeyOne.ToString();
                string prkey2 = rsa.PrivateKey.KeyTwo.ToString();
                Console.WriteLine("key1: {0}", prkey1);
                Console.WriteLine("key2: {0}", prkey2);

                File.WriteAllText("Key1.txt", prkey1);
                File.WriteAllText("Key2.txt", prkey2);
            }



            var    points   = new IPoint[pointsNum];
            var    channels = new IChannel[pointsNum];
            string exe      = mode ? "CustomModule.RSADecModule" : "CustomModule.RSAModule";

            for (int i = 0; i < pointsNum; ++i)
            {
                points[i]   = info.CreatePoint();
                channels[i] = points[i].CreateChannel();
                points[i].ExecuteClass(exe);
            }



            for (int i = 0; i < pointsNum; ++i)
            {
                channels[i].WriteData(key1);
                channels[i].WriteData(key2);
                channels[i].WriteData(texts[i]);
                Console.WriteLine("sending {0}", texts[i]);
            }
            DateTime time = DateTime.Now;

            Console.WriteLine("Waiting for result...");

            string res     = "";
            var    builder = new StringBuilder();

            for (int i = pointsNum - 1; i >= 0; --i)
            {
                builder.Append(channels[i].ReadString());
                builder.Append("|");
                //res += channels[i].ReadString();
            }

            res = mode ? builder.ToString().Replace("|", "") : builder.ToString().TrimEnd('|');

            Console.WriteLine("Result found: res = {0}, time = {1}", res, System.Math.Round((DateTime.Now - time).TotalSeconds, 3));



            if (mode)
            {
                Console.WriteLine(res);
                File.WriteAllText("TextSample.txt", res, Encoding.Default);
            }
            else
            {
                File.WriteAllText("Encoded.txt", res);
            }

            Console.WriteLine("Press any key to exit");
        }
Example #9
0
        public override void Run(ModuleInfo info, CancellationToken token = default(CancellationToken))
        {
            int n = options.dimension;

            Matrix a = new Matrix(n, n, true);
            Matrix b = new Matrix(n, 1, true);

            a.WriteToFile("a.txt");
            b.WriteToFile("b.txt");

            int pointsNum = options.PointsNum;

            _log.InfoFormat("Starting Cramer`s rule Module on {0} points", pointsNum);

            var points   = new IPoint[pointsNum];
            var channels = new IChannel[pointsNum];

            for (int i = 0; i < pointsNum; ++i)
            {
                points[i]   = info.CreatePoint();
                channels[i] = points[i].CreateChannel();
                points[i].ExecuteClass("NewMatrixModule.Det");
            }

            DateTime time = DateTime.Now;

            _log.Info("Waiting for a result...");

            double mainDet    = a.Det();
            int    partsCount = n / pointsNum;

            for (int i = 0; i < pointsNum; i++)
            {
                Matrix[] arr = new Matrix[partsCount];

                for (int j = 0; j < partsCount; j++)
                {
                    Matrix m = new Matrix(n, n);
                    m.Assign(a);
                    m.FillSubMatrix(b, 0, i + j);
                    arr[j] = m;
                }

                channels[i].WriteObject(arr);
            }

            LogSendingTime(time);
            double[] dets = new double [n];

            for (int i = 0; i < pointsNum; i++)
            {
                double[] channelDets = channels[i].ReadObject <double[]>();
                for (int j = 0; j < partsCount; j++)
                {
                    dets[i + j] = channelDets[j];
                }
            }

            double[] result = new double[n];
            for (int i = 0; i < n; i++)
            {
                result[i] = dets[i] / mainDet;
            }

            using (var writer = new StreamWriter(fileName))
            {
                for (int i = 0; i < n; i++)
                {
                    writer.Write(result[i]);
                    writer.WriteLine();
                }
            }


            LogResultFoundTime(time);
            Console.ReadKey();
        }