Giter VIP home page Giter VIP logo

Comments (9)

bradbell avatar bradbell commented on June 11, 2024 1

Given the sparsity patterns for matrix and the desired elements
https://coin-or.github.io/CppAD/doc/sparse_rc.htm

I would use row_major (or col_major) order to, in one pass of over the elements, determine which elements of the desired pattern are in the matrix pattern.

Perhaps it would be useful to write a general purpose function like this:
intersect_rc = intersection(first_rc, second_rc)
where _rc denotes a row and column sparsity pattern.

from cppadcodegen.

bradbell avatar bradbell commented on June 11, 2024 1

I have improved the error message for this case; see CppAD commit
coin-or/CppAD@30e4e4a

from cppadcodegen.

bradbell avatar bradbell commented on June 11, 2024

CppAD should give an error from a know soruce for this case and report the cause.
The problem is that the subset of elements requested must all be in the sparsity pattern.

This requirement is documented under the heading 'subset' on
https://coin-or.github.io/CppAD/doc/sparse_jac.htm

'All of the row, column pairs in subset must also appear in pattern ; i.e., they must be possibly non-zero. '

from cppadcodegen.

EmbersArc avatar EmbersArc commented on June 11, 2024

Okay so I correctly understood what the problem is. What's the best way to assure they are in the pattern?

from cppadcodegen.

bradbell avatar bradbell commented on June 11, 2024

Elements that are not in the pattern have value zero, so you do not need to calculate them.
The reason I did not handle this case is that it seems like a good cross check (that the pattern and subset of the pattern are consistent).

from cppadcodegen.

EmbersArc avatar EmbersArc commented on June 11, 2024

I guess what I'm asking is how to check if those values are zero and don't have to be added to my custom sparsity with setCustomSparseJacobianElements. So something like this:

// Jacobian sparsity
std::vector<size_t> rows;
std::vector<size_t> cols;
for (size_t row = 0; row < jacobian_t::RowsAtCompileTime; row++)
{
    for (size_t col = 0; col < jacobian_t::ColsAtCompileTime; col++)
    {
        if (/* (row, col) not already in pattern */)
        {
            rows.push_back(row);
            cols.push_back(col);
        }
    }
}

Or if there's a way to edit the current pattern so that I can remove the values that are not needed.

from cppadcodegen.

EmbersArc avatar EmbersArc commented on June 11, 2024

Okay with your help I ended up with something that works:

// sparsity pattern for the identity matrix
size_t nr = x.size();
size_t nc = x.size();
size_t nnz_in = x.size();
CppAD::sparse_rc<std::vector<size_t>> pattern_in(nr, nc, nnz_in);
for (size_t k = 0; k < nnz_in; k++)
{
    size_t r = k;
    size_t c = k;
    pattern_in.set(k, r, c);
}

CppAD::sparse_rc<std::vector<size_t>> pattern_out;
f_.for_jac_sparsity(pattern_in, false, false, false, pattern_out);

const std::vector<size_t> &row(pattern_out.row());
const std::vector<size_t> &col(pattern_out.col());

// reduced Jacobian sparsity
std::vector<size_t> rows;
std::vector<size_t> cols;

for (size_t i = 0; i < row.size(); i++)
{
    if (col[i] < desired_column_count)
    {
        rows.push_back(row[i]);
        cols.push_back(col[i]);
    }
}
cgen.setCustomSparseJacobianElements(rows, cols);

This is not actually faster than just computing the full dense Jacobian with all the parameter derivatives in it and then discarding the unused elements. Was this to be expected?

from cppadcodegen.

bradbell avatar bradbell commented on June 11, 2024

I am not sure how this all plays out in the context of CppADCodeGen, but in the context of CppAD,
it would be better to use the dynamic parameter option for the parameters that you are not taking
derivatives w.r.t.; see
https://coin-or.github.io/CppAD/doc/new_dynamic.htm

In addition, I only expect sparse versions to be faster when the matrices are large
and sparse. If you have such a case, in the context of CppAD, where the dense Jacobian is as fast as the sparse one, you could open an issue about it for CppAD; see
https://github.com/coin-or/CppAD/issues

from cppadcodegen.

EmbersArc avatar EmbersArc commented on June 11, 2024

I've been using CppAD parameters as well. The reason why I switched to code generation is the 10x speedup (even with the full Jacobian) that is absolutely necessary for my application.

The matrix is indeed pretty small with 20 independent parameters of which 10 are parameters, and 10 outputs. So the slower sparse Jacobian is not surprising.

I'll be very happy the day CppADCodegen impements parameters, but I understand it's a pretty big change and will take time.

Thanks again for your help.

from cppadcodegen.

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.