Advertisement
Guest User

Untitled

a guest
May 17th, 2024
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. PS C:\WINDOWS\system32> ollama list
  2. NAME ID SIZE MODIFIED
  3. llama3:latest a6990ed6be41 4.7 GB 4 days ago
  4. mixtral:latest 7708c059a8bb 26 GB 10 days ago
  5. mixtral:8x7b 7708c059a8bb 26 GB 2 weeks ago
  6. codellama:latest 8fdf8f752f6e 3.8 GB 2 weeks ago
  7. PS C:\WINDOWS\system32> ollama run mixtral:latest
  8. >>> Create a Delphi console program with a class which create two different threaded functions using TTask. In the first function calculate the first 1000 pi values. In the second function create first 1000 fibonacci values. PI and Fibonacci must be placed in two thread
  9. ... safe arrays. The classes exposes two events which are called by threads in way that are executed by main process thread passing the percentual of the completation so the I can use them to write that value in the main delphi idle flow.
  10. Here is an example of a Delphi console program that creates two different threaded functions using TTask and uses events to report progress back to the main thread:
  11.  
  12. ```delphi
  13. program ThreadExample;
  14.  
  15. {$APPTYPE CONSOLE}
  16.  
  17. uses
  18. System.SysUtils, System.Threading, System.Generics.Collections;
  19.  
  20. type
  21. TFibonacciProgressEvent = procedure(const APercentage: Integer) of object;
  22. TPiProgressEvent = procedure(const APercentage: Integer) of object;
  23.  
  24. TCalculator = class
  25. private
  26. FOnFibonacciProgress: TFibonacciProgressEvent;
  27. FOnPiProgress: TPiProgressEvent;
  28. FCancel: Boolean;
  29. function CalculatePi(const AIndex: Integer): Extended;
  30. function CalculateFibonacci(const AIndex: Integer): UInt64;
  31. public
  32. property Cancel: Boolean read FCancel write FCancel;
  33. procedure Execute;
  34. property OnFibonacciProgress: TFibonacciProgressEvent read FOnFibonacciProgress write FOnFibonacciProgress;
  35. property OnPiProgress: TPiProgressEvent read FOnPiProgress write FOnPiProgress;
  36. end;
  37.  
  38. var
  39. Calculator: TCalculator;
  40. PiValues: TThreadList<Extended>;
  41. FibonacciValues: TThreadList<UInt64>;
  42.  
  43. function ThreadSafeAdd(const AList: TThreadList<Integer>; const AValue: Integer): Boolean;
  44. begin
  45. Result := True;
  46. if not AList.LockList then
  47. Exit;
  48. try
  49. AList.Add(AValue);
  50. finally
  51. AList.UnlockList;
  52. end;
  53. end;
  54.  
  55. { TCalculator }
  56.  
  57. constructor TCalculator.Create;
  58. begin
  59. inherited;
  60. FCancel := False;
  61. PiValues := TThreadList<Extended>.Create;
  62. FibonacciValues := TThreadList<UInt64>.Create;
  63. end;
  64.  
  65. procedure TCalculator.Execute;
  66. var
  67. I: Integer;
  68. begin
  69. for I := 0 to 999 do
  70. begin
  71. if FCancel then
  72. Break;
  73. ThreadSafeAdd(FibonacciValues, CalculateFibonacci(I));
  74. If Assigned(FOnFibonacciProgress) then
  75. Synchronize(Procedure
  76. begin
  77. FOnFibonacciProgress(Trunc((I + 1) / 1000 * 100));
  78. end
  79. );
  80.  
  81. ThreadSafeAdd(PiValues, CalculatePi(I));
  82. If Assigned(FOnPiProgress) then
  83. Synchronize(Procedure
  84. begin
  85. FOnPiProgress(Trunc((I + 1) / 1000 * 100));
  86. end
  87. );
  88. end;
  89. end;
  90.  
  91. function TCalculator.CalculateFibonacci(const AIndex: Integer): UInt64;
  92. begin
  93. if (AIndex = 0) then
  94. Result := 0
  95. else if (AIndex = 1) then
  96. Result := 1
  97. else
  98. Result := TThreadPool.Default.Run(Procedure
  99. begin
  100. Result := CalculateFibonacci(AIndex - 1) + CalculateFibonacci(AIndex - 2);
  101. end
  102. );
  103. end;
  104.  
  105. function TCalculator.CalculatePi(const AIndex: Integer): Extended;
  106. begin
  107. if (AIndex = 0) then
  108. Result := 0
  109. else if (AIndex = 1) then
  110. Result := 4
  111. else
  112. Result := TThreadPool.Default.Run(Procedure
  113. begin
  114. Result := CalculatePi(AIndex - 1) + 4 * (-1) ** AIndex / (2 * AIndex + 1);
  115. end
  116. );
  117. end;
  118.  
  119. var
  120. I: Integer;
  121. begin
  122. try
  123. Calculator := TCalculator.Create;
  124. try
  125. Calculator.OnFibonacciProgress := Procedure(const APercentage: Integer)
  126. begin
  127. WriteLn('Fibonacci progress: ' + IntToStr(APercentage) + '%');
  128. end;
  129.  
  130. Calculator.OnPiProgress := Procedure(const APercentage: Integer)
  131. begin
  132. WriteLn('PI progress: ' + IntToStr(APercentage) + '%');
  133. end;
  134.  
  135. TTask.Run(Calculator.Execute);
  136. ReadLn;
  137. finally
  138. Calculator.Free;
  139. end;
  140. except
  141. on E: Exception do
  142. Writeln(E.ClassName, ': ', E.Message);
  143. end;
  144. end.
  145. ```
  146.  
  147. This program uses two TThreadList objects to store the calculated PI and Fibonacci values in a thread-safe manner. The TCalculator class contains two functions, CalculatePi and CalculateFibonacci, that calculate the respective values using recursion. The Execute
  148. method starts two separate tasks to calculate the first 1000 PI and Fibonacci values and reports progress back to the main thread using events.
  149.  
  150. The program uses the TTask.Run method to execute the Execute method in a separate thread, and the Synchronize method is used to call the event handlers on the main thread so that they can safely update the user interface.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement