Advertisement
Guest User

Untitled

a guest
Mar 11th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 4.41 KB | None | 0 0
  1. ------------------------------------------
  2. --Laboratory work #1. Ada. Semaphore
  3. --
  4. --Task: MA = min(Z)*MO+d*(MK*MS)
  5. --
  6. --Author: Igor Dudchenko, IO-61
  7. --Date: 12.03.2019
  8. --
  9. ------------------------------------------
  10.  
  11. with Ada.Text_IO; use Ada.Text_IO;
  12. with Ada.Numerics.Real_Arrays;  use Ada.Numerics.Real_Arrays;
  13. with System.Multiprocessors; use System.Multiprocessors;
  14. with Ada.Synchronous_Task_Control; use Ada.Synchronous_Task_Control;
  15.  
  16. procedure Lab1 is
  17.    N : Integer := 4;
  18.    H : Integer := N / 2;
  19.  
  20.    type Vector is array(1..N) of Integer;
  21.    type Matrix is array(1..N) of Vector;
  22.    
  23.    z : Integer := 100;
  24.    d : Integer;
  25.    MK : Matrix;
  26.    MA : Matrix;
  27.  
  28.    Z : Vector;
  29.    z_half: Integer;
  30.    MO : Matrix;
  31.    MS : Matrix;
  32.  
  33.    S1, S2, S3, Sk1, Sk2 : Suspension_Object;
  34.  
  35.    procedure Input(matr : out Matrix; num : in Integer) is
  36.    begin
  37.       for i in 1..N loop
  38.          for j in 1..N loop
  39.             matr(i)(j) := num + j - i;
  40.          end loop;
  41.       end loop;
  42.    end Input;
  43.  
  44.    procedure Input(vec : out Vector; num : in Integer) is
  45.    begin
  46.       for i in 1..N loop
  47.          vec(i) := num + i;
  48.       end loop;
  49.    end Input;
  50.  
  51.    procedure Output(vect : in Vector) is
  52.    begin
  53.       for i in 1..N loop
  54.          Put(Integer'Image(vect(i)));
  55.       end loop;
  56.       Put_Line("");
  57.    end Output;
  58.  
  59.  
  60.    procedure Output(matr : in Matrix) is
  61.    begin
  62.       for i in 1..N loop
  63.          Output(matr(i));
  64.       end loop;
  65.    end Output;
  66.  
  67.  
  68.    task T1 is
  69.       pragma Priority(1);
  70.       pragma CPU(0);
  71.       pragma Storage_Size(100_000_000);
  72.    end;
  73.  
  74.    task body T1 is
  75.       rand : Integer := 1;
  76.       z1 : Integer;
  77.       MK1 : Matrix;
  78.       d1 : Integer;
  79.       Alloc : Matrix;
  80.       tmp : Integer;
  81.    begin
  82.       Put_Line("T1 started");
  83.  
  84.       ----INPUT
  85.       d := rand;
  86.       Input(vec => Z, num => rand);
  87.       Input(matr => MK, num => rand);
  88.  
  89.       -----Signal
  90.       Set_True(S1);
  91.  
  92.       -----Wait
  93.       Suspend_Until_True(S2);
  94.  
  95.      -----Minimum
  96.       minimum : Integer := Z(1);
  97.  
  98.       for I+1 in 1..H loop
  99.          if z(I) < minimum then
  100.             minimum := z(I);
  101.  
  102.      z_half = minimum;
  103.        
  104.       -----Copy
  105.       Suspend_Until_True(Sk1);
  106.       if z > z_half then
  107.          z = z_half;
  108.  
  109.       -----Signal
  110.       Set_True(Sk1);
  111.  
  112.      -----Copy
  113.       Suspend_Until_True(Sk2);
  114.       z1 := z;
  115.       MK1 := Mk;
  116.       d1 := d;
  117.       Set_True(Sk2);
  118.  
  119.       -----compute
  120.       for i in 1..N loop
  121.          for j in 1..H  loop
  122.             Alloc(i)(j) := 0;
  123.             for k in 1..N loop
  124.                Alloc(i)(j):=Alloc(i)(j)+d1*(MK1(i)(k)*MS(k)(j));
  125.             end loop;
  126.          end loop;
  127.       end loop;
  128.    
  129.       for i in 1..N loop
  130.          for j in 1..H  loop
  131.             MO(i)(j):=MO(i)(j)*z1;
  132.    
  133.       for i in 1..N loop
  134.          for j in 1..H  loop
  135.             MA(i)(j):=Alloc(i)(j)+MO(i)(j);
  136.  
  137.       Set_True(S3);
  138.       Put_Line("T1 finished");
  139.    end;
  140.  
  141.  
  142.    task T2 is
  143.       pragma Priority(1);
  144.       pragma CPU(1);
  145.       pragma Storage_Size(100_000_000);
  146.    end;
  147.  
  148.    task body T2 is
  149.       rand : Integer := 1;
  150.       MO1 : Matrix;
  151.       MS1: Matrix;
  152.       Alloc : Matrix;
  153.       tmp : Integer;
  154.    begin
  155.       Put_Line("T2 started");
  156.  
  157.       ----Input
  158.       Input(MO, rand);
  159.       Input(MS, rand);
  160.      
  161.       ----Signal
  162.       Set_True(S2);
  163.    
  164.       -----Wait
  165.       Suspend_Until_True(S1);
  166.  
  167.      -----Minimum
  168.       minimum : Integer := Z(1);
  169.  
  170.       for I+1 in 1..H loop
  171.          if z(I) < minimum then
  172.             minimum := z(I);
  173.  
  174.      z_half = minimum;
  175.        
  176.       -----Copy
  177.       Suspend_Until_True(Sk1);
  178.       if z > z_half then
  179.          z = z_half;
  180.  
  181.       -----Signal
  182.       Set_True(Sk1);
  183.  
  184.      -----Copy
  185.       Suspend_Until_True(Sk2);
  186.       z2:= z;
  187.       MK2 := Mk;
  188.       d2 := d;
  189.       Set_True(Sk2);
  190.  
  191.       -----compute
  192.       for i in 1..N loop
  193.          for j in 1..H  loop
  194.             Alloc(i)(j) := 0;
  195.             for k in 1..N loop
  196.                Alloc(i)(j):=Alloc(i)(j)+d1*(MK1(i)(k)*MS(k)(j));
  197.             end loop;
  198.          end loop;
  199.       end loop;
  200.    
  201.       for i in 1..N loop
  202.          for j in 1..H  loop
  203.             MO(i)(j):=MO(i)(j)*z1;
  204.    
  205.       for i in 1..N loop
  206.          for j in 1..H  loop
  207.             MA(i)(j):=Alloc(i)(j)+MO(i)(j);
  208.  
  209.       Suspend_Until_True(S3);
  210.  
  211.       Output(MA);
  212.       Put_Line("T2 finished");
  213.    end;
  214.  
  215. begin
  216.    Set_True(S0);
  217.    Put_Line("Main start");
  218.    null;
  219. end Lab1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement