简体   繁体   中英

Combining arrays created via multiple threads

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM