ocarina-transfo-optim.adb 38.6 KB
Newer Older
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                           OCARINA COMPONENTS                             --
--                                                                          --
--                O C A R I N A . T R A N S F O . O P T I M                 --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--       Copyright (C) 2009 Telecom ParisTech, 2010-2015 ESA & ISAE.        --
--                                                                          --
-- Ocarina  is free software; you can redistribute it and/or modify under   --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. Ocarina is distributed in the hope that it will be useful, but     --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
26
--                                                                          --
jhugues's avatar
jhugues committed
27 28
--                 Ocarina is maintained by the TASTE project               --
--                      (taste-users@lists.tuxfamily.org)                   --
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
--                                                                          --
------------------------------------------------------------------------------

with Ocarina.Transfo.Optim.Eval;
with Ocarina.Transfo.Fusions;
with Ocarina.Transfo.Move;
with Ocarina.ME_AADL;
with Ocarina.ME_AADL.AADL_Instances.Nodes;
with Ocarina.ME_AADL.AADL_Tree.Nodes;
with Ocarina.ME_AADL.AADL_Instances.Entities;
with Ocarina.ME_AADL.AADL_Instances.Nutils;
with Ocarina.Instances;
with Ocarina.REAL_Values;
with Ocarina.Instances.REAL_Finder;
with Ocarina.Instances.REAL_Checker.Queries;
with Ocarina.Analyzer.AADL;
with GNAT.Heap_Sort_G;
with GNAT.OS_Lib;
with Ocarina.Backends;

with Ocarina.ME_AADL.AADL_Instances.Debug;
use Ocarina.ME_AADL.AADL_Instances.Debug;
51
with Ocarina.Namet; use Ocarina.Namet;
52 53 54 55 56 57 58 59 60 61 62 63 64

package body Ocarina.Transfo.Optim is
   use Ocarina.ME_AADL.AADL_Instances.Nodes;
   use Ocarina.Transfo.Optim.Eval;
   use Ocarina.ME_AADL.AADL_Instances.Nutils;
   use Ocarina.Instances.REAL_Finder;
   use Ocarina.REAL_Values;
   use Set;

   package AIN renames Ocarina.ME_AADL.AADL_Instances.Nodes;

   Current_Solution_Set : Solution_Set;

65
   Cost_Limit : constant Float := -5.0;
66 67
   --  minimum distance to deadline, in ms

68
   Non_Optim_Str : Name_Id;
69 70 71 72

   Iteration_Number : Natural := 0;

   procedure Break_All_Threads
73
     (Instance_Root :     Node_Id;
74 75 76 77 78 79 80 81
      Success       : out Boolean);
   pragma Unreferenced (Break_All_Threads);
   --  Launched at init, split threads that contain
   --  more than one call sequence

   procedure Build_Actual_Merged_System
     (Instance_Root : in out Node_Id;
      Owner_Process : in out Node_Id;
82
      Solution      :        Solution_Set);
83 84 85 86 87
   --  Build the system, fusionning all threads presents
   --  in the solution

   procedure Build_Actual_Moved_System
     (Instance_Root : in out Node_Id;
88 89
      Solution      :        Solution_Set;
      Process       :        Node_Id);
90 91 92 93 94 95 96 97 98 99 100
   --  pragma Unreferenced (Build_Actual_Moved_System);
   --  Build the system, moving all thread from solution into
   --  process

   function Find_Best_Move
     (System, Dest_Process : Node_Id) return Solution_Set;
   --  Find a single yet optimal move

   function Is_In (E : Node_Id; Set : Solution_Set) return Boolean;
   --  Return true if the element E is in the solution set Set

101
   procedure Clean_All_Process_Features (Instance_Root : in out Node_Id);
102 103 104 105
   --  Clean all useless process features
   --  Used when move terminated before cleaning

   function Find_Subcomponent_By_Name
106 107
     (Component         : Node_Id;
      Subcomponent_Name : Name_Id) return Node_Id;
108 109 110 111 112 113 114 115 116 117 118 119 120

   ----------
   -- Init --
   ----------

   procedure Init (AADL_Instance : Node_Id) is
      use Ocarina.Backends;
   begin
      Init (Current_Solution_Set);
      Ocarina.Transfo.Fusions.Init;
      Ocarina.Transfo.Optim.Eval.Init (AADL_Instance);
      Set_Current_Backend_Name ("aadl");

121
      Non_Optim_Str := Get_String_Name ("transformations::no_optimization");
122 123 124 125 126 127 128
   end Init;

   -----------------------
   -- Break_All_Threads --
   -----------------------

   procedure Break_All_Threads
129
     (Instance_Root :     Node_Id;
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      Success       : out Boolean)
   is
      pragma Unreferenced (Instance_Root);
   begin
      Success := True;
      --  FIXME TODO
   end Break_All_Threads;

   --------------------------------
   -- Build_Actual_Merged_System --
   --------------------------------

   procedure Build_Actual_Merged_System
     (Instance_Root : in out Node_Id;
      Owner_Process : in out Node_Id;
145
      Solution      :        Solution_Set)
146 147 148 149 150 151 152 153 154 155 156
   is
      use Ocarina.Transfo.Fusions;
      use Ocarina.Instances;
      use Ocarina.Analyzer.AADL;
      use Ocarina.Backends;

      package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;

      T1, T2    : Node_Id;
      T_Merged  : Node_Id;
      Success   : Boolean;
157 158 159 160
      AADL_Tree : constant Node_Id :=
        ATN.Parent
          (ATN.Namespace
             (AIN.Corresponding_Declaration (Root_System (Instance_Root))));
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
   begin
      T1 := Solution.Table (First).Thread_Node;

      for I in First + 1 .. Last (Solution) loop
         T2 := Solution.Table (I).Thread_Node;

         W_Line
           ("**  merged : " &
            Get_Name_String (AIN.Name (AIN.Identifier (T1))) &
            " & " &
            Get_Name_String (AIN.Name (AIN.Identifier (T2))));

         Fusion_Threads
           (AADL_Tree,
            AIN.Name (AIN.Identifier (Owner_Process)),
            AIN.Name (AIN.Identifier (AIN.Parent_Subcomponent (T1))),
            AIN.Name (AIN.Identifier (AIN.Parent_Subcomponent (T2))),
            T_Merged,
            Success);
         if not Success then
            raise Program_Error with "thread fusion failed";
         end if;

         Generate_Code (AADL_Tree);

         Ocarina.Transfo.Fusions.Reset;

         Ocarina.Analyzer.AADL.Reset;

         if Analyze_Model (AADL_Tree) then
            Instance_Root := Instantiate_Model (AADL_Tree);
            if No (Instance_Root) then
193 194
               raise Program_Error
                 with "Cannot instantiate the " & "fusioned AADL model";
195 196
            end if;
         else
197 198
            raise Program_Error
              with "Cannot analyze the " & "fusioned AADL model";
199 200
         end if;

201 202 203 204
         T1            := AIN.First_Node (ATN.Instances (T_Merged));
         Owner_Process :=
           AIN.Parent_Subcomponent
             (AIN.Parent_Component (AIN.Parent_Subcomponent (T1)));
205 206 207 208 209 210 211 212 213 214 215
      end loop;

      Ocarina.Instances.REAL_Checker.Queries.Init (Instance_Root);
   end Build_Actual_Merged_System;

   -------------------------------
   -- Build_Actual_Moved_System --
   -------------------------------

   procedure Build_Actual_Moved_System
     (Instance_Root : in out Node_Id;
216 217
      Solution      :        Solution_Set;
      Process       :        Node_Id)
218 219 220 221 222 223 224 225
   is
      use Ocarina.Instances;
      use Ocarina.Analyzer.AADL;
      use Ocarina.Transfo.Move;
      use Ocarina.Backends;

      package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;

226 227 228 229
      AADL_Tree : constant Node_Id :=
        ATN.Parent
          (ATN.Namespace
             (AIN.Corresponding_Declaration (Root_System (Instance_Root))));
230 231 232 233 234 235 236 237 238 239

      T : Node_Id;
      P : Node_Id;
      M : Boolean;
   begin

      for I in First .. Last (Solution) loop
         T := Parent_Subcomponent (Solution.Table (I).Thread_Node);
         P := Parent_Subcomponent (Parent_Component (T));

240 241 242 243 244 245 246 247
         W_Line
           ("move " &
            Get_Name_String (Name (Identifier (T))) &
            " from " &
            Get_Name_String (Name (Identifier (P))) &
            " to " &
            Get_Name_String
              (Name (Identifier (Parent_Subcomponent (Process)))));
248 249 250 251 252 253 254 255 256 257 258 259 260 261

         Move_Thread
           (Name (Identifier (T)),
            Name (Identifier (P)),
            Name (Identifier (Parent_Subcomponent (Process))));

         Generate_Code (AADL_Tree);

         Ocarina.Transfo.Move.Reset (AADL_Tree);
         Ocarina.Analyzer.AADL.Reset;

         if Analyze_Model (AADL_Tree) then
            Instance_Root := Instantiate_Model (AADL_Tree);
            if No (Instance_Root) then
262 263
               raise Program_Error
                 with "Cannot instantiate the " & "moved AADL model";
264 265
            end if;
         else
266
            W_Line ("Cannot analyze the " & "moved AADL model");
267
            W_Line ("");
268 269 270 271
            W_Line
              ("STEP => iteration number : " &
               Int'Image (Int (Iteration_Number)));
            GNAT.OS_Lib.OS_Exit (1);
272 273
         end if;

274 275 276 277 278 279 280 281 282
         M :=
           Clean_Obsolete_Features
             (Root_System (Instance_Root),
              Name (Identifier (P)));
         M :=
           Clean_Obsolete_Features
             (Root_System (Instance_Root),
              Name (Identifier (Parent_Subcomponent (Process))))
           or else M;
283 284 285 286 287 288 289 290

         if M then
            Ocarina.Transfo.Move.Reset (AADL_Tree);
            Ocarina.Analyzer.AADL.Reset;

            if Analyze_Model (AADL_Tree) then
               Instance_Root := Instantiate_Model (AADL_Tree);
               if No (Instance_Root) then
291 292
                  raise Program_Error
                    with "Cannot instantiate the " & "moved AADL model";
293 294
               end if;
            else
295
               W_Line ("Cannot analyze the " & "moved AADL model");
296
               W_Line ("");
297 298 299 300
               W_Line
                 ("STEP => iteration number : " &
                  Int'Image (Int (Iteration_Number)));
               GNAT.OS_Lib.OS_Exit (1);
301 302 303 304 305 306 307 308 309 310 311
            end if;
         end if;

      end loop;
      Ocarina.Instances.REAL_Checker.Queries.Init (Instance_Root);
   end Build_Actual_Moved_System;

   ------------------
   -- Copy_Threads --
   ------------------

312
   function Copy_Threads (Process : Node_Id) return Solution_Set is
313 314 315
      use Ocarina.ME_AADL;
      use Ocarina.ME_AADL.AADL_Instances.Entities;

316
      Process_Subcomponent : Node_Id := First_Node (Subcomponents (Process));
317 318 319 320 321 322 323 324 325 326 327
      Unsorted_List        : Solution_Set;
      N                    : Node_Id;
      TU                   : Thread_Unit;
   begin
      --  Build the non-sorted list

      Init (Unsorted_List);

      while Present (Process_Subcomponent) loop

         N := Corresponding_Instance (Process_Subcomponent);
328 329 330
         if Kind (N) = K_Component_Instance
           and then Get_Category_Of_Component (N) = CC_Thread
         then
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

            TU.Thread_Node := N;
            Append (Unsorted_List, TU);
         end if;

         Process_Subcomponent := Next_Node (Process_Subcomponent);
      end loop;

      return Unsorted_List;
   end Copy_Threads;

   ------------------
   -- Sort_Threads --
   ------------------

346
   function Sort_Threads (Process : Node_Id) return Solution_Set is
347 348 349
      use Ocarina.ME_AADL;
      use Ocarina.ME_AADL.AADL_Instances.Entities;

350 351 352
      pragma Assert
        (Kind (Process) = K_Component_Instance
         and then Get_Category_Of_Component (Process) = CC_Process);
353

354
      Array_Max_Size : constant Natural := Length (Subcomponents (Process));
355 356 357 358 359 360 361 362 363 364 365

      type Node_Table is array (0 .. Array_Max_Size) of Node_Id;

      Threads : Node_Table;

      procedure Internal_Move (From, To : Natural);
      --  Perform a move on the Threads node_table

      function Internal_Compare (Left, Right : Natural) return Boolean;
      --  Compare two indexes of the Threads node_table

366
      function True_Connections_Number (Node : Node_Id) return Natural;
367 368 369 370
      --  Compute teh number of connections to and from
      --  threads from the same process

      package Node_Sort is new GNAT.Heap_Sort_G
371 372
        (Move => Internal_Move,
         Lt   => Internal_Compare);
373 374 375 376 377

      -----------------------------
      -- True_Connections_Number --
      -----------------------------

378 379 380 381
      function True_Connections_Number (Node : Node_Id) return Natural is
         Feat          : constant List_Id := Features (Node);
         N, N2, Parent : Node_Id;
         Val_Left      : Natural          := 0;
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
      begin
         if not Is_Empty (Feat) then
            N := First_Node (Feat);
            while Present (N) loop

               if Kind (N) = K_Port_Spec_Instance then

                  if Is_In (N) then

                     N2 := First_Node (Sources (N));
                     while Present (N2) loop

                        --  Check if the source feature is in the same
                        --  process

                        Parent := Item (N2);
                        if Kind (Parent) = K_Port_Spec_Instance then
                           Parent := Parent_Component (Parent);
                           if Get_Category_Of_Component (Parent) =
401 402
                             CC_Thread
                           then
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
                              Val_Left := Val_Left + 1;
                           end if;
                        end if;

                        N2 := Next_Node (N2);
                     end loop;
                  end if;

                  if Is_Out (N) then
                     N2 := First_Node (Destinations (N));
                     while Present (N2) loop

                        --  Check if the destination feature is in the same
                        --  process

                        Parent := Item (N2);
                        if Kind (Parent) = K_Port_Spec_Instance then
                           Parent := Parent_Component (Parent);
                           if Get_Category_Of_Component (Parent) =
422 423
                             CC_Thread
                           then
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
                              Val_Left := Val_Left + 1;
                           end if;
                        end if;

                        N2 := Next_Node (N2);
                     end loop;
                  end if;
               end if;

               N := Next_Node (N);
            end loop;
         end if;

         return Val_Left;
      end True_Connections_Number;

      -------------------
      -- Internal_Move --
      -------------------

      procedure Internal_Move (From, To : Natural) is
      begin
         Threads (To) := Threads (From);
      end Internal_Move;

      ----------------------
      -- Internal_Compare --
      ----------------------

453
      function Internal_Compare (Left, Right : Natural) return Boolean is
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
         Val_Left  : Natural;
         Val_Right : Natural;
      begin
         --  FIXME :
         --  Currently only look at the number of connections
         --  Should call a REAL theorem

         Val_Left := True_Connections_Number (Threads (Left));

         Val_Right := True_Connections_Number (Threads (Right));

         return (Val_Left > Val_Right);
      end Internal_Compare;

      Process_Subcomponent : Node_Id := First_Node (Subcomponents (Process));
      Sorted_List          : Solution_Set;
      N                    : Node_Id;
      It                   : Natural := 0;
      TU                   : Thread_Unit;
   begin
      --  Build the original, non-sorted list

      while Present (Process_Subcomponent) loop

         N := Corresponding_Instance (Process_Subcomponent);
479 480 481 482
         if Kind (N) = K_Component_Instance
           and then Get_Category_Of_Component (N) = CC_Thread
         then
            It           := It + 1;
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
            Threads (It) := N;
         end if;

         Process_Subcomponent := Next_Node (Process_Subcomponent);
      end loop;

      --  Proceed to sort

      Node_Sort.Sort (It);

      --  Create the corresponding sorted list

      Init (Sorted_List);
      for I in 1 .. It loop
         TU.Thread_Node := Threads (I);
         Append (Sorted_List, TU);
      end loop;

      return Sorted_List;
   end Sort_Threads;

   --------------------------------
   -- Clean_All_Process_Features --
   --------------------------------

508
   procedure Clean_All_Process_Features (Instance_Root : in out Node_Id) is
509 510 511 512 513 514 515 516 517 518
      use Ocarina.Transfo.Move;
      use Ocarina.ME_AADL.AADL_Instances.Entities;
      use Ocarina.ME_AADL;
      use Ocarina.Instances;
      use Ocarina.Analyzer.AADL;
      use Ocarina.Backends;

      package ATN renames Ocarina.ME_AADL.AADL_Tree.Nodes;

      System    : constant Node_Id := AIN.Root_System (Instance_Root);
519 520 521 522
      AADL_Tree : constant Node_Id :=
        ATN.Parent (ATN.Namespace (AIN.Corresponding_Declaration (System)));
      S         : Node_Id := First_Node (Subcomponents (System));
      N         : Node_Id;
523 524 525 526 527
      Local_Mod : Boolean;
      Modified  : Boolean := False;
   begin
      while Present (S) loop
         N := Corresponding_Instance (S);
528 529 530
         if Kind (N) = K_Component_Instance
           and then Get_Category_Of_Component (N) = CC_Process
         then
531

532 533
            Local_Mod :=
              Clean_Obsolete_Features (System, Name (Identifier (S)));
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
            Modified := Modified or else Local_Mod;
         end if;

         S := Next_Node (S);
      end loop;

      if not Modified then
         return;
      end if;

      Generate_Code (AADL_Tree);

      Ocarina.Analyzer.AADL.Reset;
      if Analyze_Model (AADL_Tree) then
         Instance_Root := Instantiate_Model (AADL_Tree);
         if No (Instance_Root) then
550
            W_Line ("Cannot instantiate the " & "cleaned AADL model");
551
            W_Line ("");
552
            GNAT.OS_Lib.OS_Exit (1);
553 554
         end if;
      else
555
         W_Line ("Cannot analyze the " & "cleaned AADL model");
556
         W_Line ("");
557
         GNAT.OS_Lib.OS_Exit (1);
558 559 560 561 562 563 564 565
      end if;
   end Clean_All_Process_Features;

   --------------------
   -- Find_Best_Move --
   --------------------

   function Find_Best_Move
566
     (System, Dest_Process : Node_Id) return Solution_Set
567
   is
568 569 570 571 572 573 574 575 576
      Processes              : Node_Id := First_Node (Subcomponents (System));
      Process                : Node_Id;
      Threads                : Node_Id;
      Thread                 : Node_Id;
      Non_Optim              : Value_Id;
      Optimize               : Boolean;
      Candidate              : Node_Id := No_Node;
      Candidate_Value        : Float   := 0.0;
      Best_Value             : Float   := 0.0;
577
      Candidate_Solution_Set : Solution_Set;
578 579
      TU                     : Thread_Unit;
      Success                : Boolean;
580 581 582 583 584
   begin
      while Present (Processes) loop
         Process := Corresponding_Instance (Processes);

         Non_Optim := Get_Property_Value (Non_Optim_Str, Process);
585 586 587
         Optimize  :=
           ((Non_Optim = No_Value)
            or else (not Get_Value_Type (Non_Optim).BVal));
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
         if Process /= Dest_Process and then Optimize then

            if not Is_Empty (Subcomponents (Process)) then
               Threads := First_Node (Subcomponents (Process));
            else
               Threads := No_Node;
            end if;
            while Present (Threads) loop
               Thread := Corresponding_Instance (Threads);

               --  We build an heterogenous set

               TU.Thread_Node := Thread;
               Init (Candidate_Solution_Set);
               Append (Candidate_Solution_Set, TU);
               TU.Thread_Node := Dest_Process;
               Append (Candidate_Solution_Set, TU);

               Compute_Relative_Move_Value
                 (Candidate_Solution_Set,
608 609
                  Candidate_Value,
                  Success);
610 611 612 613 614
               if not Success then
                  raise Program_Error;
               end if;

               if Candidate_Value > Best_Value then
615
                  Candidate  := Thread;
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
                  Best_Value := Candidate_Value;
               end if;

               Free (Candidate_Solution_Set);
               Threads := Next_Node (Threads);
            end loop;

         end if;

         Processes := Next_Node (Processes);
      end loop;

      Init (Candidate_Solution_Set);
      if Present (Candidate) then
         TU.Thread_Node := Candidate;
         Append (Candidate_Solution_Set, TU);
      end if;

      return Candidate_Solution_Set;
   end Find_Best_Move;

   -------------------------------
   -- Find_Subcomponent_By_Name --
   -------------------------------

   function Find_Subcomponent_By_Name
642 643
     (Component         : Node_Id;
      Subcomponent_Name : Name_Id) return Node_Id
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
   is
      pragma Assert (AIN.Kind (Component) = K_Component_Instance);

      N : Node_Id := First_Node (Subcomponents (Component));

   begin
      while Present (N) loop
         if Name (Identifier (N)) = Subcomponent_Name then
            return N;
         end if;
         N := AIN.Next_Node (N);
      end loop;

      return No_Node;
   end Find_Subcomponent_By_Name;

   ----------------------
   -- Greedy_Heuristic --
   ----------------------

   procedure Greedy_Heuristic
     (Instance_Root : in out Node_Id;
666
      Success       :    out Boolean)
667 668 669 670 671 672
   is
      use Ocarina.ME_AADL.AADL_Instances.Entities;
      use Ocarina.ME_AADL;

      pragma Assert (Kind (Instance_Root) = K_Architecture_Instance);

673 674 675 676 677 678 679 680 681
      System                 : Node_Id;
      Subcomp                : Node_Id;
      Best_Candidate         : Node_Id;
      System_Value           : Float;
      Process                : Node_Id;
      Non_Optim              : Value_Id;
      Optimize               : Boolean;
      Process_Name           : Name_Id;
      Found                  : Boolean;
682 683 684 685 686
      Candidate_Solution_Set : Solution_Set;
   begin
      Success := True;

      Clean_All_Process_Features (Instance_Root);
687
      System  := Root_System (Instance_Root);
688 689 690 691 692 693 694 695 696 697 698 699 700
      Subcomp := First_Node (Subcomponents (System));

      Compute_System_Value (System_Value, Success);
      if not Success then
         return;
      end if;
      Compute_System_Cost (System_Value, Success);
      if not Success then
         return;
      end if;

      while Present (Subcomp) loop
         Process_Name := Name (Identifier (Subcomp));
701
         Process      := Corresponding_Instance (Subcomp);
702 703

         Non_Optim := Get_Property_Value (Non_Optim_Str, Process);
704 705 706 707
         Optimize  :=
           (((Non_Optim = No_Value)
             or else (not Get_Value_Type (Non_Optim).BVal))
            and then (Get_Category_Of_Component (Process) = CC_Process));
708 709 710 711 712 713 714

         if Optimize then

            W_Line ("");
            W_Line ("process " & Get_Name_String (Process_Name));

            declare
715 716 717 718 719 720 721 722 723 724
               Thread, T, T_Subcomp : Node_Id;
               Thread_List          : List_Id;
               Continue             : Boolean;
               Candidate_Value      : Float;
               Local_Value          : Float;
               Local_Cost           : Float;
               Local_Best_Value     : Float;
               Sorted_Threads_List  : Solution_Set;
               TU                   : Thread_Unit;
               Pre_Found            : Boolean;
725 726 727 728 729 730 731 732 733 734 735 736
            begin
               while True loop
                  --  Reload the system, since the model has been
                  --  reinstantiated

                  System := Root_System (Instance_Root);

                  --  Since fusion exchange the process defintion
                  --  but does not change teh related subcomponent
                  --  name, we can find back teh current process
                  --  after the first iteration

737
                  Subcomp := Find_Subcomponent_By_Name (System, Process_Name);
738 739 740 741 742
                  if No (Process) then
                     raise Program_Error;
                  end if;
                  Process := Corresponding_Instance (Subcomp);

743
                  Thread_List         := Subcomponents (Process);
744
                  Sorted_Threads_List := Sort_Threads (Process);
745 746
                  Found               := False;
                  Candidate_Value     := 0.0;
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762

                  for I in Set.First .. Last (Sorted_Threads_List) loop
                     Thread := Sorted_Threads_List.Table (I).Thread_Node;

                     --  We try a thread as first element of the set of
                     --  fusionable threads

                     TU.Thread_Node := Thread;
                     Init (Candidate_Solution_Set);
                     Append (Candidate_Solution_Set, TU);
                     Local_Best_Value := 0.0;

                     --  Look for optimal candidates for fusion with the
                     --  previous thread

                     Pre_Found := False;
763
                     Continue  := True;
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
                     while Continue loop

                        Best_Candidate := No_Node;

                        T_Subcomp := First_Node (Thread_List);
                        while Present (T_Subcomp) loop
                           T := Corresponding_Instance (T_Subcomp);

                           if T /= Thread
                             and then not Is_In (T, Candidate_Solution_Set)
                           then
                              TU.Thread_Node := T;
                              Append (Candidate_Solution_Set, TU);

                              --  1/ evaluate

                              Compute_Relative_Cost
                                (Candidate_Solution_Set,
782 783 784
                                 Local_Cost,
                                 Success);
                              if Success and then Local_Cost < Cost_Limit then
785 786
                                 Compute_Relative_Value
                                   (Candidate_Solution_Set,
787 788
                                    Local_Value,
                                    Success);
789 790 791 792 793 794 795 796 797
                              end if;
                              if not Success then
                                 return;
                              end if;

                              --  2/ Tag the current thread as candidate if
                              --  it has a better value than the previous
                              --  ones.

798 799 800 801
                              if Local_Value > Local_Best_Value
                                and then Local_Cost < Cost_Limit
                              then
                                 Best_Candidate   := T;
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
                                 Local_Best_Value := Local_Value;
                              end if;
                              Decrement_Last (Candidate_Solution_Set);

                              Iteration_Number := Iteration_Number + 1;
                           end if;

                           T_Subcomp := Next_Node (T_Subcomp);
                        end loop;

                        Continue := Present (Best_Candidate);

                        if Continue then
                           TU.Thread_Node := Best_Candidate;
                           Append (Candidate_Solution_Set, TU);
                           Pre_Found := True;
                        end if;
                     end loop;

                     --  FIXME
                     --  More precise evaluation is not possible without
                     --  actual fusion.

825 826 827
                     if Local_Best_Value > Candidate_Value
                       and then Pre_Found
                     then
828 829
                        Copy (Candidate_Solution_Set, Current_Solution_Set);
                        Candidate_Value := Local_Best_Value;
830
                        Found           := True;
831 832 833 834 835 836
                        exit;
                     end if;
                  end loop;

                  Free (Sorted_Threads_List);

837
                  if Found then
838
                     Build_Actual_Merged_System
839 840 841
                       (Instance_Root,
                        Subcomp,
                        Current_Solution_Set);
842 843 844
                     declare
                        Move_Set : Solution_Set;
                     begin
845 846 847
                        System  := Root_System (Instance_Root);
                        Subcomp :=
                          Find_Subcomponent_By_Name (System, Process_Name);
848 849 850 851 852 853 854 855
                        if No (Process) then
                           raise Program_Error;
                        end if;
                        Process := Corresponding_Instance (Subcomp);

                        Move_Set := Find_Best_Move (System, Process);
                        if Last (Move_Set) > 0 then
                           Build_Actual_Moved_System
856 857 858
                             (Instance_Root,
                              Move_Set,
                              Process);
859
                           Free (Move_Set);
860
                        end if;
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
                     end;

                     --  FIXME
                     --  Binary analysis
                     --  Update timing properties

                     Free (Sorted_Threads_List);
                     Free (Candidate_Solution_Set);
                  else
                     Free (Sorted_Threads_List);
                     Free (Candidate_Solution_Set);
                     exit;
                  end if;
               end loop;
            end;
         end if;

         --  This is ok because the subcomponent has been replaced in
         --  the system subcomponent list, hence its place is the same
         --  as the original subcomponent

         Subcomp := Next_Node (Subcomp);
      end loop;

885 886
      W_Line
        ("END => iteration number : " & Int'Image (Int (Iteration_Number)));
887 888 889 890 891 892 893 894
   end Greedy_Heuristic;

   ----------------------------------
   -- Exhaustive_Space_Exploration --
   ----------------------------------

   procedure Exhaustive_Space_Exploration
     (Instance_Root : in out Node_Id;
895
      Success       :    out Boolean)
896 897 898 899 900 901
   is
      use Ocarina.ME_AADL.AADL_Instances.Entities;
      use Ocarina.ME_AADL;

      pragma Assert (Kind (Instance_Root) = K_Architecture_Instance);

902 903 904 905 906 907 908 909 910 911 912 913
      System         : Node_Id;
      Subcomp        : Node_Id;
      Best_Candidate : Node_Id;
      System_Value   : Float;
      Process        : Node_Id;

      Non_Optim_Str : constant Name_Id :=
        Get_String_Name ("transformations::no_optimization");
      Non_Optim              : Value_Id;
      Optimize               : Boolean;
      Process_Name           : Name_Id;
      Found                  : Boolean;
914 915 916 917 918
      Candidate_Solution_Set : Solution_Set;
   begin
      Success := True;

      Clean_All_Process_Features (Instance_Root);
919
      System  := Root_System (Instance_Root);
920 921 922 923 924 925 926 927 928 929 930 931 932
      Subcomp := First_Node (Subcomponents (System));

      Compute_System_Value (System_Value, Success);
      if not Success then
         return;
      end if;
      Compute_System_Cost (System_Value, Success);
      if not Success then
         return;
      end if;

      while Present (Subcomp) loop
         Process_Name := Name (Identifier (Subcomp));
933
         Process      := Corresponding_Instance (Subcomp);
934 935

         Non_Optim := Get_Property_Value (Non_Optim_Str, Process);
936 937 938 939
         Optimize  :=
           (((Non_Optim = No_Value)
             or else (not Get_Value_Type (Non_Optim).BVal))
            and then (Get_Category_Of_Component (Process) = CC_Process));
940 941 942 943 944 945 946

         if Optimize then

            W_Line ("");
            W_Line ("process " & Get_Name_String (Process_Name));

            declare
947 948 949 950 951 952 953 954 955 956
               Thread, T, T_Subcomp  : Node_Id;
               Thread_List           : List_Id;
               Continue              : Boolean;
               Candidate_Value       : Float;
               Local_Value           : Float;
               Local_Cost            : Float;
               Local_Best_Value      : Float;
               Unsorted_Threads_List : Solution_Set;
               TU                    : Thread_Unit;
               Pre_Found             : Boolean;
957 958 959 960 961 962 963 964 965 966 967 968
            begin
               while True loop
                  --  Reload the system, since the model has been
                  --  reinstantiated

                  System := Root_System (Instance_Root);

                  --  Since fusion exchange the process defintion
                  --  but does not change teh related subcomponent
                  --  name, we can find back teh current process
                  --  after the first iteration

969
                  Subcomp := Find_Subcomponent_By_Name (System, Process_Name);
970 971 972 973
                  if No (Process) then
                     raise Program_Error;
                  end if;
                  Process := Corresponding_Instance (Subcomp);
974
                  Found   := False;
975

976
                  Thread_List           := Subcomponents (Process);
977
                  Unsorted_Threads_List := Copy_Threads (Process);
978
                  Candidate_Value       := 0.0;
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
                  for I in Set.First .. Last (Unsorted_Threads_List) loop
                     Thread := Unsorted_Threads_List.Table (I).Thread_Node;
                     Local_Best_Value := 0.0;

                     --  We try a thread as first element of the set of
                     --  fusionable threads

                     TU.Thread_Node := Thread;
                     Init (Candidate_Solution_Set);
                     Append (Candidate_Solution_Set, TU);

                     --  Look for optimal candidates for fusion with the
                     --  previous thread

                     Pre_Found := False;
994
                     Continue  := True;
995 996
                     while Continue loop
                        Best_Candidate := No_Node;
997
                        T_Subcomp      := First_Node (Thread_List);
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

                        while Present (T_Subcomp) loop
                           T := Corresponding_Instance (T_Subcomp);

                           if T /= Thread
                             and then not Is_In (T, Candidate_Solution_Set)
                           then
                              TU.Thread_Node := T;
                              Append (Candidate_Solution_Set, TU);

                              --  1/ evaluate

                              Compute_Relative_Cost
                                (Candidate_Solution_Set,
1012 1013 1014
                                 Local_Cost,
                                 Success);
                              if Success and then Local_Cost < Cost_Limit then
1015 1016
                                 Compute_Relative_Value
                                   (Candidate_Solution_Set,
1017 1018
                                    Local_Value,
                                    Success);
1019 1020 1021 1022 1023 1024 1025 1026 1027
                              end if;
                              if not Success then
                                 return;
                              end if;

                              --  2/ Tag the current thread as candidate if
                              --  it has a better value than the previous
                              --  ones.

1028 1029 1030 1031
                              if Local_Value > Local_Best_Value
                                and then Local_Cost < Cost_Limit
                              then
                                 Best_Candidate   := T;
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
                                 Local_Best_Value := Local_Value;
                              end if;

                              Iteration_Number := Iteration_Number + 1;
                              Decrement_Last (Candidate_Solution_Set);
                           end if;

                           T_Subcomp := Next_Node (T_Subcomp);
                        end loop;

                        Continue := Present (Best_Candidate);

                        if Continue then
                           TU.Thread_Node := Best_Candidate;
                           Append (Candidate_Solution_Set, TU);
                           Pre_Found := True;
                        end if;

                     end loop;

                     --  FIXME
                     --  More precise evaluation is not possible without
                     --  actual fusion.

1056 1057 1058
                     if Local_Best_Value > Candidate_Value
                       and then Pre_Found
                     then
1059 1060
                        Copy (Candidate_Solution_Set, Current_Solution_Set);
                        Candidate_Value := Local_Best_Value;
1061
                        Found           := True;
1062 1063 1064 1065 1066
                     end if;
                  end loop;

                  if Found then
                     Build_Actual_Merged_System
1067 1068 1069
                       (Instance_Root,
                        Subcomp,
                        Current_Solution_Set);
1070 1071 1072 1073

                     declare
                        Move_Set : Solution_Set;
                     begin
1074 1075 1076
                        System  := Root_System (Instance_Root);
                        Subcomp :=
                          Find_Subcomponent_By_Name (System, Process_Name);
1077 1078 1079 1080 1081 1082 1083 1084
                        if No (Process) then
                           raise Program_Error;
                        end if;
                        Process := Corresponding_Instance (Subcomp);

                        Move_Set := Find_Best_Move (System, Process);
                        if Last (Move_Set) > 0 then
                           Build_Actual_Moved_System
1085 1086 1087
                             (Instance_Root,
                              Move_Set,
                              Process);
1088
                           Free (Move_Set);
1089
                        end if;
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
                     end;

                     --  FIXME
                     --  Binary analysis
                     --  Update timing properties

                     Free (Candidate_Solution_Set);
                  else
                     Free (Unsorted_Threads_List);
                     Free (Candidate_Solution_Set);
                     exit;
                  end if;

                  Free (Unsorted_Threads_List);
                  Free (Candidate_Solution_Set);
               end loop;
            end;
         end if;

         --  This is ok because the subcomponent has been replaced in
         --  the system subcomponent list, hence its place is the same
         --  as the original subcomponent

         Subcomp := Next_Node (Subcomp);
      end loop;

1116 1117
      W_Line
        ("END => iteration number : " & Int'Image (Int (Iteration_Number)));
1118 1119 1120 1121 1122 1123
   end Exhaustive_Space_Exploration;

   -----------
   -- Is_In --
   -----------

1124
   function Is_In (E : Node_Id; Set : Solution_Set) return Boolean is
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
   begin
      for N in First .. Last (Set) loop
         if Set.Table (N).Thread_Node = E then
            return True;
         end if;
      end loop;

      return False;
   end Is_In;

end Ocarina.Transfo.Optim;