I'm using Parallel.Foreach
to operate over a large array where each array element corresponds to a line in a bitmap image. However, Parallel.Foreach
appears to allocate a fix number of threads, say 6 threads, and gives 1/6 of the array to the first thread, and 1/6 of the array to the next thread in order. example:
[0]=>thread1, [1]=>thread1, [2]=>thread1, [3]=>thread1
[4]=>thread2, [5]=>thread2, [6]=>thread2, [7]=>thread2,
.. and so forth
roughly speaking, want I want is an interlace pattern that alternates between threads for each increment of array index...for example:
[0]=>thread1 [1]=>thread2 [2]=>thread3 [4]=>thread4, [5]=>thread1, [6]=>thread2, etc...
is there a way to change the interlace pattern that Parallel.Foreach assigns to each parallel thread actively executing?
I'm trying to get the image to render using a gif interlaced pattern rather than having the bitmap lines rendered sequential in 6 different lines on the image that fill downwards...
using System.Threading.Tasks;
using System.Threading;
using System.Collections.Concurrent;
//…
void button_click() {
Task.Run(() =>{
start_task();
});
}
int ds_height=600;
public bool start_task()
{
var cpu75 = Convert.ToInt32(Math.Ceiling((
Environment.ProcessorCount * 0.75) * 1.0));
//MandelInit();
int[] ytmp;
int version = 2;
if (version == 1) {
ytmp = MandelYLace();
}
else {
int[] ytmp = new int[ds_height];
for(int i=0; i < ds_height; i++)
{
ytmp[i] = i;
}
Parallel.ForEach(
ytmp, //ylace,
new ParallelOptions { MaxDegreeOfParallelism = cpu75 },
yy => {
//ybuff[yy] = MandelLine(yy);
//ydone.Enqueue(yy);
}
);
stop = true;
return true;
}
// Interlace Y-Lines using GIF Interlaced method
int[] MandelYLace()
{
var ylace = new int[ds_height];
for (int y = 0, yg=0, yy=0; y < ds_height; y++)
{
ylace[y] = yy;
if (yg == 0 || yg == 1)
yy += 8;
else if (yg == 2)
yy += 4;
else if (yg == 3)
yy += 2;
if (yy >= ds_height)
{
yg = (yg + 1) % 4;
if (yg == 1) yy = 4;
else if (yg == 2) yy = 2;
else if (yg == 3) yy = 1;
else if (yg == 0) yy = 0;
}
}
return ylace;
}