GithubHelp home page GithubHelp logo

Comments (20)

bradbell avatar bradbell commented on July 23, 2024

This good idea.
I am not sure how soon I can get to it.

from cppad.

joaoleal avatar joaoleal commented on July 23, 2024

Started an implementation of these methods here:
joaoleal@6b52e59
No tests yet.

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I have started by reviewing / editing the source and developer documentation for CppAD::vector.
In addition, I have added scripts to make it easier for other developers to build the documentation.
Please try running
bin/get_highlight.sh
bin/get_omhelp.sh
which should install a version of omhelp in
build/prefix/bin
Then try
bin/run_omhelp.sh doc
bin/run_omhelp.sh dev
which should build the user documentation in ./doc and the developer documentation in ./dev

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I am in the process of implementing iterators for CppAD::vector that includes checking memory access when NDEBUG is not defined.

I now realize that c++20 has a new way of handling this issue; see
https://en.cppreference.com/w/cpp/iterator
and that theses are Legacy iterators.

from cppad.

joaoleal avatar joaoleal commented on July 23, 2024

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I have pushed a current version of the vector branch I am using for this work; see
https://github.com/coin-or/CppAD/tree/vector
You can see the correspond example and tests that are wroking so far iin
https://github.com/coin-or/CppAD/blob/vector/example/utility/cppad_vector.cpp
https://github.com/coin-or/CppAD/blob/vector/test_more/general/cppad_vector.cpp

I have a problem that perhaps you can help me with.
If you look at the file
https://github.com/coin-or/CppAD/blob/vector/include/cppad/local/utility/cppad_vector_itr.hpp
You will see the comment
// It seems that difference_type should be std::ptrdiff_t but clang
// generates a conversion warning when this is the case.
You should be able to verify this by changing the difference_type to std::ptrdiff_t and then running

bin/run_cmake.sh --clang
bin/test_one.sh example/utility/cppad_vector.cpp

I get the following output
... snip ...
Begin test group example/utility
CppAD_vector OK
memory_leak OK
All 2 tests passed.
End test group example/utility
/home/bradbell/repo/cppad.git/include/cppad/local/utility/cppad_vector_itr.hpp:391:56: warning: implicit conversion changes signedness: 'long' to 'size_t' (aka 'unsigned long') [-Wsign-conversion]
...

See the file ./test_one.err for more complete version of the warning.

from cppad.

joaoleal avatar joaoleal commented on July 23, 2024

I see why clang is showing the warning:

In file included from /home/joao/Development/cppad/git/CppAD/include/cppad/utility/vector.hpp:24:
/home/joao/Development/cppad/git/CppAD/include/cppad/local/utility/cppad_vector_itr.hpp:391:56: warning: implicit conversion changes signedness: 'long' to 'size_t' (aka 'unsigned long') [-Wsign-conversion]
    {   return CPPAD_VECTOR_ITR(data_, length_, index_ + n);
               ~~~~~~~~~~~~~~~~                 ~~~~~~~^~~

the result of index_ + n is a signed long because n_ is also signed.
When the result is cast to the index type in the constructor, which is unsigned, we get the warning.
If n is negative, then we can face an integer overflow.
Actually, I would expect g++ to also provide a similar warning but it does not.
You could check if the result would is not negative and then make an explicit cast.

By the way, the following:

void operator=(const CPPAD_VECTOR_ITR& other) CPPAD_NOEXCEPT

should provably be

CPPAD_VECTOR_ITR& operator=(const CPPAD_VECTOR_ITR& other) CPPAD_NOEXCEPT

Although this iterator implementation is safer because it checks the limits when we try to access the underlying value, it may have an impact on the performance.
It will be slower than a regular iteration using size_t or the iterators from std::vector.
Using iterators is commonly safe because one retrieves the limits from begin() and end().

from cppad.

bradbell avatar bradbell commented on July 23, 2024

You are correct, it was the type of index in the constructor for the iterator. I fixed this in the commit
a3052e2

As for the speed. Perhaps you could test the speed of your version versus mine for a large sort
( with -D NDEBUG ) defined ?

from cppad.

joaoleal avatar joaoleal commented on July 23, 2024

I did a quick benchmark with the help of the code in the folder speed/.
These are the results:

gcc, RELEASE (-O3 -DNDEBUG)

iterator class
cppad_cppad_vector_sort_rate = [ 14998439, 15323218, 15360534, 15356313, 15319491 ]

pointer iterator
cppad_cppad_vector_sort_rate = [ 23021714, 22461399, 22589682, 22155894, 22441345 ]

The pointer iterator is faster than the iterator class.

gcc, DEBUG

iterator class
cppad_cppad_vector_sort_rate = [ 876440, 875996, 878266, 864370, 872525 ]

pointer iterator
cppad_cppad_vector_sort_rate = [ 3223518, 3195039, 3178879, 3141978, 3144250 ]

Oddly enough, the DEBUG version of pointer iterator is faster than the RELEASE version.

I created a new file with the following functions:

bool link_vector_cppad_it(CppAD::vector<size_t> x)
{
    std::sort(x.begin(), x.end());
    return true;
}

bool speed_cppad_vector_it(
        size_t                           size     ,
        size_t                           repeat   )
{
    // -----------------------------------------------------
    // setup
    using CppAD::vector;

    // free statically allocated memory
    if( size == 0 && repeat == 0 )
        return false;

    CppAD::vector<size_t> x(size);
    for (size_t i = 0; i < size; ++i)
        x[i] = size - i;

    // ------------------------------------------------------
    while(repeat--)
    {
        link_vector_cppad_it(x);
    }

    return true;
}

I also had to make some changes in speed/main.cpp to call these functions.
The following code is using these functions.

run_speed(speed_cppad_vector_it, size_cppad_vector_sort, "cppad_vector_sort");

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I added a sort speed test that compares raw pointers with CppAD::vector::iterator; see
https://github.com/coin-or/CppAD/blob/vector/bug/vec_itr_speed.sh
(Note line 26 where I commented out the cmake command because it only needs to be executed once.)

Here is what I get when I run that test using a vbox virtual machine:
cppad.git>bug/vec_itr_speed.sh
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
g++ -I../../include -Wall -pedantic-errors -std=c++11 -Wshadow -Wconversion -DNDEBUG -O2 vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
time_itr=0.00930823, repeat_itr=128
time_ptr=0.00837168, repeat_ptr=128

bug/vec_itr_speed.sh: OK

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I will change the bug/vec_itr_speed.sh to be more like the test you have and see what I get.

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I modified my test case and got at bigger difference but not as large you you; see below.
I suspect that the sort routine is detecting that the vector is sorted when it repeats the loop and so my test case resets the vector each time through the loop. Perhaps you could try that in your test.
Here is what I get for the current version of
https://github.com/coin-or/CppAD/blob/vector/bug/vec_itr_speed.sh

This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
g++ -I../../include -Wall -pedantic-errors -std=c++11 -Wshadow -Wconversion -DNDEBUG -O2 vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
time_itr=0.0038541, repeat_itr=512
time_ptr=0.0027051, repeat_ptr=512

bug/vec_itr_speed.sh: OK

I changed the -O2 -> -O3 and got
time_itr=0.00290378, repeat_itr=512
time_ptr=0.00270937, repeat_ptr=512
I think the improvement is because it is now detecting that the calls to check_element and check_op do not do anything when NDEBUG is defined.

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I added a test or std::reverse as well as debug and opt_level options to vec_itr_speed.sh.
Below is an example usage with optimization levels (zero and one) and NDEBUG defined.
It appears that, if one does not do any optimization there is a big difference between the pointers and iterator. But even for optimization one, there is not much difference ?

cppad.git>bug/vec_itr_speed.sh no_debug 0
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
g++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O0 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.129424, repeat=8
sort_ptr_sec=0.0266072, repeat=64
rev_itr_sec=0.00446941, repeat=256
rev_ptr_sec=0.00142547, repeat=1024
bug/vec_itr_speed.sh: OK

cppad.git>bug/vec_itr_speed.sh no_debug 1
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
g++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O1 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.00367038, repeat=512
sort_ptr_sec=0.00317483, repeat=512
rev_itr_sec=0.000102517, repeat=16384
rev_ptr_sec=9.53924e-05, repeat=16384
bug/vec_itr_speed.sh: OK

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I have merged the current version of the iterators into the master; see the heading Iterators on
https://coin-or.github.io/CppAD/doc/cppad_vector.htm#Iterators

Also see the use of iterators in
https://coin-or.github.io/CppAD/doc/cppad_vector.cpp.htm

from cppad.

bradbell avatar bradbell commented on July 23, 2024

There was a problem with the install of OMhelp on recent Ubuntu systems; see the heading 19-08-29 on
https://bradbell.github.io/omhelp/doc/whatsnew.htm#19-08-29
This was fix is included bin/get_omhelp.sh in the commit
8ab6195

I have started by reviewing / editing the source and developer documentation for CppAD::vector.
In addition, I have added scripts to make it easier for other developers to build the documentation.
Please try running
bin/get_highlight.sh
bin/get_omhelp.sh
which should install a version of omhelp in
build/prefix/bin
Then try
bin/run_omhelp.sh doc
bin/run_omhelp.sh dev
which should build the user documentation in ./doc and the developer documentation in ./dev

from cppad.

bradbell avatar bradbell commented on July 23, 2024

y
yRuning bug/vec_itr_speed.sh on a ubuntu-18 system I get
cppad.git>bug/vec_itr_speed.sh no_debug 3
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
g++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O3 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.00217435, repeat=512
sort_ptr_sec=0.00207816, repeat=512
rev_itr_sec=7.5284e-05, repeat=16384
rev_ptr_sec=7.26916e-05, repeat=16384
bug/vec_itr_speed.sh: OK

If I change the script to use clang; I get a different result
cppad.git>bug/vec_itr_speed.sh no_debug 3
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
clang++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O3 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.00208246, repeat=512
sort_ptr_sec=0.00172917, repeat=1024
rev_itr_sec=0.000154773, repeat=8192
rev_ptr_sec=7.33489e-05, repeat=16384

The ubuntu system is using clang 6.0.0. My fedora system is using clang 8.0.0.
On the Fedora system I get a similar result to the ubuntu system.
clang++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O3 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.00250505, repeat=512
sort_ptr_sec=0.0023544, repeat=512
rev_itr_sec=0.000156105, repeat=8192
rev_ptr_sec=8.0911e-05, repeat=16384
bug/vec_itr_speed.sh: OK

from cppad.

joaoleal avatar joaoleal commented on July 23, 2024

Hello Brad,

Running in Ubuntu 19.04 with gcc 8.3.0:

bug/vec_itr_speed.sh yes_debug 0
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
g++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O0 vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.0592777, repeat=32
sort_ptr_sec=0.0120314, repeat=128
rev_itr_sec=0.00205744, repeat=512
rev_ptr_sec=0.00077551, repeat=2048

bug/vec_itr_speed.sh: OK
bug/vec_itr_speed.sh no_debug 3
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
g++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O3 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.00212732, repeat=512
sort_ptr_sec=0.0018671, repeat=1024
rev_itr_sec=5.63016e-05, repeat=32768
rev_ptr_sec=5.75984e-05, repeat=32768

bug/vec_itr_speed.sh: OK

now with clang 8.0.0:

bug/vec_itr_speed.sh yes_debug 0
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
clang++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O0 vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.0526799, repeat=32
sort_ptr_sec=0.012087, repeat=128
rev_itr_sec=0.00186193, repeat=1024
rev_ptr_sec=0.000622082, repeat=2048

bug/vec_itr_speed.sh: OK
bug/vec_itr_speed.sh no_debug 3
This is not a bug but rather a speed to comparing the speed of CppAD
vector iterators and raw pointer using the std::sort algorithm.
clang++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O3 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.00197217, repeat=512
sort_ptr_sec=0.0019287, repeat=1024
rev_itr_sec=0.000129458, repeat=8192
rev_ptr_sec=7.37673e-05, repeat=16384

bug/vec_itr_speed.sh: OK

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I am getting strange results from the clang optimizer.
It seems that -O3 does worse than -O2.
I checked in a new version of bug/vec_itr_speed.sh to make it easier to reproduce these results:

cppad.git>bug/vec_itr_speed.sh clang++ no 3
This is speed test (not a bug report) comparing the speed using CppAD vector
iterators and raw pointer with the algorithms std::sort and std::reverse.
clang++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O3 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.00257528, repeat=512
sort_ptr_sec=0.0024007, repeat=512
rev_itr_sec=0.000157028, repeat=8192
rev_ptr_sec=8.5186e-05, repeat=16384
bug/vec_itr_speed.sh: OK
cppad.git>bug/vec_itr_speed.sh clang++ no 2
This is speed test (not a bug report) comparing the speed using CppAD vector
iterators and raw pointer with the algorithms std::sort and std::reverse.
clang++ -I../../include -Wall -std=c++11 -Wshadow -Wconversion -O2 -DNDEBUG vec_itr_speed.cpp -o vec_itr_speed
build/bug/vec_itr_speed
sort_itr_sec=0.00254406, repeat=512
sort_ptr_sec=0.00245514, repeat=512
rev_itr_sec=8.23689e-05, repeat=16384
rev_ptr_sec=8.23767e-05, repeat=16384
bug/vec_itr_speed.sh: OK

from cppad.

bradbell avatar bradbell commented on July 23, 2024

I have not added the reverse iterators. Is this important to you ?
If not, and I have satisfied your other concerns, would you please close this issue.

from cppad.

joaoleal avatar joaoleal commented on July 23, 2024

Thank you for adding this to CppAD. Closing the issue.

from cppad.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.