Skip to content

Commit e9cbf1b

Browse files
committed
OpenMP --> std::thread 化 #92
1 parent cea95dd commit e9cbf1b

File tree

6 files changed

+121
-140
lines changed

6 files changed

+121
-140
lines changed

sakura/sakura.vcxproj

-5
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@
101101
<WarningLevel>Level3</WarningLevel>
102102
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
103103
<MultiProcessorCompilation>true</MultiProcessorCompilation>
104-
<OpenMPSupport>true</OpenMPSupport>
105104
</ClCompile>
106105
<Link>
107106
<AdditionalDependencies>$(SolutionDir)$(Platform)\$(Configuration)\libpcre2-16-static.lib;comctl32.lib;Imm32.lib;mpr.lib;imagehlp.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -142,7 +141,6 @@
142141
<WarningLevel>Level3</WarningLevel>
143142
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
144143
<MultiProcessorCompilation>true</MultiProcessorCompilation>
145-
<OpenMPSupport>true</OpenMPSupport>
146144
</ClCompile>
147145
<Link>
148146
<AdditionalDependencies>$(SolutionDir)$(Platform)\$(Configuration)\libpcre2-16-static.lib;comctl32.lib;Imm32.lib;mpr.lib;imagehlp.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -180,7 +178,6 @@
180178
<WarningLevel>Level3</WarningLevel>
181179
<DebugInformationFormat>EditAndContinue</DebugInformationFormat>
182180
<MultiProcessorCompilation>true</MultiProcessorCompilation>
183-
<OpenMPSupport>true</OpenMPSupport>
184181
</ClCompile>
185182
<Link>
186183
<AdditionalDependencies>$(SolutionDir)$(Platform)\$(Configuration)\libpcre2-16-static.lib;comctl32.lib;Imm32.lib;mpr.lib;imagehlp.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -218,7 +215,6 @@
218215
<WarningLevel>Level3</WarningLevel>
219216
<DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
220217
<MultiProcessorCompilation>true</MultiProcessorCompilation>
221-
<OpenMPSupport>true</OpenMPSupport>
222218
</ClCompile>
223219
<Link>
224220
<AdditionalDependencies>$(SolutionDir)$(Platform)\$(Configuration)\libpcre2-16-static.lib;comctl32.lib;Imm32.lib;mpr.lib;imagehlp.lib;Shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
@@ -437,7 +433,6 @@
437433
<ClInclude Include="..\sakura_core\mem\CNativeW.h" />
438434
<ClInclude Include="..\sakura_core\mem\CRecycledBuffer.h" />
439435
<ClInclude Include="..\sakura_core\mfclike\CMyWnd.h" />
440-
<ClInclude Include="..\sakura_core\openmp.h" />
441436
<ClInclude Include="..\sakura_core\outline\CDlgFileTree.h" />
442437
<ClInclude Include="..\sakura_core\outline\CDlgFuncList.h" />
443438
<ClInclude Include="..\sakura_core\outline\CFuncInfo.h" />

sakura/sakura.vcxproj.filters

-1
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,6 @@
10911091
<ClInclude Include="..\libs\pcre2\include\pcre2.h">
10921092
<Filter>Other Files</Filter>
10931093
</ClInclude>
1094-
<ClInclude Include="..\sakura_core\openmp.h" />
10951094
</ItemGroup>
10961095
<ItemGroup>
10971096
<None Include="..\sakura_core\Funccode_x.hsrc">

sakura_core/CReadManager.cpp

+68-53
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,13 @@ EConvertResult CReadManager::ReadFile_To_CDocLineMgr(
7878

7979
EConvertResult eRet = RESULT_COMPLETE;
8080

81-
std::vector<CFileLoad> cfl( OmpMaxThreadNum );
82-
std::vector<CDocLineMgr> cDocMgr( OmpMaxThreadNum );
81+
UINT uMaxThreadNum = std::thread::hardware_concurrency();
82+
83+
std::vector<CFileLoad> cfl( uMaxThreadNum );
84+
std::vector<CDocLineMgr> cDocMgr( uMaxThreadNum );
85+
std::vector<std::thread> cThread;
86+
87+
volatile bool bBreakRead = false;
8388

8489
try{
8590
cfl[ 0 ].SetEncodingConfig( type->m_encoding );
@@ -102,65 +107,75 @@ EConvertResult CReadManager::ReadFile_To_CDocLineMgr(
102107
pFileInfo->SetFileTime( FileTime );
103108
}
104109

105-
for( int i = 1; i < OmpMaxThreadNum; ++i ){
110+
for( UINT u = 1; u < uMaxThreadNum; ++u ){
106111
// cfl インスタンスコピー
107-
cfl[ i ].Copy( cfl[ 0 ]);
112+
cfl[ u ].Copy( cfl[ 0 ]);
108113
}
109114

110-
#pragma omp parallel for
111-
for( int iThread = 0; iThread < OmpMaxThreadNum; ++iThread ){
112-
113-
// 処理開始・終了位置探索
114-
size_t uBeginPos;
115-
if( iThread == 0 ){
116-
uBeginPos = 0;
117-
}else{
118-
uBeginPos = cfl[ iThread ].GetNextLineTop(
119-
cfl[ iThread ].GetFileSize() * iThread / OmpMaxThreadNum
120-
);
121-
}
122-
123-
size_t uEndPos;
124-
if( iThread == OmpMaxThreadNum - 1 ){
125-
uEndPos = cfl[ iThread ].GetFileSize();
126-
}else{
127-
uEndPos = cfl[ iThread ].GetNextLineTop(
128-
cfl[ iThread ].GetFileSize() * ( iThread + 1 ) / OmpMaxThreadNum
129-
);
130-
}
131-
132-
cfl[ iThread ].SetBufLimit( uBeginPos, uEndPos );
133-
#ifdef DEBUG
134-
MYTRACE( L"pid:%d range:%u - %u\n", iThread, ( UINT )uBeginPos, ( UINT )uEndPos );
135-
#endif
136-
137-
// ReadLineはファイルから 文字コード変換された1行を読み出します
138-
// エラー時はthrow CError_FileRead を投げます
139-
int nLineNum = 0;
140-
CEol cEol;
141-
CNativeW cUnicodeBuffer;
142-
EConvertResult eRead;
143-
144-
while( RESULT_FAILURE != (eRead = cfl[ iThread ].ReadLine( &cUnicodeBuffer, &cEol ))){
145-
if(eRead==RESULT_LOSESOME){
146-
eRet = RESULT_LOSESOME;
115+
for( UINT uThread = 0; uThread < uMaxThreadNum; ++uThread ){
116+
cThread.emplace_back( std::thread( [ &, this, uThread ]{
117+
118+
// 処理開始・終了位置探索
119+
size_t uBeginPos;
120+
if( uThread == 0 ){
121+
uBeginPos = 0;
122+
}else{
123+
uBeginPos = cfl[ uThread ].GetNextLineTop(
124+
cfl[ uThread ].GetFileSize() * uThread / uMaxThreadNum
125+
);
147126
}
148-
const wchar_t* pLine = cUnicodeBuffer.GetStringPtr();
149-
int nLineLen = cUnicodeBuffer.GetStringLength();
150-
++nLineNum;
151-
cDocMgr[ iThread ].AddNewLine( pLine, nLineLen );
152-
//経過通知
153-
if( nLineNum % 512 == 0 ){
154-
if( iThread == 0 ) NotifyProgress( cfl[ 0 ].GetPercent());
155-
// 処理中のユーザー操作を可能にする
156-
if( !::BlockingHook( NULL ) ){
157-
throw CAppExitException(); //中断検出
127+
128+
size_t uEndPos;
129+
if( uThread == uMaxThreadNum - 1 ){
130+
uEndPos = cfl[ uThread ].GetFileSize();
131+
}else{
132+
uEndPos = cfl[ uThread ].GetNextLineTop(
133+
cfl[ uThread ].GetFileSize() * ( uThread + 1 ) / uMaxThreadNum
134+
);
135+
}
136+
137+
cfl[ uThread ].SetBufLimit( uBeginPos, uEndPos );
138+
#ifdef DEBUG
139+
MYTRACE( L"pid:%d range:%u - %u\n", uThread, ( UINT )uBeginPos, ( UINT )uEndPos );
140+
#endif
141+
142+
// ReadLineはファイルから 文字コード変換された1行を読み出します
143+
// エラー時はthrow CError_FileRead を投げます
144+
int nLineNum = 0;
145+
CEol cEol;
146+
CNativeW cUnicodeBuffer;
147+
EConvertResult eRead;
148+
149+
while( RESULT_FAILURE != (eRead = cfl[ uThread ].ReadLine( &cUnicodeBuffer, &cEol ))){
150+
if(eRead==RESULT_LOSESOME){
151+
eRet = RESULT_LOSESOME;
152+
}
153+
const wchar_t* pLine = cUnicodeBuffer.GetStringPtr();
154+
int nLineLen = cUnicodeBuffer.GetStringLength();
155+
++nLineNum;
156+
cDocMgr[ uThread ].AddNewLine( pLine, nLineLen );
157+
//経過通知
158+
if( nLineNum % 512 == 0 ){
159+
if( uThread == 0 ){
160+
//NotifyProgress( cfl[ 0 ].GetPercent());
161+
// 処理中のユーザー操作を可能にする
162+
if( !::BlockingHook( NULL )) bBreakRead = true;
163+
}
164+
165+
if( bBreakRead ) break;
158166
}
159167
}
160-
}
168+
}));
161169
}
162170

163-
for( int i = 0; i < OmpMaxThreadNum; ++i ) pcDocLineMgr->Cat( &cDocMgr[ i ]);
171+
// 全スレッド終了待ち
172+
for( UINT u = 0; u < uMaxThreadNum; ++u ){
173+
cThread[ u ].join();
174+
}
175+
176+
if( bBreakRead ) throw CAppExitException(); //中断検出
177+
178+
for( UINT u = 0; u < uMaxThreadNum; ++u ) pcDocLineMgr->Cat( &cDocMgr[ u ]);
164179

165180
// 巨大ファイル判定
166181
pFileInfo->SetLargeFile(

sakura_core/StdAfx.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include <algorithm>
8484
#include <memory>
8585
#include <utility>
86+
#include <thread>
8687
#endif // ifndef SAKURA_PCH_MODE_MIN
8788

8889
#define PCRE2_STATIC
@@ -164,8 +165,6 @@
164165
#include "uiparts/CGraphics.h"
165166
#endif // SAKURA_PCH_MODE_MAX
166167

167-
#include "openmp.h"
168-
169168
//{{AFX_INSERT_LOCATION}}
170169
// Microsoft Visual C++ は前行の直前に追加の宣言を挿入します。
171170

sakura_core/doc/layout/CLayoutMgr_New.cpp

+52-49
Original file line numberDiff line numberDiff line change
@@ -326,70 +326,73 @@ BOOL CLayoutMgr::CalculateTextWidth( BOOL bCalLineLen, CLayoutInt nStart, CLayou
326326
}
327327
#endif
328328

329+
UINT uMaxThreadNum = std::thread::hardware_concurrency();
330+
std::vector<std::thread> cThread;
331+
329332
// レイアウト行の最大幅を取り出す
330-
CLayoutInt *nMaxLenL = new CLayoutInt[ OmpMaxThreadNum ];
331-
CLayoutInt *nMaxLineNumL = new CLayoutInt[ OmpMaxThreadNum ];
333+
std::vector<CLayoutInt> nMaxLenL( uMaxThreadNum );
334+
std::vector<CLayoutInt> nMaxLineNumL( uMaxThreadNum );
332335

333-
#pragma omp parallel for
334-
for( int iProcs = 0; iProcs < OmpMaxThreadNum; ++iProcs ){
335-
336-
CLayoutInt nMaxLen = CLayoutInt(0);
337-
CLayoutInt nMaxLineNum = CLayoutInt(0);
338-
339-
CLayoutInt nStartL = ( nEnd - nStart ) * OmpThreadId / OmpMaxThreadNum + nStart;
340-
CLayoutInt nEndL = ( nEnd - nStart ) * ( OmpThreadId + 1 ) / OmpMaxThreadNum + nStart;
341-
342-
CLayout* pLayout;
343-
344-
// 算出開始レイアウト行を探す
345-
if( nStartL == 0 ){
346-
pLayout = m_pLayoutTop;
347-
}else{
348-
pLayout = SearchLineByLayoutY( nStartL );
349-
}
350-
351-
for( CLayoutInt i = nStartL; i < nEndL; i++ ){
352-
if( !pLayout )
353-
break;
336+
for( UINT uThread = 0; uThread < uMaxThreadNum; ++uThread ){
337+
cThread.emplace_back( std::thread( [ &, this, uThread ]{
338+
339+
CLayoutInt nMaxLen = CLayoutInt(0);
340+
CLayoutInt nMaxLineNum = CLayoutInt(0);
341+
342+
CLayoutInt nStartL = ( nEnd - nStart ) * ( int ) uThread / ( int )uMaxThreadNum + nStart;
343+
CLayoutInt nEndL = ( nEnd - nStart ) * ( int )( uThread + 1 ) / ( int )uMaxThreadNum + nStart;
354344

355-
// レイアウト行の長さを算出する
356-
if( bCalLineLen ){
357-
CLayoutInt nWidth = pLayout->CalcLayoutWidth(*this) + CLayoutInt(pLayout->GetLayoutEol().GetLen()>0?1+m_nSpacing:0);
358-
pLayout->SetLayoutWidth( nWidth );
345+
CLayout* pLayout;
346+
347+
// 算出開始レイアウト行を探す
348+
if( nStartL == 0 ){
349+
pLayout = m_pLayoutTop;
350+
}else{
351+
pLayout = SearchLineByLayoutY( nStartL );
359352
}
360353

361-
// 最大幅を更新
362-
if( nMaxLen < pLayout->GetLayoutWidth() ){
363-
nMaxLen = pLayout->GetLayoutWidth();
364-
nMaxLineNum = i; // 最大幅のレイアウト行
365-
366-
// アプリケーションの最大幅となったら算出は停止
367-
if( nMaxLen >= MAXLINEKETAS * GetWidthPerKeta() && !bCalLineLen )
354+
for( CLayoutInt i = nStartL; i < nEndL; i++ ){
355+
if( !pLayout )
368356
break;
357+
358+
// レイアウト行の長さを算出する
359+
if( bCalLineLen ){
360+
CLayoutInt nWidth = pLayout->CalcLayoutWidth(*this) + CLayoutInt(pLayout->GetLayoutEol().GetLen()>0?1+m_nSpacing:0);
361+
pLayout->SetLayoutWidth( nWidth );
362+
}
363+
364+
// 最大幅を更新
365+
if( nMaxLen < pLayout->GetLayoutWidth() ){
366+
nMaxLen = pLayout->GetLayoutWidth();
367+
nMaxLineNum = i; // 最大幅のレイアウト行
368+
369+
// アプリケーションの最大幅となったら算出は停止
370+
if( nMaxLen >= MAXLINEKETAS * GetWidthPerKeta() && !bCalLineLen )
371+
break;
372+
}
373+
374+
// 次のレイアウト行のデータ
375+
pLayout = pLayout->GetNextLayout();
369376
}
370377

371-
// 次のレイアウト行のデータ
372-
pLayout = pLayout->GetNextLayout();
373-
}
374-
375-
nMaxLenL [ OmpThreadId ] = nMaxLen;
376-
nMaxLineNumL[ OmpThreadId ] = nMaxLineNum;
378+
nMaxLenL [ uThread ] = nMaxLen;
379+
nMaxLineNumL[ uThread ] = nMaxLineNum;
380+
}));
377381
}
378382

379383
// reduction
380-
CLayoutInt nMaxLen = nMaxLenL[ 0 ];
381-
CLayoutInt nMaxLineNum = nMaxLineNumL[ 0 ];
384+
CLayoutInt nMaxLen = CLayoutInt( 0 );
385+
CLayoutInt nMaxLineNum = CLayoutInt( 0 );
382386

383-
for( int i = 1; i < OmpMaxThreadNum; ++i ){
384-
if( nMaxLen < nMaxLenL[ i ]){
385-
nMaxLen = nMaxLenL[ i ];
386-
nMaxLineNum = nMaxLineNumL[ i ]; // 最大幅のレイアウト行
387+
for( UINT u = 0; u < uMaxThreadNum; ++u ){
388+
cThread[ u ].join();
389+
390+
if( nMaxLen < nMaxLenL[ u ]){
391+
nMaxLen = nMaxLenL[ u ];
392+
nMaxLineNum = nMaxLineNumL[ u ]; // 最大幅のレイアウト行
387393
}
388394
}
389395

390-
delete [] nMaxLenL;
391-
delete [] nMaxLineNumL;
392-
393396
// テキストの幅の変化をチェック
394397
if( Int(nMaxLen) ){
395398
// 最大幅が拡大した または 最大幅の拡大のみチェックでない

sakura_core/openmp.h

-30
This file was deleted.

0 commit comments

Comments
 (0)