jupyter-xeus / xeus-cling

Jupyter kernel for the C++ programming language
BSD 3-Clause "New" or "Revised" License
3.06k stars 296 forks source link

Problems using random number generators #213

Open jogojapan opened 5 years ago

jogojapan commented 5 years ago

Trying to install xeus-cling I had the same problems as described in https://github.com/QuantStack/xeus-cling/issues/187 and following the instructions there, I re-installed it in a clean conda environment using

conda install xeus-cling notebook -c conda-forge/label/gcc7

That allowed me to use xeus-cling notebooks, and for the most part it works well.

However I ran into an error (see at the end of my comment) when compiling the minimal example below, using random number generators and a lambda expression.

There are two things that confuse me about this:

Code:

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <random>

void test() {
  std::random_device rd;
  std::mt19937 gen(rd());
  std::uniform_int_distribution<int> dist(0,std::numeric_limits<int>::max());

  std::vector<int> vec(10,0);
  std::generate(begin(vec),end(vec),[&dist,&gen](){return dist(gen);});

  std::copy(begin(vec),end(vec),
           std::ostream_iterator<int>(std::cout," "));
  std::cout << std::endl;
}

test();

Error from the xeus-cling notebook (C++14 kernel):

In file included from input_line_5:1:
In file included from /space/conda/xeus/include/xeus/xinterpreter.hpp:16:
In file included from /space/conda/xeus/include/xeus/xcomm.hpp:18:
In file included from /space/conda/xeus/include/xeus/xguid.hpp:12:
In file included from /space/conda/xeus/include/xtl/xbasic_fixed_string.hpp:20:
In file included from /space/conda/xeus/include/nlohmann/json.hpp:37:
In file included from /space/conda/xeus/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/algorithm:62:
In file included from /space/conda/xeus/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/bits/stl_algo.h:66:
/space/conda/xeus/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/bits/uniform_int_dist.h:228:61: error: no type named 'type' in 'std::common_type<unsigned long, unsigned int>'
        typedef typename std::common_type<_Gresult_type, __utype>::type
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/space/conda/xeus/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/bits/uniform_int_dist.h:169:24: note: in instantiation of function template specialization 'std::uniform_int_distribution<int>::operator()<std::mersenne_twister_engine<unsigned long, 32, 624, 397, 31, 2567483615, 11,
      4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253> >' requested here
        { return this->operator()(__urng, _M_param); }
                       ^
input_line_9:6:63: note: in instantiation of function template specialization 'std::uniform_int_distribution<int>::operator()<std::mersenne_twister_engine<unsigned long, 32, 624, 397, 31, 2567483615, 11,
      4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253> >' requested here
  std::generate(begin(vec),end(vec),[&dist,&gen](){return dist(gen);});
                                                              ^
In file included from input_line_5:1:
In file included from /space/conda/xeus/include/xeus/xinterpreter.hpp:16:
In file included from /space/conda/xeus/include/xeus/xcomm.hpp:18:
In file included from /space/conda/xeus/include/xeus/xguid.hpp:12:
In file included from /space/conda/xeus/include/xtl/xbasic_fixed_string.hpp:20:
In file included from /space/conda/xeus/include/nlohmann/json.hpp:37:
In file included from /space/conda/xeus/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/algorithm:62:
In file included from /space/conda/xeus/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/bits/stl_algo.h:66:
/space/conda/xeus/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/bits/uniform_int_dist.h:231:36: error: no member named 'min' in 'std::mersenne_twister_engine<unsigned long, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>'
        const __uctype __urngmin = __urng.min();
                                   ~~~~~~ ^
/space/conda/xeus/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/include/c++/7.3.0/bits/uniform_int_dist.h:232:36: error: no member named 'max' in 'std::mersenne_twister_engine<unsigned long, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>'
        const __uctype __urngmax = __urng.max();
                                   ~~~~~~ ^

Interpreter Error: 
SylvainCorlay commented 5 years ago

Will respond with more details, but what makes you think that the gcc version in the conda environment is 8?

jogojapan commented 5 years ago

Thank you!

The reason I make that assumption about the GCC version in the conda environment is that it says so:

(/space/conda/xeus) 19:12 localhost:~$ gcc --version
gcc (GCC) 8.2.1 20181215 (Red Hat 8.2.1-6)
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
SylvainCorlay commented 5 years ago

Ah, if you do which gcc, you should see your system's gcc. Conda's gcc is not invoked with the gcc command.

SylvainCorlay commented 5 years ago

Hum. Trying your code on the binder appears to work.

screenshot from 2019-01-04 11-19-28

jogojapan commented 5 years ago

Ah, if you do which gcc, you should see your system's gcc. Conda's gcc is not invoked with the gcc command.

Oh, I see. I had assumed the effect of the GCC 7 option would be similar to installing a different GCC version in the environment and making that the one that would be called by default. I should have checked more carefully.

SylvainCorlay commented 5 years ago

OK, gcc7 is a bad name for the "rebuild" channel of conda-forge, which is ongoing a large rebuild endeavor to migrate to the latest conda-build tooling. On linux, it migrates conda-forge from gcc 4.8 to 7.3 at the same time hence the name of the channel. Although the channel name is also used for the os x rebuild...

The page https://conda-forge.org/status/ shows the rebuild progress. This channel will replace the main channel when the rebuild is done.