Подтвердить что ты не робот

С++ 11 альтернатива OpenMP с clang

Clang не поддерживает OpenMP (пока), но возможно ли реализовать параллель для С++ 11?

4b9b3361

Ответ 1

Версия OpenMP:

// parallelfor_gcc.cpp
// g++ -O2 -Wall -std=c++11 -fopenmp parallelfor_gcc.cpp
#include <cmath>
#include <vector>
int main() {
  unsigned int size = 1e8;
  std::vector<double> vect(size);
#pragma omp parallel for
  for (unsigned int i=0; i<size; i++) {
    vect[i] = sin(2*M_PI*i/(double)size);
  }
  return 0;
}

Версия С++ 11:

// parallelfor_clang.cpp
// clang++ -O4 -Wall -std=c++11 -lpthread parallelfor_clang.cpp
#include <cmath>
#include <thread>
#include <vector>
void parallelFor(const unsigned int size, 
                 std::function<void(const unsigned int)> func) {
  const unsigned int nbThreads = std::thread::hardware_concurrency();
  std::vector < std::thread > threads;
  for (unsigned int idThread = 0; idThread < nbThreads; idThread++) {
    auto threadFunc = [=, &threads]() {
      for (unsigned int i=idThread; i<size; i+=nbThreads) {
        func(i);
      }
    };
    threads.push_back(std::thread(threadFunc));
  }
  for (auto & t : threads) t.join();
}
int main() {
  unsigned int size = 1e8;
  std::vector<double> vect(size);
  auto myFunc = [=, &vect](unsigned int i){
    vect[i] = sin(2*M_PI*i/(double)size);
  };
  parallelFor(size, myFunc);
  return 0;
}

Классы OpenMP (firstprivate...) могут быть реализованы одинаково, но немного (немного) работают...