I have this simple function that is used to generate neuron connections position:
using System.Collections.Generic;
using System.Drawing;
public static List<Point> GetConnectionsConnectedPosition(int connectedLayerI, int startingConnectedNeuronIndex, int outputLength)
{
var connections = new List<Point>();
for (int i = 0; i < outputLength; i++)
{
connections.Add(new Point(connectedLayerI, startingConnectedNeuronIndex + i));
}
return connections;
}
And now I want to create all the connections positions for a specific neuron connections using this constructor code:
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
internal NeuronConnectionsInfo(int layerIndex, int previousLayerLength, double minWeight, double maxWeight, double valueClosestTo0)
{
Weights = new List<double>();
ConnectedNeuronsPos = new List<Point>();
int connectionsPerTask = 750;
// specify task job
int taskCount = previousLayerLength / connectionsPerTask;
int leftConnectionCount = previousLayerLength % connectionsPerTask;
// Initialize tasks
List<Task<List<double>>> weigthsTasks = new List<Task<List<double>>>();
List<Task<List<Point>>> positionsTasks = new List<Task<List<Point>>>();
for (int i = 0; i < taskCount; i++)
{
weightsTasks.Add(Task.Run(() => GenerateWeigths(connectionsPerTask, minWeigth, maxWeight, valueClosestTo0));
positionsTasks.Add(Task.Run(() => GetConnectionsConnectedTo(layerIndex - 1, connectionsPerTask * i, connectionsPerTask);
}
weightsTasks.Add(Task.Run(() => GenerateWeights(connectionsPerTask, minWeight, maxWeight, valueClosestTo0));
positionsTasks.Add(Task.Run(() => GetConnectionsConnectedTo(layerIndex - 1, connectionsPerTask * taskCount, leftConnectionsCount));
//wait for tasks to finish
bool isFinished = false;
while (!isFinished)
{
isFinished = true;
Thread.Sleep(20);
foreach (weightsTask in weightsTasks)
{
isFinished = isFinished && weightTask.IsCompleted;
}
if (isFinished)
foreach (positionTask in positionsTasks)
{
isFinished = isFinished && positionTask.isCompleted;
}
}
for (int i = 0; i < weightsTasks; i++)
{
Weights.AddRange(weightsTasks[i].Result);
ConnectedNeuronsPos.AddRange(positionsTasks[i].Result);
}
}
Weights are being generated alright, but the problem begins with positions' X (connectedLayerI) starting at connectionsPerTask * taskCount, basically position's X starting where the last task X position should start even though y (connectedNeuronI) is being generated ok and Task's result length is also being generated alright.
While debugging I noticed that if I executed the instructions at a human speed the result was alright, then, I tried adding Thread.Sleep(20); before and after calling Task.Run(() => GetConnectionsConnectedTo(...));
This seems to solve the issue but I'm not confident that it'll solve the issue with all processors because it seems to be an error with C#'s static programming.
All answers will be accepted so don't have any hesitation on answering with your opinion or with a more optimized or secure method. Thanks in advance.