private void AddRemainders(object jobPartObject)
        {
            JobPart part     = (JobPart)jobPartObject;
            int     startRow = part.Start;
            int     endRow   = part.End;

            for (int i = startRow; i < endRow; i++)
            {
                for (int j = 0; j < _c; j++)
                {
                    _result[i, j] = _result[i, j] + _g[i, _b - 1] * _h[_b - 1, j];
                }
            }
        }
        private void DoParallel(ParameterizedThreadStart job)
        {
            Thread[] threads = new Thread[_totalThreads];

            int threadRange = _lineCount / _totalThreads;
            int remainder   = _lineCount % _totalThreads;

            int last = 0;

            for (int i = 0; i < _totalThreads; i++)
            {
                threads[i] = new Thread(job);

                int     start   = i * threadRange;
                int     end     = start + threadRange;
                JobPart jobPart = new JobPart(start, end);
                threads[i].Start(jobPart);
                last = end;
            }

            // Adding remainders
            List <JobPart> jobParts = new List <JobPart>(remainder);

            for (int i = 0; i < remainder; i++)
            {
                jobParts.Add(new JobPart(last, last + 1));
                last++;
            }

            // Searching for finished threads
            while (jobParts.Count > 0)
            {
                for (int i = 0; i < threads.Length; i++)
                {
                    if (!threads[i].IsAlive)
                    {
                        //threads[i].Start(jobParts[0]);
                        threads[i] = new Thread(job);
                        threads[i].Start(jobParts[0]);
                        break;
                    }
                }
                jobParts.RemoveAt(0);
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }
        }
        private void GetColFactors(object jobPartObject)
        {
            JobPart part     = (JobPart)jobPartObject;
            int     startRow = part.Start;
            int     endRow   = part.End;

            for (int i = startRow; i < endRow; i++)
            {
                _colFactors[i] = _h[0, i] * _h[1, i];
                for (int j = 2; j <= _d; j++)
                {
                    _colFactors[i] = _colFactors[i] + _h[2 * j - 2, i] * _h[2 * j - 1, i];
                }
            }
        }
        private void GetRowFactors(object jobPartObject)
        {
            JobPart part     = (JobPart)jobPartObject;
            int     startRow = part.Start;
            int     endRow   = part.End;

            for (int i = startRow; i < endRow; i++)
            {
                _rowFactors[i] = _g[i, 0] * _g[i, 1];
                for (int j = 2; j <= _d; j++)
                {
                    _rowFactors[i] = _rowFactors[i] + _g[i, 2 * j - 2] * _g[i, 2 * j - 1];
                }
            }
        }
        private void MatrixCalculation(object jobPartObject)
        {
            JobPart part     = (JobPart)jobPartObject;
            int     startRow = part.Start;
            int     endRow   = part.End;

            for (int i = startRow; i < endRow; i++)
            {
                for (int j = 0; j < _c; j++)
                {
                    _result[i, j] = -_rowFactors[i] - _colFactors[j];
                    for (int k = 1; k <= _d; k++)
                    {
                        _result[i, j] = _result[i, j] +
                                        (_g[i, 2 * k - 2] + _h[2 * k - 1, j]) *
                                        (_g[i, 2 * k - 1] + _h[2 * k - 2, j]);
                    }
                }
            }
        }
Example #6
0
        private void Multiply(object jobPartObj)
        {
            JobPart jobPart  = (JobPart)jobPartObj;
            int     startRow = jobPart.Start;
            int     endRow   = jobPart.End;

            if (endRow > _lineCount)
            {
                endRow = _lineCount;
            }

            for (int i = startRow; i < endRow; i++)
            {
                for (int j = 0; j < _result.Cols; j++)
                {
                    _result[i, j] = 0;
                    for (int k = 0; k < _a.Rows; k++)
                    {
                        _result[i, j] = _result[i, j] + _a[i, k] * _b[k, j];
                    }
                }
            }
        }
Example #7
0
        public void Process(int threadCount, Matrix a, Matrix b)
        {
            _totalThreads = threadCount;
            _a            = a;
            _b            = b;

            _result    = new Matrix(a.Cols, b.Rows);
            _lineCount = _result.Rows;

            Thread[] threads = new Thread[threadCount];

            Stopwatch timer = new Stopwatch();

            timer.Start();

            // Task scheduling here
            int            oneThreadRange = _lineCount / threadCount;
            int            remainder      = _lineCount % threadCount;
            List <JobPart> jobParts       = new List <JobPart>(remainder);

            int last = 0;

            for (int i = 0; i < threadCount; i++)
            {
                int     start   = i * oneThreadRange;
                int     end     = start + oneThreadRange;
                JobPart jobPart = new JobPart(start, end);
                threads[i] = new Thread(Multiply);
                threads[i].Start(jobPart);
                last = end;
            }

            // Adding remainders
            for (int i = 0; i < remainder; i++)
            {
                jobParts.Add(new JobPart(last, last + 1));
                last++;
            }

            // Searching for finished threads
            while (jobParts.Count > 0)
            {
                for (int i = 0; i < threads.Length; i++)
                {
                    if (!threads[i].IsAlive)
                    {
                        //threads[i].Start(jobParts[0]);
                        threads[i] = new Thread(Multiply);
                        threads[i].Start(jobParts[0]);
                        break;
                    }
                }
                jobParts.RemoveAt(0);
            }

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i].Join();
            }

            // end of scheduling

            timer.Stop();
            _ticks = timer.ElapsedTicks;
        }