Advertisement
wwilkowski

Untitled

Mar 28th, 2021
1,759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.54 KB | None | 0 0
  1. // Wykorzystując klasy Thread i Fiber napisać program przyjmujący cztery
  2. // parametry m, n, p, q wywołania programu będące liczbami całkowitymi.
  3. // Wątek główny programu tworzy m na n elementową tablicę elementów typu
  4. // real i uruchamia m wątków potomnych i przekazuje każdemu z nich po
  5. // jednym wierszu tej tablicy. Każdy z wątków potomnych tworzy p ścieżek
  6. // i każda ścieżka wylicza sumę kwadratów części wiersza wykonując q iteracji.
  7. // Wątki potomne przełączają się pomiędzy poszczególnymi ścieżkami do momentu
  8. // gdy zostanie wyliczona suma kwadratów całego wiersza. Wątki potomne przekazują
  9. // wyniki do wątku głównego, który wylicza sumę wyników i wypisuje ją na ekranie.
  10. // Uwaga. Wątki muszą pracować równolegle i nie mogą używać zmiennych globalnych.
  11.  
  12. import core.thread;
  13. import std.stdio : write, writeln, writef, writefln;
  14. import std.conv : parse;
  15. import std.random : uniform;
  16. import std.math : pow;
  17.  
  18. class DerivedFiber : Fiber {
  19.     private int begin_index;
  20.     private int iterations;
  21.     private real[] row;
  22.     private real* r;
  23.     this(real[] row, int begin_index, int iterations, real* result) {
  24.         this.begin_index = begin_index;
  25.         this.iterations = iterations;
  26.         this.row = row;
  27.         this.r = result;
  28.         super( &run );
  29.     }
  30.  
  31. private :
  32.     void run() {
  33.         for (int i=0; i<this.iterations; i++) {
  34.             *(this.r) += pow(this.row[begin_index + i], 2);
  35.             this.yield();
  36.         }
  37.     }
  38. }
  39.  
  40.  
  41. class DerivedThread : Thread {
  42.     private real[] i;
  43.     private real result;
  44.     private Fiber[] fibers;
  45.     this(real[] i, int p, int q) {
  46.         this.i = i;
  47.         this.result = 0;
  48.         for (int j=0; j<p; j++) {
  49.             fibers ~= new DerivedFiber(this.i, cast(int)(j * (i.length / p)), q, &this.result);
  50.         }
  51.         super(&run);
  52.     }
  53.  
  54.     private void run() {
  55.         foreach(fiber; fibers) {
  56.             while (fiber.state != Fiber.State.TERM) {
  57.                 fiber.call();
  58.             }
  59.         }
  60.     }
  61.  
  62.     public real getResult() {
  63.         return this.result;
  64.     }
  65. }
  66.  
  67. int main(string[] args) {
  68.     if (args.length < 5) {
  69.         writeln("Command: ", args[0], " number_m number_n number_p number_q");
  70.         return 1;
  71.     }
  72.  
  73.     int m = parse!int(args[1]);
  74.     int n = parse!int(args[2]);
  75.     int p = parse!int(args[3]);
  76.     int q = parse!int(args[4]);
  77.  
  78.     if (n % p !=0) {
  79.         writeln("WARNING: The number of fibers should be divisible by row length!");
  80.         return 1;
  81.     }
  82.  
  83.     if (p * q != n) {
  84.         writeln("WARNING: The number of iterations should be equal to the row length divided by the number of fibers!");
  85.         return 1;
  86.     }
  87.  
  88.     real[][] matrix = new real[][](m, n);
  89.     for (int i = 0; i < matrix.length; i++) {
  90.         for (int j = 0; j < matrix[i].length; j++) {
  91.             matrix[i][j] = uniform(0, 50);
  92.         }
  93.     }
  94.  
  95.     writeln("Utworzono tablicę...");
  96.  
  97.     foreach (real[] key; matrix) {
  98.         writeln(key);
  99.     }
  100.  
  101.     writeln("Tworzę wątki...");
  102.  
  103.     scope group = new ThreadGroup;
  104.  
  105.     for (int i = 0; i < m; i++){
  106.         group.add(new DerivedThread(matrix[i], p, q));
  107.     }
  108.  
  109.     writeln("Liczę...");
  110.  
  111.     foreach (thread; group)
  112.         thread.start();
  113.  
  114.     writeln("Zbieram odpowiedzi...");
  115.  
  116.     group.joinAll();
  117.  
  118.     real result = 0;
  119.     foreach (thread; group) {
  120.         result += pow((cast(DerivedThread)thread).getResult(), 2);
  121.     }
  122.  
  123.     writeln("Suma kwadratów wynosi: ", result);
  124.     return 0;
  125. }
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement