Combining arrays created via multiple threads - c#

I have been trying to create couple of 2-D arrays via multi-threading. Each threading will generate a small 2-D array. All of the 2-D will be consolidated and that is where I am having issue. I commented "//!this is causing error" towards the bottom of SimulatingMethod method. Please share your insight. Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ThreadExample
{
class Program
{
static void Main(string[] args)
{
double[,] randSims;
randSims = SimulatingClass.SimulatingMethod();
}
}
class SimulatingClass
{
public static double[,] SimulatingMethod()
{
int rowCount = 9;
int columnCount = 1;
int NumberOfCores = System.Environment.ProcessorCount;
int RowsForEachThread = rowCount / NumberOfCores;
Thread[] arrayOfThread = new Thread[NumberOfCores];
DataStuff[] dataStuff= new DataStuff[NumberOfCores];
for (int i = 0; i < NumberOfCores; i++)
{
dataStuff[i] = new DataStuff(RowsForEachThread, columnCount);
arrayOfThread[i] = new Thread(new ThreadStart(dataStuff[i].UpdateMatrixData));
arrayOfThread[i].Name = "Thread" + i;
arrayOfThread[i].Start();
}
for (int i = 0; i < NumberOfCores; i++)
{
arrayOfThread[i].Join();
}
//start combining arrays from different threads
var list = new List<double[,]>();
for (int m = 0; m < NumberOfCores; m++)
{
list.AddRange(dataStuff[m]); //!this is causing error
}
//trying to convert list back to array
double[,] array3 = list.ToArray(); //!this is causing error
return array3;
}
}
class DataStuff
{
public double G;
public double[,] M;
public long steps, trials;
public DataStuff(long _steps, long _trials)
{
M = new Double[_steps, _trials]; // <- M is created in the constructor
G = 60;
steps = _steps;
trials = _trials;
}
public void UpdateMatrixData()
{
for (int i = 0; i < steps; i++)
{
for (int j = 0; j < trials; j++)
{
M[i, j] = i + j;
}
}
}
}
}

You should specify the property as follows:
list.Add(dataStuff[m].M);
It's because the dataStuff[m] is of type DataStuff, but the type double[,] expected as the list item.
If I understood you correctly, you need a consolidated 2D array. Try to declare it initially with desired dimensions:
double[,] array3 = new double[rowCount, columnCount];
And copy data from dataStuff array to it after processing:
for (int m = 0; m < NumberOfCores; m++)
{
Array.Copy(dataStuff[m].M, 0, array3, m * columnCount * RowsForEachThread, dataStuff[m].M.Length);
}
return array3;
And you don't need list at all.
Please note, that you have possible problems related to the rounding:
int RowsForEachThread = rowCount / NumberOfCores;
You should handle the situation when the rowCount is not divisible by the NumberOfCores.

Related

Why can't get matrix what I passed to Thread?

I want to do some basic stuff: get a matrix, and iterate on it with multiple threads. For example, if I have a 20x20 sized matrix, and I have 4 threads, then the first Thread have to iterate on the 5x20 sized matrix, the second one has to iterate on the same size, but from the 6th row to the 10th one, and so on. But, my program gets only the first part, the second, and third, fourth threads can't see their submatrixes. Why? Can anyone help me with this?
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace multi_thred_test
{
class thredData
{
public int my_simulation_size;
public int[,] my_simulation;
public string my_path;
public int my_nrOfAvailableThreads;
public int my_oneThreeadSubsimulationSize;
public thredData(ref int simsize, ref int[,] simu, ref string path, ref int availabelthrd, ref int subsimsize )
{
my_simulation_size = simsize;
my_simulation = new int[simsize, simsize];
for (int i = 0; i < simsize; i++)
{
for (int j = 0; j < simsize; j++)
{
my_simulation[i, j] = simu[i, j];
}
}
my_path = path;
my_nrOfAvailableThreads = availabelthrd;
my_oneThreeadSubsimulationSize = subsimsize;
}
}
class Program
{
public static int simulation_size = 20;
public static int[,] simulation = new int[20, 20];
public static string path = Directory.GetCurrentDirectory();
public static int nrOfAvailableThreads = Environment.ProcessorCount;
public static int oneThreeadSubsimulationSize = simulation_size / nrOfAvailableThreads;
public static thredData tmp;
public static void Main(string[] args)
{
for (int idx = 0; idx < simulation_size; idx++)
{
for (int jdx = 0; jdx < simulation_size; jdx++)
{
simulation[idx, jdx] = idx * 100 + jdx;
}
}
StreamWriter sw = new StreamWriter(path + "_matrix.txt");
for (int idx = 0; idx < simulation_size; idx++)
{
for (int jdx = 0; jdx < simulation_size; jdx++)
{
sw.Write(simulation[idx, jdx]+" ");
}
sw.WriteLine(" ");
}
sw.Close();
tmp = new thredData(ref simulation_size, ref simulation, ref path, ref nrOfAvailableThreads, ref oneThreeadSubsimulationSize);
//generate threads
for (int thrdnr = 0; thrdnr < nrOfAvailableThreads; thrdnr++)
{
Thread newThread = new Thread(ThreadMethod);
newThread.Name = Convert.ToString(thrdnr);
newThread.Start();
}
Console.ReadKey();
}
private static void ThreadMethod()
{
Thread thr = Thread.CurrentThread;
int simulationSize = Convert.ToInt32(thr.Name);
int thrd_simulation_size;
int[,] thrd_simulation;
string thrd_path;
int thrd_nrOfAvailableThreads;
int thrd_oneThreeadSubsimulationSize;
lock (tmp)
{
thrd_simulation_size = tmp.my_simulation_size;
thrd_simulation = new int[thrd_simulation_size, thrd_simulation_size];
for (int i = 0; i < tmp.my_simulation_size; i++)
{
for (int j = 0; j < tmp.my_simulation_size; j++)
{
thrd_simulation[i, j] = tmp.my_simulation[i, j];
}
}
thrd_path = tmp.my_path;
thrd_nrOfAvailableThreads = tmp.my_nrOfAvailableThreads;
thrd_oneThreeadSubsimulationSize = tmp.my_oneThreeadSubsimulationSize;
}
for (int i = thrd_simulation_size * simulationSize; i < thrd_oneThreeadSubsimulationSize; i++)
{
for (int j = 0; j < thrd_simulation_size; j++)
{
Console.Write(thr.Name +":"+ thrd_simulation[i,j] + " ");
}
Console.Write("\n");
}
}
}
}
Problem solved, the log part was wrong:
for (int i = thrd_oneThreeadSubsimulationSize * simulationSize; i < (thrd_oneThreeadSubsimulationSize * simulationSize) +thrd_oneThreeadSubsimulationSize; i++)
{
for (int j = 0; j < thrd_simulation_size; j++)
{
Console.Write(thr.Name + ":" + thrd_simulation[i, j] + " ");
}
Console.Write("\n");
}

Adding an int array to a list without it only takes the last version of the array

This is a simplified version of the problem im trying to solve. Im trying to add an int[] to a List, but it updates all the arrays in the List with the one from the last iteration. Why is that? And how does I solve this problem? This isn't a problem if it's just 1 int for example.
intArray = new int[9];
for (int i = 0; i < 9; i++)
{
intArray[i] = i;
}
Test.Add(intArray);
for (int i = 0; i < 9; i++)
{
intArray[i] = i * 2;
}
Test.Add(intArray);
foreach (var item in Test)
{
for (int i = 0; i < 9; i++)
{
Console.WriteLine(item[i]);
}
}
Console.ReadKey();
}
public static int[] intArray { get; set; }
public static List<int[]> Test = new List<int[]>();
Using new keyword you create intArray only once. Then there is only one reference. After that you add intArray reference to collection List for multiple times.
Try this,
intArray = new int[9];
for (int i = 0; i < 9; i++)
{
intArray[i] = i;
}
Test.Add(intArray);
intArray = new int[9]; // create new intArray here
for (int i = 0; i < 9; i++)
{
intArray[i] = i * 2;
}
Test.Add(intArray);
after u added your intArray object into your arraylist ,simply create another object of that
intArray = new int[length];
hope it will works

How can define a 26x2 array and then use LINQ on its rows?

Grrrr I have C# and multidimensional arrays. For some reason, coming from a C/C++ background, they really annoy me.
So when I run
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
static void Main(String[] args)
{
int T = Int32.Parse(Console.ReadLine());
for(int t = 0; t < T; ++t)
{
string str = Console.ReadLine();
if(str.Length % 2 == 1)
{
Console.WriteLine(-1);
continue;
}
int n = str.Length / 2;
// determine how many replacements s1 needs to be an anagram of s2
string s1 = str.Substring(0, n);
string s2 = str.Substring(n, n);
int[][] counter = new int[26][2];
int ascii_a = (int)'a';
for(int i = 0; i < n; ++i)
{
counter[(int)s1[i] - ascii_a][0] += 1;
counter[(int)s2[i] - ascii_a][1] += 1;
}
int count = counter.Select((pair => Math.Abs(pair[0] - pair[1]))).Sum();
Console.WriteLine(count);
}
}
}
I get
solution.cs(22,42): error CS0029: Cannot implicitly convert type int'
toint[][]' Compilation failed: 1 error(s), 0 warnings
No idea why.
I can change it to
int[,] counter = new int[26,2];
int ascii_a = (int)'a';
for(int i = 0; i < n; ++i)
{
counter[(int)s1[i] - ascii_a, 0] += 1;
counter[(int)s2[i] - ascii_a, 1] += 1;
}
int count = counter.Select((pair => Math.Abs(pair[0] - pair[1]))).Sum();
but then, of course, my LINQ statement breaks.
If you change
int[][] counter = new int[26][2];
to
int[][] counter = new int[26][];
for (int i = 0; i < counter.Length; i++)
counter[i] = new int[2];
code compiles. You can test the rest as you like. As you haven't provided necessary input in OP.
You can't define jagged array like that:
int[][] counter = new int[26][2];
I recommend reading on jagged arrays:
https://msdn.microsoft.com/en-us/library/2s05feca.aspx
https://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
In your case I'd suggest using not jagged, but multi-dimentional array:
var counter = new int[26,2];
What you are using here is a jagged array, and you can't new one like that:
int[][] counter = new int[26][2];
You have to declare the inner array separately :
int[][] counter = new int[26][];
for (int i = 0; i < 26; i++)
{
counter[i] = new int[2];
}
Alternatively, as #IvanStoev suggested, you can also use a LINQ one liner:
var counter = Enumerable.Range(0, 26).Select(_ => new int[2]).ToArray();
You can also use a 2-dimensional array, such as this one :
// notice there is only one bracket
int[,] counter = new int[26,2];
int ascii_a = (int)'a';
for(int i = 0; i < n; ++i)
{
counter[(int)s1[i] - ascii_a, 0] += 1;
counter[(int)s2[i] - ascii_a, 1] += 1;
}
// and, you will need to update your query,
// as linq would implicitly flatten the array
var count = Enumerable.Range(0, 26)
.Select(x => counter[x, 0] - counter[x, 1])
.Sum();
I would suggest defining a Counter struct then use an array of those instead of a multi-dimensional array.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Solution
{
struct Counter
{
public int c1;
public int c2;
}
static void Main(String[] args)
{
int T = Int32.Parse(Console.ReadLine());
for (int t = 0; t < T; ++t)
{
string str = Console.ReadLine();
if (str.Length % 2 == 1)
{
Console.WriteLine(-1);
continue;
}
int n = str.Length / 2;
// determine how many replacements s1 needs to be an anagram of s2
string s1 = str.Substring(0, n);
string s2 = str.Substring(n, n);
Counter[] counter = new Counter[26];
int ascii_a = (int)'a';
for (int i = 0; i < n; ++i)
{
counter[(int)s1[i] - ascii_a].c1 += 1;
counter[(int)s2[i] - ascii_a].c2 += 1;
}
int count = counter.Select((pair => Math.Abs(pair.c1 - pair.c2))).Sum();
Console.WriteLine(count);
}
}
}

How to use a Linked-List implementation with this Hash Function to avoid collisions?

So for my assignment in my class this week, I have to demonstrate a hash function that stores data into a data structure and use a linked list implementation to avoid collisions. Given the source code from my professor, he stated that the code was correct but to change the array solution to a linked list. I'm not sure what he meant by that but here is the code below:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hashing
{
class hashFunction
{
public hashFunction() { }
public int HashFunc(String s, String[] arr)
{
int total = 0;
char[] cname = s.ToCharArray();
for (int i = 0; i < cname.Length; i++)
total += 37 * total + (int)cname[i];
total = total % arr.Length;
if (total < 0)
total += arr.Length;
return (int)total;
}
public int Collision(int oldHashKey, String[] arr)
{
int newHashKey = 0;
for (int i = 0; i < arr.Length; i++)
{
newHashKey = 2 * oldHashKey - 1;
if (arr[newHashKey] == null)
break;
}
return (int)newHashKey;
}
}
class Program
{
static void Main(string[] args)
{
String[] names = new String[10007];
String[] Animals = new String[] { "Lions", "Tigers", "Bears", "Aligators", "Snakes", "Eagles" };
storeMessage(names, Animals);
}
public static void storeMessage(String[] arrMessage, String[] arrAnimal)
{
hashFunction newHashKey = new hashFunction();
int[] arrayKeys = new int[arrAnimal.Length];
String nm; int hashVal;
for (int i = 0; i < 6; i++)
{
nm = arrAnimal[i];
hashVal = newHashKey.HashFunc(nm, arrMessage);
while (arrMessage[hashVal] != null)
hashVal = newHashKey.Collision(hashVal, arrMessage);
arrMessage[hashVal] = nm;
arrayKeys[i] = hashVal;
}
}
}
}
It is somewhere with the method for the Collisions that it has to be linked list according to his instruction but I'm not sure.
See LinkedList.
LinkedList allows fast inserts and removes. It implements a linked
list. Each object is separately allocated. Certain operations do not
require the whole collection to be copied. In many common cases
LinkedList hinders performance.
An example for implementing this in the Collisions:
public int Collision(int oldHashKey, LinkedList<string> arr)
{
int newHashKey = 0;
for (int i = 0; i < arr.Count; i++)
{
newHashKey = 2 * oldHashKey - 1;
if (arr[newHashKey] == null)
break;
}
return (int)newHashKey;
}
Notice that nothing much really changed. It's just that a LinkedList behaves like a List because it implements ICollection and IEnumerable. It's more convenient than a plain old array because you can just call the method Add and Remove if needed.

Calculating Mahalanobis distance with C#

I'm trying to calculate the mahalanobis distance with c#. I can't find any real good examples online and I'm new to C#. I am especially having trouble getting the covariance matrix to run right. Any help would be appreciated. Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathNet.Numerics.LinearAlgebra.Double;
namespace MahalanobisDistance
{
class Program
{
static void Main(string[] args)
{
Program p = new Program();
DenseVector vector1 = new DenseVector(4);
DenseVector vector2 = new DenseVector(4);
DenseMatrix matrix1 = new DenseMatrix(vector1.Count/2);
vector1[0] = 1;
vector1[1] = 2;
vector1[2] = 3;
vector1[3] = 4;
vector2[0] = 2;
vector2[1] = 12;
vector2[2] = 14;
vector2[3] = 18;
matrix1 = p.twoPassCovariance(vector1, vector2);
for(int i = 0; i < matrix1.RowCount; i++)
{
for(int k = 0; k < matrix1.ColumnCount; k++)
{
Console.Write(matrix1[k, i] + " ");
}
//Mahalanobis2(v1, v2, covariance);
Console.Write("\n");
}
}
public DenseMatrix twoPassCovariance(DenseVector data1, DenseVector data2)
{
int n = data1.Count;
double mean1 = data1.Average();
double mean2 = data2.Average();
DenseMatrix covariance = new DenseMatrix(data1.Count);
double x;
for(int i = 0; i < 2; i++)
{
for (int k = 0; k < n; k++)
{
double a = data1[i] - mean1;
double b = data2[k] - mean2;
x = a*b;
covariance[i, k] = x;
}
}
covariance.Multiply(1/n);
return covariance;
}
}}
In the i loop in the twoPassCovariance method, I believe you should loop to i < n rather than i < 2.

Categories

Resources