Advertisement
Guest User

Prolog Problem GitHub Timbo925

a guest
Dec 31st, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 5.03 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %            fib_small_uc.pl            %
  3. %                                       %
  4. %   Scheduling of a parallel fibonacci  %
  5. % computation on a homogeoneous system  %
  6. %     (uniform communication costs)     %
  7. %                                       %
  8. %       Declarative Programming         %
  9. %              2014-2015                %
  10. %                                       %
  11. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  12.  
  13. %%%%%%%%%%%%%%%%
  14. %%%   Cores  %%%
  15. %%%%%%%%%%%%%%%%
  16. % The cores of the machine
  17. % core(Id): a core with unique identifier 'Id'.
  18. core(c1).
  19. core(c2).
  20. core(c3).
  21. core(c4).
  22.  
  23. %%%%%%%%%%%%%%%%%%
  24. %%%   Tasks    %%%
  25. %%%%%%%%%%%%%%%%%%
  26. % The tasks the application is made up of, which are to be scheduled.
  27. % task(Id): a task with unique identifier 'Id'.
  28. task(t7).
  29. task(t1).
  30. task(t6).
  31. task(t2).
  32. task(t4).
  33. task(t3).
  34. task(t5).
  35.  
  36. %%%%%%%%%%%%%%%%%%
  37. %% Dependencies %%
  38. %%%%%%%%%%%%%%%%%%
  39. % The execution order dependencies between tasks
  40. % depends_on(Ta,Tb,Data): before task 'Ta' can be executed,
  41. % task 'Tb' must have been executed and thereafter 'Data' megabytes of data (result of/produced by 'Tb') must have been moved from the processor that executed 'Tb' to the processor that will execute 'Ta'.
  42.  
  43. % %In this benchmark tasks are interdependent, but no data is communicated between tasks.
  44. depends_on(t7,t2,0).
  45. depends_on(t7,t6,0).
  46. depends_on(t6,t4,0).
  47. depends_on(t6,t5,0).
  48. depends_on(t2,t1,0).
  49. depends_on(t4,t3,0).
  50. depends_on(t3,t1,0).
  51. depends_on(t5,t3,0).
  52.  
  53. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  54. %%%   Processing Costs   %%%
  55. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  56. % Specifies how long the processing of each task takes on each core.
  57. % process_cost(T,C,Time): It takes 'Time' ms to execute task 'T' on core 'C'.
  58.  
  59. %In this benchmark any task takes as long on any core (i.e. homogeneous system)
  60. process_cost(T,C,10) :- task(T), core(C).
  61.  
  62. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  63. %%%  Channel Properties  %%%
  64. %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  65. % Specifies the properties of the communication channel between each of the cores
  66. % channel(Ca,Cb,Latency,Bandwidth): The channel to communicate from core 'Ca' to core 'Cb' has a latency 'Latency' and bandwidth 'Bandwidth'.
  67. % Note that sending 'X' megabytes of data, over a channel, takes Latency + X/Bandwidth ms.
  68.  
  69. %In this benchmark sending a given message take as long, independent of sender/recipient/message size (uniform access).
  70. channel(C,C,15,1) :- !,core(C).
  71. channel(C1,C2,15,1) :- C1 \= C2, core(C1), core(C2).
  72.  
  73. %:- use_module(library(clpfd)).
  74. :- use_module(library(clpq)).
  75. schedule(BestSchedule, BestTotTime) :-
  76.     findall(T,task(T),Tasks),
  77.     findall(C,core(C),Cores),
  78.     init_time,
  79.     const_time(Tasks,Schedule,Cores,TotTime),
  80.     assign_processor(Schedule,Cores, TotTime),
  81.     %const_channel(Schedule),
  82.     minimize(TotTime),
  83.     update_time(Schedule, TotTime),
  84.     write(TotTime), writeln(Schedule),
  85.     fail
  86.     ;
  87.     bestsofar(BestSchedule,BestTotTime)
  88.     .
  89.  
  90.  %Setting Time Contraints on certain variables
  91.  const_time([],[], _ ,TotTime).
  92.  const_time([T|Ts], [task(T,Start,Duration,Core)|Rest], Cores,TotTime) :-
  93.      %member(Core,Cores),
  94.      %process_cost(T,Core,Duration),     %Duration is based on core and task
  95.      { Start >= 0,
  96.      Start + Duration =< TotTime },       %Set TotTime as maximum of all end task time
  97.      const_time(Ts, Rest, Cores,TotTime),
  98.      const_order(task(T,Start,Duration,Core), Rest).
  99.  
  100. %Set constarints for start time based on dependancy and channel communication
  101. const_order(_,[]).
  102. const_order(task(T,S,D,_),[task(T2,S2,D2,_)|Rest]) :-
  103.     (depends_on(T,T2,_),!, { S2 + D2 =< S };
  104.      depends_on(T2,T,_),!, { S + D =< S2 };
  105.      true),
  106.      const_order(task(T,S,D,_),Rest).
  107.      
  108. %Set constarints for start time based on dependancy and channel communication
  109. const_channel([]).
  110. const_channel([task(T,S,D,C)|Rest]) :-
  111.      const_channel(task(T,S,D,C), Rest),
  112.      const_channel(Rest).
  113.  
  114. const_channel(_,[]).
  115. const_channel(task(T,S,D,C),[task(T2,S2,D2,C2)|Rest]) :-
  116.     (depends_on(T,T2,Da),channel(C,C2,L,B),!, { S2 + D2 =< S };
  117.      depends_on(T2,T,Da),channel(C2,C,L,B),!, { S + D  =< S2 };
  118.      true),
  119.      const_channel(task(T,S,D,C),Rest).
  120.  
  121. assign_processor([], _, _).  
  122. assign_processor([task(T,S,D,C)|Rest], Cores, TotTime) :-
  123.     assign_processor(Rest,Cores,TotTime),
  124.     %member(C,Cores),
  125.     core(C),
  126.     process_cost(T,C,D),
  127.     const_resource(task(T,S,D,C),Rest),
  128.     bestsofar(_,CurrentBestTime),
  129.     {TotTime < CurrentBestTime}.
  130.    
  131.    
  132. const_resource(_,[]).
  133. const_resource(Task,[Task2|Rest]) :-
  134.     no_data(Task,Task2),
  135.     no_conflict(Task,Task2),
  136.     const_resource(Task, Rest).  
  137.    
  138. no_conflict(task(_,S,D,C),task(_,S2,D2,C2)) :-
  139.     C \== C2,!;
  140.     { S+D =< S2;
  141.      S2+D2 =< S }.  
  142.      
  143. no_data(task(T,S,D,C), task(T2,S2,D2,C2)) :-
  144.     depends_on(T,T2,_),channel(C,C2,L,_),!, { S2 + D2  =< S - L };
  145.     depends_on(T2,T,_),!,channel(C2,C,L,_),!, { S + D   =< S2 - L};
  146.     true.
  147.  
  148. init_time :-
  149.     retract(bestsofar(_,_)), fail
  150.     ;
  151.     assert(bestsofar(foobar, 1000)).
  152.    
  153. update_time(Schedule,TotTime) :-
  154.     retract(bestsofar(_,_)),!,
  155.     assert(bestsofar(Schedule,TotTime)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement