Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 alternative to OpenMP with clang

Clang doesn't support OpenMP (yet) but is it possible to implement a "parallel for" with C++11 ?

like image 307
user2508888 Avatar asked Jun 21 '13 12:06

user2508888


People also ask

Does clang support OpenMP?

Clang fully supports OpenMP 4.5. Clang supports offloading to X86_64, AArch64, PPC64[LE] and has basic support for Cuda devices.

Is OpenMP still used?

OpenMP is extensively used as a second level to improve parallelism inside each MPI domain.

What languages support OpenMP?

OpenMP (Open Multi-Processing) is an application programming interface (API) that supports multi-platform shared-memory multiprocessing programming in C, C++, and Fortran, on many platforms, instruction-set architectures and operating systems, including Solaris, AIX, FreeBSD, HP-UX, Linux, macOS, and Windows.

Which type of parallel programming is supported by OpenMP API?

OpenMP is an Application Programming Interface (API) to write shared memory parallel applications in C, C++ and Fortran.


1 Answers

OpenMP version :

// 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;
}

C++11 version:

// 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 clauses (firstprivate...) can be implemented in the same way but it's (a little) more work...

like image 179
user2508888 Avatar answered Oct 16 '22 07:10

user2508888