Ejemplo n.º 1
0
        public override IEnumerable<PvcStream> Execute(IEnumerable<PvcCore.PvcStream> inputStreams)
        {
            var outputStreams = new List<PvcStream>();
            foreach (var inputStream in inputStreams.AsParallel())
            {
                var browserifyBundle = PvcRuntimeNodeJs<PvcBrowserify>.Execute(
                    new
                    {
                        entryPoint = Path.Combine(Directory.GetCurrentDirectory(), Path.GetFileName(inputStream.StreamName))
                    },
                    @"
                        var browserify = require('browserify');

                        browserify(pvc.data.entryPoint)
                            .bundle(function (err, output) {
                                pvc.doneCallback(err, output);
                            })
                            .on('error', function (err) {
                                console.error(err);
                            });
                    "
                );

                outputStreams.Add(PvcUtil.StringToStream(browserifyBundle, inputStream.StreamName));
            }

            return outputStreams;
        }
        /// <summary>
        /// Change time creating photo for all photofiles in folder
        /// </summary>
        /// <param name="pathToFiles">path to files</param>
        /// <param name="l_minutes">minutes for change (allow plus or minus value)</param>
        /// <returns>count changed files</returns>
        public int ChangeCreatingDateTimeForFiles(IEnumerable<string> pathToFiles, int minutes, out string errors)
        {
            StringBuilder errorFiles = new StringBuilder();
            errors = "";
            if (pathToFiles.Count() == 0)
                return 0;

            var countChangedFiles =
                pathToFiles.AsParallel()
                         .Where(filePath =>
                            {
                                string error;
                                var res =
                                     changeExifInformationInFile(filePath,
                                     createDirectoryForOutput(filePath), out error,
                                     (EXIFFileWorker photoExifInfo, out string errorInMethod) =>
                                     {
                                         errorInMethod = "";
                                         photoExifInfo.ChangeTime(minutes);
                                         return true;
                                     });
                                if (!res)
                                    errorFiles.AppendLine(error);
                                return res;
                            })
                          .Count();

            // return count changed files
            errors = errorFiles.ToString();
            return countChangedFiles;
        }
 public IEnumerable<SelectedImage> UpdateExifProperties(IEnumerable<SelectedImage> selectedImages)
 {
     if (selectedImages == null)
         return selectedImages;
     selectedImages.AsParallel().ForAll(selectedImage =>
     {
         try
         {
             var serverPath = _mediaFileSystemService.GetServerPath(selectedImage.FilePath);
             if (!File.Exists(serverPath))
                 return;
             var fileInfo = new FileInfo(serverPath);
             if (fileInfo.Extension.ToLower() == ".jpg")
             {
                 var reader = new ExifReader(serverPath);
                 selectedImage.Latitude = reader.GetLat();
                 selectedImage.Longtitude = reader.GetLon();
             }
         }
         catch (Exception e)
         {
         } 
     });
     return selectedImages;
 }
Ejemplo n.º 4
0
        public IEnumerable<FileInfo> OrderFiles(BundleContext context, IEnumerable<FileInfo> files)
        {
            var workingItems = files.AsParallel().Select(i => new _WorkingItem {
                Path = i.FullName,
                FileInfo = i,
                Dependencies = this._GetDependencies(i.FullName),
            });

            var fileDependencies = new Dictionary<string, _WorkingItem>(_DependencyNameComparer);
            foreach (var item in workingItems) {
                _WorkingItem duplicate;
                if (fileDependencies.TryGetValue(item.Path, out duplicate))
                    throw new ArgumentException(string.Format("During dependency resolution, a collision between '{0}' and '{1}' was detected. Files in a bundle must not collide with respect to the dependency name comparer.", Path.GetFileName(item.Path), Path.GetFileName(duplicate.Path)));

                fileDependencies.Add(item.Path, item);
            }

            foreach (var item in fileDependencies.Values) {
                foreach (var dependency in item.Dependencies) {
                    if (!fileDependencies.ContainsKey(dependency))
                        throw new ArgumentException(string.Format("Dependency '{0}' referenced by '{1}' could not found. Ensure the dependency is part of the bundle and its name can be detected by the dependency name comparer. If the dependency is not supposed to be in the bundle, add it to the list of excluded dependencies.", Path.GetFileName(dependency), Path.GetFileName(item.Path)));
                }
            }

            while (fileDependencies.Count > 0) {
                var result = fileDependencies.Values.FirstOrDefault(f => f.Dependencies.All(d => !fileDependencies.ContainsKey(d)));
                if (result == null)
                    throw new ArgumentException(string.Format("During dependency resolution, a cyclic dependency was detected among the remaining dependencies {0}.", string.Join(", ", fileDependencies.Select(d => "'" + Path.GetFileName(d.Value.Path) + "'"))));
                yield return result.FileInfo;
                fileDependencies.Remove(result.Path);
            }
        }
		private IEnumerable<SharpTreeNode> FindReferences(IEnumerable<LoadedAssembly> assemblies, CancellationToken ct)
		{
			assemblies = assemblies.Where(asm => asm.AssemblyDefinition != null);

			// use parallelism only on the assembly level (avoid locks within Cecil)
			return assemblies.AsParallel().WithCancellation(ct).SelectMany((LoadedAssembly asm) => FindReferences(asm, ct));
		}
        /// <summary>
        ///		<para><see cref="IFrameworkContainer"/> 컨테이너에 등록할 개체를 Task Parallel Library 를 이용하여 병렬로 처리합니다.</para>
        ///		<para>단, 컨테이너에 개체를 등록할 때 CPU Process 의 개수를 이용하여 등록합니다.</para>
        ///		<para>단, 오버헤드가 높을 수 있는 작업이므로 <see cref="IFrameworkContainer"/> 의 내부적인 모든 작업을 병렬화 합니다.</para>
        ///		<para>단, 병렬 작업은 .NET Framework 4.0 빌드에서만 동작합니다.</para>
        /// </summary>
        /// <param name="container"></param>
        /// <param name="action"></param>
        public static void RegisterTypeAsParallel(this IFrameworkContainer container, IEnumerable<Action> action)
        {
            ConcurrentQueue<Exception> exceptions = null;
            try
            {
                exceptions = new ConcurrentQueue<Exception>();

                try
                {
                    action.AsParallel()
                            .WithDegreeOfParallelism(Environment.ProcessorCount)
                            .WithExecutionMode(ParallelExecutionMode.ForceParallelism)
                            .ForAll( o => o());

                }
                catch (Exception ex)
                {
                    exceptions.Enqueue(ex);
                }
            }
            catch (Exception)
            {
                if( exceptions != null )
                    exceptions.ToList().ForEach( o => Trace.WriteLine( o.Message ));

                throw;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Durchsucht die nach länge gruppierte Liste von Teilzeichenolgen nach Wiederholungen.
        /// </summary>
        /// <param name="substringsGroupedByLength">Die nach länge gruppierte Liste von Teilzeichenfolgen.</param>
        /// <param name="cores">Die Anzahl der zu benutzenden CPU-Kerne. 0 verwendet 
        /// alle zur Verfügung stehenden.</param>
        /// <returns>Eine Liste aller Teilzeichenfolgen in <paramref name="substringsGroupedByLength"/>,
        /// wobei wiederholungen in einem Element zusammen gefasst wurden.</returns>
        public static IEnumerable<Occurence> FindRepetitions(IEnumerable<IEnumerable<Substring>> substringsGroupedByLength, int cores = 0)
        {
            var enu = substringsGroupedByLength.AsParallel();
            if (cores > 0)
            {
                enu = enu.WithDegreeOfParallelism(cores);
            }
            foreach (var group in enu)
            {
                var items = group.ToArray();
                for (var index1 = 0; index1 < items.Length; ++index1)
                {
                    if (items[index1] == null) { continue; }
                    var occ = new Occurence(items[index1].Text);
                    occ.Indices.Add(items[index1].Index);
                    for (var index2 = index1 + 1; index2 < items.Length; ++index2)
                    {
                        if (items[index2] == null || items[index1].Text != items[index2].Text) { continue; }

                        occ.Indices.Add(items[index2].Index);
                        items[index2] = null;
                    }
                    yield return occ;
                }
            }
        }
        private static void UseCancellation(IEnumerable<int> data)
        {
            WriteLine(nameof(UseCancellation));
            var cts = new CancellationTokenSource();

            Task.Run(() =>
            {
                try
                {
                    var res = (from x in data.AsParallel().WithCancellation(cts.Token)
                               where Math.Log(x) < 4
                               select x).Average();

                    WriteLine($"query finished, sum: {res}");
                }
                catch (OperationCanceledException ex)
                {
                    WriteLine(ex.Message);
                }
            });

            WriteLine("query started");
            Write("cancel? ");
            string input = ReadLine();
            if (input.ToLower().Equals("y"))
            {
                cts.Cancel();
            }

            WriteLine();
        }
Ejemplo n.º 9
0
 public static void ImportAccounts(IEnumerable<string> importAccounts)
 {
     importAccounts
         .AsParallel()
         .ForAll(account => {
             var completionTask = ImportAccountAsync(account);
         });
 }
          Encrypt(IEnumerable<string> data)
        {

            OrderedParallelQuery<string> parallelGroups =
            data.AsParallel().OrderBy(item => item);

            // Show the total count of items still
            // matches the original count
            System.Diagnostics.Trace.Assert(
                data.Count() == parallelGroups.Sum(
                    item => item.Count()));
            // ...


            return data.AsParallel().Select(
                item => Encrypt(item)).ToList();
        }
Ejemplo n.º 11
0
 public static void sameUnitPriceOriginalPLINQ(IEnumerable<Product> products)
 {
     TestingEnvironment.BenchmarkQuery(() => products.AsParallel().Where(p => categoriesToAudit.Contains(p.category)).SelectMany(p => products.Where(p2 => !p2.category.Equals(p.category) && p2.unitPrice == p.unitPrice).Select(p2 => p.productName + " and " + p2.productName)),
                ref products,
                "Original Same unitPrice Lambda Expession with PLINQ",
                "products.AsParallel().Where(p => categoriesToAudit.Contains(p.category)).SelectMany(p => products.Where(p2 => !p2.category.Equals(p.category) && p2.unitPrice == p.unitPrice).Select(p2 => p.productName + \" and \" + p2.productName))"
                );
 }
 private static void LinqQuery(IEnumerable<int> data)
 {
     WriteLine(nameof(LinqQuery));
     var res = (from x in data.AsParallel()
                where Math.Log(x) < 4
                select x).Average();
     WriteLine($"result from {nameof(LinqQuery)}: {res}");
     WriteLine();
 }
 public UpdaterClientEventArgs(IEnumerable<AvailableUpdate> items)
     : this("Finished checking for Updates")
 {
     items.AsParallel().ForAll(item =>
     {
         this.items.Add(item.Date, item);
     });
     this.Completed = true;
 }
 private static IEnumerable<DownloaderMatch> SearchDownloaders(IEnumerable<IEpisodeSubtitleDownloader> compatibleDownloaders, Language[] languageArray, TvReleaseIdentity tvReleaseIdentity)
 {
     var searchResults = compatibleDownloaders
         .AsParallel()
         .SelectMany(downloader => downloader.SearchSubtitle(tvReleaseIdentity, languageArray)
             .Select(match => new DownloaderMatch(downloader, match)))
         .AsSequential().ToArray();
     return FilterOutLanguagesNotInRequest(searchResults, languageArray);
 }
Ejemplo n.º 15
0
        /// <summary>
        /// Resolves the fastest Uri in a collection.
        /// </summary>
        /// <param name="uris">Enumerable collection of Uris.</param>
        /// <returns>Fastest Uri in the collection.</returns>
        public Uri Fastest(IEnumerable<Uri> uris)
        {
            if (uris == null)
                throw new ArgumentNullException("uris");

            return uris.AsParallel()
                .OrderBy(_ => _score.Score(_.DnsSafeHost))
                .First();
        }
Ejemplo n.º 16
0
 public static Company FunctionalParallelMerge(Company bigCompany, IEnumerable<Company> smallCompanies)
 {
     return smallCompanies
         .AsParallel()
         .Aggregate(CreateShell,
             (shell, smallCompany) => shell.Merge(smallCompany),
             (shell1, shell2) => shell1.Merge(shell2),
             bigCompany.Merge);
 }
 public UpdaterClientEventArgs(IEnumerable<Release> items)
     : this("Finished checking for Updates")
 {
     items.AsParallel().ForAll(item =>
     {
         var update = new AvailableUpdate(item);
         this.items.Add(update.Date, update);
     });
     this.Completed = true;
 }
Ejemplo n.º 18
0
        public void Play(IEnumerable<FileNode> content, IVideoPlayerController videoPlayer)
        {
            var statMusicFolder = _processor.Process(new GetFolderFromStringSettingQuery()
            {
                SettingName = Constants.PlayerStatMusicFolderSettingName
            });

            var statVideoFolder = _processor.Process(new GetFolderFromStringSettingQuery()
            {
                SettingName = Constants.PlayerStatVideoFolderSettingName
            });

            var statPictureFolder = _processor.Process(new GetFolderFromStringSettingQuery()
            {
                SettingName = Constants.PlayerStatPictureFolderSettingName
            });

            // TODO : If folder is null then notify user

            var music = content.AsParallel().SingleOrDefault(x => statMusicFolder.Contains(x));
            var video = content.AsParallel().SingleOrDefault(x => statVideoFolder.Contains(x)); // TODO : Fix if no Video exists
            var picture = content.AsParallel().SingleOrDefault(x => statPictureFolder.Contains(x));

            if (video == null)
            {
                var pictureFolder = _processor.Process(new GetFolderFromStringSettingQuery()
                {
                    SettingName = Constants.PlayerPictureFolderSettingName
                });
                video = content.AsParallel().SingleOrDefault(x => pictureFolder.Contains(x));
            }

            var res = new List<FileNode>()
            {
                music, video, picture
            }.Where(x => x != null);

            videoPlayer.Queue.SetQueue(res);

            Console.WriteLine("Using PlayerStatPlayStrategy");

            videoPlayer.PlayQueue();
        }
        private static void ExtensionMethods(IEnumerable<int> data)
        {
            WriteLine(nameof(ExtensionMethods));
            var res = data.AsParallel()
                .Where(x => Math.Log(x) < 4)
                .Select(x => x).Average();

            WriteLine($"result from {nameof(ExtensionMethods)}: {res}");
            WriteLine();
        }
Ejemplo n.º 20
0
        private static void OrderBy(IEnumerable<int> numbers)
        {
            var evenNumbers =
                from number in numbers
                    .AsParallel()
                where number%2 == 0
                orderby number
                select number;

            evenNumbers.AsEnumerable().Print();
        }
Ejemplo n.º 21
0
        public static void sameUnitPricePLINQ(IEnumerable<Product> products)
        {
            TestingEnvironment.BenchmarkQuery(() => products.AsParallel().GroupBy(p => p.unitPrice).SelectMany(prodGroup => prodGroup.Where(p => categoriesToAudit.Contains(p.category)).SelectMany(p => prodGroup.Where(p2 => !p2.category.Equals(p.category)).Select(p2 => p.productName + " and " + p2.productName))),
                           ref products,
                           "Groupby Same unitPrice Lambda Expession with PLINQ",
                           "products.AsParallel().GroupBy(p => p.unitPrice).SelectMany(prodGroup => prodGroup.Where(p => categoriesToAudit.Contains(p.category)).SelectMany(p => prodGroup.Where(p2 => !p2.category.Equals(p.category)).Select(p2 => p.productName + \" and \" + p2.productName)))"
                           );

            TestingEnvironment.BenchmarkQuery(() => products.GroupBy(p => p.unitPrice).AsParallel().SelectMany(prodGroup => prodGroup.Where(p => categoriesToAudit.Contains(p.category)).SelectMany(p => prodGroup.Where(p2 => !p2.category.Equals(p.category)).Select(p2 => p.productName + " and " + p2.productName))),
                           ref products,
                           "Groupby Same unitPrice Lambda Expession with PLINQv2",
                           "products.GroupBy(p => p.unitPrice).AsParallel().SelectMany(prodGroup => prodGroup.Where(p => categoriesToAudit.Contains(p.category)).SelectMany(p => prodGroup.Where(p2 => !p2.category.Equals(p.category)).Select(p2 => p.productName + \" and \" + p2.productName)))"
                           );

            TestingEnvironment.BenchmarkQuery(() => OptimizerExtensions.AsGroupSuspendedThreadSafe(() => products.ToRelaxedVolatileIndex(p => p.unitPrice)).SelectMany(prodPriceIdxThunk => products.AsParallel().Where(p => categoriesToAudit.Contains(p.category)).SelectMany(p => prodPriceIdxThunk.Value.Lookup(() => p.unitPrice).Where(p2 => !p2.category.Equals(p.category)).Select(p2 => p.productName + " and " + p2.productName))),
                           ref products,
                           "Relaxed (Exception Ignoring) Volatile Index Same unitPrice Lambda Expession with PLINQ",
                           "OptimizerExtensions.AsGroupSuspendedThreadSafe(() => products.ToRelaxedVolatileIndex(p => p.unitPrice)).SelectMany(prodPriceIdxThunk => products.AsParallel().Where(p => categoriesToAudit.Contains(p.category)).SelectMany(p => prodPriceIdxThunk.Value.lookup(() => p.unitPrice).Where(p2 => !p2.category.Equals(p.category)).Select(p2 => p.productName + \" and \" + p2.productName)))"
                           );
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RouteReader"/> class.
        /// </summary>
        /// <param name="routeSources">The route sources.</param>
        /// <param name="interceptors">Route interceptors</param>
        public RouteReader(IEnumerable<IRouteSource> routeSources, IEnumerable<IRouteInterceptor> interceptors)
        {
            if (routeSources == null) throw new ArgumentNullException("routeSources");
            if (interceptors == null) throw new ArgumentNullException("interceptors");

            this.Definitions = from source in routeSources
                               from route in source.Routes
                               let def = new RouteDefinition(route.Item2) { Name = route.Item1 }
                               where interceptors.AsParallel().All(interceptor => interceptor.Intercept(def))
                               select def;
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Filtert eine Liste von Vorkommen danach, ob die Vorkommen in anderen 
 /// Teilzeichenfolgen enthalten sind, die genauso oft vorkommen.
 /// </summary>
 /// <param name="repetitions">Eine zu filtrnde Liste von Teilzeichenfolgen. 
 /// Diese wird einmal durchlaufen</param>
 /// <param name="repetitions2">Die Vergleichsliste zu <paramref name="repetitions"/>. 
 /// Die Listen sollten die gleichen Elemente enbthalten, diese hier 
 /// wird jedoch mehrfach durchlaufen.</param>
 /// <param name="cores">Die Anzahl der zu benutzenden CPU-Kerne. 0 verwendet 
 /// alle zur Verfügung stehenden.</param>
 /// <returns>Eine Auflistung mit den Elementen aus <paramref name="repetitions"/>
 /// ohne denen, deren Teilzeichenfolge in einer anderen, gleichhäufig enthaltenen, 
 /// auftritt.</returns>
 public static IEnumerable<Occurence> FilterRepetitions(IEnumerable<Occurence> repetitions, IEnumerable<Occurence> repetitions2, int cores = 0)
 {
     var enu = repetitions.AsParallel();
     if (cores > 0)
     {
         enu = enu.WithDegreeOfParallelism(cores);
     }
     return enu.Where(first => !repetitions2.Any(second => first.Indices.Count == second.Indices.Count
                                                        && first.Text.Length != second.Text.Length
                                                        && second.Text.Contains(first.Text)));
 }
Ejemplo n.º 24
0
        private IEnumerable<Product> FindChangedProducts(IEnumerable<Product> products, IEnumerable<ProductSignature> existsProducts) {
            Func<string, string, string> createKey = (source, number) => {
                return source + "-" + number;
            };
            var existsProductsMap = existsProducts.AsParallel().GroupBy(p => createKey(p.Source, p.Number), p => p.Signature).ToDictionary(p => p.Key, p => p.First());

            return products.AsParallel().Where(p => {
                var key = createKey(p.Source, p.Number);
                return !existsProductsMap.ContainsKey(key) || !Signature.IsMatch(existsProductsMap[key], p.Signature);
            });
        }
          Encrypt(IEnumerable<string> data)
        {

            ParallelQuery<IGrouping<char, string>> parallelGroups;
            parallelGroups =
                from text in data.AsParallel()
                orderby text
                group text by text[0];


            // Show the total count of items still
            // matches the original count
            System.Diagnostics.Trace.Assert(
                data.Count() == parallelGroups.Sum(
                    item => item.Count()));
            // ...


            return data.AsParallel().Select(
                item => Encrypt(item)).ToList();
        }
Ejemplo n.º 26
0
 private static IEnumerable<Type> SelectPotentialTypes(IEnumerable<ITypesProvider> typesProviders, ITypeSelector convention)
 {
     try
     {
         var potentialTypes = typesProviders.AsParallel().SelectMany(provider => provider.RetrieveTypes(convention));
         return potentialTypes.ToList();
     }
     catch (AggregateException e)
     {
         throw e.FlattenTryBubbleUp();
     }
 }
Ejemplo n.º 27
0
 internal static async Task AddDevices(Environment environment, IEnvironmentService service,
     IEnumerable<Guid> deviceIds, INotifyList<IDevice, Guid> devices)
 {
     IEnumerable<IDevice> localDevices = null;
     await Task.Run(() =>
     {
         localDevices = deviceIds.
           AsParallel().AsOrdered().
           Select(did => Device.Create(did, environment, service).Result);
     });
     devices.Add(localDevices);
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Updates the bag.
 /// </summary>
 /// <param name="types">The types.</param>
 private void UpdateBag(IEnumerable<SubscriptionKey> types)
 {
     types.AsParallel().ForAll(x =>
                                 {
                                     if (!_bagTypes.ContainsKey(x))
                                     {
                                         _bagTypes.TryAdd(x,
                                                          new MessageType
                                                              { FullName = x.Key});
                                     }
                                 });
 }
Ejemplo n.º 29
0
 public static IEnumerable<ServiceDescriptor> Describe(IEnumerable<Type> typesToRegistration)
 {
     try
     {
         var serviceDescriptors = typesToRegistration.AsParallel().SelectMany(CreateDescriptors);
         return serviceDescriptors.ToList();
     }
     catch (AggregateException e)
     {
         throw e.FlattenTryBubbleUp();
     }
 }
        public IEnumerable<WordsForPrefix> GetWordsForPrefixes(IEnumerable<string> prefixes)
        {
            bool useParallelization = prefixes.Count() >= Configuration.Default.MinPrefixesNumberForConcurrentProcessing;

            var prefixToMostPopularWords = useParallelization
                                           ? prefixes
                                             .AsParallel().AsOrdered()
                                             .Select(prefix => new WordsForPrefix(prefix, GetMostPopularWords(prefix)))
                                           : prefixes
                                             .Select(prefix => new WordsForPrefix(prefix, GetMostPopularWords(prefix)));
            return prefixToMostPopularWords.ToArray();
        }
Ejemplo n.º 31
0
        // Паралельна функцiя для знаходження кiлькостi елементiв за умовою
        public static long CalcElementsOnCondition(IEnumerable <long> elementsList, Func <long, bool> selector)
        {
            long elementsCount = 0;

            elementsList.AsParallel().ForAll(e =>
            {
                if (selector(e))
                {
                    Interlocked.Increment(ref elementsCount);
                }
            });

            return(elementsCount);
        }
Ejemplo n.º 32
0
 public static void Iterate <T>(this IEnumerable <T> items, Action <T> action, bool pll = false)
 {
     if (pll)
     {
         items.AsParallel().ForAll(action);
     }
     else
     {
         foreach (var item in items)
         {
             action(item);
         }
     }
 }
        public static HashSet <Type> GetTuples(this IEnumerable <Type> types)
        {
            if (types == null)
            {
                return(new HashSet <Type>());
            }

            var enumTypes =
                from type in types.AsParallel()
                where type.IsTuple()
                select type;

            return(new HashSet <Type>(enumTypes));
        }
Ejemplo n.º 34
0
        private void FillTrailers(List <ResultEntry> movies)
        {
            Parallel.ForEach(movies, x =>
            {
                var temp = trailerSearchersRepo
                           .AsParallel()
                           .SelectMany(searcher => searcher.Search(new SearchQuery
                {
                    Title = ComposeVideoSearchQuery(x)
                })).ToList();

                x.Videos = temp;
            });
        }
Ejemplo n.º 35
0
        private static IEnumerable <Student> SortBySecondNameAlphabet(IEnumerable <Student> students, bool useParalel = true)
        {
            if (useParalel)
            {
                students = students.AsParallel();
            }

            var result = from s in students
                         where s.FirstName.CompareTo(s.LastName) < 0
                         orderby s.FirstName
                         select s;

            return(result);
        }
Ejemplo n.º 36
0
        static void Main(string[] args)
        {
            var watch = Stopwatch.StartNew();

            Write("Press ENTER to start: ");
            ReadLine();
            watch.Start();
            IEnumerable <int> numbers = Enumerable.Range(1, 200_000_000);
            //var squares = numbers.Select(number => number * 10).ToArray();
            var squares = numbers.AsParallel().Select(number => number * 10).ToArray();

            watch.Stop();
            WriteLine($"{watch.ElapsedMilliseconds:#,##0} elapsed milliseconds.");
        }
Ejemplo n.º 37
0
        public override IDictionary <String, Object> GetValues(IEnumerable <String> keys, String regionName = null)
        {
            Contract.Requires <RegionUnsupportedException>(regionName == null);
            Contract.Requires <ArgumentNullException>(keys != null);

            var keyCount = keys.Count();
            var result   = keys.ToDictionary(key => key, key => (Object)null);

            keys.AsParallel()
            .WithDegreeOfParallelism(keyCount > Environment.ProcessorCount ? Environment.ProcessorCount : keyCount)
            .ForAll(key => result[key] = GetValue(key));

            return(result);
        }
Ejemplo n.º 38
0
        public static void Perform()
        {
            IEnumerable <int> numbers = Enumerable.Range(-5, 10);
            var query = from n in numbers
                        select 100 / n;

            try
            {
                foreach (var n in query)
                {
                    WriteLine(n);
                }
            }
            catch (Exception)
            {
                WriteLine("Divied by zero!");
            }

            WriteLine("---");
            WriteLine("Sequential LINQ query processing.");
            WriteLine();

            var paralelQuery = from n in numbers.AsParallel()
                               select 100 / n;

            try
            {
                paralelQuery.ForAll(WriteLine);
                //foreach (var n in paralelQuery)
                //{
                //    WriteLine(n);
                //}
            }
            catch (DivideByZeroException e)
            {
                WriteLine("divided by zero - usual exception handler.");
            }
            catch (AggregateException e)
            {
                e.Flatten().Handle(ex =>
                {
                    if (ex is DivideByZeroException)
                    {
                        WriteLine("Divided by zero - aggreagate exception handler.");
                        return(true);
                    }
                    return(false);
                });
            }
        }
Ejemplo n.º 39
0
        public void Test01()
        {
            var watch = Stopwatch.StartNew();

            Write("Pres ENTER to start.");
            ReadLine();
            watch.Start();
            IEnumerable <int> numbers = Enumerable.Range(1, 200000000);
            //var squares = numbers.Select(number => number * 2).ToArray();
            var squares = numbers.AsParallel().Select(number => number * 2).ToArray();

            watch.Stop();
            WriteLine($"{watch.ElapsedMilliseconds:#,##0} elapsed miliseconds");
        }
Ejemplo n.º 40
0
        private IReadOnlyCollection <UserClaim> Map(IEnumerable <Claim> claims)
        {
            if (claims == null)
            {
                return(new List <UserClaim>());
            }

            var userClaims = claims
                             .AsParallel()
                             .Select(Map)
                             .ToList();

            return(userClaims);
        }
        /// <summary>
        /// Converts query result rows into Row objects, and adds them to the collection.
        /// </summary>
        internal void AddRows(DatabaseQuery query, IEnumerable <object> rows)
        {
            if (query.Raw.OrderBy == null || query.Raw.OrderBy.Count == 0)
            {
                // We can access the enumerable in parallel, the order doesn't matter.
                Rows.AddRange(rows.AsParallel().Select(
                                  obj => CreateRowFromObservableCollection(obj)));

                return;
            }

            // Access the enumerable in a series, because the order matters.
            Rows.AddRange(rows.Select(obj => CreateRowFromObservableCollection(obj)));
        }
Ejemplo n.º 42
0
        /// <summary>
        /// Calculate standard deviation
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static double SD(
            this IEnumerable <double> self)
        {
            var avg = self.Average();

            if (self.Count() > 1000000)
            {
                return(self.AsParallel().Select(x => Math.Pow((x - avg), 2)).Average());
            }
            else
            {
                return(self.Select(x => Math.Pow((x - avg), 2)).Average());
            }
        }
        public void Get_types_using_predicate()
        {
            // Arrange
            IEnumerable <Type> types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes());

            types.AsParallel().ForAll(type => PropertyCache.GetPropertiesForType(type));

            // Act
            IEnumerable <Type> typeCollection = TypeCache.GetTypes(t => t.IsSubclassOf(typeof(ComponentFixture)));

            // Assert
            Assert.IsTrue(typeCollection.Any());
            Assert.IsTrue(typeCollection.All(t => t.IsSubclassOf(typeof(ComponentFixture))));
        }
Ejemplo n.º 44
0
        private IList <(string SensorId, double Temperature)> GetAllTemperatureSensorsData()
        {
            IList <(string SensorId, double Temperature)> temperatureSensorDataList = new List <(string SensorId, double Temperature)>();

            IEnumerable <TemperatureSensor> temperaturesSensors = _sensors.Where(sensor => sensor is TemperatureSensor).Cast <TemperatureSensor>();

            temperaturesSensors.AsParallel().ForAll(temperatureSensor =>
            {
                var temperatureSensorData = GetTemperatureSensorData(temperatureSensor);
                temperatureSensorDataList.Add(temperatureSensorData);
            });

            return(temperatureSensorDataList);
        }
Ejemplo n.º 45
0
 static void ZippingLogs(IEnumerable <LogItem> logs1, IEnumerable <LogItem> logs2)
 {
     //  each data source can be processed concurrently with different computations
     // results could be combined with Zip operator
     var result = logs1
                  .AsParallel()
                  .AsOrdered()
                  .Select(v => ProcessLog(v))
                  .Zip(logs2
                       .AsParallel()
                       .AsOrdered()
                       .Select(v => ProcessLog(v)),
                       (first, second) => Combine(first, second));
 }
Ejemplo n.º 46
0
        private IEnumerable <T> GetEntitiesFromDao(IEnumerable <string> identifiers)
        {
            if (_dataAccessObject is IDataSourceBulkReader <T> bulkReader)
            {
                return(bulkReader.GetEntities(identifiers));
            }

            if (_dataAccessObject is IDataSourceReader <T> justReader)
            {
                return(identifiers.AsParallel().Select(justReader.GetEntity).Where(x => x != null));
            }

            throw new InvalidOperationException($"DataAccessObject {_dataAccessObject.GetType()} for type {typeof(T).FullName} does not implement neither {nameof(IDataSourceBulkReader<T>)} nor {nameof(IDataSourceReader<T>)}");
        }
Ejemplo n.º 47
0
        //private Func<T, T> _cloner;

        public DataCache(IEnumerable <T> source)//, Func<T, T> cloner
        {
            _innerData = new ConcurrentDictionary <string, T>();

            if (source != null)
            {
                source.AsParallel(new ParallelLinqOptions()
                {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                }).ForAll(i => { _innerData.AddOrUpdate(i.EntityId, i, (key, oldValue) => i); });
            }

            //  _cloner = cloner;
        }
Ejemplo n.º 48
0
    protected static Dictionary <TKey, TVal> IterateAllToDict <TIn, TOut, TKey, TVal>(IEnumerable <TIn> elements, Func <TIn, TOut> func, Func <TOut, TKey> outToKey, Func <TOut, TVal> outToVal)
    {
#if DEBUG
        var dict = new Dictionary <TKey, TVal>();
        foreach (var nxNode in elements)
        {
            var ret = func(nxNode);
            dict[outToKey(ret)] = outToVal(ret);
        }
        return(dict);
#else
        return(elements.AsParallel().Select(x => func(x)).ToDictionary(outToKey, outToVal));
#endif
    }
Ejemplo n.º 49
0
        /// <summary>
        /// 处理PLINQ查询中的异常
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            IEnumerable <int> numbers = Enumerable.Range(-5, 10);

            Console.WriteLine("执行顺序的LINQ查询");
            var query = from number in numbers
                        select 100 / number;

            try
            {
                foreach (var n in query)
                {
                    Console.WriteLine(n);
                }
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("被0除!");
            }

            Console.WriteLine("---");
            Console.WriteLine();

            Console.WriteLine("执行并行的LINQ查询");
            var parallelQuery = from number in numbers.AsParallel()
                                select 100 / number;

            try
            {
                parallelQuery.ForAll(Console.WriteLine);
            }
            catch (DivideByZeroException)
            {
                Console.WriteLine("被0除! - 通常的异常处理");
            }
            catch (AggregateException e)
            {
                e.Flatten().Handle(ex => {
                    if (ex is DivideByZeroException)
                    {
                        Console.WriteLine("被0除! - Aggregate异常处理");
                        return(true);
                    }
                    return(false);
                });
            }
            Console.WriteLine("---");

            Console.ReadLine();
        }
Ejemplo n.º 50
0
        public static async Task CreateCacheSounds(IEnumerable <string> paths)
        {
            await Task.Run(() =>
            {
                paths.AsParallel()
                .WithDegreeOfParallelism(Environment.ProcessorCount > 1 ? Environment.ProcessorCount - 1 : 1)
                .ForAll(k => CreateCacheSound(k, false).Wait());
            }).ConfigureAwait(false);

            //foreach (var path in paths)
            //{
            //    await CreateCacheSound(path, false).ConfigureAwait(false); // Cache each file once before play.
            //}
        }
Ejemplo n.º 51
0
        public async Task <IReadOnlyList <string> > LoopAsyncResult(IEnumerable <String> thingsToLoop)
        {
            List <Task <string> > listTasks = new List <Task <string> >();

            var result =
                thingsToLoop
                ?.AsParallel()
                ?.AsSequential()
                ?.Select((item) => this.DoAsyncResult(item))
                ?.ToList()
                ?.AsReadOnly();

            return(await Task.WhenAll <string>(result));
        }
Ejemplo n.º 52
0
 public ChunkCache3D(IEnumerable <Voxel> voxels, Logger logger, CanvasType canvas)
 {
     interactiveMode = true;
     this.canvas     = canvas;
     this.logger     = logger;
     logger.LogTechState("Calculating list of chunks...");
     chunks = voxels.AsParallel().Select(p =>
     {
         VoxelMap.AbsoluteToRelative(p.Item1, out byte chunkX, out _);
         VoxelMap.AbsoluteToRelative(p.Item2, out byte chunkY, out _);
         return(chunkX, chunkY);
     }).Distinct().ToList();
     logger.LogTechInfo("Chunk list is calculated");
 }
        static void Main(string[] args)
        {
            IEnumerable <int> numbers = Enumerable.Range(-5, 10);

            var query = from number in numbers
                        select 100 / number;

            try
            {
                foreach (var n in query)
                {
                    WriteLine(n);
                }
            }
            catch (DivideByZeroException)
            {
                WriteLine("Divided by zero!");
            }

            WriteLine("---");
            WriteLine("Sequential LINQ query processing");
            WriteLine();

            var parallelQuery = from number in numbers.AsParallel()
                                select 100 / number;

            try
            {
                parallelQuery.ForAll(WriteLine);
            }
            catch (DivideByZeroException)
            {
                WriteLine("Divided by zero! - usual exception handler!");
            }
            catch (AggregateException e)
            {
                e.Flatten().Handle(ex =>
                {
                    if (ex is DivideByZeroException)
                    {
                        WriteLine("Divided by zero - aggregate exception handler!");
                        return(true);
                    }
                    return(false);
                });
            }

            WriteLine("---");
            WriteLine("Parallel LINQ query processing and results merging");
        }
Ejemplo n.º 54
0
        public async Task SaveAsync <T>(IEnumerable <T> data)
            where T : BaseModel
        {
            //_logger.LogInformation($"Saving new data to DataBase, DataType { data.GetType() }");

            // ? Creating a new DataBase Context
            var unitOfWork = _contextFactory.GetUnitOfWork();
            // ? Trying to get context of IRepository based on a given data type
            var context = _contextFactory.GetContextBasedOn <T>(unitOfWork);

            if (context == null)
            {
                var ex = new NotSupportedException($"Given type is not supported by ContextFactory. Type {typeof(T)}");
                //_logger.LogError("Saving to DB Error", ex);
                return; // ? Reject operation
            }

            // ? Get records from DB and converting to IEntityBase instances in order to make scanning by VKEY/Primary key
            var sourceBase = await context.GetAllAsync(includeReleted : true);

            //if (sourceBase == null)
            //{
            //    var ex = new NotSupportedException($"DataBase hasn't records according to given data type - {typeof(T)}");
            //    //_logger.LogError("Saving to DB Error", ex);
            //}

            // ? Filter out new records if they exsit
            if (sourceBase.Any())
            {
                data = data.AsParallel().Where(d => sourceBase.All(s => d.VKEY != s.VKEY)).ToList();
            }
            // ? Trying to Add new records to DataBase
            try
            {
                // ? Converting back to TEntity in order to save a result to DB
                context.AddAsync(data);
                await unitOfWork.CompleteAsync();

                //_logger.LogInformation($"The Data was stored in DB. Argument metadata { data.GetType() }");
            }
            catch (Exception)
            {
                //_logger.LogError($"Unable to save data to DataBase", ex);
            }
            finally
            {
                unitOfWork.Dispose();
            }
        }
Ejemplo n.º 55
0
        private async Task <IResult <List <AnnotatedEnvironmentKey> > > AnnotateEnvironmentKeys(IDictionary <string, string> environment,
                                                                                                IEnumerable <ConfigurationIdentifier> structures)
        {
            var tasks = structures.AsParallel()
                        .Select(cid => (ConfigId: cid, Task: _store.Configurations
                                        .GetUsedConfigurationKeys(cid,
                                                                  DateTime.MinValue,
                                                                  QueryRange.All)))
                        .ToList();

            await Task.WhenAll(tasks.Select(t => t.Task));

            var results   = tasks.Select(t => (t.ConfigId, t.Task.Result)).ToList();
            var errors    = results.Where(r => r.Result.IsError).ToList();
            var successes = results.Where(r => !r.Result.IsError).ToList();

            if (errors.Any())
            {
                Response.OnStarting(_ =>
                {
                    var joinedFailedConfigs = string.Join(";", errors.Select(e => $"{e.ConfigId.Structure.Name}/{e.ConfigId.Structure.Version}"));
                    Response.Headers.Add("x-omitted-configs", joinedFailedConfigs);
                    return(Task.CompletedTask);
                }, null);

                foreach (var(_, error) in errors)
                {
                    Logger.LogError($"{error.Code} - {error.Message}");
                }
            }

            var annotatedEnv = new List <AnnotatedEnvironmentKey>(environment.Select(kvp => new AnnotatedEnvironmentKey
            {
                Key   = kvp.Key,
                Value = kvp.Value
            }));

            // go through each built configuration, and annotate the used Environment-Keys with their Structure-Id
            // at the end we have a list of Keys and Values, each with a list of structures that used this key
            successes.AsParallel()
            .ForAll(t => t.Result
                    .Data
                    .AsParallel()
                    .ForAll(key => annotatedEnv.FirstOrDefault(a => a.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
                            ?.Structures
                            .Add(t.ConfigId.Structure)));

            return(Common.Result.Success(annotatedEnv));
        }
 private async static Task writeJson <T>(string file, IEnumerable <T> data) =>
 await Task.Run(() => {
     using (var fs = new FileStream(file, FileMode.Create))
         using (var streamWriter = new StreamWriter(fs))
             using (var jsonWriter = new JsonWriter(streamWriter)) {
                 var context = BsonSerializationContext.CreateRoot(jsonWriter);
                 jsonWriter.WriteStartDocument();
                 var serializedCollection =
                     data.AsParallel()
                     .ToJson()
                     .ToString();
                 streamWriter.WriteLine(serializedCollection);
                 jsonWriter.WriteEndDocument();
             }
 });
Ejemplo n.º 57
0
 /// <summary>
 /// Iterates over items sequentially or in parallel, invokes the supplied action for each one
 /// and then returns the unit value
 /// </summary>
 /// <typeparam name="T">The item type</typeparam>
 /// <param name="items">The items</param>
 /// <param name="action">The action to apply</param>
 /// <param name="pll">Whether parallel iteration should be invoked</param>
 public static Unit iter <T>(IEnumerable <T> items, Action <T> action, bool pll = false)
 {
     if (pll)
     {
         items.AsParallel().ForAll(item => action(item));
     }
     else
     {
         foreach (var item in items)
         {
             action(item);
         }
     }
     return(Unit.Value);
 }
Ejemplo n.º 58
0
        private void GenerateBones()
        {
            string[] extensions = new string[] { ".M2", ".PHYS", ".SKEL" };
            var      basefiles  = FileNames.Where(x => HasExtension(x, extensions)).Select(x => PathWithoutExtension(x)).Distinct();

            IEnumerable <string> lineendings = Enumerable.Range(0, 100).Select(x => $"_{x.ToString("00")}.BONE");

            Console.WriteLine("  Generating M2 Bones");
            foreach (var basefile in basefiles)
            {
                IEnumerable <string> files = lineendings.AsParallel().Select(ext => basefile + ext);
                files = files.Except(FileNames).Distinct();
                Validate(files);
            }
        }
Ejemplo n.º 59
0
        /// <summary>
        /// Checks whether an ordered pipelining merge produces the correct output.
        /// </summary>
        private static bool OrderedPipeliningTest1(int dataSize, bool buffered)
        {
            TestHarness.TestLog("OrderedPipeliningTest1: dataSize={0}, buffered={1}", dataSize, buffered);
            ParallelMergeOptions merge = buffered ? ParallelMergeOptions.FullyBuffered : ParallelMergeOptions.NotBuffered;

            IEnumerable <int> src = Enumerable.Range(0, dataSize);

            if (!Enumerable.SequenceEqual(src.AsParallel().AsOrdered().WithMergeOptions(merge).Select(x => x), src))
            {
                TestHarness.TestLog("> FAILED: Incorrect output.");
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Locals the modular supervisor.
        /// </summary>
        /// <param name="plants">The plants.</param>
        /// <param name="specifications">The specifications.</param>
        /// <param name="conflictResolvingSupervisor">The conflict resolving supervisor.</param>
        /// <returns>IEnumerable&lt;DFA&gt;.</returns>
        /// <exception cref="Exception">conflicting supervisors</exception>
        public static IEnumerable <DFA> LocalModularSupervisor(IEnumerable <DFA> plants, IEnumerable <DFA> specifications, IEnumerable <DFA> conflictResolvingSupervisor = null)
        {
            conflictResolvingSupervisor ??= new DFA[0];

            var supervisors = specifications.AsParallel().WithDegreeOfParallelism(NumberOfThreads).Select(e => MonolithicSupervisor(plants.Where(p => p._eventsUnion.Intersect(e._eventsUnion).Any()), new[] { e })).ToList();

            var complete = supervisors.Union(conflictResolvingSupervisor).ToList();

            if (IsConflicting(complete))
            {
                throw new Exception("Conflicting Supervisors");
            }
            GC.Collect();
            return(complete);
        }