ddemidov / vexcl

VexCL is a C++ vector expression template library for OpenCL/CUDA/OpenMP
http://vexcl.readthedocs.org
MIT License
701 stars 81 forks source link

Expression templates, temporaries in subexpressions #51

Closed roystgnr closed 11 years ago

roystgnr commented 11 years ago

I managed to get a segfault out of libstdc++ valarray by indirectly forcing it to store a reference to a temporary subexpression object: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57997

And I've since managed to get corrupted data out of libstdc++ valarray without even returning anything other than a full valarray object from a function:

  auto expr_templ = make_temp(1.f)*make_temp(1.f)/2;
  std::valarray<float> val = expr_templ;

(The results of make_temp go out of scope after that first operator= finishes, at which point expr_templ is left holding more dangling references.)

I haven't managed to get any segfaults or even regression failures out of VexCL or Eigen expression templates yet, but now I'm starting to wonder: are correct results in these cases guaranteed by your library design, or am I writing unsupported code and just getting lucky?

ddemidov commented 11 years ago

I can not comment on eigen library implementation, but I did explicitly try to support this kind of workflow with vexcl. Vexcl expressions store everything except for vectors and multivectors by value, so the lack of segfaults is expected :).

See https://github.com/ddemidov/vexcl/blob/master/tests/generator.cpp#L151for the example of how this may be used. Btw, the comment about how x is read several times there is outdated, since I started to use tags. But the test is still less effective than the one that uses generator. On Aug 2, 2013 1:24 AM, "roystgnr" notifications@github.com wrote:

I managed to get a segfault out of libstdc++ valarray by indirectly forcing it to store a reference to a temporary subexpression object: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57997

And I've since managed to get corrupted data out of libstdc++ valarray without even returning anything other than a full valarray object from a function:

auto expr_templ = make_temp(1.f)*make_temp(1.f)/2; std::valarray val = expr_templ;

(The results of make_temp go out of scope after that first operator= finishes, at which point expr_templ is left holding more dangling references.)

I haven't managed to get any segfaults or even regression failures out of VexCL or Eigen expression templates yet, but now I'm starting to wonder: are correct results in these cases guaranteed by your library design, or am I writing unsupported code and just getting lucky?

— Reply to this email directly or view it on GitHubhttps://github.com/ddemidov/vexcl/issues/51 .