I am trying to use a Parallel.for loop to speed up my process. I have not been able to make this work, because the indices that I used ran out of bounds. After researching the site, I think I know what I am doing wrong, and I thought that I found the solution too in the form of temporary variables that store the loop variables before entering the action in the code. Yet, this does not work in my example. I have found the link that somebody provided to System.Collections.Concurrent, which supposedly provides safe threading for situations like these, yet I do not know how to use that collection. How do I go about this?
I have tried to create a copy-paste code for you guys to run, but I do something wrong there which probably indicates my inexperience:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text;
namespace empty
{
class Program
{
double[, , ,] info = new double[100, 100, 10, 5];
void calculate()
{
int row;
int layer, tex_class;
double result;
try
{
for (row = 0; row < 100; row++)
{
Parallel.For(0, 99, col =>
{
int tempcol = col;
for (layer = 0; layer < 10; layer++)
{
int templayer = layer;
for (tex_class = 0; tex_class < 5; tex_class++)
{
int tempclass = tex_class;
result = info[row, tempcol, templayer, tempclass];
}
//other code
}
});
}
}
catch { }
}
static void Main(string[] args)
{
calculate();
}
}
}