線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:9023
推到 Plurk!
推到 Facebook!

如何讓程式使用到多核心

缺席
slanla2007
一般會員


發表:3
回覆:4
積分:6
註冊:2007-10-04

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-10-04 01:00:09 IP:59.127.xxx.xxx 訂閱
因為程式會用到大量計算..而在雙核心的電腦中..僅僅只會使用到50%的CPU...
我之前測試過Thread...產生了10個...依然也是50%XD
所以想大家請教要如何才能夠讓程式讓雙核心(甚至四核心)100%計算呢?
harpist
資深會員


發表:3
回覆:251
積分:430
註冊:2002-10-03

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-10-04 13:14:02 IP:211.74.xxx.xxx 未訂閱
------
~§~迷時師渡,悟了自渡~§~
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-10-04 19:06:30 IP:59.105.xxx.xxx 訂閱
可以試試 SetThreadAffinityMask, SetProcessAffinityMask
可設定執行緒或Process使用那個 CPU 作處理。

Remarks

A thread affinity mask is a bit vector in which each bit represents the processors that a thread is allowed to run on. A thread affinity mask must be a subset of the process affinity mask for the containing process of a thread. A thread can only run on the processors its process can run on.

Setting an affinity mask for a process or thread can result in threads receiving less processor time, as the system is restricted from running the threads on certain processors. In most cases, it is better to let the system select an available processor.

If the new thread affinity mask does not specify the processor that is currently running the thread, the thread is rescheduled on one of the allowable processors.

其中的 affinity mask 值若是 0x01 表示使用第一個CPU, 0x02 表示使用第 2 個 CPU, 0x04 表示使用第三個 CPU,0x03 表示使用第1,2個CPU(應該是 1或2由系統自行分配)。

我沒雙核無法測試...

P.S. 以上文字我也在 MSDN 回覆過別人的問題。

VMASK - ViewMove Automation Software KernelVMIO-Server/SECS/GEMdllee's blogdllee's StatPlus
------
http://www.ViewMove.com
編輯記錄
dllee 重新編輯於 2007-10-04 19:10:29, 註解 無‧
slanla2007
一般會員


發表:3
回覆:4
積分:6
註冊:2007-10-04

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-10-05 23:54:47 IP:59.127.xxx.xxx 訂閱
謝謝兩位抽空回答我的問題..
OpenMP目前還沒測試過...因為目前還不想用VS2005開發程式...不知道C Builder2008未來會不會支援呢~@@?

另外我測試過了
dllee所提議的SetThreadAffinityMask,不過似乎無法達到這效果
並且參考這篇..http://delphi.ktop.com.tw/board.php?cid=168&fid=914&tid=88750
不過GetProcessAffinityMask似乎沒有對應的GetThreadAffinityMask這指令...
slanla2007
一般會員


發表:3
回覆:4
積分:6
註冊:2007-10-04

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-10-13 02:14:55 IP:59.127.xxx.xxx 訂閱
我用了VS2005撰寫了一個具有OpenML之功能的dll
並且由BCB呼叫該dll...確實可以達到100%
不過有個缺點....當在執行dll時...BCB會呈現當機狀態耶..直到dll執行完畢..才正常
我試過以Timer以及Thread來執行Application->ProcessMessages();
但是仍然一樣..呈現當機狀態..XD

後來我嘗試著在dll檔中用SendMessage來傳送一訊號...
讓BCB接收後..執行Application->ProcessMessages();
後來居然可以了耶XD
我把原始碼放上來...大家參考一下...雖然離我的目標..還有一段距離...不過暫且先這樣用...
VC採用VS2005...OpenML選項有打勾..
VC-Lib.h
[code cpp]
#ifndef LIB_H
#define LIB_H
#include
extern "C" int __declspec(dllexport)add(HWND hwnd,int x, int y);
#endif

[/code]

VC-Lib.cpp
[code cpp]
#include "lib.h"
#include
#include
using namespace std;
#include <math.h><br />int Test(HWND hwnd,int n )
{
for( int i = 0; i < 10000000; i )
{
if(i0000==0)
SendMessage(hwnd,1401,0,0);
float a=sqrt(sin(rand()/32767.0f));
}
return 101;
}
int add(HWND hwnd,int x, int y)
{
#pragma omp parallel for
for( int i = 0; i < 10; i )
Test( hwnd,i );
return x y;
}
[/code]

BCB-Unit1.h
[code cpp]
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp><br />#include
//---------------------------------------------------------------------------
#define TSLEEP (1401)
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
void __fastcall TSleep(TMessage &Message);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(TSLEEP, TMessage, TSleep)
END_MESSAGE_MAP(TForm)
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

[/code]

BCB-Unit1.cpp
[code cpp]
//---------------------------------------------------------------------------
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
typedef int(*lpAddFun)(HANDLE,int, int);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Button1->Enabled =false;
HINSTANCE hInst=LoadLibrary("dllTest.dll");
if(!hInst)
{
ShowMessage("無法載入!");
}
else
{
lpAddFun addFun=(lpAddFun)GetProcAddress(hInst, "add");
if (addFun != NULL)
{
int r = addFun(this->Handle,2, 3);
ShowMessage(r);
}
FreeLibrary( hInst );
}
Button1->Enabled =true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TSleep(TMessage &Message)
{
this->Caption=rand();
Application->ProcessMessages();
}
[/code]
ps:為什麼不能上傳檔案呢~@"@...
編輯記錄
slanla2007 重新編輯於 2007-10-13 02:20:47, 註解 無‧
slanla2007
一般會員


發表:3
回覆:4
積分:6
註冊:2007-10-04

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-10-14 00:04:23 IP:59.127.xxx.xxx 訂閱
呼~我試出來了!!
原來Mutli-thread是可以的!
但是若是用到Synchronize,則會是只有main thread在執行..
所以即使有再多的thread..
也是只有一個thread在執行

參考來源:http://delphi.ktop.com.tw/board.php?cid=168&fid=912&tid=62520
dllee
站務副站長


發表:321
回覆:2519
積分:1711
註冊:2002-04-15

發送簡訊給我
#7 引用回覆 回覆 發表時間:2007-11-13 09:05:00 IP:220.134.xxx.xxx 訂閱
感謝分享喔 ^_^

對了,如果您要上傳檔案,可以修改本討論您的第一篇文章,就可以上傳檔案了。


VMASK - ViewMove Automation Software KernelVMIO-Server/SECS/GEMdllee's blogdllee's StatPlus
------
http://www.ViewMove.com
系統時間:2024-04-25 15:16:09
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!