Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4.  
  5. namespace LU_cSharp
  6. {
  7.     class Program
  8.     {
  9.         static int THREAD_COUNT = 1;
  10.         static int SIZE = 3;
  11.         static double[] matrix = new double[SIZE * SIZE];
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             var rng = new Random();
  16.             var sw = new Stopwatch();
  17.  
  18.             for (int i = 0; i < SIZE * SIZE; ++i)
  19.             {
  20.                 matrix[i] = rng.Next(10) + 1;
  21.             }
  22.  
  23.             sw.Start();
  24.  
  25.             var threads = new Thread[THREAD_COUNT];
  26.  
  27.             for (int i = 0; i < THREAD_COUNT; ++i)
  28.             {
  29.                 threads[i] = new Thread(new ParameterizedThreadStart(LuDecompose));
  30.                 threads[i].Start(i);
  31.             }
  32.             for (int i = 0; i < THREAD_COUNT; ++i)
  33.             {
  34.                 threads[i].Join();
  35.             }
  36.             sw.Stop();
  37.  
  38.             Console.WriteLine("L");
  39.             for (int i = 0; i < SIZE; ++i)
  40.             {
  41.                 for (int j = 0; j < SIZE; ++j)
  42.                 {
  43.                     if (i > j)
  44.                     {
  45.                         Console.Write(matrix[i * SIZE + j] + "\t");
  46.                     }
  47.                     else if (i == j)
  48.                     {
  49.                         Console.Write(1.0 + "\t");
  50.                     }
  51.                     else
  52.                     {
  53.                         Console.Write(0.0 + "\t");
  54.                     }
  55.                 }
  56.                 Console.WriteLine("");
  57.             }
  58.  
  59.             Console.WriteLine("U");
  60.             for (int i = 0; i < SIZE; ++i)
  61.             {
  62.                 for (int j = 0; j < SIZE; ++j)
  63.                 {
  64.                     if (i <= j)
  65.                     {
  66.                         Console.Write(matrix[i * SIZE + j] + "\t");
  67.                     }
  68.                     else
  69.                     {
  70.                         Console.Write(0.0 + "\t");
  71.                     }
  72.                 }
  73.                 Console.WriteLine("");
  74.             }
  75.  
  76.  
  77.             Console.WriteLine("finished with " + THREAD_COUNT + " threads after: " + sw.ElapsedMilliseconds + " ms");
  78.  
  79.             Console.ReadKey();
  80.         }
  81.        
  82.         public static void LuDecompose(object threadId)
  83.         {
  84.             int rowID;
  85.             int workShare = SIZE / THREAD_COUNT;
  86.             for (int columnID = workShare * (int)threadId; columnID < SIZE && columnID < workShare * ((int)threadId + 1); ++columnID)
  87.             {
  88.                 for (rowID = columnID + 1; rowID < SIZE; rowID++)
  89.                 {
  90.                     matrix[rowID * SIZE + columnID] /= matrix[columnID * SIZE + columnID];
  91.                 }
  92.  
  93.                 for (rowID = columnID + 1; rowID < SIZE; rowID++)
  94.                 {
  95.                     int innerColumnID;
  96.                     double factor = matrix[rowID * SIZE + columnID];
  97.                     for (innerColumnID = columnID + 1; innerColumnID < SIZE; innerColumnID++)
  98.                     {
  99.                         matrix[rowID * SIZE + innerColumnID] -= factor * matrix[columnID * SIZE + innerColumnID];
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement