Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /WinCS/BMSingleDlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (hide annotations) (download) (as text)
Wed Feb 10 18:21:00 2010 UTC (14 years, 2 months ago) by sho1get
File MIME type: text/x-c++src
File size: 13901 byte(s)


1 sho1get 11 #include "stdafx.h"
2     #include "WinCS.h"
3     #include "BMSingleDlg.h"
4     #include "Flops.h"
5     #include "FlopsHelpDlg.h"
6    
7     IMPLEMENT_DYNAMIC(CBMSingleDlg, CDialog)
8    
9     BEGIN_MESSAGE_MAP(CBMSingleDlg, CDialog)
10     ON_BN_CLICKED(IDOK, &CBMSingleDlg::OnBnClickedOk)
11     ON_WM_CTLCOLOR()
12     ON_WM_SYSCOMMAND()
13     ON_MESSAGE(WM_BMSYSTEM_FINISH, &CBMSingleDlg::OnBMTestFinish)
14     ON_BN_CLICKED(IDC_BTN_BMSINGLE_CLOSE, &CBMSingleDlg::OnBnClickedBtnBmsingleClose)
15     ON_BN_CLICKED(IDC_BTN_BMSINGLE_HELP, &CBMSingleDlg::OnBnClickedBtnBmsingleHelp)
16     END_MESSAGE_MAP()
17    
18     //////////////////////////////////////////////////////////////////////////
19    
20     CBMSingleDlg::CBMSingleDlg(CWnd* pParent /*=NULL*/)
21     : CDialog(CBMSingleDlg::IDD, pParent),
22     m_hTimer(NULL),
23     m_dwProcessors(0),
24     m_fEnd(FALSE),
25     m_wNodeType(WCS_NODE_UNDEFINED)
26     {
27     m_hIcon = AfxGetApp()->LoadIcon(IDI_BENCHMARK);
28     }
29    
30     CBMSingleDlg::~CBMSingleDlg()
31     {
32     CloseHandleList();
33     }
34    
35     void CBMSingleDlg::DoDataExchange(CDataExchange* pDX)
36     {
37     CDialog::DoDataExchange(pDX);
38     DDX_Control(pDX, IDC_LIST_BMSINGLE_DATA, m_xDataList);
39     DDX_Control(pDX, IDC_IMG_BMSINGLE_LOADER, m_xImgLoader);
40     DDX_Control(pDX, IDC_EDIT_BMSINGLE_AVERAGE, m_xEditAverage);
41     DDX_Control(pDX, IDC_EDIT_BMSINGLE_RUNTIME, m_xEditRuntime);
42     DDX_Control(pDX, IDC_EDIT_BMSINGLE_PROCESSORS, m_xEditProcessors);
43     DDX_Control(pDX, IDC_PRG_BMSINGLE_PROGRESS, m_xProgress);
44     DDX_Control(pDX, IDC_TXT_BMSINGLE_PERCENT, m_xTxtPercent);
45     DDX_Control(pDX, IDC_TXT_BMSINGLE_STATE, m_xTxtState);
46     DDX_Control(pDX, IDC_BTN_BMSINGLE_CLOSE, m_xBtnClose);
47     }
48    
49    
50     void CBMSingleDlg::OnSysCommand(UINT nID, LPARAM lParam)
51     {
52     if (nID == SC_CLOSE)
53     {
54     return;
55     }
56    
57     CDialog::OnSysCommand(nID, lParam);
58     }
59    
60     void CBMSingleDlg::CloseHandleList()
61     {
62     CSingleLock sl(&m_cs, TRUE);
63     UINT nSize = static_cast<UINT>(m_ThdList.GetSize());
64    
65     if (nSize > 0)
66     {
67     for (UINT i = 0; i < nSize; i++)
68     {
69     if (m_ThdList[i] != NULL)
70     {
71     CloseHandle(m_ThdList[i]);
72     }
73     }
74    
75     m_ThdList.RemoveAll();
76     }
77     }
78    
79     void CBMSingleDlg::OnBnClickedBtnBmsingleClose()
80     {
81     CloseSingleBenchmark();
82     }
83    
84     void CBMSingleDlg::CancelBenchmarkTest()
85     {
86     CloseSingleBenchmark();
87     }
88    
89     void CBMSingleDlg::CloseSingleBenchmark()
90     {
91     CString cs;
92     UINT nSize;
93     int nRet;
94    
95     if (!m_fEnd)
96     {
97     if (m_wNodeType == WCS_NODE_MASTER)
98     {
99     cs.LoadString(IDS_WCS_TEXT_BMTEST_EXIT_CONFIRM);
100     nRet = CMsgBox::Question(MC_CStoSTR(cs), _T("Benchmark Test"));
101    
102     if (nRet != IDYES)
103     {
104     return;
105     }
106     }
107    
108     nSize = static_cast<UINT>(m_ThdList.GetSize());
109    
110     for (UINT i = 0; i < nSize; i++)
111     {
112     m_fpList[i].fAlive = FALSE;
113     }
114    
115     StopTimer(TRUE);
116     WaitForMultipleObjects(nSize, &m_ThdList[0], TRUE, INFINITE);
117     CloseHandleList();
118     EndDialog(IDCANCEL);
119     }
120     else
121     {
122     // Default finish
123     CloseHandleList();
124     EndDialog(IDOK);
125     }
126     }
127    
128     BOOL CBMSingleDlg::OnInitDialog()
129     {
130     CDialog::OnInitDialog();
131    
132     LVCOLUMN lvc;
133     CRect cr;
134    
135     SetIcon(m_hIcon, TRUE);
136     SetIcon(m_hIcon, FALSE);
137    
138     m_bmResult.dwAftyCnt = 0;
139     ZeroMemory(&m_bmResult.bm, sizeof(m_bmResult.bm));
140    
141     m_fpList.SetSize(m_dwProcessors);
142     m_bpList.SetSize(m_dwProcessors);
143     m_ThdIDList.SetSize(m_dwProcessors);
144     m_ThdList.SetSize(m_dwProcessors);
145    
146     lvc.mask = (LVCF_FMT | LVCF_TEXT | LVCF_WIDTH | LVCF_SUBITEM);
147     m_xDataList.GetClientRect(&cr);
148    
149     lvc.fmt = LVCFMT_RIGHT;
150     lvc.cx = (cr.right - cr.left) * 2 / 10;
151     lvc.pszText = _T("Module");
152     m_xDataList.InsertColumn(0, &lvc);
153    
154     lvc.fmt = LVCFMT_RIGHT;
155     lvc.cx = (cr.right - cr.left) * 4 / 10;
156     lvc.pszText = _T("Runtime");
157     m_xDataList.InsertColumn(1, &lvc);
158    
159     lvc.fmt = LVCFMT_RIGHT;
160     lvc.cx = (cr.right - cr.left) * 4 / 10;
161     lvc.pszText = _T("FLOPS");
162     m_xDataList.InsertColumn(2, &lvc);
163    
164     m_xDataList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
165     m_xProgress.SetRange(0, 100);
166     m_xBtnClose.EnableWindow(m_wNodeType == WCS_NODE_MASTER);
167     m_xBtnClose.ShowWindow((m_wNodeType == WCS_NODE_MASTER) ? SW_SHOW : SW_HIDE);
168    
169     if (m_xImgLoader.Load(MAKEINTRESOURCE(IDR_LOADER_INDICATOR), _T("GIF")))
170     {
171     m_xImgLoader.Draw();
172     }
173    
174     m_xEditAverage.SetWindowText(STRING_NONE);
175     m_xEditProcessors.SetWindowText(DwToString(m_dwProcessors));
176    
177     for (UINT i = 0; i < 8; i++)
178     {
179     m_Affinity.dwAftyCntArray[i] = 0;
180     m_Affinity.bpArray[i].nIndex = i;
181     m_Affinity.bpArray[i].dbError = 0.0;
182     m_Affinity.bpArray[i].dbGFLOPS = 0.0;
183     m_Affinity.bpArray[i].dbRuntime = 0.0;
184     }
185    
186     for (UINT i = 0; i < 11; i++)
187     {
188     m_PrgPoint.dwPointArray[i] = 0;
189     }
190    
191     if (!StartTimer())
192     {
193     TRACE1("***** ERROR: StartTimer(%d) *****\n", GetLastError());
194     return EndDialog(IDCANCEL), FALSE;
195     }
196    
197     if (!StartBenchmarkTest())
198     {
199     TRACE1("***** ERROR: StartBenchmarkTest(%d) *****\n", GetLastError());
200     return EndDialog(IDCANCEL), FALSE;
201     }
202    
203     return TRUE;
204     }
205    
206     HBRUSH CBMSingleDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
207     {
208     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
209     int nCtlID = pWnd->GetDlgCtrlID();
210    
211     switch (nCtlID)
212     {
213     case IDC_EDIT_BMSINGLE_AVERAGE:
214     case IDC_EDIT_BMSINGLE_RUNTIME:
215     case IDC_EDIT_BMSINGLE_PROCESSORS:
216     hbr = static_cast<HBRUSH>(GetStockObject(WHITE_BRUSH));
217     pDC->SetBkColor(RGB(255, 255, 255));
218     break;
219     }
220    
221     return hbr;
222     }
223    
224     LRESULT CBMSingleDlg::OnBMTestFinish(WPARAM wParam, LPARAM lParam)
225     {
226     BenchmarkTestFinish();
227     return 0;
228     }
229    
230     BOOL CBMSingleDlg::StartTimer()
231     {
232     m_Stopwatch.Start();
233     RuntimeTimer();
234    
235     if (!CreateTimerQueueTimer(&m_hTimer, NULL, doRuntimeTimer, this, 500, 500, 0))
236     {
237     m_hTimer = NULL;
238     return FALSE;
239     }
240    
241     return TRUE;
242     }
243    
244     void CBMSingleDlg::StopTimer(BOOL fForce)
245     {
246     if (((fForce) || (m_bmResult.dwAftyCnt == m_dwProcessors)) && (m_hTimer != NULL))
247     {
248     DeleteTimerQueueTimer(NULL, m_hTimer, NULL);
249     m_hTimer = NULL;
250     }
251     }
252    
253     DWORD WINAPI CBMSingleDlg::doBenchmark(LPVOID pvParam)
254     {
255     typedef std::tr1::shared_ptr<CFlops> FlopsPtr;
256    
257     LPFLOPS_PARAMS lpFP = static_cast<LPFLOPS_PARAMS>(pvParam);
258     CBMSingleDlg *dlg = lpFP->obj;
259     DWORD dwAffinity = lpFP->dwAffinity;
260     DWORD dwIndex = lpFP->dwIndex;
261     FlopsPtr flops;
262     BM_DATA bm;
263     HANDLE hCurrent;
264     BOOL fOutput;
265    
266     // Set Affinity
267     hCurrent = GetCurrentThread();
268    
269     if (SetThreadAffinityMask(hCurrent, dwAffinity) == 0)
270     {
271     TRACE2("***** ERROR: SetThreadAffinityMask(%d) Processor(%d)", GetLastError(), dwIndex);
272     return 0;
273     }
274     /*if (SetThreadIdealProcessor(hCurrent, dwAffinity) == -1)
275     {
276     TRACE2("***** ERROR: SetThreadIdealProcessor(%d) Processor(%d)", GetLastError(), dwIndex);
277     return 0;
278     }*/
279    
280     try
281     {
282     flops = FlopsPtr(new CFlops(dlg));
283     }
284     catch (std::bad_alloc &)
285     {
286     TRACE0("***** ERROR: shared_ptr(bad_alloc) *****\n");
287     return 0;
288     }
289    
290     if (flops->Start(dwIndex))
291     {
292     bm.dbRlt1 = flops->GetGFLOPS1();
293     bm.dbRlt2 = flops->GetGFLOPS2();
294     bm.dbRlt3 = flops->GetGFLOPS3();
295     bm.dbRlt4 = flops->GetGFLOPS4();
296     bm.dwIterations = flops->GetIterations();
297     bm.dbRuntime = dlg->GetTimer().NowBySecond();
298    
299     dlg->SetBMData(dwIndex, bm, &fOutput);
300    
301     if (fOutput)
302     {
303     dlg->OutputBenchmarkResult(); // Result output
304     }
305     }
306    
307     dlg->PostMessage(WM_BMSYSTEM_FINISH);
308    
309     return 0;
310     }
311    
312     void WINAPI CBMSingleDlg::doRuntimeTimer(LPVOID pvContext, BOOLEAN fTimeout)
313     {
314     ((CBMSingleDlg *)pvContext)->RuntimeTimer();
315     }
316    
317     void CBMSingleDlg::RuntimeTimer()
318     {
319     CString cs;
320     m_Stopwatch.GetHMS(cs);
321     m_xEditRuntime.SetWindowText(cs);
322     }
323    
324     BOOL CBMSingleDlg::StartBenchmarkTest()
325     {
326     // Create thread
327     for (UINT i = 0; i < m_dwProcessors; i++)
328     {
329     m_fpList[i].obj = this;
330     m_fpList[i].dwIndex = i;
331     m_fpList[i].dwAffinity = (0x0001 << i);
332     m_fpList[i].fAlive = TRUE;
333    
334     m_ThdList[i] = MC_BEGINTHREADEX(NULL,
335     0,
336     CBMSingleDlg::doBenchmark,
337     &m_fpList[i],
338     CREATE_SUSPENDED,
339     &m_ThdIDList[i]);
340     }
341    
342     // Start thread
343     for (UINT i = 0; i < m_dwProcessors; i++)
344     {
345     ResumeThread(m_ThdList[i]);
346     }
347    
348     for (UINT i = 0; i < m_dwProcessors; i++)
349     {
350     if (m_ThdList[i] == NULL)
351     {
352     return FALSE;
353     }
354     }
355    
356     return TRUE;
357     }
358    
359     void CBMSingleDlg::BenchmarkTestFinish()
360     {
361     DWORD dwCount;
362    
363     StopTimer(FALSE);
364    
365     if (m_wNodeType == WCS_NODE_SLAVE)
366     {
367     if (m_bmResult.dwAftyCnt == m_dwProcessors)
368     {
369     // Automatic termination(Slave)
370     dwCount = static_cast<DWORD>(m_ThdList.GetSize());
371     WaitForMultipleObjects(dwCount, m_ThdList.GetData(), TRUE, INFINITE);
372     CloseHandleList();
373     EndDialog(IDOK);
374     }
375     }
376     }
377    
378     void CBMSingleDlg::OutputBenchmarkResult()
379     {
380     CString csAvg;
381    
382     if (m_bmResult.dwAftyCnt == m_dwProcessors)
383     {
384     csAvg.Format(_T("%.4lf GFLOPS"), m_bmResult.bm.dbRltAvg);
385     m_xEditAverage.SetWindowText(csAvg);
386     m_xTxtState.SetWindowText(_T("Finish!"));
387     m_xImgLoader.Stop();
388     m_xImgLoader.ShowWindow(SW_HIDE);
389     SaveReport();
390     m_fEnd = TRUE;
391     }
392     }
393    
394     void CBMSingleDlg::SetBMData(DWORD dwIndex, BM_DATA bm, LPBOOL lpfOutput)
395     {
396     m_bmResult.lock.Enter();
397    
398     m_bmResult.dwAftyCnt++;
399     m_bmResult.bm.dbRlt1 += bm.dbRlt1;
400     m_bmResult.bm.dbRlt2 += bm.dbRlt2;
401     m_bmResult.bm.dbRlt3 += bm.dbRlt3;
402     m_bmResult.bm.dbRlt4 += bm.dbRlt4;
403     m_bmResult.bm.dbRuntime += bm.dbRuntime;
404    
405     m_bpList[dwIndex].bm = bm;
406     m_bmResult.bm.fSet = (m_bmResult.dwAftyCnt == m_dwProcessors);
407     *lpfOutput = (m_bmResult.dwAftyCnt == m_dwProcessors);
408    
409     if (m_bmResult.bm.fSet)
410     {
411     m_bmResult.bm.dbRuntime /= m_bmResult.dwAftyCnt;
412     m_bmResult.bm.dwIterations = bm.dwIterations;
413     m_bmResult.bm.dwProcessors = m_dwProcessors;
414    
415     for (UINT i = 0; i < m_dwProcessors; i++)
416     {
417     m_bmResult.bm.dbRltAvg += m_Affinity.bpArray[i].dbGFLOPS;
418     }
419    
420     m_bmResult.bm.dbRltAvg /= m_dwProcessors;
421     }
422    
423     m_bmResult.lock.Leave();
424     }
425    
426     void CBMSingleDlg::SetProgressPoint(int nPoint)
427     {
428     TCHAR szParcent[5];
429     BOOL fOutput;
430     int nProgPoint = nPoint / 10;
431    
432     m_PrgPoint.lock.Enter();
433     m_PrgPoint.dwPointArray[nProgPoint]++;
434     fOutput = (m_PrgPoint.dwPointArray[nProgPoint] == m_dwProcessors);
435     m_PrgPoint.lock.Leave();
436    
437     if (fOutput)
438     {
439     wsprintf(szParcent, _T("%d%%"), nPoint);
440     m_xTxtPercent.SetWindowText(szParcent);
441     m_xProgress.SetPos(nPoint);
442    
443     if (m_wNodeType == WCS_NODE_SLAVE)
444     {
445     // Send progress status
446     GetParent()->SendMessage(WM_BMSYSTEM_PROGRESS, (WPARAM)nProgPoint);
447     }
448     }
449     }
450     void CBMSingleDlg::SetProcessData(BM_PROCESS bp)
451     {
452     CString cs;
453     LVITEM lvi;
454     BOOL fOutput;
455     UINT nListSize;
456    
457     m_Affinity.lock.Enter();
458    
459     if (m_Affinity.dwAftyCntArray[bp.nIndex - 1] < m_dwProcessors)
460     {
461     m_Affinity.dwAftyCntArray[bp.nIndex - 1]++;
462     m_Affinity.bpArray[bp.nIndex - 1].nIndex = bp.nIndex;
463     m_Affinity.bpArray[bp.nIndex - 1].dbGFLOPS += bp.dbGFLOPS;
464     m_Affinity.bpArray[bp.nIndex - 1].dbRuntime += bp.dbRuntime;
465     m_Affinity.bpArray[bp.nIndex - 1].dbError += bp.dbError;
466    
467     fOutput = (m_Affinity.dwAftyCntArray[bp.nIndex - 1] == m_dwProcessors);
468    
469     if (fOutput)
470     {
471     m_Affinity.bpArray[bp.nIndex - 1].dbRuntime /= m_dwProcessors;
472     m_Affinity.bpArray[bp.nIndex - 1].dbError /= m_dwProcessors;
473     }
474     }
475    
476     m_Affinity.lock.Leave();
477    
478     if (fOutput)
479     {
480     lvi.mask = (LVIF_TEXT | LVIF_IMAGE);
481     nListSize = m_xDataList.GetItemCount();
482    
483     cs = DwToString(bp.nIndex);
484     lvi.iItem = nListSize;
485     lvi.iSubItem = 0;
486     lvi.pszText = MC_CStoSTR(cs);
487     m_xDataList.InsertItem(&lvi);
488    
489     cs.Format(_T("%.4lf sec"), m_Affinity.bpArray[bp.nIndex - 1].dbRuntime);
490     lvi.iItem = nListSize;
491     lvi.iSubItem = 1;
492     lvi.pszText = MC_CStoSTR(cs);
493     m_xDataList.SetItem(&lvi);
494    
495     cs.Format(_T("%.4lf GFLOPS"), m_Affinity.bpArray[bp.nIndex - 1].dbGFLOPS += bp.dbGFLOPS);
496     lvi.iItem = nListSize;
497     lvi.iSubItem = 2;
498     lvi.pszText = MC_CStoSTR(cs);
499     m_xDataList.SetItem(&lvi);
500     }
501     }
502    
503     void CBMSingleDlg::OnBnClickedBtnBmsingleHelp()
504     {
505     CFlopsHelpDlg dlg;
506     dlg.DoModal();
507     }
508    
509     BOOL CBMSingleDlg::SaveReport()
510     {
511     SYSTEMTIME st;
512     CString csRuntime, csReport, csText, csTime, csTxtPath;
513     HANDLE hFile;
514     double dbMax, dbMin;
515     DWORD dwFileSize, dwWriteSize;
516    
517     GetLocalTime(&st);
518     csTxtPath.Format(_T("./Log/Benchmark/Single_%04d%02d%02d%02d%02d%02d.html"),
519     st.wYear, st.wMonth, st.wDay,
520     st.wHour, st.wMinute, st.wSecond);
521    
522     dbMax = 0.0;
523     dbMin = static_cast<double>(INT_MAX);
524    
525     for (UINT i = 0; i < 8; i++)
526     {
527     dbMax = max(dbMax, m_Affinity.bpArray[i].dbGFLOPS);
528     dbMin = min(dbMin, m_Affinity.bpArray[i].dbGFLOPS);
529     }
530    
531     m_xEditRuntime.GetWindowText(csRuntime);
532     csTime.Format(_T("%04d/%02d/%02d %02d:%02d:%02d"),
533     st.wYear, st.wMonth, st.wDay,
534     st.wHour, st.wMinute, st.wSecond);
535     csReport.Format(LOG_TEMPLATE_HTML_BENCHMARK_SINGLE_DATA,
536     m_Affinity.bpArray[0].dbGFLOPS,
537     m_Affinity.bpArray[1].dbGFLOPS,
538     m_Affinity.bpArray[2].dbGFLOPS,
539     m_Affinity.bpArray[3].dbGFLOPS,
540     m_Affinity.bpArray[4].dbGFLOPS,
541     m_Affinity.bpArray[5].dbGFLOPS,
542     m_Affinity.bpArray[6].dbGFLOPS,
543     m_Affinity.bpArray[7].dbGFLOPS);
544    
545     csText.Format(LOG_TEMPLATE_HTML_BENCHMARK_CONTAINER,
546     _T("Single"), // Mode
547     csTime, // DateTime
548     1, // Nodes
549     m_dwProcessors, // Processors
550     csRuntime, // Runtime
551     STRING_EMPTY, // (Total)
552     m_bmResult.bm.dbRltAvg, // Average
553     dbMax, // High score
554     dbMin, // Low score
555     csReport); // Report data
556    
557     hFile = CreateFile(csTxtPath, GENERIC_WRITE, 0, NULL, CREATE_NEW,
558     FILE_ATTRIBUTE_NORMAL, NULL);
559    
560     if (hFile != INVALID_HANDLE_VALUE)
561     {
562     dwFileSize = csText.GetLength() * sizeof(TCHAR);
563     WriteFile(hFile, csText, dwFileSize, &dwWriteSize, NULL);
564     CloseHandle(hFile);
565     return TRUE;
566     }
567    
568     return FALSE;
569     }
570    
571     //////////////////////////////////////////////////////////////////////////
572    

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26