OMP并行化不适用于可变CRTP类

时间:2018-10-01 21:15:28

标签: c++ performance openmp crtp

我有一个CRTP基类的编码问题,该基类提供了一个在派生类的循环中调用的函数

template<typename Derived>
class Func
{
public:
  double func(int i)
  {
    return pow(i + 1, 2.5);
  }
};

template<template<typename> typename... Args>
class Loop : public Args<Loop<Args...>>...
{
public:
  static const size_t N = 100000000;
  std::array<double, N> _array;

  void loop()
  {
#pragma omp parallel for schedule(static)
    for (int i = 0; i < N; i++)
    {
      _array[i] = func(i);
    }
  }
};

(在此示例中,不需要crtp,但是在我的代码中)。 在Visual Studio 2017中使用/ MD / MP / O2 / Oi / Ot / fp:precise / D NDEBUG / openmp / GL进行标记,该循环未并行化,而当我放下可变参数模板时

class Loop2 : public Func<Loop<Func>>
{
public:
  static const size_t N = 100000000;
  std::array<double, N> _array;

  void loop()
  {
#pragma omp parallel for schedule(static)
    for (int i = 0; i < N; i++)
    {
        _array[i] = func(i);
    }
  }
};

循环正在并行运行。 是否可以保持可变参数模板的舒适性并使循环并行化?

编辑:使用Clang和Visual Studio作为编译器,两个循环都并行化

0 个答案:

没有答案