Why do these tasks execute sequentially? - c#

Why does the following code execute sequentially?
List<Task> tasks = new List<Task>();
for (int i = 0; i <= max; i += block)
{
if (i + block >= max)
tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block)));
else
block = max - i;
}
Task.WaitAll(tasks.ToArray());
I have also tested a version of this using Parallel.Invoke; it, too, fails to execute in parallel. There's bound to be something I'm not understanding, but when I try Googling this, I mostly get instructions on how to force sequential execution.
As a response to one of the caveats given in an answer below, I have included the following method for reference:
static void Count(ref int counter, int num)
{
int localCounter = 0;
for (int i = 0; i < num; i++)
if (Coin()) localCounter++;
System.Threading.Interlocked.Add(ref counter, localCounter);
}
Edited again: Thank you all!

Just replace tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block))); with Console.WriteLine and debug your code.
You never create more than one task.
for (int i = 0; i <= max; i += block)
{
if (i + block >= max)
Console.WriteLine(i);
else
block = max - i;
}

why does the following code execute sequentially?
It doesn't, unless you have something within the Count method that's synchronizing access to a single resource. If it can be parallelized, then this will run in parallel.
If Count executes very quickly then you'll find that the tasks are finished faster than new tasks get scheduled, which is why they may all execute in order.

Seems to me there is something wrong with your loop/if statement:
for (int i = 0; i <= max; i += block)
{
if (i + block >= max)
tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block)));
else
block = max - i;
}
If I am reading it right, you will only add a task if i + block >= max and you will only loop if i + block <= max (showing both the counter increase and the condition check). As such you will only add a task once.
In addition, you are changing block when you are not adding a task. I expect you want something more like the following, though I can not be sure without more code:
for (int i = 0; i <= max; i += block)
{
tasks.Add(Task.Factory.StartNew(() => Count(ref counter, block)));
if (i + block >= max) { block = max - i; }
}

Related

Weird behavior of multithread random numbers generator

Please check below code, this code try to compute birthday conflict possibility. To my surprise, if i execute those code with sequence, the result is expected around 0.44; but if try on PLinq, the result is 0.99.
Anyone can explain the result?
public static void BirthdayConflict(int num = 5, int people = 300) {
int N = 100000;
int act = 0;
Random r = new Random();
Action<int> action = (a) => {
List<int> p = new List<int>();
for (int i = 0; i < people; i++)
{
p.Add(r.Next(364) + 1);
}
p.Sort();
bool b = false;
for (int i = 0; i < 300; i++)
{
if (i + num -1 >= people) break;
if (p[i] == p[i + num -1])
b = true;
}
if (b)
Interlocked.Increment(ref act);
// act++;
};
// Result is around 0.99 - which is not OK
// Parallel.For( 0, N, action);
//Result is around 0.44 - which is OK
for (int i = 0; i < N; i++)
{
action(0);
}
Console.WriteLine(act / 100000.0);
Console.ReadLine();
}
You're using a shared (between threads) instance System.Random. It's not thread-safe then you're getting wrong results (well actually it just doesn't work and it'll return 0). From MSDN:
If your app calls Random methods from multiple threads, you must use a synchronization object to ensure that only one thread can access the random number generator at a time. If you don't ensure that the Random object is accessed in a thread-safe way, calls to methods that return random numbers return 0.
Simple (but not so efficient for parallel execution) solution is to use a lock:
lock (r)
{
for (int i = 0; i < people; i++)
{
p.Add(r.Next(364) + 1);
}
}
To improve performance (but you should measure) you may use multiple instances of System.Random, be careful to initialize each one with a different seed.
I find a useful explanation why random does not work under multi-thread, although it was original for Java, still can be benefitical.

Strange behaviour in for loop with System.Threading.Tasks

I'm creating System.Threading.Tasks from within a for loop, then running a ContinueWith within the same loop.
int[,] referencedArray = new int[input.Length / 4, 5];
for (int i = 0; i <= input.Length/4; i += 2048)
{
int[,] res = new int[input.Length / 4, 5];
int[,] arrayToReference = new int[input.Length / 4, 5];
Array.Clear(arrayToReference, 0, arrayToReference.Length);
Array.Copy(input, i, arrayToReference, 0, input.Length / 4);
Task<Tuple<int[,], int>> test = Task.Factory.StartNew(() => addReferences(arrayToReference, i));
test.ContinueWith(t => { Array.Copy(t.Result.Item1, 0, referencedArray, t.Result.Item2, input.Length / 4); MessageBox.Show("yai", t.Result.Item2.ToString()); });
Array.Copy(res, 0, referencedArray, i, input.Length / 4);
where the addReference sbroutine is:
public Tuple<int[,],int> addReferences(int[,] input, int index)
{
for (int i = 0; i < 2048; i++)
{
for (int j = 0; j < i; j++)
{
if ((input[i, 0] == input[j, 0]) && (input[i, 1] == input[j, 1]) && (input[i, 2] == input[j, 2]) && (input[i, 3] == input[j, 3]))
{
input[i, 4] = (j - i);
}
}
}
}
return new Tuple<int[,],int>(input,index);
}
However, I am getting really strange results:
1.The index (generated from the loop counter when the task is started) somehow becomes too large. I initially thought that this was because the loop counter had incremented past its maximum, stopping the loop, but when the continueWith executed it used the new value. However, even when I send the loop counter's value to the task as index when it starts, the error persists. Edit: Solved
I made I mistake in the original count
2.For some reason fewer than expected tasks are actually completed (e.g. even though 85 tasks are expected to be created, based on a loop counter maximum of 160,000 divided by a iteration of 2,048 = 78, only 77 pop ups appeared) - Edit Checking the console, it's apparent that the loop counter got up until about 157,696 before suddenly stopping.
I'm really confused by Tasks and Threading, so if you could be of any assistance that would be really useful - thanks in advance.
Edit I've made the changes suggested by Douglas, which solved my first problem - I'm still stuck on the second.
This might not completely resolve the issues you're having, but as a starting point, you need to avoid the closure issue by copying your loop counter to an inner variable before referencing it in your anonymous method:
for (int i = 0; i <= input.Length/4; i += 2048)
{
// ...
int iCopy = i;
var test = Task.Factory.StartNew(() => addReferences(arrayToReference, iCopy));
// ...
}
See Jon Skeet's answer (particularly the linked article) for an explanation of why this is necessary.
Edit: Regarding your second problem, I suspect it might have to do with integer division. What is the value of input.Length for which you're expecting 85 iterations? You say that 174,000 divided by 2,048 should give 85, but in practice, it will give 84, since integer division causes results to be truncated.
Edit2: Another reason you're not seeing all expected iterations could be that your program is terminating without waiting for the tasks (which typically execute on background threads) to complete. Check whether waiting on the tasks resolves your issue:
List<Task> tasks = new List<Task>();
for (int i = 0; i <= input.Length/4; i += 2048)
{
// ...
var test = Task.Factory.StartNew(() => addReferences(arrayToReference, iCopy));
tasks.Add(test);
// ...
}
Task.WaitAll(tasks);

Why is await async so slow?

I finally got VS2012 and got a simple demo up and working to check out the potential performance boost of async and await, but to my dismay it is slower! Its possible I'm doing something wrong, but maybe you can help me out. (I also added a simple Threaded solution, and that runs faster as expected)
My code uses a class to sum an array, based on the number of cores on your system (-1) Mine had 4 cores, so I saw about a 2x speed up (2.5 threads) for threading, but a 2x slow down for the same thing but with async/await.
Code: (Note you will need to added the reference to System.Management to get the core detector working)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Management;
using System.Diagnostics;
namespace AsyncSum
{
class Program
{
static string Results = "";
static void Main(string[] args)
{
Task t = Run();
t.Wait();
Console.WriteLine(Results);
Console.ReadKey();
}
static async Task Run()
{
Random random = new Random();
int[] huge = new int[1000000];
for (int i = 0; i < huge.Length; i++)
{
huge[i] = random.Next(2);
}
ArraySum summer = new ArraySum(huge);
Stopwatch sw = new Stopwatch();
sw.Restart();
long tSum = summer.Sum();
for (int i = 0; i < 100; i++)
{
tSum = summer.Sum();
}
long tticks = sw.ElapsedTicks / 100;
long aSum = await summer.SumAsync();
sw.Restart();
for (int i = 0; i < 100; i++)
{
aSum = await summer.SumAsync();
}
long aticks = sw.ElapsedTicks / 100;
long dSum = summer.SumThreaded();
sw.Restart();
for (int i = 0; i < 100; i++)
{
dSum = summer.SumThreaded();
}
long dticks = sw.ElapsedTicks / 100;
long pSum = summer.SumParallel();
sw.Restart();
for (int i = 0; i < 100; i++)
{
pSum = summer.SumParallel();
}
long pticks = sw.ElapsedTicks / 100;
Program.Results += String.Format("Regular Sum: {0} in {1} ticks\n", tSum, tticks);
Program.Results += String.Format("Async Sum: {0} in {1} ticks\n", aSum, aticks);
Program.Results += String.Format("Threaded Sum: {0} in {1} ticks\n", dSum, dticks);
Program.Results += String.Format("Parallel Sum: {0} in {1} ticks\n", pSum, pticks);
}
}
class ArraySum
{
int[] Data;
int ChunkSize = 1000;
int cores = 1;
public ArraySum(int[] data)
{
Data = data;
cores = 0;
foreach (var item in new System.Management.ManagementObjectSearcher("Select * from Win32_Processor").Get())
{
cores += int.Parse(item["NumberOfCores"].ToString());
}
cores--;
if (cores < 1) cores = 1;
ChunkSize = Data.Length / cores + 1;
}
public long Sum()
{
long sum = 0;
for (int i = 0; i < Data.Length; i++)
{
sum += Data[i];
}
return sum;
}
public async Task<long> SumAsync()
{
Task<long>[] psums = new Task<long>[cores];
for (int i = 0; i < psums.Length; i++)
{
int start = i * ChunkSize;
int end = start + ChunkSize;
psums[i] = Task.Run<long>(() =>
{
long asum = 0;
for (int a = start; a < end && a < Data.Length; a++)
{
asum += Data[a];
}
return asum;
});
}
long sum = 0;
for (int i = 0; i < psums.Length; i++)
{
sum += await psums[i];
}
return sum;
}
public long SumThreaded()
{
long sum = 0;
Thread[] threads = new Thread[cores];
long[] buckets = new long[cores];
for (int i = 0; i < cores; i++)
{
int start = i * ChunkSize;
int end = start + ChunkSize;
int bucket = i;
threads[i] = new Thread(new ThreadStart(() =>
{
long asum = 0;
for (int a = start; a < end && a < Data.Length; a++)
{
asum += Data[a];
}
buckets[bucket] = asum;
}));
threads[i].Start();
}
for (int i = 0; i < cores; i++)
{
threads[i].Join();
sum += buckets[i];
}
return sum;
}
public long SumParallel()
{
long sum = 0;
long[] buckets = new long[cores];
ParallelLoopResult lr = Parallel.For(0, cores, new Action<int>((i) =>
{
int start = i * ChunkSize;
int end = start + ChunkSize;
int bucket = i;
long asum = 0;
for (int a = start; a < end && a < Data.Length; a++)
{
asum += Data[a];
}
buckets[bucket] = asum;
}));
for (int i = 0; i < cores; i++)
{
sum += buckets[i];
}
return sum;
}
}
}
Any thoughts? Am I doing async/await wrong? I'll be happy to try any suggestions.
It's important to separate "asynchrony" from "parallelization". await is there to help make writing asynchronous code easier. Code that runs in parallel may (or may not) involve asynchrony, and code that is asynchronous may or may not run in parallel.
Nothing about await is designed to make parallel code faster. The purpose of await is to make writing asynchronous code easier, while minimizing the negative performance implications. Using await won't ever be faster than correctly written non-await asynchronous code (although because writing correct code with await is easier, it will sometimes be faster because the programmer isn't capable of writing that asynchronous code correctly without await, or isn't willing to put the time in to do so. If the non-async code is written well it will perform about as well, if not a tad better, than the await code.
C# does have support specifically for parallelization, it's just not specifically though await. The Task Parallel Library (TPL) as well as Parallel LINQ (PLINQ) have several very effective means of parallelizing code that is generally more efficient than naive threaded implementations.
In your case, an effective implementation using PLINQ might be something like this:
public static int Sum(int[] array)
{
return array.AsParallel().Sum();
}
Note that this will take care of efficiently partitioning the input sequence into chunks that will be run in parallel; it will take care of determining the appropriate size of chunks, and the number of concurrent workers, and it will appropriately aggregate the results of those workers in a manor that is both properly synchronized to ensure a correct result (unlike your threaded example) and efficient (meaning that it won't completely serialize all aggregation).
async isn't intended for heavy-duty parallel computation. You can do basic parallel work using Task.Run with Task.WhenAll, but any serious parallel work should be done using the task parallel library (e.g., Parallel). Asynchronous code on the client side is about responsiveness, not parallel processing.
A common approach is to use Parallel for the parallel work, and then wrap it in a Task.Run and use await on it to keep the UI responsive.
Your benchmark has a couple of flaws:
You are timing the first run which includes initialization time (loading class Task, JIT-compilation etc.)
You are using DateTime.Now, which is too inaccurate for timings in the millisecond range. You'll need to use StopWatch
With these two issues fixed; I get the following benchmark results:
Regular Sum: 499946 in 00:00:00.0047378
Async Sum: 499946 in 00:00:00.0016994
Threaded Sum: 499946 in 00:00:00.0026898
Async now comes out as the fastest solution, taking less than 2ms.
This is the next problem: timing something as fast as 2ms is extremely unreliable; your thread can get paused for longer than that if some other process is using the CPU in the background. You should average the results over several thousands of benchmark runs.
Also, what's going on with your number of core detection? My quad-core is using a chunk size of 333334 which allows only 3 threads to run.
On a quick look, the results are expected: your async sum is using just one thread, while you asynchronously wait for it to finish, so it's slower than the multi-threaded sum.
You'd use async in case you have something else to finish while it's doing its job. So, this wouldn't be the right test for any speed/response improvements.

Parallel loop in winrt

for (int j = 0; j < 10; j++)
{
for (long i = 0; i < bound / 10; i++)
{
routeLine.Locations.Add(new Location
{
Latitude = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[k][0],
Longitude = ((BingMapsRESTService.Route)(r.ResourceSets[0].Resources[0])).RoutePath.Line.Coordinates[k][1]
});
k++;
}
await Task.Delay(TimeSpan.FromMilliseconds(1));
Temp("Drawing Route (" + ((j * 10)/2).ToString() + "%)"); // to show progress,Temp sets text property of a textbox
}
Bound has value between 6000 to 10000. This loop takes time and hang the UI that's why i divided the loop into 10 parts and used task.delay.
Is it possible to run all ten loops in parallel? I can't use thread as I can't create object of Location class in new thread. It throws an error of task being marshaled by some another thread
Yes it is possible; you can use TPL and Parallel.ForEach:
TPL
Parallel.ForEach

For Loop Constructs

Will this loop execute exactly N Times?
for (int i = 0; i < N; i++)
{
//statement
someMethodCall();
}
Will this loop execute at most N Times?
for (int i = 1; i < N; i++)
{
someMethodCall();
}
Will this loop execute at least N Times?
for (int i = 0; i <= N; i++)
{
//statement
someMethodCall();
}
What should I do if I need to execute the statement between m and n times, e.g. calling a method?
The answers to your three questions are yes, no, and yes, I suppose, although that third answer is a bit deceptive; it'll execute N times, no more and no less (unless there's an exception which terminates the loop abnormally.) You can write a for loop to loop a definite number of times, or until some condition becomes true, and then you have the ability to use break or return to terminate the loop early.
But there's no notion of executing "at least N times;" it's simply not part of this -- or any other -- computer language.
A loop formula can be calculated as
Math.Round (condition - initialization )/increment
In first case its
(N-0)/1 which evaluates to N times
In second case its
(N-1)/1 which evaluates to N-1 times
In third case its
(N-0+1)/1 which evaluates to N+1 times
How would I do if I need to execute statement between m and n times? For instance I want to call one method between m and n times?
then check it in condition
for (int i = m; i < n; i++)
{
someMethodCall();
}
In for loop you can have these code,
N Time
for (int i = 0; i < N; i++) {
}
N+1 Time
for (int i = 0; i <= N; i++) {
}
And if there isn't any time and you want handle it your self you can use this,
for (;;) {
//do something and don't forget use break or return !
}
OR
while(x>10){
}
and loop in loop is good as Ernest Friedman-Hill said
for (int i = 0; i <=10; i++) {
for (int i = 0; i < length; i++) {
}
}
use 2xTab for visual studio help.

Categories

Resources