Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi BeginThreadAffinity() or SetThreadAffinity (https://www.delphipraxis.net/113427-beginthreadaffinity-setthreadaffinity.html)

Razor 7. Mai 2008 17:40


BeginThreadAffinity() or SetThreadAffinity
 
Well how to make it like that the functionons or procedures run on multiple cores,example 1 function to be run on all 4 cores of a cpu.And example if anybody knows how to do it,yes ive checked the msdn searched google no luck... :)

Muetze1 7. Mai 2008 17:42

Re: BeginThreadAffinity() or SetThreadAffinity
 
1. It is default for every process/thread to run on all cores/CPUs of the system.
2. MSDN-Library durchsuchenSetThreadAffinityMask() & MSDN-Library durchsuchenSetProcessAffinityMask() need a mask on call, so you define the usable cores/CPUs

grenzgaenger 7. Mai 2008 17:42

Re: BeginThreadAffinity() or SetThreadAffinity
 
you can use different threads.

in general you use only one thread, and so your program work sequential.

kind regards
GG

Razor 7. Mai 2008 17:44

Re: BeginThreadAffinity() or SetThreadAffinity
 
Yea but how to make threads ,becouse of this code
I figured myself..and intel manuals..

Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
VAR
EAX,EDX:DWORD;
Temperature ,delta,maxcores,x,TJunction:integer;

begin
InitializeDll();
cxCpu401.Available.Available:=maxcores;









Rdmsr($00EE,EAX,EDX);
IF EAX and $40000000 = 0 then TJunction := 100 Else TJunction := 85;
form1.Caption:=inttostr(TJunction);


For X := 1 to MaxCores do
Begin
Delta := TJunction;
SetThreadAffinty(X) ;// makes that the function now runs on core X //this<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
Rdmsr($019,EAX,EDX);
If HiWord(EAX) and $8000 > 0 then
Delta := HiWord(EAX) and $7F; // Delta-value from the temperature for reading TJunction
Temperature := TJunction-Delta;
End;

grenzgaenger 7. Mai 2008 17:47

Re: BeginThreadAffinity() or SetThreadAffinity
 
Zitat:

Zitat von Razor
Delphi-Quellcode:
For X := 1 to MaxCores do
Begin
Delta := TJunction;
SetThreadAffinty(X) ;// makes that the function now runs on core X //this<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
Rdmsr($019,EAX,EDX);
If HiWord(EAX) and $8000 > 0 then
Delta := HiWord(EAX) and $7F; // Delta-value from the temperature for reading TJunction
Temperature := TJunction-Delta;
End;

you can use descantands of the class tThread (take a look to your delphi help). but don't use things like your code above. the distribution of the threads to the differend cores and cpu's is the task of your operating system.

Muetze1 7. Mai 2008 17:49

Re: BeginThreadAffinity() or SetThreadAffinity
 
He tries to get the core temperatures for each core, so he needs this assignment...

Razor 7. Mai 2008 17:49

Re: BeginThreadAffinity() or SetThreadAffinity
 
so you think it should work without that thread distrubution setting wich is crazy imho :?

grenzgaenger 7. Mai 2008 17:52

Re: BeginThreadAffinity() or SetThreadAffinity
 
Zitat:

Zitat von Muetze1
He tries to get the core temperatures for each core, so he needs this assignment...

mhh, in this case, he has to distribute his threads manually.

kind regards
GG

Razor 7. Mai 2008 18:01

Re: BeginThreadAffinity() or SetThreadAffinity
 
Threads is this cool to use?

Delphi-Quellcode:
unit PrimeForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TPrimeFrm = class(TForm)
    NumEdit: TEdit;
    SpawnButton: TButton;
    ResultsMemo: TMemo;
    procedure SpawnButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
    FThreadRefCount: integer;
    procedure HandleTerminate(Sender: TObject);
  public
    { Public declarations }
  end;

var
  PrimeFrm: TPrimeFrm;

implementation

uses PrimeThread;

{$R *.DFM}

procedure TPrimeFrm.SpawnButtonClick(Sender: TObject);

var
  NewThread: TPrimeThrd;

begin
  NewThread := TPrimeThrd.Create(True);
  NewThread.FreeOnTerminate := True;
  try
    with NewThread do
    begin
      TestNumber := StrToInt(NumEdit.Text);
      Inc(FThreadRefCount);
      OnTerminate := HandleTerminate;
      Resume;
    end;
  except on EConvertError do
    begin
      NewThread.Free;
      ShowMessage('That is not a valid number!');
    end;
  end;
end;

procedure TPrimeFrm.FormCreate(Sender: TObject);
begin
  FThreadRefCount := 0;
end;

procedure TPrimeFrm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := true;
  if FThreadRefCount > 0 then
  begin
    if MessageDlg('Threads active. Do you still want to quit?',
      mtWarning, [mbYes, mbNo], 0) = mrNo then
      CanClose := false;
  end;
end;

procedure TPrimeFrm.HandleTerminate(Sender: TObject);
begin
  Dec(FThreadRefCount);
end;

end.

Razor 9. Mai 2008 12:53

Re: BeginThreadAffinity() or SetThreadAffinity
 
Here we go this is more like it 8)

AffinityMask CPU to use
1 0
2 1
3 0,1
4 2
5 0,2
6 0,1,2

function SetThreadAffinityMask(hThread: THandle; dwThreadAffinityMask: DWORD): DWORD; stdcall;


Steps:
1. Create a new class derived from TThread.
2. Include an AffinityMask property (or type DWORD) with a SetAffinityMask procedure.
3. Call SetThreadAffinityMask on the SetAffinityMask procedure.
4. Change your programs to inherit from your new thread class.
5. Use your new property MyThread.AffinityMask = 3 (3 for dual processor systems)

Delphi-Quellcode:
Code


unit ExThread;
 
interface
 
uses
  Classes;
 
type
  TExThread = class(TThread)
  private
    FAffinityMask: DWord;
    procedure SetAffinity(const Value: DWord);
    { Private declarations } 
  protected
    procedure Execute; override;
  public
    property AffinityMask : DWord read FAffinityMask write SetAffinity;
  end;
 
implementation
 
{ Important: Methods and properties of objects in VCL can only be used in a
  method called using Synchronize, for example,
 
      Synchronize(UpdateCaption);
 
  and UpdateCaption could look like,
 
    procedure TExThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; } 
 
{ TExThread } 
 
procedure TExThread.Execute;
begin
  { Place thread code here } 
end;
 
procedure TExThread.SetAffinity(const Value: DWord);
begin
  FAffinityMask := SetThreadAffinityMask(Handle,Value);
  if FAffinityMask = 0 then raise Exception.Create('Error setting thread affinity mask : ' + IntToStr(GetLastError));
end;
 
end.


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:51 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz