Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AsParallel () and Any()?

Tags:

c#

.net-4.0

plinq

I've seen this code which check a condition using AsParallel() and Any() :

bool IsAnyDeviceConnected()
{
   return m_devices.Any(d => d.IsConnected);
}

and to make it faster :

bool IsAnyDeviceConnected()
{
   return m_devices.AsParallel().Any(d => d.IsConnected);
}

But looking at Any() :

 internal static bool Any<T>(this IEnumerable<T> source, Func<T, bool> predicate) {
            foreach (T element in source) {
                if (predicate(element)) {
                    return true;
                }
            }
            return false;
        }

I don't see (obviously) - that it does care about cancellation of other workers - once found.

However - this (other) code does "finish - soon as possible" + cancel other future work :

bool IsAnyDeviceConnected()
{
   var res = Parallel.ForEach(m_devices,
      (d,loopState) => {  
         if (d.IsConnected) 
            loopState.Stop();
      });
   return !res.IsCompleted;
}

Question :

Does my diagnostics correct? Does Any() - once found item , doesn't cancel other threads ( in AsParallel context)

nb , my fear is that I might looking at the wrong source code.

like image 961
Royi Namir Avatar asked Aug 19 '14 08:08

Royi Namir


People also ask

What is AsParallel?

AsParallel(IEnumerable) Enables parallelization of a query. AsParallel<TSource>(Partitioner<TSource>) Enables parallelization of a query, as sourced by a custom partitioner that is responsible for splitting the input sequence into partitions.

When should I use parallel ForEach When should I use Plinq?

use the Parallel. ForEach method for the simplest use case, where you just need to perform an action for each item in the collection. use the PLINQ methods when you need to do more, e.g. query the collection or to stream the data.

Does LINQ select run in Parallel?

LINQ to Objects and LINQ to XML queries are designed to work sequentially, and do not involve multi-threading, concurrency, or parallel computing.

What is Parallel LINQ in. net?

Parallel LINQ (PLINQ) is a parallel implementation of the Language-Integrated Query (LINQ) pattern. PLINQ implements the full set of LINQ standard query operators as extension methods for the System. Linq namespace and has additional operators for parallel operations.


3 Answers

AsParallel() returns a ParallelQuery, so if you call AsParallel().Any(...) you're not calling Enumerable.Any, but ParallelEnumerable.Any.

The reference source code for ParallelEnumerable.Any is here.

When you dig e.g. into the AnyAllSearchOperatorEnumerator class, you see that a flag called resultFoundFlag is used to tell other workers that a result is found so they can stop searching.

like image 96
sloth Avatar answered Oct 06 '22 07:10

sloth


You're looking at the wrong code. AsParallel returns a ParallelQuery<TSource>, and ParellelQuery has another overload for Any.

'Any' creates a new AnyAllSearchOperator object and aggregates it. If you dig deeper into that chain of method calls and objects you'll find that the QueryOpeningEnumerator does support cancellation.


Unfortunately the reference source links to those particular member functions are bugged.

like image 26
Dirk Avatar answered Oct 06 '22 06:10

Dirk


You are looking at the wrong code. ParallelEnumerable.AsParallel returns a ParallelQuery<>. ParallelEnumerable also defines its own Any extension method.

In order to specify cancellation, degrees of parallelism etc you need to use ParallelEnumerable's WithXXX extension methods like WithCancellation and WithDegreeOfParallelism. ParallelEnumerable.Any doesn't allow you to specify those options to preserve a similar signature as Enumerable.Any

like image 43
Panagiotis Kanavos Avatar answered Oct 06 '22 06:10

Panagiotis Kanavos