Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using System.Threading;
- namespace LU_cSharp
- {
- class Program
- {
- static int THREAD_COUNT = 1;
- static int SIZE = 3;
- static double[] matrix = new double[SIZE * SIZE];
- static void Main(string[] args)
- {
- var rng = new Random();
- var sw = new Stopwatch();
- for (int i = 0; i < SIZE * SIZE; ++i)
- {
- matrix[i] = rng.Next(10) + 1;
- }
- sw.Start();
- var threads = new Thread[THREAD_COUNT];
- for (int i = 0; i < THREAD_COUNT; ++i)
- {
- threads[i] = new Thread(new ParameterizedThreadStart(LuDecompose));
- threads[i].Start(i);
- }
- for (int i = 0; i < THREAD_COUNT; ++i)
- {
- threads[i].Join();
- }
- sw.Stop();
- Console.WriteLine("L");
- for (int i = 0; i < SIZE; ++i)
- {
- for (int j = 0; j < SIZE; ++j)
- {
- if (i > j)
- {
- Console.Write(matrix[i * SIZE + j] + "\t");
- }
- else if (i == j)
- {
- Console.Write(1.0 + "\t");
- }
- else
- {
- Console.Write(0.0 + "\t");
- }
- }
- Console.WriteLine("");
- }
- Console.WriteLine("U");
- for (int i = 0; i < SIZE; ++i)
- {
- for (int j = 0; j < SIZE; ++j)
- {
- if (i <= j)
- {
- Console.Write(matrix[i * SIZE + j] + "\t");
- }
- else
- {
- Console.Write(0.0 + "\t");
- }
- }
- Console.WriteLine("");
- }
- Console.WriteLine("finished with " + THREAD_COUNT + " threads after: " + sw.ElapsedMilliseconds + " ms");
- Console.ReadKey();
- }
- public static void LuDecompose(object threadId)
- {
- int rowID;
- int workShare = SIZE / THREAD_COUNT;
- for (int columnID = workShare * (int)threadId; columnID < SIZE && columnID < workShare * ((int)threadId + 1); ++columnID)
- {
- for (rowID = columnID + 1; rowID < SIZE; rowID++)
- {
- matrix[rowID * SIZE + columnID] /= matrix[columnID * SIZE + columnID];
- }
- for (rowID = columnID + 1; rowID < SIZE; rowID++)
- {
- int innerColumnID;
- double factor = matrix[rowID * SIZE + columnID];
- for (innerColumnID = columnID + 1; innerColumnID < SIZE; innerColumnID++)
- {
- matrix[rowID * SIZE + innerColumnID] -= factor * matrix[columnID * SIZE + innerColumnID];
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement