public void ValidateSparseMatrixAsDenseAnsi()
        {
            SparseMatrix<string, string, double> sparseMatrixObj = GetSparseMatrix();

            ParallelOptions parallelOptObj = new ParallelOptions();

            DenseAnsi denseAnsiObj =
                sparseMatrixObj.AsDenseAnsi<double>(parallelOptObj);

            // Validate all properties of Ansi and SparseMatrix
            Assert.AreEqual(sparseMatrixObj.ColCount, denseAnsiObj.ColCount);
            Assert.AreEqual(sparseMatrixObj.ColKeys.Count, denseAnsiObj.ColKeys.Count);
            Assert.AreEqual(sparseMatrixObj.IndexOfColKey.Count, denseAnsiObj.IndexOfColKey.Count);
            Assert.AreEqual(sparseMatrixObj.IndexOfRowKey.Count, denseAnsiObj.IndexOfRowKey.Count);
            Assert.AreEqual("?", denseAnsiObj.MissingValue.ToString((IFormatProvider)null));
            Assert.AreEqual(sparseMatrixObj.RowCount, denseAnsiObj.RowCount);
            Assert.AreEqual(sparseMatrixObj.RowKeys.Count, denseAnsiObj.RowKeys.Count);
            Assert.AreEqual(utilityObj.xmlUtil.GetTextValue(
                Constants.SimpleMatrixNodeName,
                Constants.DenseAnsiStringNode),
                denseAnsiObj.ToString2D().Replace("\r", "").Replace("\n", "").Replace("\t", ""));

            ApplicationLog.WriteLine(
                "SparseMatrix BVT : Validation of AsDenseAnsi() method successful");
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;

            Console.WriteLine("Press enter to start");
            Console.ReadLine();

            // Batch the connections, once they're all active though it's fine
            int connectionBatchSize = 10;
            int connectionSleepInterval = 1000;
            int maxClients = 3000;

            var options = new ParallelOptions
            {
                MaxDegreeOfParallelism = connectionBatchSize
            };

            for (int x = 0; x < maxClients; x += connectionBatchSize)
            {
                Parallel.For(0, connectionBatchSize, options, i =>
                    {
                        StartClient();
                    });

                System.Threading.Thread.Sleep(connectionSleepInterval);
            }

            Console.Read();
        }
        public void ValidateRowKeysPairAnsiGetInstanceFromPairAnsi()
        {
            ParallelOptions parOptObj = new ParallelOptions();

            UOPair<char> uoPairObj = new UOPair<char>('?', '?');
            DensePairAnsi dpaObj =
                DensePairAnsi.CreateEmptyInstance(
                new string[] { "R0", "R1", "R2" },
                new string[] { "C0", "C1", "C2", "C3" },
                uoPairObj);

            dpaObj.WriteDensePairAnsi(Constants.FastQTempTxtFileName,
                parOptObj);

            using (RowKeysPairAnsi rkaObj =
                 RowKeysPairAnsi.GetInstanceFromPairAnsi(
                 Constants.FastQTempTxtFileName, parOptObj))
            {
                Assert.AreEqual(4, rkaObj.ColCount);
                Assert.AreEqual(3, rkaObj.RowCount);
            }

            if (File.Exists(Constants.FastQTempTxtFileName))
                File.Delete(Constants.FastQTempTxtFileName);

            ApplicationLog.WriteLine(
                "RowKeysPairAnsi BVT : Validation of GetInstanceFromPairAnsi() method successful");
        }
Beispiel #4
0
        public void Read_Streams()
        {
            long values = 0;
            int streams = 0;
            var watch = Stopwatch.StartNew();

            var opt = new ParallelOptions { MaxDegreeOfParallelism = 4 };

            Parallel.ForEach(GetStreams(), opt, stream =>
            //foreach (var stream in GetStreams())
            {
                streams++;
                var repository = BuildRepository(stream);
                foreach (var value in repository.GetValues())
                    values++;

                var speed = values / watch.Elapsed.TotalSeconds;
                Console.WriteLine("Completed {0} number {1} : {2:0} total of {3} ", stream, streams, speed, values);
            }
            );

            watch.Stop();

            Console.WriteLine("read {0} values in {1} streams in {2}", values, streams, watch.Elapsed);
        }
Beispiel #5
0
 public static BitmapSource GetColorBrushImage(Color color)
 {
     var stream = new MemoryStream();
     Resource.EmptyBrush.Save(stream, ImageFormat.Png);
     stream.Seek(0, SeekOrigin.Begin);
     var bitmap = CreateBitmapImage(stream);
     int width = bitmap.PixelWidth;
     int height = bitmap.PixelHeight;
     var resultBitmap = new WriteableBitmap(width, height, 92, 92, PixelFormats.Bgra32, bitmap.Palette);
     var imageBytes = bitmap.GetBytes();
     DisposeImage(bitmap);
     var po = new ParallelOptions {MaxDegreeOfParallelism = 4};
     Parallel.For(0, width*height, po, i =>
     {
         int index = i*4;
         if (imageBytes[index + 3] < 16)
         {
             imageBytes[index] = color.B;
             imageBytes[index + 1] = color.G;
             imageBytes[index + 2] = color.R;
             imageBytes[index + 3] = color.A;
         }
     });
     resultBitmap.WritePixels(new Int32Rect(0, 0, width,  height), imageBytes, 4*width, 0);
     if (resultBitmap.CanFreeze)
     {
         resultBitmap.Freeze();
     }
     return resultBitmap;
 }
Beispiel #6
0
        public void ImportAllStreams()
        {
            var ids = new List<int>();
            using (var con = new System.Data.SqlClient.SqlConnection(_cs))
            {
                con.Open();
                ids = GetStreamIds(con).OrderBy(d => d).ToList();
            }
            var po = new ParallelOptions { MaxDegreeOfParallelism = 4 };
            //Parallel.ForEach(ids, po, id =>
            //{
            //    var sw = Stopwatch.StartNew();
            //    TryImportStream(id);
            //    sw.Stop();
            //    ImportedStreams++;

            //    Console.WriteLine(" Done in {0}", sw.Elapsed);
            //});
            foreach (var id in ids)
            {
                var sw = Stopwatch.StartNew();

                TryImportStream(id);
                sw.Stop();
                ImportedStreams++;

                Console.WriteLine(" Done in {0}", sw.Elapsed);
            }
        }
Beispiel #7
0
        public void Build(MetricDB db, int k, Index ref_index)
        {
            this.DB = db;
            this.K = k;
            this.R = ref_index;
            int sigma = this.R.DB.Count;
            this.INVINDEX = new List<List<int>> (sigma);
            for (int i = 0; i < sigma; ++i) {
                this.INVINDEX.Add(new List<int>());
            }
            var A = new int[this.DB.Count][];
            int count = 0;
            var compute_one = new Action<int>(delegate(int objID) {
                var u = this.GetKnr(this.DB[objID], this.K);
                A[objID] = u;
                ++count;
                if (count % 1000 == 0) {
                    Console.WriteLine ("==== {0}/{1} db: {2}, k: {3}", count, this.DB.Count, this.DB.Name, k);
                }
            });
            ParallelOptions ops = new ParallelOptions();
            ops.MaxDegreeOfParallelism = -1;
            Parallel.ForEach(new ListGen<int>((int i) => i, this.DB.Count), ops, compute_one);

            for (int objID = 0; objID < this.DB.Count; ++objID) {
                var u = A[objID];
                for (int i = 0; i < this.K; ++i) {
                    this.INVINDEX[u[i]].Add (objID);
                }
            }
        }
Beispiel #8
0
        public List<BuildInfoDto> GetBuildInfoDtosPolling(String filterDate)
        {
            var buildInfoDtos = new List<BuildInfoDto>();
            try
            {
                var sinceDateTime = DateTime.Now.Subtract(new TimeSpan(int.Parse(filterDate), 0, 0));


                var tfsServer = _helperClass.GetTfsServer();

                // Get the catalog of team project collections
                var teamProjectCollectionNodes = tfsServer.CatalogNode.QueryChildren(
                    new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None);

                var parallelOptions = new ParallelOptions() {MaxDegreeOfParallelism = 1};
                Parallel.ForEach(teamProjectCollectionNodes, parallelOptions, teamProjectCollectionNode =>
                {
                    var taskBuidInfos = GetBuildInfoDtosPerTeamProject(teamProjectCollectionNode, tfsServer, sinceDateTime);
                    taskBuidInfos.ConfigureAwait(false);
                    taskBuidInfos.Wait();

                    lock (buildInfoDtos)
                    {
                        buildInfoDtos.AddRange(taskBuidInfos.Result);
                    }
                });
            }
            catch (Exception e)
            {
                LogService.WriteError(e);
                throw;
            }

            return buildInfoDtos;
        }
Beispiel #9
0
        public List<BuildInfoDto> GetBuildInfoDtos()
        {
            var buildInfoDtos = new ConcurrentBag<BuildInfoDto>();
            try
            {
                
                var tfsServer = _helperClass.GetTfsServer();
                // Get the catalog of team project collections
                var teamProjectCollectionNodes = tfsServer.CatalogNode.QueryChildren(
                    new[] { CatalogResourceTypes.ProjectCollection }, false, CatalogQueryOptions.None);
                var parallelOptions = new ParallelOptions {MaxDegreeOfParallelism = 1};
                Parallel.ForEach(teamProjectCollectionNodes, parallelOptions, teamProjectCollectionNode =>
                {
                    
                        var task = GetBuildInfoDtosPerTeamProject(teamProjectCollectionNode, tfsServer, DateTime.MinValue);
                        task.ConfigureAwait(false);
                        task.Wait();    
                        var buildInfos = task.Result;

                        foreach (var buildInfoDto in buildInfos)
                        {
                            buildInfoDtos.Add(buildInfoDto);
                        }
                        
                    
                });
            }
            catch (Exception e)
            {
                LogService.WriteError(e);
                throw;
            }

            return buildInfoDtos.ToList();
        }
Beispiel #10
0
        public override async Task ExecuteAsync()
        {
            // Create 100 Data objects
            var data = Enumerable.Range(1, 100).Select(i => new Data { Number = i });
            
            // Option 1a: use Upsert with an IDictionary<string, object> of documents in upsert
            var bulkData = data.ToDictionary(d => "dotnetDevguideExample-" + d.Number);
            // Note: There is no UpsertAsync overload that takes multiple values (yet)
            // which is why this example wraps the synchronized method in a new Task.
            // If your code is fully synchronous, you can simply call _bucket.Upsert(...)
            var bulkResult = await Task.Run(() => _bucket.Upsert(bulkData));

            var successCount = bulkResult.Where(r => r.Value.Success).Count();
            Console.WriteLine("Upserted {0} values", bulkResult.Count);

            // Option 1b: Specify ParallelOptions to customize how the client parallelizes the upserts
            var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 32 };
            var bulkResult2 = await Task.Run(() => _bucket.Upsert(bulkData, parallelOptions));

            successCount = bulkResult2.Where(r => r.Value.Success).Count();
            Console.WriteLine("Upserted {0} values", successCount);

            // Option 2: Spawn multiple Upsert tasks and wait on for all to complete
            var bulkTasks = bulkData.Select(d => _bucket.UpsertAsync(d.Key, d.Value));
            var bulkResults = await Task.WhenAll(bulkTasks);

            successCount = bulkResults.Where(r => r.Success).Count();
            Console.WriteLine("Upserted {0} values", successCount);
        }
Beispiel #11
0
        bool RunParallelAotCompiler(List <string> nativeLibs)
        {
            try {
                ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
                    CancellationToken = Token,
                    TaskScheduler     = ThreadingTasks.TaskScheduler.Default,
                };

                ThreadingTasks.Parallel.ForEach(GetAotConfigs(), options,
                                                config => {
                    if (!config.Valid)
                    {
                        Cancel();
                        return;
                    }

                    if (!RunAotCompiler(config.AssembliesPath, config.AotCompiler, config.AotOptions, config.AssemblyPath))
                    {
                        LogCodedError("XA3001", "Could not AOT the assembly: {0}", Path.GetFileName(config.AssemblyPath));
                        Cancel();
                        return;
                    }

                    lock (nativeLibs)
                        nativeLibs.Add(config.OutputFile);
                }
                                                );
            } catch (OperationCanceledException) {
                return(false);
            }

            return(true);
        }
        public void Test_Timed_Execution_Parallel_Client()
        {
            var options = new ParallelOptions { MaxDegreeOfParallelism = 4 };
            var n = 1000;//set to a higher # if needed

            using (var cluster = new Cluster("couchbaseClients/couchbase"))
            {
                using (var bucket = cluster.OpenBucket())
                {
                    using (new OperationTimer())
                    {
                        var temp = bucket;
                        Parallel.For(0, n, options, i =>
                        {
                            var key = string.Format("key{0}", i);
                            var value = (int?) i;
                            var result = temp.Upsert(key, value);
                            Assert.IsTrue(result.Success);

                            var result1 = temp.Get<int?>(key);
                            Assert.IsTrue(result1.Success);
                            Assert.AreEqual(i, result1.Value);
                        });
                    }
                }
            }
        }
Beispiel #13
0
        private void ParseEntries(IEnumerable<FileInfo> files, LogEntry lastEntry, out long count)
        {
            count = 0;

            // Only allow as many parallel threads as the size of the connection pool.
            var options = new ParallelOptions { MaxDegreeOfParallelism = this.db.MaxConcurrentConnections };

            var subCounts = new Dictionary<string, long>();

            Parallel.ForEach(files, options, (file) =>
            {
                long subCount;

                // Read entries from file and apply transformations.
                var entries = this.ParseEntries(file, this.transformations).Where(AllowInsert(lastEntry));

                // Write entries to the database.
                this.db.Write(entries, out subCount);

                subCounts.Add(file.FullName, subCount);
            });

            // Set count to sum of all entry counts per file.
            count = subCounts.Values.Sum();
        }
        public void ValidateRowKeysAnsiGetInstanceFromDenseAnsi()
        {
            DenseMatrix<string, string, double> denseMatObj =
                GetDenseMatrix();
            ParallelOptions parOptObj = new ParallelOptions();

            denseMatObj.WriteDenseAnsi(Constants.FastQTempTxtFileName,
                parOptObj);

            using (RowKeysAnsi rkaObj =
                RowKeysAnsi.GetInstanceFromDenseAnsi(Constants.FastQTempTxtFileName,
                parOptObj))
            {

                Assert.AreEqual(denseMatObj.ColCount, rkaObj.ColCount);
                Assert.AreEqual(denseMatObj.RowCount, rkaObj.RowCount);
                Assert.AreEqual(denseMatObj.RowKeys.Count, rkaObj.RowKeys.Count);
                Assert.AreEqual(denseMatObj.ColKeys.Count, rkaObj.ColKeys.Count);
            }

            if (File.Exists(Constants.FastQTempTxtFileName))
                File.Delete(Constants.FastQTempTxtFileName);

            ApplicationLog.WriteLine(
                "RowKeysAnsi BVT : Validation of GetInstanceFromDenseAnsi() method successful");
        }
        /// <summary>
        ///     Initialize the experiment with configuration file parameters.
        /// </summary>
        /// <param name="name">The name of the experiment</param>
        /// <param name="xmlConfig">The parent XML configuration element</param>
        public virtual void Initialize(string name, XmlElement xmlConfig)
        {
            // Set all properties
            Name = name;
            DefaultPopulationSize = XmlUtils.GetValueAsInt(xmlConfig, "PopulationSize");
            Description = XmlUtils.GetValueAsString(xmlConfig, "Description");

            // Set all internal class variables
            _activationScheme = ExperimentUtils.CreateActivationScheme(xmlConfig, "Activation");
            ComplexityRegulationStrategy = XmlUtils.TryGetValueAsString(xmlConfig, "ComplexityRegulationStrategy");
            Complexitythreshold = XmlUtils.TryGetValueAsInt(xmlConfig, "ComplexityThreshold");
            ParallelOptions = ExperimentUtils.ReadParallelOptions(xmlConfig);

            // Set evolution/genome parameters
            NeatEvolutionAlgorithmParameters = new NeatEvolutionAlgorithmParameters
            {
                SpecieCount = XmlUtils.GetValueAsInt(xmlConfig, "SpecieCount"),
                InterspeciesMatingProportion = XmlUtils.GetValueAsDouble(xmlConfig,
                    "InterspeciesMatingProbability"),
                MinTimeAlive = XmlUtils.GetValueAsInt(xmlConfig, "MinTimeAlive")
            };
            NeatGenomeParameters = ExperimentUtils.ReadNeatGenomeParameters(xmlConfig);

            // Set experiment-specific parameters
            MaxTimesteps = XmlUtils.TryGetValueAsInt(xmlConfig, "MaxTimesteps");
            MinSuccessDistance = XmlUtils.TryGetValueAsInt(xmlConfig, "MinSuccessDistance");
            MaxDistanceToTarget = XmlUtils.TryGetValueAsInt(xmlConfig, "MaxDistanceToTarget");
            MazeVariant =
                MazeVariantUtl.convertStringToMazeVariant(XmlUtils.TryGetValueAsString(xmlConfig, "MazeVariant"));         
        }
        /// <summary>Executes a for loop in which iterations may run in parallel.</summary>
        /// <param name="fromInclusive">The start index, inclusive.</param>
        /// <param name="toExclusive">The end index, exclusive.</param>
        /// <param name="options">A System.Threading.Tasks.ParallelOptions instance that configures the behavior of this operation.</param>
        /// <param name="body">The delegate that is invoked once per iteration.</param>
        public static void For(BigInteger fromInclusive, BigInteger toExclusive, ParallelOptions options, Action<BigInteger> body)
        {
            // Determine how many iterations to run...
            var range = toExclusive - fromInclusive;

            // ... and run them.
            if (range <= 0)
            {
                // If there's nothing to do, bail
                return;
            }
                // Fast path
            else if (range <= Int64.MaxValue)
            {
                // If the range is within the realm of Int64, we'll delegate to Parallel.For's Int64 overloads.
                // Iterate from 0 to range, and then call the user-provided body with the scaled-back value.
                Parallel.For(0, (long)range, options, i => body(i + fromInclusive));
            }
                // Slower path
            else
            {
                // For a range larger than Int64.MaxValue, we'll rely on an enumerable of BigInteger.
                // We create a C# iterator that yields all of the BigInteger values in the requested range
                // and then ForEach over that range.
                Parallel.ForEach(Range(fromInclusive, toExclusive), options, body);
            }
        }
        private static void DownloadInParallel(IEnumerable<Show> showsToDownload)
        {
            var po = new ParallelOptions {MaxDegreeOfParallelism = MaxSimultaneousDownloads};
            Parallel.ForEach(showsToDownload, po, show =>
                {
                    try
                    {
                        using (var httpClient = new HttpClient())
                        {
                            var downloadStream = httpClient.GetStreamAsync(show.Mp3Uri).Result;

                            var file = new FileInfo(_saveDirectory + string.Format(@"\Hanselminutes_{0}.mp3", show.Id));
                            using (downloadStream)
                            using (var fileStream = file.Create())
                            {
                                Console.WriteLine(string.Format("Downloading show {0}", show.Id));
                                downloadStream.CopyTo(fileStream);
                            }

                            Console.WriteLine(string.Format("Show {0} downloaded to {1}", show.Id, file));
                        }
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine(e);
                    }
                });
        }
Beispiel #18
0
 public static void ParallelRun(params IModelSimulation[] simulations)
 {
     var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = -1 };
     Parallel.ForEach(simulations, parallelOptions,
         s => s.Execute()
     );
 }
Beispiel #19
0
 public virtual void Build(MetricDB db, int num_pairs, int maxCandidates = -1)
 {
     this.DB = db;
     this.Fingerprints = new BinQ8HammingSpace (1);
     this.Sample = new SampleSpace("", this.DB, num_pairs * 2);
     this.MaxCandidates = maxCandidates;
     var n = this.DB.Count;
     var A = new byte[n][];
     int pc = this.DB.Count / 100 + 1;
     int advance = 0;
     var create_one = new Action<int> (delegate(int i) {
         var fp = this.GetFP(this.DB[i]);
         A[i] = fp;
         if (advance % pc == 0) {
             Console.WriteLine ("DEBUG {0}  ({1}/{2}), db: {3}, num_pairs: {4}, timestamp: {5}", this, advance, n, db.Name, num_pairs, DateTime.Now);
         }
         advance++;
     });
     ParallelOptions ops = new ParallelOptions();
     ops.MaxDegreeOfParallelism = -1;
     Parallel.For (0, n, create_one);
     foreach (var fp in A) {
         this.Fingerprints.Add( fp );
     }
     var s = new Sequential ();
     s.Build (this.Fingerprints);
     this.InternalIndex = s;
 }
Beispiel #20
0
        /// <summary>
        /// Execute Download Action
        /// </summary>
        /// <param name="files">files to download</param>
        /// <returns>lis of files downloaded</returns>
        public List<string> Run(List<string> files)
        {
            List<string> downloadedFiles = new List<string>();
            bool runNonMultitrhead = true;
            if (d.MultiThreadOption != null)
            {
                if (d.MultiThreadOption.enableMultithread)
                {
                    runNonMultitrhead = false;
                    ParallelOptions options = new ParallelOptions();
                    if (d.MultiThreadOption.setThreadMaxNumbers)
                        options.MaxDegreeOfParallelism = d.MultiThreadOption.ThreadNumbers;

                    //Run links multithread
                    System.Threading.Tasks.Parallel.For(0, files.Count, options, ii =>
                    {
                        downloadedFiles.Add(downloadFile(files[ii]));
                    });
                }
                
            }
            if (runNonMultitrhead)
            {
                foreach (string file in files)
                    downloadedFiles.Add(downloadFile(file));                
            }
          

            return downloadedFiles;
        }
        private static void DownloadProjectsFromGitHub(IEnumerable<Repository> searchRepositoryResult,
            string buildFolder)
        {
            var parallelOptions = new ParallelOptions {MaxDegreeOfParallelism = 1};
            Parallel.ForEach(searchRepositoryResult, parallelOptions, r =>
            {
                var archivePath = Path.Combine(buildFolder, r.Name + ".zip");
                var destinationDirectoryName = Path.Combine(buildFolder, r.Name);
                if (!File.Exists(archivePath))
                {
                    var downloadUrl = r.HtmlUrl + "/archive/master.zip";
                    Console.WriteLine("Downloading from: {0}", downloadUrl);
                    using (var client = new WebClient())
                    {
                        client.DownloadFile(downloadUrl, archivePath);
                    }
                }

                try
                {
                    Console.WriteLine("Extracting to: {0}", destinationDirectoryName);
                    ZipFile.ExtractToDirectory(archivePath, destinationDirectoryName);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Directory.Delete(destinationDirectoryName, true);
                }
            });
        }
 public void MaxDegreeOfParallelismPropertyBehavior()
 {
     var po = new ParallelOptions {MaxDegreeOfParallelism = 10};
     Assert.That(po.MaxDegreeOfParallelism, Is.EqualTo(10));
     po.MaxDegreeOfParallelism = 3;
     Assert.That(po.MaxDegreeOfParallelism, Is.EqualTo(3));
 }
Beispiel #23
0
        public override async Task ExecuteAsync()
        {
            // Call BulkInsert to generate some data
            await new BulkInsert().ExecuteAsync();

            // Generate the keys to retrieve
            var keys = Enumerable.Range(1, 100).Select(i => "dotnetDevguideExample-" + i).ToList();


            // Option 1a: use Get with a IList<string> of document keys to retrieve
            // Note: There is no GetAsync overload that takes multiple keys (yet)
            // which is why this example wraps the synchronized method in a new Task.
            // If your code is fully synchronous, you can simply call _bucket.Get(...)
            var bulkResult = await Task.Run(() => _bucket.Get<Data>(keys));

            var successCount = bulkResult.Where(r => r.Value.Success).Count();
            Console.WriteLine("Got {0} values", bulkResult.Count);

            // Option 1b: Specify ParallelOptions to customize how the client parallelizes the gets
            var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = 32 };
            var bulkResult2 = await Task.Run(() => _bucket.Get<Data>(keys, parallelOptions));

            successCount = bulkResult2.Where(r => r.Value.Success).Count();
            Console.WriteLine("Got {0} values", successCount);

            // Option 2: Spawn multiple Upsert tasks and wait on for all to complete
            var bulkTasks = keys.Select(key => _bucket.GetAsync<Data>(key));
            var bulkResults = await Task.WhenAll(bulkTasks);

            successCount = bulkResults.Where(r => r.Success).Count();
            Console.WriteLine("Got {0} values", successCount);

        }
        public void ValidateRowKeysPDGetInstanceFromPaddedDoubleFileAccess()
        {
            DenseMatrix<string, string, double> denseMatObj =
                GetDenseMatrix();
            ParallelOptions parOptObj = new ParallelOptions();

            denseMatObj.WritePaddedDouble(Constants.FastQTempTxtFileName,
                parOptObj);

            using (RowKeysPaddedDouble rkpdObj =
                RowKeysPaddedDouble.GetInstanceFromPaddedDouble(
                Constants.FastQTempTxtFileName, parOptObj, FileAccess.ReadWrite,
                FileShare.ReadWrite))
            {
                Assert.AreEqual(denseMatObj.ColCount, rkpdObj.ColCount);
                Assert.AreEqual(denseMatObj.RowCount, rkpdObj.RowCount);
                Assert.AreEqual(denseMatObj.RowKeys.Count, rkpdObj.RowKeys.Count);
                Assert.AreEqual(denseMatObj.ColKeys.Count, rkpdObj.ColKeys.Count);
            }

            if (File.Exists(Constants.FastQTempTxtFileName))
                File.Delete(Constants.FastQTempTxtFileName);

            ApplicationLog.WriteLine(
                "RowKeysPaddedDouble BVT : Validation of GetInstanceFromPaddedDouble(file-access) method successful");
        }
 public int ConvertAllSvgToIco(string folder, bool recursive, bool refresh, int[] sizes,
     int maxDegreeOfParallelism)
 {
     var resultCode = 0;
     var iconInfos = _iconInfoProvider.GetIconInfos(folder, recursive, sizes);
     var parallelOptions = new ParallelOptions() {MaxDegreeOfParallelism = maxDegreeOfParallelism};
     Parallel.ForEach(iconInfos, parallelOptions, (iconInfo, state) =>
     {
         try
         {
             var isupdated = ConvertSvgToPngs(iconInfo, sizes, refresh);
             var iconFileExists = iconInfo.IconFile.Exists;
             if (isupdated || !iconFileExists || refresh)
             {
                 _imageMagicProvider.CreateIconFromPngFilesFromSvg(iconInfo);
             }
             else
             {
                 _logger.Info("Icon up to date: " + iconInfo.IconFile.FullName);
             }
         }
         catch (Exception ex)
         {
             _logger.Error(ex.Message);
         }
     });
     return resultCode;
 }
        public virtual  void Initialize(string name, XmlElement xmlConfig)
        {
            _name = name;
            _populationSize = XmlUtils.GetValueAsInt(xmlConfig, "PopulationSize");
            _specieCount = XmlUtils.GetValueAsInt(xmlConfig, "SpecieCount");
            _activationScheme = ExperimentUtils.CreateActivationScheme(xmlConfig, "Activation");
            _complexityRegulationStr = XmlUtils.TryGetValueAsString(xmlConfig,
                "DefaultComplexityRegulationStrategy");
            _complexityThreshold = XmlUtils.TryGetValueAsInt(xmlConfig, "ComplexityThreshold");
            _description = XmlUtils.TryGetValueAsString(xmlConfig, "Description");
            _parallelOptions = ExperimentUtils.ReadParallelOptions(xmlConfig);

            _eaParams = new NeatEvolutionAlgorithmParameters();
            _eaParams.SpecieCount = _specieCount;
            _neatGenomeParams = new NeatGenomeParameters();
            _neatGenomeParams.FeedforwardOnly = _activationScheme.AcyclicNetwork;

            DefaultComplexityRegulationStrategy = ExperimentUtils.CreateComplexityRegulationStrategy(
                _complexityRegulationStr,
                _complexityThreshold);
            DefaultSpeciationStrategy = new KMeansClusteringStrategy<NeatGenome>(new ManhattanDistanceMetric(1.0, 0.0, 10.0));
            DefaultNeatEvolutionAlgorithm = new NeatEvolutionAlgorithm<NeatGenome>(
                    NeatEvolutionAlgorithmParameters,
                    DefaultSpeciationStrategy,
                    DefaultComplexityRegulationStrategy);
        }
        public void ValidateMatrixFactoryParse()
        {
            denseMatObj = GetDenseMatrix();

            MatrixFactory<String, String, Double> mfObj =
                MatrixFactory<String, String, Double>.GetInstance();

            ParallelOptions poObj = new ParallelOptions();

            TryParseMatrixDelegate<string, string, double> a =
                new TryParseMatrixDelegate<string, string, double>(this.TryParseMatrix);
            mfObj.RegisterMatrixParser(a);
            // Writes the text file
            denseMatObj.WritePaddedDouble(Constants.FastQTempTxtFileName, poObj);

            Matrix<string, string, double> newMatObj =
                mfObj.Parse(Constants.FastQTempTxtFileName, double.NaN, poObj);

            Assert.AreEqual(denseMatObj.RowCount, newMatObj.RowCount);
            Assert.AreEqual(denseMatObj.RowKeys.Count, newMatObj.RowKeys.Count);
            Assert.AreEqual(denseMatObj.ColCount, newMatObj.ColCount);
            Assert.AreEqual(denseMatObj.ColKeys.Count, newMatObj.ColKeys.Count);
            Assert.AreEqual(denseMatObj.Values.Count(), newMatObj.Values.Count());

            ApplicationLog.WriteLine(
                "MatrixFactory BVT : Successfully validated Parse() method");
        }
        public MarketStateCollection(string fixtureId, IUpdatableMarketStateCollection collection)
            : this(fixtureId)
        {

            if (collection == null)
                return;

            Sport = collection.Sport;

            // this is just for creating the nodes
            foreach (var mkt_id in collection.Markets)
            {
                _States[mkt_id] = null;

            }

            ParallelOptions options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount };

            // fill the nodes and as they are already created, there is no race-condition here
            Parallel.ForEach(collection.Markets, options, x =>
                {
                    _States[x] = ((IUpdatableMarketState)collection[x]).Clone();
                }
            );
        }
        static void ParallerInsert(CouchbaseClient client, int n)
        {
            var options = new ParallelOptions { MaxDegreeOfParallelism = 4 };

            Parallel.For(0, n, options, i =>
            {
                var key = "key" + i;
                var value = "value" + i;

                var result = client.ExecuteStore(StoreMode.Set, key, value);
                if (result.Success)
                {
                    Console.WriteLine("Write Key: {0} - Value: {1}", key, value);
                    var result2 = client.ExecuteGet<string>(key);
                    if (result2.Success)
                    {
                        Console.WriteLine("Read Key: {0} - Value: {1}", key, result2.Value);
                    }
                    else
                    {
                        Console.WriteLine("Read Error: {0} - {1}", key, result.Message);
                    }
                }
                else
                {
                    Console.WriteLine("Write Error: {0} - {1}", key, result.Message);
                }
            });
        }
Beispiel #30
0
        private void Form1_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            if (Path.GetExtension(files[0]).ToUpper() == ".WIL" ||
                Path.GetExtension(files[0]).ToUpper() == ".WZL" ||
                Path.GetExtension(files[0]).ToUpper() == ".MIZ")
            {
                try
                {
                    ParallelOptions options = new ParallelOptions { MaxDegreeOfParallelism = 8 };
                    Parallel.For(0, files.Length, options, i =>
                    {
                        if (Path.GetExtension(files[i]) == ".wtl")
                        {
                            WTLLibrary WTLlib = new WTLLibrary(files[i]);
                            WTLlib.ToMLibrary();
                        }
                        else
                        {
                            WeMadeLibrary WILlib = new WeMadeLibrary(files[i]);
                            WILlib.ToMLibrary();
                        }
                        toolStripProgressBar.Value++;
                    });
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }

                toolStripProgressBar.Value = 0;

                MessageBox.Show(
                    string.Format("Successfully converted {0} {1}",
                    (OpenWeMadeDialog.FileNames.Length).ToString(),
                    (OpenWeMadeDialog.FileNames.Length > 1) ? "libraries" : "library"));
            }
            else if (Path.GetExtension(files[0]).ToUpper() == ".LIB")
            {
                ClearInterface();
                ImageList.Images.Clear();
                PreviewListView.Items.Clear();
                _indexList.Clear();

                if (_library != null) _library.Close();
                _library = new MLibraryV2(files[0]);
                PreviewListView.VirtualListSize = _library.Images.Count;
                PreviewListView.RedrawItems(0, PreviewListView.Items.Count - 1, true);

                // Show .Lib path in application title.
                this.Text = files[0].ToString();
            }
            else
            {
                return;
            }
        }
        /// <summary>Repeatedly executes an operation in parallel while the specified condition evaluates to true.</summary>
        /// <param name="parallelOptions">A ParallelOptions instance that configures the behavior of this operation.</param>
        /// <param name="condition">The condition to evaluate.</param>
        /// <param name="body">The loop body.</param>
        public static void ParallelWhile(
            ParallelOptions parallelOptions, Func<bool> condition, Action body)
        {
            if (parallelOptions == null) throw new ArgumentNullException("parallelOptions");
            if (condition == null) throw new ArgumentNullException("condition");
            if (body == null) throw new ArgumentNullException("body");

            Parallel.ForEach(SingleItemPartitioner.Create(IterateUntilFalse(condition)), parallelOptions, ignored => body());
        }
Beispiel #32
0
        void DoExecute()
        {
            LoadResourceCaseMap();

            ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
                CancellationToken = Token,
                TaskScheduler     = ThreadingTasks.TaskScheduler.Default,
            };

            ThreadingTasks.Parallel.ForEach(ResourceDirectories, options, ProcessDirectory);
        }
Beispiel #33
0
        void DoExecute()
        {
            LoadResourceCaseMap();

            assemblyMap.Load(Path.Combine(WorkingDirectory, AssemblyIdentityMapFile));

            ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
                CancellationToken = Token,
                TaskScheduler     = ThreadingTasks.TaskScheduler.Default,
            };

            ThreadingTasks.Parallel.ForEach(ManifestFiles, options, ProcessManifest);
        }
Beispiel #34
0
 /// <summary>
 /// Executes a while loop in parallel.
 /// </summary>
 /// <param name="parallelOptions">Parallel options.</param>
 /// <param name="condition">Condition function. Returns true if the loop should advance, false otherwise.</param>
 /// <param name="body">Body function.</param>
 public static void While(System.Threading.Tasks.ParallelOptions parallelOptions, Func <bool> condition, Action <ParallelLoopState> body)
 {
     System.Threading.Tasks.Parallel.ForEach(new InfinitePartitioner(), parallelOptions,
                                             (ignored, loopState) =>
     {
         if (condition())
         {
             body(loopState);
         }
         else
         {
             loopState.Stop();
         }
     });
 }
Beispiel #35
0
        void DoExecute()
        {
            if (ResourceNameCaseMap != null)
            {
                foreach (var arr in ResourceNameCaseMap.Split(';').Select(l => l.Split('|')).Where(a => a.Length == 2))
                {
                    resource_name_case_map [arr [1]] = arr [0];                     // lowercase -> original
                }
            }
            assemblyMap.Load(Path.Combine(WorkingDirectory, AssemblyIdentityMapFile));

            ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
                CancellationToken = Token,
                TaskScheduler     = ThreadingTasks.TaskScheduler.Default,
            };

            ThreadingTasks.Parallel.ForEach(ManifestFiles, options, ProcessManifest);
        }
Beispiel #36
0
        public override bool Execute()
        {
            Log.LogDebugMessage("Aapt Task");
            Log.LogDebugMessage("  AssetDirectory: {0}", AssetDirectory);
            Log.LogDebugTaskItems("  ManifestFiles: ", ManifestFiles);
            Log.LogDebugMessage("  ResourceDirectory: {0}", ResourceDirectory);
            Log.LogDebugMessage("  JavaDesignerOutputDirectory: {0}", JavaDesignerOutputDirectory);
            Log.LogDebugMessage("  PackageName: {0}", PackageName);
            Log.LogDebugMessage("  UncompressedFileExtensions: {0}", UncompressedFileExtensions);
            Log.LogDebugMessage("  ExtraPackages: {0}", ExtraPackages);
            Log.LogDebugTaskItems("  AdditionalResourceDirectories: ", AdditionalResourceDirectories);
            Log.LogDebugTaskItems("  AdditionalAndroidResourcePaths: ", AdditionalAndroidResourcePaths);
            Log.LogDebugTaskItems("  LibraryProjectJars: ", LibraryProjectJars);
            Log.LogDebugMessage("  ExtraArgs: {0}", ExtraArgs);
            Log.LogDebugMessage("  CreatePackagePerAbi: {0}", CreatePackagePerAbi);
            Log.LogDebugMessage("  ResourceNameCaseMap: {0}", ResourceNameCaseMap);
            Log.LogDebugMessage("  VersionCodePattern: {0}", VersionCodePattern);
            Log.LogDebugMessage("  VersionCodeProperties: {0}", VersionCodeProperties);
            if (CreatePackagePerAbi)
            {
                Log.LogDebugMessage("  SupportedAbis: {0}", SupportedAbis);
            }

            if (ResourceNameCaseMap != null)
            {
                foreach (var arr in ResourceNameCaseMap.Split(';').Select(l => l.Split('|')).Where(a => a.Length == 2))
                {
                    resource_name_case_map [arr [1]] = arr [0];                     // lowercase -> original
                }
            }
            assemblyMap.Load(AssemblyIdentityMapFile);

            ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
                CancellationToken = Token,
                TaskScheduler     = ThreadingTasks.TaskScheduler.Current,
            };

            ThreadingTasks.Parallel.ForEach(ManifestFiles, options, () => 0, DoExecute, (obj) => { Complete(); });

            base.Execute();

            return(!Log.HasLoggedErrors);
        }
        bool RunParallelAotCompiler(List <string> nativeLibs)
        {
            CancellationTokenSource cts = null;

            try {
                cts = new CancellationTokenSource();

                ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
                    CancellationToken = cts.Token,
                    TaskScheduler     = ThreadingTasks.TaskScheduler.Current,
                };

                ThreadingTasks.Parallel.ForEach(GetAotConfigs(), options,
                                                config => {
                    if (!config.Valid)
                    {
                        cts.Cancel();
                        return;
                    }

                    if (!RunAotCompiler(config.AssembliesPath, config.AotCompiler, config.AotOptions, config.AssemblyPath, cts.Token))
                    {
                        LogCodedError("XA3001", "Could not AOT the assembly: {0}", Path.GetFileName(config.AssemblyPath));
                        cts.Cancel();
                        return;
                    }

                    lock (nativeLibs)
                        nativeLibs.Add(config.OutputFile);
                }
                                                );
            } catch (OperationCanceledException) {
                return(false);
            } finally {
                if (cts != null)
                {
                    cts.Dispose();
                }
            }

            return(true);
        }
        public override bool Execute()
        {
            LogDebugMessage("Crunch Task");
            LogDebugTaskItems("  SourceFiles:", SourceFiles);
            // copy the changed files over to a temp location for processing
            var imageFiles = SourceFiles.Where(x => string.Equals(Path.GetExtension(x.ItemSpec), ".png", StringComparison.OrdinalIgnoreCase));

            if (!imageFiles.Any())
            {
                return(true);
            }

            ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
                CancellationToken = Token,
                TaskScheduler     = ThreadingTasks.TaskScheduler.Current,
            };

            var imageGroups = imageFiles.GroupBy(x => Path.GetDirectoryName(Path.GetFullPath(x.ItemSpec)));

            ThreadingTasks.Parallel.ForEach(imageGroups, options, () => 0, DoExecute, (obj) => { Complete(); });

            return(!Log.HasLoggedErrors);
        }
Beispiel #39
0
        public static Result Decide(Matrix aProposition, int aSecondsTimeout)
        {
            string lPositiveInput;
            string lNegativeInput;

            try
            {
                lPositiveInput = aProposition.Prover9Input;
                lNegativeInput = Factory.Not(aProposition).Prover9Input;
            }
            catch (EngineException lException)
            {
                if (lException.Message.Contains("modal"))
                {
                    return(Result.NoDecision);
                }
                else
                {
                    throw lException;
                }
            }

            bool lPositiveCounterexampleFound      = false;
            bool lNegativeCounterexampleFound      = false;
            bool lFoundThatPropositionIsNecessary  = false;
            bool lFoundThatPropositionIsImpossible = false;

            // Time out value for the cancellation tokens. -1 signifies no time out.
            int aMillisecondsTimeout = aSecondsTimeout >= 0
        ? aSecondsTimeout * 1000
        : -1;

            // Time out argument for prover9 and mace4.  Leave blank if the time out is negative.
            string aTimeoutArgument = aSecondsTimeout >= 0
        ? string.Format("-t {0}", aSecondsTimeout)
        : "";

            CancellationTokenSource lPositiveCaseTokenSource = new System.Threading.CancellationTokenSource(aMillisecondsTimeout);
            CancellationTokenSource lNegativeCaseTokenSource = new System.Threading.CancellationTokenSource(aMillisecondsTimeout);

            ParallelOptions lParallelOptions = new System.Threading.Tasks.ParallelOptions();

            lParallelOptions.MaxDegreeOfParallelism = System.Environment.ProcessorCount;

            Parallel.Invoke(
                lParallelOptions,
                // Try to prove that the proposition is necessary.
                BackgroundProcesses.ExcuteUntilExitOrCancellation(
                    lPositiveCaseTokenSource.Token,
                    new ShellParameters(prover9, aTimeoutArgument, lPositiveInput),
                    // If the proposition is proven, set a flag and cancel all other operations.
                    (fOutput) => {
                if (TheoremProved.IsMatch(fOutput))
                {
                    lFoundThatPropositionIsNecessary = true;
                    lPositiveCaseTokenSource.Cancel();
                    lNegativeCaseTokenSource.Cancel();
                }
            }),
                // Try to prove that the proposition is impossible.
                BackgroundProcesses.ExcuteUntilExitOrCancellation(
                    lNegativeCaseTokenSource.Token,
                    new ShellParameters(prover9, aTimeoutArgument, lNegativeInput),
                    // If the negation of the proposition is proven, set a flag and cancel all other operations.
                    (fOutput) => {
                if (TheoremProved.IsMatch(fOutput))
                {
                    lFoundThatPropositionIsImpossible = true;
                    lPositiveCaseTokenSource.Cancel();
                    lNegativeCaseTokenSource.Cancel();
                }
            }),
                // Try to find a counterexample for the proposition.
                BackgroundProcesses.ExcuteUntilExitOrCancellation(
                    lPositiveCaseTokenSource.Token,
                    new ShellParameters(mace4, aTimeoutArgument, lPositiveInput),
                    // If a counterexample is found for the positive case,
                    // set a flag and cancel the attempt to prove that the proposition is necessary.
                    (fOutput) => {
                if (CounterexampleFound.IsMatch(fOutput))
                {
                    lPositiveCounterexampleFound = true;
                    lPositiveCaseTokenSource.Cancel();
                }
            }),
                // Try to find a counterexample to the negative case.
                BackgroundProcesses.ExcuteUntilExitOrCancellation(
                    lNegativeCaseTokenSource.Token,
                    new ShellParameters(mace4, aTimeoutArgument, lNegativeInput),
                    // If a counterexample is found for the negative case,
                    // set a flag and cancel the attempt to prove that the proposition is impossible.
                    (fOutput) => {
                if (CounterexampleFound.IsMatch(fOutput))
                {
                    lNegativeCounterexampleFound = true;
                    lNegativeCaseTokenSource.Cancel();
                }
            })
                );

            if (lPositiveCounterexampleFound && lFoundThatPropositionIsNecessary)
            {
                throw new EngineException("Inconsistent result!");
            }

            if (lNegativeCounterexampleFound && lFoundThatPropositionIsImpossible)
            {
                throw new EngineException("Inconsistent result!");
            }

            if (lFoundThatPropositionIsNecessary && lFoundThatPropositionIsImpossible)
            {
                throw new EngineException("Inconsistent result!");
            }

            if (lFoundThatPropositionIsNecessary)
            {
                return(Result.Necessary);
            }

            if (lFoundThatPropositionIsImpossible)
            {
                return(Result.Impossible);
            }

            if (lPositiveCounterexampleFound && lNegativeCounterexampleFound)
            {
                return(Result.Contingent);
            }

            if (lPositiveCounterexampleFound)
            {
                return(Result.Unnecessary);
            }

            if (lNegativeCounterexampleFound)
            {
                return(Result.Possible);
            }

            return(Result.NoDecision);
        }
Beispiel #40
0
 private TaskReplicator(ParallelOptions options, bool stopOnFirstFailure)
 {
     _scheduler          = options.TaskScheduler ?? TaskScheduler.Current;
     _stopOnFirstFailure = stopOnFirstFailure;
 }
Beispiel #41
0
        bool Generate(BindingGenerator generator)
        {
            var         layoutGroups = new Dictionary <string, LayoutGroup> (StringComparer.Ordinal);
            string      layoutGroupName;
            LayoutGroup group;

            foreach (ITaskItem item in ResourceFiles)
            {
                if (item == null)
                {
                    continue;
                }

                if (!GetRequiredMetadata(item, CalculateLayoutCodeBehind.LayoutGroupMetadata, out layoutGroupName))
                {
                    return(true);                    // We need Complete() to be called by Execute above
                }
                if (!layoutGroups.TryGetValue(layoutGroupName, out group) || group == null)
                {
                    group = new LayoutGroup {
                        Items = new List <ITaskItem> ()
                    };
                    layoutGroups [layoutGroupName] = group;
                }
                group.Items.Add(item);
            }

            if (PartialClassFiles != null && PartialClassFiles.Length > 0)
            {
                foreach (ITaskItem item in PartialClassFiles)
                {
                    if (!GetRequiredMetadata(item, CalculateLayoutCodeBehind.LayoutGroupMetadata, out layoutGroupName))
                    {
                        return(true);
                    }
                    if (!layoutGroups.TryGetValue(layoutGroupName, out group) || group == null)
                    {
                        Log.LogError($"Partial class item without associated binding for layout group '{layoutGroupName}'");
                        return(true);
                    }

                    string partialClassName;
                    if (!GetRequiredMetadata(item, CalculateLayoutCodeBehind.PartialCodeBehindClassNameMetadata, out partialClassName))
                    {
                        return(true);
                    }

                    string outputFileName;
                    if (!GetRequiredMetadata(item, CalculateLayoutCodeBehind.LayoutPartialClassFileNameMetadata, out outputFileName))
                    {
                        return(true);
                    }

                    if (group.Classes == null)
                    {
                        group.Classes = new List <PartialClass> ();
                    }
                    if (group.ClassNames == null)
                    {
                        group.ClassNames = new HashSet <string> ();
                    }

                    if (group.ClassNames.Contains(partialClassName))
                    {
                        continue;
                    }

                    string shortName;
                    string namespaceName;
                    int    idx = partialClassName.LastIndexOf('.');
                    if (idx >= 0)
                    {
                        shortName     = partialClassName.Substring(idx + 1);
                        namespaceName = partialClassName.Substring(0, idx);
                    }
                    else
                    {
                        shortName     = partialClassName;
                        namespaceName = null;
                    }

                    group.Classes.Add(new PartialClass {
                        Name           = shortName,
                        Namespace      = namespaceName,
                        OutputFilePath = Path.Combine(MonoAndroidCodeBehindDir, outputFileName)
                    });
                    group.ClassNames.Add(partialClassName);
                }
            }

            IEnumerable <string> generatedFilePaths = null;
            bool needComplete = false;

            if (ResourceFiles.Length >= CalculateLayoutCodeBehind.ParallelGenerationThreshold)
            {
                // NOTE: Update the tests in $TOP_DIR/tests/CodeBehind/UnitTests/BuildTests.cs if this message
                // is changed!
                Log.LogDebugMessage($"Generating binding code in parallel (threshold of {CalculateLayoutCodeBehind.ParallelGenerationThreshold} layouts met)");
                var fileSet = new ConcurrentBag <string> ();
                TPL.ParallelOptions options = new TPL.ParallelOptions {
                    CancellationToken = Token,
                    TaskScheduler     = TPL.TaskScheduler.Default,
                };
                TPL.Task.Factory.StartNew(
                    () => TPL.Parallel.ForEach(layoutGroups, options, kvp => GenerateSourceForLayoutGroup(generator, kvp.Value, rpath => fileSet.Add(rpath))),
                    Token,
                    TPL.TaskCreationOptions.None,
                    TPL.TaskScheduler.Default
                    ).ContinueWith(t => Complete());
                generatedFilePaths = fileSet;
            }
            else
            {
                var fileSet = new List <string> ();
                foreach (var kvp in layoutGroups)
                {
                    GenerateSourceForLayoutGroup(generator, kvp.Value, rpath => fileSet.Add(rpath));
                }
                generatedFilePaths = fileSet;
                needComplete       = true;
            }

            GeneratedFiles = generatedFilePaths.Where(gfp => !String.IsNullOrEmpty(gfp)).Select(gfp => new TaskItem(gfp)).ToArray();
            if (GeneratedFiles.Length == 0)
            {
                Log.LogWarning("No layout binding source files generated");
            }
            Log.LogDebugTaskItems("  GeneratedFiles:", GeneratedFiles);
            return(needComplete);
        }
Beispiel #42
0
        public override bool Execute()
        {
            Log.LogDebugMessage("CalculateLayoutCodeBehind Task");
            Log.LogDebugMessage($"  OutputLanguage: {OutputLanguage}");
            Log.LogDebugMessage($"  OutputFileExtension: {OutputFileExtension}");
            Log.LogDebugMessage($"  BaseNamespace: {BaseNamespace}");
            Log.LogDebugMessage($"  BindingDependenciesCacheFile: {BindingDependenciesCacheFile}");
            Log.LogDebugTaskItems("  BoundLayouts:", BoundLayouts);

            widgetWithId = XPathExpression.Compile("//*[@android:id and string-length(@android:id) != 0] | //include[not(@android:id)]");

            GenerateLayoutBindings.BindingGeneratorLanguage gen;
            if (!GenerateLayoutBindings.KnownBindingGenerators.TryGetValue(OutputLanguage, out gen) || gen == null)
            {
                Log.LogDebugMessage($"Language {OutputLanguage} isn't supported, will use {GenerateLayoutBindings.DefaultOutputGenerator.Name} instead");
                sourceFileExtension = GenerateLayoutBindings.DefaultOutputGenerator.Extension;
            }
            else
            {
                sourceFileExtension = OutputFileExtension;
            }

            string partialClassNames = null;
            var    layoutsByName     = new Dictionary <string, LayoutGroup> (StringComparer.OrdinalIgnoreCase);

            foreach (ITaskItem item in BoundLayouts)
            {
                if (item == null)
                {
                    continue;
                }

                AddLayoutFile(item, ref layoutsByName);
            }

            var layoutBindingFiles      = new List <ITaskItem> ();
            var layoutPartialClassFiles = new List <ITaskItem> ();

            if (layoutsByName.Count >= ParallelGenerationThreshold)
            {
                // NOTE: Update the tests in $TOP_DIR/tests/CodeBehind/UnitTests/BuildTests.cs if this message
                // is changed!
                Log.LogDebugMessage($"Parsing layouts in parallel (threshold of {ParallelGenerationThreshold} layouts met)");

                var cts = new CancellationTokenSource();
                TPL.ParallelOptions options = new TPL.ParallelOptions {
                    CancellationToken = cts.Token,
                    TaskScheduler     = TPL.TaskScheduler.Default,
                };
                TPL.Task.Factory.StartNew(
                    () => TPL.Parallel.ForEach(layoutsByName, options, kvp => ParseAndLoadGroup(layoutsByName, kvp.Key, kvp.Value.InputItems, ref kvp.Value.LayoutBindingItems, ref kvp.Value.LayoutPartialClassItems)),
                    cts.Token,
                    TPL.TaskCreationOptions.None,
                    TPL.TaskScheduler.Default
                    ).ContinueWith(t => Complete());

                base.Execute();

                foreach (var kvp in layoutsByName)
                {
                    LayoutGroup group = kvp.Value;
                    if (group == null)
                    {
                        continue;
                    }
                    if (group.LayoutBindingItems != null && group.LayoutBindingItems.Count > 0)
                    {
                        layoutBindingFiles.AddRange(group.LayoutBindingItems);
                    }
                    if (group.LayoutPartialClassItems != null && group.LayoutPartialClassItems.Count > 0)
                    {
                        layoutPartialClassFiles.AddRange(group.LayoutPartialClassItems);
                    }
                }
            }
            else
            {
                foreach (var kvp in layoutsByName)
                {
                    ParseAndLoadGroup(layoutsByName, kvp.Key, kvp.Value.InputItems, ref layoutBindingFiles, ref layoutPartialClassFiles);
                }
                Complete();
            }

            LayoutBindingFiles = layoutBindingFiles.ToArray();
            if (LayoutBindingFiles.Length == 0)
            {
                Log.LogDebugMessage("  No layout file qualifies for code-behind generation");
            }
            LayoutPartialClassFiles = layoutPartialClassFiles.ToArray();

            Log.LogDebugTaskItems("  LayoutBindingFiles:", LayoutBindingFiles, true);
            Log.LogDebugTaskItems("  LayoutPartialClassFiles:", LayoutPartialClassFiles, true);

            return(!Log.HasLoggedErrors);
        }