public void Thread_mult(int thread_am) { int partition = n / thread_am; // 3=6/2 int start = 0, finish = partition + (n - partition * thread_am); // 3 = 3 + (6 - 3 * 2) if (thread_am > n) { partition = 1; finish = 1; } int count; ConcurrentBag <Thread> thread_list = new ConcurrentBag <Thread>(); ConcurrentBag <Mult> mul_list = new ConcurrentBag <Mult>(); for (int k = 0; k < thread_am; k++) { Mult mult = new Mult(n, m, l, mtr1, mtr2); mult.Set_bounds(start, finish); Thread thread = new Thread(mult.Multiply); thread.Start(); thread_list.Add(thread); mul_list.Add(mult); count = thread_list.Count; for (int i = 0; i < count; i++) { thread_list.TryTake(out Thread curr); curr.Join(); } count = mul_list.Count; for (int i = 0; i < count; i++) { mul_list.TryTake(out Mult mul); mul.Copy(ref result); } start = finish; finish += partition; if (finish > n) { finish = n; } } }
public void Thread_vinograd(int thread_am) { int[] row_fact, col_fact; int coef = m / 2; int partition = n / thread_am; int start = 0, finish = partition + (n - partition * thread_am); if (thread_am > n) { partition = 1; finish = partition; } int count; row_fact = new int[n]; col_fact = new int[l]; for (int i = 0; i < n; i++) { for (int j = 0; j < coef; j++) { row_fact[i] += mtr1[i, 2 * j + 1] * mtr1[i, 2 * j]; } } for (int i = 0; i < l; i++) { for (int j = 0; j < coef; j++) { col_fact[i] = col_fact[i] + mtr2[2 * j + 1, i] * mtr2[2 * j, i]; } } ConcurrentBag <Thread> thread_list = new ConcurrentBag <Thread>(); ConcurrentBag <Mult> mul_list = new ConcurrentBag <Mult>(); for (int k = 0; k < thread_am; k++) { Mult mult = new Mult(n, m, l, mtr1, mtr2, coef, row_fact, col_fact); mult.Set_bounds(start, finish); Thread thread = new Thread(mult.Vinograd_mult); thread.Start(); thread_list.Add(thread); mul_list.Add(mult); count = thread_list.Count; for (int i = 0; i < count; i++) { thread_list.TryTake(out Thread curr); curr.Join(); } count = mul_list.Count; for (int i = 0; i < count; i++) { mul_list.TryTake(out Mult mul); mul.Copy(ref result); } start = finish; finish += partition; if (finish > n) { finish = n; } } if (m % 2 != 0) { for (int i = 0; i < n; i++) { for (int j = 0; j < l; j++) { result[i, j] += mtr1[i, m - 1] * mtr2[m - 1, j]; } } } }
public static void Main(string[] args) { Console.WriteLine("Введите размерность матриц"); Console.WriteLine("Количество строк в первой матрице: "); int n = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Количество столбцов в первой матрице: "); int m = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Количество столбцов во второй матрице: "); int l = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Введите количество потоков: "); int thread_amount = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Результат работы алгоритмов на потоках"); Threading threading = new Threading(n, m, l); threading.Print_mtr1(); threading.Print_mtr2(); threading.Thread_mult(thread_amount); threading.Print_res(); threading.Dump_res(); threading.Thread_vinograd(thread_amount); threading.Print_res(); Console.WriteLine("Результат работы алгоритмов на основном потоке"); Mult mult_alg = new Mult(n, m, l); mult_alg.Print_mtr1(); mult_alg.Print_mtr2(); mult_alg.Standart_alg(); mult_alg.Print_res(); mult_alg.Dump_res(); mult_alg.Standart_vinograd_mult(); mult_alg.Print_res(); /*Console.WriteLine("Провести замер времени?(y/n)"); * if (Convert.ToChar(Console.ReadLine()) == 'y') * { * Console.WriteLine("Стандартный алгоритм на потоках"); * for (int j = 1; j < Environment.ProcessorCount * 4 + 1; j += 1) * { * int i = 1001; * Threading thread_mult = new Threading(i, i, i); * * var start = Process.GetCurrentProcess().UserProcessorTime; * thread_mult.Thread_mult(j); * var finish = Process.GetCurrentProcess().UserProcessorTime; * * var mult_time = (finish - start).Ticks; * * Console.Write("," + mult_time.ToString()); * } * Console.WriteLine(); * Console.WriteLine("Алгоритм Винограда на потоках"); * for (int j = 1; j < Environment.ProcessorCount * 4 + 1; j += 1) * { * int i = 1001; * * Threading thread_mult = new Threading(i, i, i); * var start = Process.GetCurrentProcess().UserProcessorTime; * thread_mult.Thread_vinograd(j); * var finish = Process.GetCurrentProcess().UserProcessorTime; * * var mult_time = (finish - start).Ticks; * * Console.Write("," + mult_time.ToString()); * } * Console.WriteLine(); * Console.WriteLine("Все алгоритмы на потоках (Стандартный, Виноград, Стандартный и Виноград на потоках)"); * for (int i = 101; i < 1012; i += 100) * { * Threading thread_mult = new Threading(i, i, i); * Mult mult = new Mult(i, i, i); * Console.Write(i.ToString()); * * var start = Process.GetCurrentProcess().UserProcessorTime; * mult.Standart_alg(); * var finish = Process.GetCurrentProcess().UserProcessorTime; * * var mult_time = (finish - start).Ticks; * * mult.Fill_mtr(); * * start = Process.GetCurrentProcess().UserProcessorTime; * mult.Standart_vinograd_mult(); * finish = Process.GetCurrentProcess().UserProcessorTime; * * var vinograd_time = (finish - start).Ticks; * * mult.Fill_mtr(); * * Console.Write("," + mult_time.ToString() + "," + vinograd_time.ToString()); * * for (int j = 1; j < Environment.ProcessorCount * 4 + 1; j *= 2) * { * start = Process.GetCurrentProcess().UserProcessorTime; * thread_mult.Thread_mult(j); * finish = Process.GetCurrentProcess().UserProcessorTime; * * mult_time = (finish - start).Ticks; * * mult.Fill_mtr(); * * start = Process.GetCurrentProcess().UserProcessorTime; * thread_mult.Thread_vinograd(j); * finish = Process.GetCurrentProcess().UserProcessorTime; * * vinograd_time = (finish - start).Ticks; * * mult.Fill_mtr(); * * Console.Write("," + mult_time.ToString() + "," + vinograd_time.ToString()); * } * Console.WriteLine(); * } * }*/ }