Enzyme icon indicating copy to clipboard operation
Enzyme copied to clipboard

OpenMP reductions seem unsupported

Open athas opened this issue 7 months ago • 0 comments

Consider this program:

#include <cmath>
#include <vector>
#include <omp.h>

extern int enzyme_dup;
extern int enzyme_dupnoneed;
extern int enzyme_out;
extern int enzyme_const;
void       __enzyme_autodiff(...) noexcept;

void primal(size_t n, const double* x, double* out) {
  double sum = 0;
#pragma omp parallel for reduction(+:sum)
  for (size_t i = 0; i < n; i++) {
    sum += x[i];
  }
  *out = sum;
}

int main() {
  size_t n = 10;
  double input[10];
  double output[10];
  double dummy, unit = 1;
  __enzyme_autodiff(primal,
                    enzyme_const, n,
                    enzyme_dup, input, output,
                    enzyme_dupnoneed, &dummy, &unit);
}

When compiled with Enzyme I get a long error, but the important part is this:

ld.lld: /build/source/enzyme/Enzyme/CallDerivatives.cpp:2345: bool AdjointGenerator::handleKnownCallDerivatives(CallInst &, Function *, StringRef, bool, const std::vector<bool> &, CallInst *const): Assertion `0 && "unhandled openmp function"' failed.

This has been reported earlier in #1246, but that issue was closed and migrated to a discussion: https://github.com/EnzymeAD/Enzyme/discussions/1251

I was encouraged by wmoses to open an issue, so here it is. Supporting OpenMP reductions would certainly be very nice, but it is not a dealbreaker, as it is not very difficult to write your own using the parts of OpenMP that Enzyme does support.

athas avatar May 15 '25 20:05 athas