This forum has been archived. All content is frozen. Please use KDE Discuss instead.

[Solved] How to named the sub expressions?

Tags: None
(comma "," separated)
forza inter
Registered Member
Posts
3
Karma
0
Before Eigen v3.3, i can use the following c++1y code to named the sub expressions to improve readability. However with the current version, i got "Segmentation fault (core dumped)".
Code: Select all
#include <iostream>
#include <exception>

#include <tbb/task_scheduler_init.h>
#include <tbb/parallel_reduce.h>
#include <tbb/blocked_range.h>

#include <eigen3/Eigen/Core>


int main(int argc, char* argv[]) try {
  using namespace std;
  Eigen::initParallel();
  tbb::task_scheduler_init init(tbb::task_scheduler_init::default_num_threads());
  const std::ptrdiff_t size(1024);
  const Eigen::MatrixXf m(Eigen::MatrixXf::Random(size, size));

  const tbb::blocked_range<std::ptrdiff_t> range(0, size);
  const float ANGAST(9.2), DFA(3.2), DFB(4.6);
  const float result(
  tbb::parallel_reduce(
          range,
          float(5),
          [=, &m]
          (const tbb::blocked_range<std::ptrdiff_t> &range, const float v) -> float {
              for(std::ptrdiff_t i(range.begin()); i<range.end(); ++i) {
                  const auto angdif(m.array()-ANGAST);
                  const auto ccos((float(2)*angdif).cos());
                  const auto df(float(.5)*(DFA*angdif+DFB+ccos*(DFA-DFB)));
                  const float ret(df.square().sum());
                  return ret;
              }
          },
          [](const float lhs, const float rhs) -> const float { return lhs>rhs?lhs:rhs; }));
  std::cout<<"output = "<<result<<"\n";
  return 0;
} catch (const std::exception& e) {
  std::cerr << e.what() << std::endl;
} catch(...) {
  std::cerr << "Exception of unknown type!" << std::endl;
}

I have to use the following, but i am still wondering are there any ways to named the sub expressions in current version?
Thanks in advance.
Code: Select all
#include <iostream>
#include <exception>

#include <tbb/task_scheduler_init.h>
#include <tbb/parallel_reduce.h>
#include <tbb/blocked_range.h>

#include <eigen3/Eigen/Core>


int main(int argc, char* argv[]) try {
  using namespace std;
  Eigen::initParallel();
  tbb::task_scheduler_init init(tbb::task_scheduler_init::default_num_threads());
  const std::ptrdiff_t size(1024);
  const Eigen::MatrixXf m(Eigen::MatrixXf::Random(size, size));

  const tbb::blocked_range<std::ptrdiff_t> range(0, size);
  const float ANGAST(9.2), DFA(3.2), DFB(4.6);
  const float result(
  tbb::parallel_reduce(
          range,
          float(5),
          [=, &m]
          (const tbb::blocked_range<std::ptrdiff_t> &range, const float v) -> float {
              for(std::ptrdiff_t i(range.begin()); i<range.end(); ++i) {
                  const float ret((float(.5)*(DFA*(m.array()-ANGAST)+DFB+(float(2)*(m.array()-ANGAST)).cos()*(DFA-DFB))).square().sum());
                  return ret;
              }
          },
          [](const float lhs, const float rhs) -> const float { return lhs>rhs?lhs:rhs; }));
  std::cout<<"output = "<<result<<"\n";
  return 0;
} catch (const std::exception& e) {
  std::cerr << e.what() << std::endl;
} catch(...) {
  std::cerr << "Exception of unknown type!" << std::endl;
}

Last edited by forza inter on Mon Oct 27, 2014 10:03 am, edited 1 time in total.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
can you show the backtrace, as in theory the upcoming 3.3 version should exhibit much fewer pitfalls regarding named expression.
forza inter
Registered Member
Posts
3
Karma
0
I changed it to single thread version as below, and the result is the same ("Segmentation fault (core dumped)").
Here's the backtrace:
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".
Core was generated by `bin/v2'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0 0x0000000000400ebc in main (argc=<optimized out>, argv=<optimized out>) at src/main.c11:10
10 const Eigen::MatrixXf m(Eigen::MatrixXf::Random(size, size));
(gdb) bt full
#0 0x0000000000400ebc in main (argc=<optimized out>, argv=<optimized out>) at src/main.c11:10
m = {<Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >> = {<Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >> = {<Eigen::DenseBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >> = {<Eigen::internal::special_scalar_op_base<Eigen::Matrix<float, -1, -1, 0, -1, -1>, float, float, false>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, -1, -1, 0, -1, -1>, 3>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, -1, -1, 0, -1, -1>, 1>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, -1, -1, 0, -1, -1>, 0>> = {<Eigen::EigenBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >> = {<No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, m_storage = {m_data = 0x7f8ea85dd010, m_rows = 1024, m_cols = 1024}}, <No data fields>}
ANGAST = 9.19999981
DFA = 3.20000005
DFB = 4.5999999
result = 5
(gdb) bt
#0 0x0000000000400ebc in main (argc=<optimized out>, argv=<optimized out>) at src/main.c11:10
(gdb) info threads
Id Target Id Frame
* 1 Thread 0x7f8eabf4a740 (LWP 32627) 0x0000000000400ebc in main (argc=<optimized out>, argv=<optimized out>) at src/main.c11:10
(gdb) thread apply all bt

Thread 1 (Thread 0x7f8eabf4a740 (LWP 32627)):
#0 0x0000000000400ebc in main (argc=<optimized out>, argv=<optimized out>) at src/main.c11:10
(gdb) thread apply all bt full

Thread 1 (Thread 0x7f8eabf4a740 (LWP 32627)):
#0 0x0000000000400ebc in main (argc=<optimized out>, argv=<optimized out>) at src/main.c11:10
m = {<Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >> = {<Eigen::MatrixBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >> = {<Eigen::DenseBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >> = {<Eigen::internal::special_scalar_op_base<Eigen::Matrix<float, -1, -1, 0, -1, -1>, float, float, false>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, -1, -1, 0, -1, -1>, 3>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, -1, -1, 0, -1, -1>, 1>> = {<Eigen::DenseCoeffsBase<Eigen::Matrix<float, -1, -1, 0, -1, -1>, 0>> = {<Eigen::EigenBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >> = {<No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, <No data fields>}, m_storage = {m_data = 0x7f8ea85dd010, m_rows = 1024, m_cols = 1024}}, <No data fields>}
ANGAST = 9.19999981
DFA = 3.20000005
DFB = 4.5999999
result = 5
(gdb)

Code: Select all
#include <iostream>
#include <exception>

#include <eigen3/Eigen/Core>


int main(int argc, char* argv[]) try {
  using namespace std;
  const std::ptrdiff_t size(1024);
  const Eigen::MatrixXf m(Eigen::MatrixXf::Random(size, size));

  const float ANGAST(9.2), DFA(3.2), DFB(4.6);
  float result(5);
  for(std::ptrdiff_t i(0); i<size; ++i) {
      const auto angdif(m.array()-ANGAST);
      const auto ccos((float(2)*angdif).cos());
      const auto df(float(.5)*(DFA*angdif+DFB+ccos*(DFA-DFB)));
      const float ret(df.square().sum());
      result = std::max(result, ret);
  }
  std::cout<<"output = "<<result<<"\n";
  return 0;
} catch (const std::exception& e) {
  std::cerr << e.what() << std::endl;
} catch(...) {
  std::cerr << "Exception of unknown type!" << std::endl;
}

ggael wrote:can you show the backtrace, as in theory the upcoming 3.3 version should exhibit much fewer pitfalls regarding named expression.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I cannot reproduce using either clang and gcc. I also run the code within valgrind, no error. Make sure your copy of the devel branch is up-to-date.
forza inter
Registered Member
Posts
3
Karma
0

Re: How to named the sub expressions?

Mon Oct 27, 2014 10:02 am
Sorry, actually i made a mistake. Before i have a system wide eigen installation (extra/eigen 3.2.2-1), after i replaced it with the latest hg version and now it's running well.
Thanks a lot.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]