"while"루프를 PPL 방식으로 병렬화해야합니다. 나는 Visual C++에서 MS VS 2013에서 다음 코드를 가지고있다.PPL을 사용하여 "while"루프를 병렬화하는 방법
int WordCount::CountWordsInTextFiles(basic_string<char> p_FolderPath, vector<basic_string<char>>& p_TextFilesNames)
{
// Word counter in all files.
atomic<unsigned> wordsInFilesTotally = 0;
// Critical section.
critical_section cs;
// Set specified folder as current folder.
::SetCurrentDirectory(p_FolderPath.c_str());
// Concurrent iteration through p_TextFilesNames vector.
parallel_for(size_t(0), p_TextFilesNames.size(), [&](size_t i)
{
// Create a stream to read from file.
ifstream fileStream(p_TextFilesNames[i]);
// Check if the file is opened
if (fileStream.is_open())
{
// Word counter in a particular file.
unsigned wordsInFile = 0;
// Read from file.
while (fileStream.good())
{
string word;
fileStream >> word;
// Count total number of words in all files.
wordsInFilesTotally++;
// Count total number of words in a particular file.
wordsInFile++;
}
// Verify the values.
cs.lock();
cout << endl << "In file " << p_TextFilesNames[i] << " there are " << wordsInFile << " words" << endl;
cs.unlock();
}
});
// Destroy critical section.
cs.~critical_section();
// Return total number of words in all files in the folder.
return wordsInFilesTotally;
}
이 코드는 외부 루프에서 std :: vector를 통해 병렬 반복을 수행한다. 병렬 처리는 concurrency :: parallel_for() 알고리즘에 의해 제공됩니다. 그러나이 코드는 또한 파일에서 읽기를 실행하는 "while"루프를 중첩했습니다. 이 중첩 된 "while"루프를 병렬 처리해야합니다. 이 중첩 된 "while"루프는 PPL 방식으로 어떻게 병렬화 될 수 있습니까? 도와주세요.
while 루프를 병렬 처리하기 전에 많은 시간을 들여 하드웨어가 어떻게 단일 파일에서 병렬 읽기를 지원하는지 자문 해보십시오. –
텍스트 파일을 병렬로 읽어야합니다. 도움! – user3769902