Ejemplo n.º 1
0
 /// <summary>
 /// Executes a for loop with 64-bit indexes and thread-local data in which iterations
 /// may run in parallel, loop options can be configured, and the state of the loop
 /// can be monitored and manipulated.
 /// </summary>
 public static SystemParallelLoopResult For <TLocal>(long fromInclusive, long toExclusive,
                                                     SystemParallelOptions parallelOptions, Func <TLocal> localInit,
                                                     Func <long, SystemParallelLoopState, TLocal, TLocal> body, Action <TLocal> localFinally)
 {
     ExceptionProvider.ThrowUncontrolledInvocationException(nameof(SystemParallel.For));
     return(SystemParallel.For(fromInclusive, toExclusive, parallelOptions, localInit, body, localFinally));
 }
Ejemplo n.º 2
0
        public override bool Execute()
        {
            Log.LogTaskName("Codesign");
            Log.LogTaskProperty("CodesignAllocate", CodesignAllocate);
            Log.LogTaskProperty("DisableTimestamp", DisableTimestamp);
            Log.LogTaskProperty("Entitlements", Entitlements);
            Log.LogTaskProperty("Keychain", Keychain);
            Log.LogTaskProperty("Resources", Resources);
            Log.LogTaskProperty("ResourceRules", ResourceRules);
            Log.LogTaskProperty("SigningKey", SigningKey);
            Log.LogTaskProperty("ExtraArgs", ExtraArgs);
            Log.LogTaskProperty("IsAppExtension", IsAppExtension);

            if (Resources.Length == 0)
            {
                return(true);
            }

            Parallel.ForEach(Resources, new ParallelOptions {
                MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1)
            }, (item) => {
                Codesign(item);
            });

            return(!Log.HasLoggedErrors);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Executes a foreach operation with thread-local data on a <see cref="OrderablePartitioner{TSource}"/>
 /// in which iterations may run in parallel, loop options can be configured, and the state of the loop
 /// can be monitored and manipulated.
 /// </summary>
 public static SystemParallelLoopResult ForEach <TSource, TLocal>(OrderablePartitioner <TSource> source,
                                                                  Func <TLocal> localInit, Func <TSource, SystemParallelLoopState, long, TLocal, TLocal> body,
                                                                  Action <TLocal> localFinally)
 {
     ExceptionProvider.ThrowUncontrolledInvocationException(nameof(SystemParallel.ForEach));
     return(SystemParallel.ForEach(source, localInit, body, localFinally));
 }
        public override bool Execute()
        {
            if (!Directory.Exists(AppBundleDir))
            {
                return(true);
            }

            var subdirs = new List <string> ();
            var dylibs  = new List <string> ();

            foreach (var path in Directory.EnumerateFileSystemEntries(AppBundleDir))
            {
                if (Directory.Exists(path))
                {
                    var name = Path.GetFileName(path);

                    if (name != "PlugIns" && name != "Watch")
                    {
                        subdirs.Add(path);
                    }
                }
                else
                {
                    if (path.EndsWith(".metallib", StringComparison.Ordinal) || path.EndsWith(".dylib", StringComparison.Ordinal))
                    {
                        if (NeedsCodesign(path))
                        {
                            dylibs.Add(path);
                        }
                    }
                }
            }

            foreach (var subdir in subdirs)
            {
                foreach (var dylib in Directory.EnumerateFiles(subdir, "*.*", SearchOption.AllDirectories))
                {
                    if (dylib.EndsWith(".metallib", StringComparison.Ordinal) || dylib.EndsWith(".dylib", StringComparison.Ordinal))
                    {
                        if (NeedsCodesign(dylib))
                        {
                            dylibs.Add(dylib);
                        }
                    }
                }
            }

            if (dylibs.Count == 0)
            {
                return(true);
            }

            Parallel.ForEach(dylibs, new ParallelOptions {
                MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1)
            }, (dylib) => {
                Codesign(dylib);
            });

            return(!Log.HasLoggedErrors);
        }
Ejemplo n.º 5
0
 public void ReorderTasks(int projectId, int storyId, List <Task> tasks)
 {
     Parallel.ForEach(tasks, t =>
     {
         var request      = BuildPutRequest();
         request.Resource = string.Format(TaskEndpoint + "/{2}?task[position]={3}", t.ProjectId, t.StoryId, t.Id, t.Position);
         RestClient.ExecuteRequestWithChecks(request);
     });
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Executes a for loop in which iterations may run in parallel.
        /// </summary>
        public static SystemParallelLoopResult For(int fromInclusive, int toExclusive, Action <int> body)
        {
            if (CoyoteRuntime.IsExecutionControlled)
            {
                return(For(fromInclusive, toExclusive, new SystemParallelOptions(), body));
            }

            return(SystemParallel.For(fromInclusive, toExclusive, body));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Executes a foreach operation on a <see cref="System.Collections.IEnumerable"/>
        /// in which iterations may run in parallel.
        /// </summary>
        public static SystemParallelLoopResult ForEach <TSource>(IEnumerable <TSource> source, Action <TSource> body)
        {
            if (CoyoteRuntime.IsExecutionControlled)
            {
                return(ForEach(source, new SystemParallelOptions(), body));
            }

            return(SystemParallel.ForEach(source, body));
        }
 public void Run(int matrixDimension, Action <int, int> loopAction)
 {
     Tpl.For(0, matrixDimension, (int i) =>
     {
         Tpl.For(i + 1, matrixDimension, (int j) =>
         {
             loopAction(i, j);
         });
     });
 }
Ejemplo n.º 9
0
        public static void ComputeInParallel(IList <TData> baseData, Func <TData, TIntermediate> parallelWork, Action <TIntermediate> recombination, int numberOfPartitions)
        {
            var intermediateResults = new BlockingCollection <ProcessedPartition>();

            ThreadParallel.Invoke(() =>
            {
                try
                {
                    var partitionSize = (int)Math.Ceiling(baseData.Count / (float)numberOfPartitions);
                    ThreadParallel.ForEach(baseData.Select((d, i) => new TaggedBaseData(d, i)).GroupBy(d => d.TaskNumber / partitionSize), g =>
                    {
                        intermediateResults.Add(new ProcessedPartition(g.Select(d => parallelWork(d.Data)), g.Key));
                    });
                }
                finally
                {
                    intermediateResults.CompleteAdding();
                }
            }, () =>
            {
                int expecting = 0;
                var backlog   = new Dictionary <int, IEnumerable <TIntermediate> >();
                foreach (var group in intermediateResults.GetConsumingEnumerable())
                {
                    if (group.TaskNumber != expecting)
                    {
                        // if we are not ready for this yet add it to the backlog
                        backlog[group.TaskNumber] = group.ProcessedData;
                        continue;
                    }
                    IEnumerable <TIntermediate> toProcess = group.ProcessedData;
                    while (true)
                    {
                        foreach (var element in toProcess)
                        {
                            recombination(element);
                        }
                        expecting++;
                        // now see if we can combine with the backlog
                        if (!backlog.TryGetValue(expecting, out toProcess))
                        {
                            // if we can't find the next task wait for another
                            // task to finish
                            break;
                        }
                        else
                        {
                            backlog.Remove(expecting);
                        }
                    }
                }
            });
        }
Ejemplo n.º 10
0
        public static void ComputeInParallel(IList <TData> baseData, Func <TData, int, TIntermediate> parallelWork, Action <TIntermediate, int> recombination)
        {
            var intermediateResults = new BlockingCollection <TaggedIntermediate>();

            ThreadParallel.Invoke(() =>
            {
                try
                {
                    ThreadParallel.ForEach(baseData.Select((d, i) => new TaggedBaseData(d, i)), d =>
                    {
                        intermediateResults.Add(new TaggedIntermediate(parallelWork(d.Data, d.TaskNumber), d.TaskNumber));
                    });
                }
                finally
                {
                    intermediateResults.CompleteAdding();
                }
            }, () =>
            {
                int expecting = 0;
                var backlog   = new Dictionary <int, TIntermediate>();
                foreach (var newData in intermediateResults.GetConsumingEnumerable())
                {
                    if (newData.TaskNumber != expecting)
                    {
                        // if we are not ready for this yet add it to the backlog
                        backlog[newData.TaskNumber] = newData.ProcessedData;
                        continue;
                    }
                    TIntermediate toProcess = newData.ProcessedData;
                    while (true)
                    {
                        recombination(toProcess, expecting);
                        expecting++;
                        // now see if we can combine with the backlog
                        if (!backlog.TryGetValue(expecting, out toProcess))
                        {
                            // if we can't find the next task wait for another
                            // task to finish
                            break;
                        }
                        else
                        {
                            backlog.Remove(expecting);
                        }
                    }
                }
            });
        }
Ejemplo n.º 11
0
        public override bool Execute()
        {
            if (Resources.Length == 0)
            {
                return(true);
            }

            Parallel.ForEach(Resources, new ParallelOptions {
                MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1)
            }, (item) => {
                Codesign(item);
            });

            return(!Log.HasLoggedErrors);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Executes a for loop in which iterations may run in parallel and loop options
        /// can be configured.
        /// </summary>
        public static SystemParallelLoopResult For(int fromInclusive, int toExclusive,
                                                   SystemParallelOptions parallelOptions, Action <int> body)
        {
            if (CoyoteRuntime.IsExecutionControlled)
            {
                return(SystemParallel.For(fromInclusive, toExclusive, new SystemParallelOptions()
                {
                    CancellationToken = parallelOptions.CancellationToken,
                    MaxDegreeOfParallelism = MaxDegreeOfParallelism,
                    TaskScheduler = CoyoteRuntime.Current.ControlledTaskScheduler
                }, body));
            }

            return(SystemParallel.For(fromInclusive, toExclusive, parallelOptions, body));
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Executes a foreach operation on a <see cref="System.Collections.IEnumerable"/>
        /// in which iterations may run in parallel and loop options can be configured.
        /// </summary>
        public static SystemParallelLoopResult ForEach <TSource>(IEnumerable <TSource> source,
                                                                 SystemParallelOptions parallelOptions, Action <TSource> body)
        {
            if (CoyoteRuntime.IsExecutionControlled)
            {
                return(SystemParallel.ForEach(source, new SystemParallelOptions()
                {
                    CancellationToken = parallelOptions.CancellationToken,
                    MaxDegreeOfParallelism = MaxDegreeOfParallelism,
                    TaskScheduler = CoyoteRuntime.Current.ControlledTaskScheduler
                }, body));
            }

            return(SystemParallel.ForEach(source, parallelOptions, body));
        }
Ejemplo n.º 14
0
        public override bool Execute()
        {
            Log.LogMessage(MessageImportance.Normal, $"Frameworks: {string.Join(", ", Frameworks.Select(c => c.ItemSpec))}"); // debug

            var dependencies = new SafeList <Dependency>();

            void AddRange(IEnumerable <string> toAdd)
            {
                foreach (var dependency in toAdd)
                {
                    if (dependencies.AddIfNotFound(c => c.Dylib == dependency, () => new Dependency(dependency)))
                    {
                        Log.LogMessage(MessageImportance.Normal, $"Swift Dependency Found: {dependency}");
                    }
                }
            }

            Parallel.ForEach(Frameworks.Select(c => c.ItemSpec), (lib) =>
            {
                AddRange(ScanForDependenciesOnFrameworks(lib));
            });

            // we could precalculate this one and publish with a
            // know dependencies list for Swift dylibs
            // but that means updating the list everytime a new version
            // is released and supporting multiple versions.
            // Update: Windows version is doing it.
            while (dependencies.Any(c => c.Pending))
            {
                var verifyList = from c in dependencies
                                 where c.Pending
                                 select c;

                Parallel.ForEach(verifyList.ToList(), (dependency) =>
                {
                    AddRange(ScanForDependenciesOnSwift(dependency.Dylib));

                    dependency.MarkAsScanned();
                });
            }

            SwiftDependencies = dependencies
                                .Select(c => new TaskItem(c.Dylib))
                                .ToArray();

            return(true);
        }
Ejemplo n.º 15
0
        public override bool Execute()
        {
            var arcs           = MtouchArch.Split(',').Select(c => c.Trim().ToLower()).ToList(); // lipo uses lower case
            var availableArchs = AvailableArchs();

            Log.LogMessage(MessageImportance.Normal, $"Copying: {string.Join(", ", Resources.Select(c => c.ItemSpec))}");
            Log.LogMessage(MessageImportance.Normal, $"Swift Arcs Needed: {MtouchArch}");
            Log.LogMessage(MessageImportance.Normal, $"Swift Arcs Available: {string.Join(", ", availableArchs)}");

            var args      = GetLipoArgs(availableArchs);
            var xcodePath = GetRuntimePath();

            Parallel.ForEach(Resources.Select(c => c.ItemSpec), (dylib) =>
            {
                Log.LogMessage(MessageImportance.Normal, $"Copying: {dylib}");
                RunLipo($"'{Path.Combine(xcodePath, dylib)}' {args} '{GetOutputPath(dylib)}'");
            });

            return(true);
        }
Ejemplo n.º 16
0
        public override bool Execute()
        {
            if (Resources.Length == 0)
            {
                return(true);
            }

            var codesignedFiles = new List <ITaskItem> ();

            Parallel.ForEach(Resources, new ParallelOptions {
                MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1)
            }, (item) => {
                Codesign(item);

                codesignedFiles.AddRange(GetCodesignedFiles(item));
            });

            CodesignedFiles = codesignedFiles.ToArray();

            return(!Log.HasLoggedErrors);
        }
Ejemplo n.º 17
0
        public List <string> GetDropItemByLevel(int level)
        {
            var itemLists = itemLevelDic.AsParallel().Where(pair => { return(pair.Key >= 0 && pair.Key <= level); }).OrderBy(pair => pair.Key);

            Parallel.ForEach(itemDataDict, pair =>
            {
            });

            int           needItemCout = 3;
            List <string> retItemList  = new List <string>();

            foreach (var itemList in itemLists)
            {
                retItemList.AddRange(itemList.Value);
                if (retItemList.Count >= needItemCout)
                {
                    break;
                }
            }

            return(retItemList);
        }
        public override bool Execute()
        {
            var arcs           = MtouchArch.Split(',').Select(c => c.Trim().ToLower()).ToList(); // lipo uses lower case
            var availableArchs = AvailableArchs();

            Log.LogMessage(MessageImportance.Normal, $"Copying: {string.Join(", ", Resources.Select(c => c.ItemSpec))}");
            Log.LogMessage(MessageImportance.Normal, $"Swift Arcs Needed: {MtouchArch}");
            Log.LogMessage(MessageImportance.Normal, $"Swift Arcs Available: {string.Join(", ", availableArchs)}");

            var archsToRemove = availableArchs.Except(arcs).Where(c => !string.IsNullOrWhiteSpace(c)).ToArray();

            StringBuilder argsBuilder = new StringBuilder();

            if (archsToRemove.Length == 0)
            {
                argsBuilder.Append(" -create -output");
            }
            else
            {
                foreach (var arch in archsToRemove)
                {
                    argsBuilder.Append($" -remove {arch}");
                }

                argsBuilder.Append(" -output");
            }

            var args      = argsBuilder.ToString();
            var xcodePath = GetRuntimePath();

            Parallel.ForEach(Resources.Select(c => c.ItemSpec), (dylib) =>
            {
                Log.LogMessage(MessageImportance.Normal, $"Copying: {dylib}");
                RunLipo($"{Path.Combine(xcodePath, dylib)} {args} {GetOutputPath(dylib)}");
            });

            return(true);
        }
Ejemplo n.º 19
0
        private bool IsWhiteRectangle(Bitmap image, IReadOnlyList <IntPoint> rectanglePoints)
        {
            BitmapData data =
                image.LockBits(
                    new Rectangle(rectanglePoints[0].X, rectanglePoints[0].Y,
                                  rectanglePoints[1].X - rectanglePoints[0].X,
                                  rectanglePoints[3].Y - rectanglePoints[0].Y),
                    ImageLockMode.ReadWrite, image.PixelFormat);

            var bytesPerPixel    = Image.GetPixelFormatSize(image.PixelFormat) / 8;
            var isWhiteRectangle = true;

            unsafe
            {
                //Nr. de pixeli pe linie * bytesPerPixel
                int   width      = (data.Width) * bytesPerPixel;
                byte *firstPixel = (byte *)data.Scan0;
                Parallel.For(0, data.Height + 1, (y, state) =>
                {
                    byte *currentLine = firstPixel + y * data.Stride;
                    for (int x = 0; x < width; x += bytesPerPixel)
                    {
                        currentLine[x]     = 0;
                        currentLine[x + 1] = 0;
                        currentLine[x + 2] = 255;
                        if (currentLine[x] != 255 && currentLine[x + 1] != 255 && currentLine[x + 2] != 255)
                        {
                            isWhiteRectangle = false;
                            state.Break();
                        }
                    }
                });
            }
            image.UnlockBits(data);
            return(isWhiteRectangle);
        }
Ejemplo n.º 20
0
        public override bool Execute()
        {
            Log.LogTaskName("CodesignNativeLibraries");
            Log.LogTaskProperty("AppBundleDir", AppBundleDir);
            Log.LogTaskProperty("CodesignAllocate", CodesignAllocate);
            Log.LogTaskProperty("DisableTimestamp", DisableTimestamp);
            Log.LogTaskProperty("IntermediateOutputPath", IntermediateOutputPath);
            Log.LogTaskProperty("Keychain", Keychain);
            Log.LogTaskProperty("SigningKey", SigningKey);
            Log.LogTaskProperty("ExtraArgs", ExtraArgs);

            if (!Directory.Exists(AppBundleDir))
            {
                return(true);
            }

            var subdirs = new List <string> ();
            var dylibs  = new List <string> ();

            foreach (var path in Directory.EnumerateFileSystemEntries(AppBundleDir))
            {
                if (Directory.Exists(path))
                {
                    var name = Path.GetFileName(path);

                    if (name != "PlugIns" && name != "Watch")
                    {
                        subdirs.Add(path);
                    }
                }
                else
                {
                    if (path.EndsWith(".metallib", StringComparison.Ordinal) || path.EndsWith(".dylib", StringComparison.Ordinal))
                    {
                        if (NeedsCodesign(path))
                        {
                            dylibs.Add(path);
                        }
                    }
                }
            }

            foreach (var subdir in subdirs)
            {
                foreach (var dylib in Directory.EnumerateFiles(subdir, "*.*", SearchOption.AllDirectories))
                {
                    if (dylib.EndsWith(".metallib", StringComparison.Ordinal) || dylib.EndsWith(".dylib", StringComparison.Ordinal))
                    {
                        if (NeedsCodesign(dylib))
                        {
                            dylibs.Add(dylib);
                        }
                    }
                }
            }

            if (dylibs.Count == 0)
            {
                return(true);
            }

            Parallel.ForEach(dylibs, new ParallelOptions {
                MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1)
            }, (dylib) => {
                Codesign(dylib);
            });

            return(!Log.HasLoggedErrors);
        }
Ejemplo n.º 21
0
        private void split(DecisionNode root, int[][] input, int[] output)
        {

            // 2. If all examples are for the same class, return the single-node
            //    tree with the output label corresponding to this common class.
            double entropy = Statistics.Tools.Entropy(output, outputClasses);

            if (entropy == 0)
            {
                if (output.Length > 0)
                    root.Output = output[0];
                return;
            }

            // 3. If number of predicting attributes is empty, then return the single-node
            //    tree with the output label corresponding to the most common value of
            //    the target attributes in the examples.
            int predictors = attributes.Count(x => x == false);

            if (predictors <= attributes.Length - maxHeight)
            {
                root.Output = Statistics.Tools.Mode(output);
                return;
            }


            // 4. Otherwise, try to select the attribute which
            //    best explains the data sample subset.

            double[] scores = new double[predictors];
            double[] entropies = new double[predictors];
            int[][][] partitions = new int[predictors][][];

            // Retrieve candidate attribute indices
            int[] candidates = new int[predictors];
            for (int i = 0, k = 0; i < attributes.Length; i++)
                if (!attributes[i]) candidates[k++] = i;


            // For each attribute in the data set
#if SERIAL
            for (int i = 0; i < scores.Length; i++)
#else
            Parallel.For(0, scores.Length, i =>
#endif
            {
                scores[i] = computeGainRatio(input, output, candidates[i],
                    entropy, out partitions[i]);
            }
#if !SERIAL
);
#endif

            // Select the attribute with maximum gain ratio
            int maxGainIndex; scores.Max(out maxGainIndex);
            var maxGainPartition = partitions[maxGainIndex];
            var maxGainEntropy = entropies[maxGainIndex];
            var maxGainAttribute = candidates[maxGainIndex];
            var maxGainRange = inputRanges[maxGainAttribute];

            attributes[maxGainAttribute] = true;

            // Now, create next nodes and pass those partitions as their responsibilities.
            DecisionNode[] children = new DecisionNode[maxGainPartition.Length];

            for (int i = 0; i < children.Length; i++)
            {
                children[i] = new DecisionNode(tree);
                children[i].Parent = root;
                children[i].Comparison = ComparisonKind.Equal;
                children[i].Value = i + maxGainRange.Min;


                int[][] inputSubset = input.Submatrix(maxGainPartition[i]);
                int[] outputSubset = output.Submatrix(maxGainPartition[i]);

                split(children[i], inputSubset, outputSubset); // recursion

                if (children[i].IsLeaf)
                {
                    // If the resulting node is a leaf, and it has not
                    // been assigned a value because there were no available
                    // output samples in this category, we will be assigning
                    // the most common label for the current node to it.
                    if (!Rejection && !children[i].Output.HasValue)
                        children[i].Output = Statistics.Tools.Mode(output);
                }
            }


            attributes[maxGainAttribute] = false;

            root.Branches.AttributeIndex = maxGainAttribute;
            root.Branches.AddRange(children);
        }
Ejemplo n.º 22
0
        private void split(DecisionNode root, int[][] input, int[] output)
        {
            // 2. If all examples are for the same class, return the single-node
            //    tree with the output label corresponding to this common class.
            double entropy = Statistics.Tools.Entropy(output, outputClasses);

            if (entropy == 0)
            {
                if (output.Length > 0)
                {
                    root.Output = output[0];
                }
                return;
            }

            // 3. If number of predicting attributes is empty, then return the single-node
            //    tree with the output label corresponding to the most common value of
            //    the target attributes in the examples.
            int predictors = attributes.Count(x => x == false);

            if (predictors == 0)
            {
                root.Output = Statistics.Tools.Mode(output);
                return;
            }


            // 4. Otherwise, try to select the attribute which
            //    best explains the data sample subset.

            double[]  scores     = new double[predictors];
            double[]  entropies  = new double[predictors];
            int[][][] partitions = new int[predictors][][];

            // Retrieve candidate attribute indices
            int[] candidates = new int[predictors];
            for (int i = 0, k = 0; i < attributes.Length; i++)
            {
                if (!attributes[i])
                {
                    candidates[k++] = i;
                }
            }


            // For each attribute in the data set
            Parallel.For(0, scores.Length, i =>
            {
                scores[i] = computeGainRatio(input, output, candidates[i],
                                             entropy, out partitions[i]);
            });

            // Select the attribute with maximum gain ratio
            int maxGainIndex; scores.Max(out maxGainIndex);
            var maxGainPartition = partitions[maxGainIndex];
            var maxGainEntropy   = entropies[maxGainIndex];
            var maxGainAttribute = candidates[maxGainIndex];
            var maxGainRange     = inputRanges[maxGainAttribute];

            attributes[maxGainAttribute] = true;

            // Now, create next nodes and pass those partitions as their responsabilities.
            DecisionNode[] children = new DecisionNode[maxGainPartition.Length];

            for (int i = 0; i < children.Length; i++)
            {
                children[i]            = new DecisionNode(tree);
                children[i].Parent     = root;
                children[i].Comparison = ComparisonKind.Equal;
                children[i].Value      = i + maxGainRange.Min;


                int[][] inputSubset  = input.Submatrix(maxGainPartition[i]);
                int[]   outputSubset = output.Submatrix(maxGainPartition[i]);

                split(children[i], inputSubset, outputSubset); // recursion
            }

            attributes[maxGainAttribute] = false;

            root.Branches = new DecisionBranchNodeCollection(maxGainAttribute, children);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Executes a foreach operation on a <see cref="System.Collections.IEnumerable"/> in which iterations
 /// may run in parallel, and the state of the loop can be monitored and manipulated.
 /// </summary>
 public static SystemParallelLoopResult ForEach <TSource>(IEnumerable <TSource> source,
                                                          Action <TSource, SystemParallelLoopState> body)
 {
     ExceptionProvider.ThrowUncontrolledInvocationException(nameof(SystemParallel.ForEach));
     return(SystemParallel.ForEach(source, body));
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Executes a foreach operation on a <see cref="OrderablePartitioner{TSource}"/>
 /// in which iterations may run in parallel, loop options can be configured, and
 /// the state of the loop can be monitored and manipulated.
 /// </summary>
 public static SystemParallelLoopResult ForEach <TSource>(OrderablePartitioner <TSource> source,
                                                          SystemParallelOptions parallelOptions, Action <TSource, SystemParallelLoopState, long> body)
 {
     ExceptionProvider.ThrowUncontrolledInvocationException(nameof(SystemParallel.ForEach));
     return(SystemParallel.ForEach(source, parallelOptions, body));
 }
        private void WriteFilesAsync(List <FileToWrite> files)
        {
            var project = new ProjectFile(files, AddNewFilesToProject, Tfs);

            if (UseTfsToCheckoutFiles)
            {
                DisplayMessage("Creating Required Directories");
                foreach (var dir in files.Select(f => f.Directory).Distinct())
                {
                    if (dir != null && !Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }
                }
                DisplayMessage("Getting Latest from TFS");
                // Get Latest Version of all files
                foreach (var batch in files.Select(f => f.Path).Batch(50))
                {
                    Tfs.Get(true, batch.ToArray());
                }

                if (Debugger.IsAttached)
                {
                    DisplayMessage("Creating Temporary Files");
                    foreach (var file in files)
                    {
                        WriteFilesForTfs(project, file);
                    }
                    CheckoutChangedFiles(files);
                    DisplayMessage("Updating Changed Files");
                    foreach (var file in files)
                    {
                        CopyChangedFiles(file);
                    }
                }
                else
                {
                    DisplayMessage("Creating Temporary Files");
                    Parallel.ForEach(files, f => WriteFilesForTfs(project, f));
                    CheckoutChangedFiles(files);
                    DisplayMessage("Updating Changed Files");
                    Parallel.ForEach(files, CopyChangedFiles);
                }
            }
            else
            {
                if (Debugger.IsAttached)
                {
                    foreach (var file in files)
                    {
                        WriteFileIfDifferent(project, file);
                    }
                }
                else
                {
                    Parallel.ForEach(files, f => WriteFileIfDifferent(project, f));
                }
            }

            if (AddNewFilesToProject && !project.ProjectFound)
            {
                Log("Unable to find a Project file to add newly created files to.  Either output the files into a directory that has a single project in it's path, or uncheck the \"Add New Files to Project\" Setting");
            }

            if (project.ProjectUpdated)
            {
                WriteFileIfDifferent(new ProjectFile(null, false, null), new FileToWrite(project.ProjectPath, project.GetContents()));
            }
        }
Ejemplo n.º 26
0
 /// <summary>
 /// Executes each of the provided actions, possibly in parallel, unless the operation is cancelled by the user.
 /// </summary>
 public static void Invoke(SystemParallelOptions parallelOptions, params Action[] actions)
 {
     ExceptionProvider.ThrowUncontrolledInvocationException(nameof(SystemParallel.Invoke));
     SystemParallel.Invoke(parallelOptions, actions);
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Executes a for loop in which iterations may run in parallel, loop options can
 /// be configured, and the state of the loop can be monitored and manipulated.
 /// </summary>
 public static SystemParallelLoopResult For(int fromInclusive, int toExclusive,
                                            SystemParallelOptions parallelOptions, Action <int, SystemParallelLoopState> body)
 {
     ExceptionProvider.ThrowUncontrolledInvocationException(nameof(SystemParallel.For));
     return(SystemParallel.For(fromInclusive, toExclusive, parallelOptions, body));
 }
Ejemplo n.º 28
0
        private void split(DecisionNode root, double[][] input, int[] output, int height)
        {

            // 2. If all examples are for the same class, return the single-node
            //    tree with the output label corresponding to this common class.
            double entropy = Statistics.Tools.Entropy(output, outputClasses);

            if (entropy == 0)
            {
                if (output.Length > 0)
                    root.Output = output[0];
                return;
            }

            // 3. If number of predicting attributes is empty, then return the single-node
            //    tree with the output label corresponding to the most common value of
            //    the target attributes in the examples.

            // how many variables have been used less than the limit
            int candidateCount = attributeUsageCount.Count(x => x < join);

            if (candidateCount == 0 || (maxHeight > 0 && height == maxHeight))
            {
                root.Output = Statistics.Tools.Mode(output);
                return;
            }


            // 4. Otherwise, try to select the attribute which
            //    best explains the data sample subset.

            double[] scores = new double[candidateCount];
            double[] thresholds = new double[candidateCount];
            int[][][] partitions = new int[candidateCount][][];

            // Retrieve candidate attribute indices
            int[] candidates = new int[candidateCount];
            for (int i = 0, k = 0; i < attributeUsageCount.Length; i++)
            {
                if (attributeUsageCount[i] < join)
                    candidates[k++] = i;
            }


            // For each attribute in the data set
#if SERIAL
            for (int i = 0; i < scores.Length; i++)
#else
            Parallel.For(0, scores.Length, i =>
#endif
            {
                scores[i] = computeGainRatio(input, output, candidates[i],
                    entropy, out partitions[i], out thresholds[i]);
            }
#if !SERIAL
);
#endif

            // Select the attribute with maximum gain ratio
            int maxGainIndex; scores.Max(out maxGainIndex);
            var maxGainPartition = partitions[maxGainIndex];
            var maxGainAttribute = candidates[maxGainIndex];
            var maxGainRange = inputRanges[maxGainAttribute];
            var maxGainThreshold = thresholds[maxGainIndex];

            // Mark this attribute as already used
            attributeUsageCount[maxGainAttribute]++;

            double[][] inputSubset;
            int[] outputSubset;

            // Now, create next nodes and pass those partitions as their responsibilities. 
            if (tree.Attributes[maxGainAttribute].Nature == DecisionVariableKind.Discrete)
            {
                // This is a discrete nature attribute. We will branch at each
                // possible value for the discrete variable and call recursion.
                DecisionNode[] children = new DecisionNode[maxGainPartition.Length];

                // Create a branch for each possible value
                for (int i = 0; i < children.Length; i++)
                {
                    children[i] = new DecisionNode(tree)
                    {
                        Parent = root,
                        Value = i + maxGainRange.Min,
                        Comparison = ComparisonKind.Equal,
                    };

                    inputSubset = input.Submatrix(maxGainPartition[i]);
                    outputSubset = output.Submatrix(maxGainPartition[i]);
                    split(children[i], inputSubset, outputSubset, height + 1); // recursion
                }

                root.Branches.AttributeIndex = maxGainAttribute;
                root.Branches.AddRange(children);
            }

            else if (maxGainPartition.Length > 1)
            {
                // This is a continuous nature attribute, and we achieved two partitions
                // using the partitioning scheme. We will branch on two possible settings:
                // either the value is greater than a currently detected optimal threshold 
                // or it is less.

                DecisionNode[] children = 
                {
                    new DecisionNode(tree) 
                    {
                        Parent = root, Value = maxGainThreshold,
                        Comparison = ComparisonKind.LessThanOrEqual 
                    },

                    new DecisionNode(tree)
                    {
                        Parent = root, Value = maxGainThreshold,
                        Comparison = ComparisonKind.GreaterThan
                    }
                };

                // Create a branch for lower values
                inputSubset = input.Submatrix(maxGainPartition[0]);
                outputSubset = output.Submatrix(maxGainPartition[0]);
                split(children[0], inputSubset, outputSubset, height + 1);

                // Create a branch for higher values
                inputSubset = input.Submatrix(maxGainPartition[1]);
                outputSubset = output.Submatrix(maxGainPartition[1]);
                split(children[1], inputSubset, outputSubset, height + 1);

                root.Branches.AttributeIndex = maxGainAttribute;
                root.Branches.AddRange(children);
            }
            else
            {
                // This is a continuous nature attribute, but all variables are equal
                // to a constant. If there is only a constant value as the predictor 
                // and there are multiple output labels associated with this constant
                // value, there isn't much we can do. This node will be a leaf.

                // We will set the class label for this node as the
                // majority of the currently selected output classes.

                outputSubset = output.Submatrix(maxGainPartition[0]);
                root.Output = Statistics.Tools.Mode(outputSubset);
            }

            attributeUsageCount[maxGainAttribute]--;
        }
Ejemplo n.º 29
0
        public override bool Execute()
        {
            if (Resources.Length == 0)
            {
                return(true);
            }

            var codesignedFiles = new List <ITaskItem> ();
            var resourcesToSign = Resources;

            // 1. Rewrite requests to sign executables inside frameworks to sign the framework itself
            //    signing a framework and a file inside a framework is not *always* identical
            //    on macOS apps {item.ItemSpec} can be a symlink to `Versions/Current/{item.ItemSpec}`
            //    and `Current` also a symlink to `A`... and `_CodeSignature` will be found there
            // 2. Resolve symlinks in the input.
            // 3. Make sure we're working with full paths.
            // All this makes it easier to sort and split the input files into buckets that can be codesigned together,
            // while also not codesigning directories before files inside them.
            foreach (var res in resourcesToSign)
            {
                var path   = res.ItemSpec;
                var parent = Path.GetDirectoryName(path);

                // so do not don't sign `A.framework/A`, sign `A.framework` which will always sign the *bundle*
                if (Path.GetExtension(parent) == ".framework" && Path.GetFileName(path) == Path.GetFileNameWithoutExtension(parent))
                {
                    path = parent;
                }

                path = PathUtils.ResolveSymbolicLinks(path);
                path = Path.GetFullPath(path);

                res.ItemSpec = path;
            }

            // first sort all the items by path length, longest path first.
            resourcesToSign = resourcesToSign.OrderBy(v => v.ItemSpec.Length).Reverse().ToArray();

            // remove items that are up-to-date
            for (var i = 0; i < resourcesToSign.Length; i++)
            {
                var item = resourcesToSign [i];
                if (!NeedsCodesign(resourcesToSign, i))
                {
                    resourcesToSign [i] = null;
                }
            }
            resourcesToSign = resourcesToSign.Where(v => v is not null).ToArray();

            // Then we need to split the input into buckets, where everything in a bucket can be signed in parallel
            // (i.e. no item in a bucket depends on any other item in the bucket being signed first).
            // any such items must go into a different bucket. The bucket themselves are also sorted, where
            // we have to sign the first bucket first, and so on.
            // Since we've sorted by path length, we know that if we find a directory, we won't find any containing
            // files from that directory later.
            var buckets = new List <List <ITaskItem> > ();

            for (var i = 0; i < resourcesToSign.Length; i++)
            {
                var res = resourcesToSign [i];
                // All files can go into the first bucket.
                if (File.Exists(res.ItemSpec))
                {
                    if (buckets.Count == 0)
                    {
                        buckets.Add(new List <ITaskItem> ());
                    }
                    var bucket = buckets [0];
                    bucket.Add(res);
                    continue;
                }

                if (Directory.Exists(res.ItemSpec))
                {
                    var dir = res.ItemSpec;

                    // Add the directory separator, so we can do easy substring matches
                    dir = EnsureEndsWithDirectorySeparator(dir);

                    // This is a directory, which can contain other files or directories that must be signed first
                    // If this item is a containing directory for any of the items in a bucket, then we need to
                    // add this item to the next bucket. So we go through the buckets in reverse order.
                    var added = false;
                    for (var b = buckets.Count - 1; b >= 0; b--)
                    {
                        var bucket            = buckets [b];
                        var anyContainingFile = bucket.Any(v => v.ItemSpec.StartsWith(dir, StringComparison.OrdinalIgnoreCase));
                        if (anyContainingFile)
                        {
                            if (b + 1 >= buckets.Count)
                            {
                                buckets.Add(new List <ITaskItem> ());
                            }
                            buckets [b + 1].Add(res);
                            added = true;
                            break;
                        }
                    }
                    if (!added)
                    {
                        // This directory doesn't contain any other signed files, so we can add it to the first bucket.
                        if (buckets.Count == 0)
                        {
                            buckets.Add(new List <ITaskItem> ());
                        }
                        var bucket = buckets [0];
                        bucket.Add(res);
                    }
                    continue;
                }

                Log.LogWarning("Unable to sign '{0}': file or directory not found.", res.ItemSpec);
            }

#if false
            Log.LogWarning("Codesigning {0} buckets", buckets.Count);
            for (var b = 0; b < buckets.Count; b++)
            {
                var bucket = buckets [b];
                Log.LogWarning($"    Bucket #{b + 1} contains {bucket.Count} items:");
                foreach (var item in bucket)
                {
                    Log.LogWarning($"        {item.ItemSpec}");
                }
            }
#endif

            for (var b = 0; b < buckets.Count; b++)
            {
                var bucket = buckets [b];
                Parallel.ForEach(bucket, new ParallelOptions {
                    MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1)
                }, (item) => {
                    Codesign(item);

                    var files = GetCodesignedFiles(item);
                    lock (codesignedFiles)
                        codesignedFiles.AddRange(files);
                });
            }

            CodesignedFiles = codesignedFiles.ToArray();

            return(!Log.HasLoggedErrors);
        }
Ejemplo n.º 30
0
 /// <summary>
 /// Executes a for loop with 64-bit indexes in which iterations may run in parallel
 /// and the state of the loop can be monitored and manipulated.
 /// </summary>
 public static SystemParallelLoopResult For(long fromInclusive, long toExclusive,
                                            Action <long, SystemParallelLoopState> body)
 {
     ExceptionProvider.ThrowUncontrolledInvocationException(nameof(SystemParallel.For));
     return(SystemParallel.For(fromInclusive, toExclusive, body));
 }