I
I
Ilya2014-06-05 09:12:27
.NET
Ilya, 2014-06-05 09:12:27

How to specify the number of cores and threads when parallelizing?

Hello! The task is quite simple, but something I ran into difficulties. The bottom line is this: you need to produce a product of matrices on several cores, on several threads.
About kernels:
This task is solved using the Parallel class :

The code
List<Action> actions = new List<Action>();
int m = a.Rows;
int step = m / countCPU;
// производим разделение начальных данных (то есть дробим матрицу на подматрицы)
for (int i = 0; i < countCPU; i++)
{
    int indexMin = i * step;
    int indexMax = (i == (countCPU- 1)) ? (m - 1) : (step * (i + 1) - 1);
    // метод CalcMulty выполняет собственное произведение матриц
    actions.Add(delegate() { CalcMulty(a.getRangeRows(indexMin, indexMax), b); });
}
// выполняем операции и делаем замер времени
Stopwatch time = new Stopwatch();
time.Start();
Parallel.Invoke(new ParallelOptions() { MaxDegreeOfParallelism = countCPU}, actions.ToArray());
time.Stop();
return time.Elapsed;

Everything is in order here, tasks are executed on cores, but the question is how to specify the number of THREADS on which this task needs to be performed (for example: 1 core 1 thread, 1 core 2 threads, 2 cores, 1 thread per core, etc)
Preferably with a small exemplary, thanks!
PS maybe this can be done with TaskScheduler ?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question