通过多线程确定每个字符在文件中出现的次数

时间:2018-01-18 08:02:17

标签: c++ multithreading

我的代码。我写道,我可以。现在需要你的帮助。告诉我 怎么了。我如何像拥有符号一样多次屏蔽?我不知道怎么做得好,我看书。但仍然没有任何效果

void TextThread(std::string str)
{
  std::ifstream text(str);
  if (!text)
    std::cout << "No open file" << "\n";

  std::istream_iterator<char> input(text);
  std::istream_iterator<char> output;

  std::vector<char> symvol(input, output);

  unsigned maxThreadCount = std::thread::hardware_concurrency();
  const std::size_t minLength = symvol.size() / maxThreadCount;
  const std::size_t modulo = symvol.size() % maxThreadCount;

  std::vector<std::size_t> results;
  results.reserve(maxThreadCount);

  std::vector<std::thread> threads;
  threads.reserve(maxThreadCount - 1);

  for (std::size_t i = 0; i < modulo; ++i)
    results.emplace_back(minLength + 1);

  if (minLength > 0)
  {
     for (std::size_t i = modulo; i < maxThreadCount; ++i)
        results.emplace_back(minLength);
  }
  for (std::size_t i = 1; i < threads.size(); ++i)
    threads.emplace_back(threads[i - 1] + 1, threads[i - 1] + threads[i]);

  std::for_each(threads.begin(), threads.end(), 
   std::mem_fn(&std::thread::join));


  for (unsigned int i = 0; i < symvol.size(); ++i)
    std::cout << symvol[i] << "\n";
}


int main()
{
  TextThread("D:\\text.txt");
  return 0;
}

UPD: 我将在屏幕上显示所有字符

void TextThread(std::string str)
 {
   std::ifstream text(str);
   if (!text)
    std::cout << "No open file" << "\n";

  std::istream_iterator<char> input(text);
  std::istream_iterator<char> output;

  std::vector<char> symvol(input, output);
  for (unsigned int i = 0; i < symvol.size(); ++i)
    std::cout << symvol[i] << "\n";
 }

UPD2:

喜欢你。它仍与多线程有关

 using Symbol = std::array<size_t, 256>;
 Symbol  CountSymbol(std::vector<char> const& data) 
 {
    Symbol countSymbol{};
    for (unsigned char ch : data) 
        countSymbol[ch] += 1;
    return countSymbol;
 }


 Symbol SymbolFile(std::string const& fname) 
 {
   std::ifstream text(fname);

   if (!text)
      throw std::runtime_error("No open file");

   std::vector<char> const data(std::istreambuf_iterator<char>{text}, {});
     return CountSymbol(data);
 }

 int main() 
 {
    Symbol  symbol = SymbolFile("D:\\text.txt");
    for (int ch = 0; ch <= 255; ++ch) 
    {
       if (symbol[ch]) 
       {
           if (std::isprint(ch))
            std::cout << "Character '" << char(ch) << "' occurs " << 
        symbol[ch] << " times\n";
           else
               std::cout << "Character #" << ch << " occurs " << symbol[ch] 
        << " times\n";
       }
    }
}

UPD3

 #include "stdafx.h"
 #include <iostream>
 #include <fstream>
 #include <iterator>
 #include <algorithm>
 #include <string>
 #include <vector>
 #include <unordered_map>
 #include <thread>
 #include <cctype>
 #include <clocale>
 #include <numeric>

 std::vector<std::pair<std::size_t, std::size_t>> GenerateIntervals(const 
       std::vector<char>& text)
 {
     std::size_t maxThreadCount = std::thread::hardware_concurrency();
     const std::size_t minLength = text.size() / maxThreadCount;
     const std::size_t modulo = text.size() % maxThreadCount;

     std::vector<std::size_t> intervalLengthPerThread;
     intervalLengthPerThread.reserve(maxThreadCount);

     for (std::size_t i = 0; i < modulo; ++i)
    intervalLengthPerThread.emplace_back(minLength + 1);

if (minLength > 0)
{
    for (std::size_t i = modulo; i < maxThreadCount; ++i)
        intervalLengthPerThread.emplace_back(minLength);
}

std::vector<std::pair<std::size_t, std::size_t>> intervals;
intervals.reserve(intervalLengthPerThread.size());

intervals.emplace_back(0, 0 + intervalLengthPerThread[0] - 1);
for (std::size_t i = 1; i < intervalLengthPerThread.size(); ++i)
    intervals.emplace_back(intervals[i - 1].second + 1, intervals[i - 1].second + intervalLengthPerThread[i]);

return intervals;
}

 void BuildHistogram(const std::pair<std::size_t, std::size_t>& textRange, 
 const std::vector<char>& text, std::unordered_map<char, std::size_t>& 
  histogram)
 {
    for (std::size_t i = textRange.first; i < textRange.second; ++i) 
        ++histogram[text[i]];
 }

 std::vector<char> ReadDataFromFile(const std::string& pathToFile)
{
std::ifstream stream(pathToFile);

if (!stream) 
{
    std::vector<char> result;
    std::cout << "Can not open file" << "\n";
    return result;
}

std::istream_iterator<char> it(stream);
std::istream_iterator<char> itEnd;

std::vector<char> text(it, itEnd);
}

 int main() 
 {
auto text = ReadDataFromFile("D:\\text.txt");

const auto textIntervals = GenerateIntervals(text);
const auto usedThreadCount = textIntervals.size();

std::vector<std::unordered_map<char, size_t>> histograms(usedThreadCount);
std::vector<std::thread> threads(usedThreadCount);

for (std::size_t i = 0; i < usedThreadCount; ++i)
    threads[i] = std::thread(BuildHistogram, std::cref(textIntervals[i]), text, std::ref(histograms[i]));

for (std::size_t i = 0; i < usedThreadCount; ++i)
    threads[i].join();

std::unordered_map<char, size_t> histogram;
for (const auto& hist : histograms)
{
    for (const auto& pair : hist)
        histogram[pair.first] += pair.second;
}

for (int ch = 0; ch < 256; ++ch) 
{
    if (text[ch]) 
    {
        std::cout << "Character '" << char(ch) << "' occurs " << text[ch] << " times\n";
    }
}
return 0;

}

1 个答案:

答案 0 :(得分:3)

我会在这里提供帮助,因为你很丢失。

目标

认为你想要一个直方图,所以像:

std::array<size_t, 256> histo;

这里,每个元素对应输入字符的整数值(例如,a为97)。

这是直截了当的实施:

using Histo = std::array<size_t, 256>;

Histo histogram(std::vector<char> const& data) {
    Histo histo {}; // value initialize, makes sure all elements are 0

    for (auto ch : data) {
        histo[ch] += 1;
    }

    return histo;
}
  

注意这里有一个非常微妙的地方,我将角色转换为unsigned char(也可以说是uint8_t)。这是因为某些平台会char进行签名,并在索引histo[ch]或比较时(例如ch <= 255始终为真)导致错误。

你可以操作这样的文件内容:

Histo histogram_file(std::string const& fname) {
    std::ifstream text(fname);

    if (!text)
        throw std::runtime_error("No open file");

    std::vector<char> const data(std::istreambuf_iterator<char>{text}, {});
    return histogram(data);
}
  

注意使用istreambuf_iterator 会跳过空格(默认情况下为istreambuf_iterator,请参阅http://en.cppreference.com/w/cpp/io/manip/skipws)。< / p>

现场演示

简单代码 Live On Coliru

#include <thread>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <iterator>

using Histo = std::array<size_t, 256>;

Histo histogram(std::vector<char> const& data) {
    Histo histo {}; // value initialize, makes sure all elements are 0

    for (unsigned char ch : data) {
        histo[ch] += 1;
    }

    return histo;
}


Histo histogram_file(std::string const& fname) {
    std::ifstream text(fname);

    if (!text)
        throw std::runtime_error("No open file");

    std::vector<char> const data(std::istreambuf_iterator<char>{text}, {});
    return histogram(data);
}

int main() {
    Histo histo = histogram_file("main.cpp");
    for (int ch = 0; ch <= 255; ++ch) {
        if (histo[ch]) {
            if (std::isprint(ch))
                std::cout << "Character '" << char(ch) << "' occurs " << histo[ch] << " times\n";
            else
                std::cout << "Character #" << ch << " occurs " << histo[ch] << " times\n";
        }
    }
}

打印类似

的内容
Character #10 occurs 41 times
Character ' ' occurs 224 times
Character '!' occurs 1 times
Character '"' occurs 16 times
Character '#' occurs 7 times
...
Character 'x' occurs 3 times
Character 'y' occurs 1 times
Character 'z' occurs 2 times
Character '{' occurs 9 times
Character '}' occurs 9 times

使其成为线程

要分发作品,我们必须为部分矢量数据调用histogram。让我们稍微修改我们的函数来实现:

template <typename Iterator> // could just use `char const*` but let's not
Histo histogram(Iterator begin, Iterator end) {
    Histo histo {}; // value initialize, makes sure all elements are 0

    for (auto it = begin; it != end; ++it) {
        histo[*it] += 1;
    }

    return histo;
}

Histo histogram(std::vector<char> const& data) {
    return histogram(data.begin(), data.end());
}

如您所见,我们可以将“旧”签名转发给新的,更灵活的界面。当然你可以直接使用char const*获得挑剔并且具有完全相同的代码,但如果输入与向量不同,它将无法正常工作。或者如果它变成了std::deque<unsigned char>等等。

执行并发

我们希望以相同的方式划分工作:

using It = std::vector<char>::const_iterator;

// calculate chunk size
auto chunk = data.size() / num_threads;
if (chunk*num_threads < data.size()) chunk += 1;
  

请注意,最后一个块可以扩展(

因为你不能(有用)在所有线程之间共享Histo对象(同步访问的成本远远超过并发处理的任何速度优势),我们必须使用Histo每个线程。

让我们先做笨拙和手动:

////////////////////////////////////////////
// start chunk threads
struct worker_t {
    Histo local; // histogram per thread
    std::thread th;

    void run(It begin, It end) {
        local = histogram(begin, end);
    }
};
std::vector<worker_t> workers(num_threads);

现在我们可以在每个工作器实例上启动run,传递计算出的子范围:

int i = 0;
for (auto& w : workers) {
    auto begin = data.begin()+chunk*i,
         end   = std::min(begin + chunk, data.end());
    w.th = std::thread(&worker_t::run, std::ref(w), begin, end);
    ++i;
}

等待和合并

////////////////////////////////////////////
// wait for completion
for (auto& w : workers) {
    if (w.th.joinable())
        w.th.join();
};

这几乎就是你已经拥有的。

////////////////////////////////////////////
// merge results
Histo histo;
for (auto& w : workers) {
    merge_into(histo, w.local);
}

return histo;

注意编写想要阅读的代码的习惯,然后实现更多细节。在这种情况下,我们仍然定义merge_into

void merge_into(Histo& into, Histo const& part) {
    for (auto& p : part)
        into[p.first] += p.second;
}

体力劳动者演示

以上 Live On Coliru MSVC on RexTester

#include <thread>
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>

using Histo = std::map<char, size_t>;

void merge_into(Histo& into, Histo const& part) {
    for (auto& p : part)
        into[p.first] += p.second;
}

template <typename Iterator> // could just use `char const*` but let's not
Histo histogram(Iterator begin, Iterator end) {
    Histo histo {}; // value initialize, makes sure all elements are 0

    for (auto it = begin; it != end; ++it) {
        histo[*it] += 1;
    }

    return histo;
}

Histo histogram(std::vector<char> const& data) {
    return histogram(data.begin(), data.end());
}

Histo parallel_histo(std::vector<char> const& data, size_t num_threads = std::thread::hardware_concurrency()) {
    using It = std::vector<char>::const_iterator;

    // calculate chunk size
    auto chunk = data.size() / num_threads;
    if (chunk*num_threads < data.size()) chunk += 1;

    ////////////////////////////////////////////
    // start chunk threads
    struct worker_t {
        Histo local; // histogram per thread
        std::thread th;

        void run(It begin, It end) {
            local = histogram(begin, end);
        }
    };
    std::vector<worker_t> workers(num_threads);

    int i = 0;
    for (auto& w : workers) {
        auto begin = data.begin()+chunk*i,
             end   = std::min(begin + chunk, data.end());
        w.th = std::thread(&worker_t::run, std::ref(w), begin, end);
        ++i;
    }

    ////////////////////////////////////////////
    // wait for completion
    for (auto& w : workers) {
        if (w.th.joinable())
            w.th.join();
    };

    ////////////////////////////////////////////
    // merge results
    Histo histo;
    for (auto& w : workers) {
        merge_into(histo, w.local);
    }

    return histo;
}

Histo histogram_file(std::string const& fname) {
    std::ifstream text(fname);

    if (!text)
        throw std::runtime_error("No open file");

    return parallel_histo({std::istreambuf_iterator<char>{text}, {}});
}

int main() {
    Histo histo = histogram_file("main.cpp");
    for (int ch = 0; ch <= 255; ++ch) {
        if (histo[ch]) {
            if (std::isprint(ch))
                std::cout << "Character '" << char(ch) << "' occurs " << histo[ch] << " times\n";
            else
                std::cout << "Character #" << ch << " occurs " << histo[ch] << " times\n";
        }
    }
}

打印与以前相同的输出。

不那么笨拙

用于分配输入和生成结果的任务的“标准库”是使用std::async,它可以简单地返回结果。它并不完美,但它的工作量却少得多:

<强> Live On Coliru

Histo parallel_histo(std::vector<char> const& data, size_t num_threads = std::thread::hardware_concurrency()) {
    // calculate chunk size
    auto chunk = data.size() / num_threads;
    if (chunk*num_threads < data.size()) chunk += 1;

    ////////////////////////////////////////////
    // start tasks
    std::vector<std::future<Histo> > tasks(num_threads);

    int i = 0;
    for (auto& t : tasks) {
        auto begin = data.begin()+chunk*i,
             end   = std::min(begin + chunk, data.end());

        t = std::async([=,&data] { return histogram(begin, end); });
    }

    Histo histo;
    for (auto& t : tasks) {
        merge_into(histo, t.get());
    }

    return histo;
}

禅师

当然,如果您真的知道标准库,那么您将使用它(c ++ 17):

#include <atomic>
#include <execution>
#include <algorithm>

using Histo = std::vector<std::atomic_size_t>;

Histo histogram(std::vector<char> const& data) {
    Histo histo(256);
    std::for_each(std::execution::par_unseq, data.begin(), data.end(), [&histo](unsigned char ch) { ++histo[ch]; });
    return histo;
}

Histo histogram_file(std::string const& fname) {
    std::ifstream text(fname);

    if (!text)
        throw std::runtime_error("No open file");

    return histogram({std::istreambuf_iterator<char>{text}, {}});
}

可悲的是,你还不能使用它,因为没有编译器供应商实际上(完全)实现它。