cusp::extract_diagonal not found - c++

I'm using CUSP for part of my GPU accelerated code. I have a need to extract the diagonal from a cusp matrix, C. Which should be put into diagonal in the below code.
cusp::extract_diagonal(C, diagonal);
However on compilation I get "cusp" has no member "extract_diagonal"
If I have:
#include <cusp/format_utils.h>
As in the example I get format_utils.h cannot be found. If I insert detail in to complete the path to the header as below:
#include <cusp/detail/format_utils.h>
Compilation is able to find format_utils.h but still says there is no "extract_diagonal"
The example I'm looking at is:
// include cusp array1d header file
#include <cusp/array1d.h>
#include <cusp/coo_matrix.h>
#include <cusp/print.h>
#incldue <cusp/gallery/poisson.h>
#include <cusp/format_utils.h>
int main()
{
// initialize 5x5 poisson matrix
cusp::coo_matrix<int,float,cusp::host_memory> A;
cusp::gallery::poisson5pt(A, 5, 5);
// allocate array to hold diagonal entries
cusp::array1d<float, cusp::host_memory> diagonal(A.num_rows);
// extract diagonal of A
cusp::extract_diagonal(A, diagonal);
// print diagonal entries
cusp::print(diagonal);
}
Versions are as below:
The following libraries were found:
CUDA v6.0
Thrust v1.7.1
Cusp v0.4.0
Am I missing some include or other? As a last resort I included all headers in the main cusp directory to no avail.
Any suggestions would be great.

You cannot find cusp/format_utils.h because it doesn't exist in the CUSP version you are using, which is close to 4 years old, and the codebase has been refactored since then. If you upgrade to CUSP 0.5.1 (for which the example you are using was written), you should find it works.
Alternatively, if you change the include to cusp/format.h, you might find it works as well, although I cannot guarantee it and am too lazy to pull an ancient version of CUSP and check.

Related

Dulmage-Mendelsohn decomposition in R

I am trying to implement Dulmage-Mendelsohn decomposition in R, which is also known as breadth-first search for coarse decomposition and I want to do this:
dulmage_mendelsohn <- function(mat) {...}
dulmage_mendelsohn(mat)
> mat_with_blocks
Dulmage-Mendelsoh is widely known in MATLAB an Octave (see, for example, https://www.mathworks.com/help/matlab/ref/dmperm.html) and there are C and C# implementations.
It is by using the Rcpp:: package that I am trying to use CSparse already existing function ::cs_dmperm (https://github.com/ibayer/CSparse/blob/master/Source/cs_dmperm.c#L1) and after editing the header in a new cpp file in this way:
#include <Rcpp.h>
#include <cs.h>
It returns the error:
cs_dmperm.cpp:2:10: fatal error: cs.h: No such file or directory
2 | #include <cs.h>
This is not really different from the steps described in https://math.stackexchange.com/a/3023469
Do you know a workaround to use the C function or an existing implementation? I have user search engines extensively with no result.

how to resolve header order issue

I have the following problem:
My code relies on two external packages
EIGEN (Headers only)
IAU SOFA (static library + headers)
If I write the following code:
#include "sofa.h"
#include "Eigen/Dense"
I get the following error messages:
/some_path/sofam.h:85:18: error: expected unqualified-id before numeric constant #define DAYSEC ( 86400.0 ) ...
and many more lines of errors.
However, if I change the order of the includes it compiles:
#include "Eigen/Dense"
#include "sofa.h"
works fine.
What is causing the problem?
My main problem is that I do not know how this behavior is called so I cannot really google it effectively.
It is not an option to change any of the source code of EIGEN or IAU SOFA (since they are maintained by other groups and we regularly update them from their webpage)
It is also not an option to just fix it once and never touch it again. The order of our header files is automatically rearranged to be in alphabetic order which is messing up everything every time we commit any change and we regularly use an optimize imports algorithm which is also changing the includes automatically.

Compiling multiple source files in Rcpp

I have the following directory structure
my_func
- my_func_r.cpp
- my_func.c
- my_func.h
- my_func_test.c
- matrix/
- matrix.h
- matrix.c
The matrix directory contains some matrix structures in matrix.h and some initialisation, free, print etc. functions in matrix.c. The my_func.h file is something like
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "matrix/matrix.h"
... some structures and templates ...
The my_func.c file is then
#include "my_func.h"
... helper functions ...
int my_func(...) {
... my_func stuff ...
return 0;
}
The my_func_test.c is something like
#include "my_func.h"
int main() {
... some test ...
return 0;
}
With gcc/g++ I can run this fine with
gcc my_func_test.c my_func.c matrix/matrix.c -o test -lm
The final file my_func_r.cpp is an interface between the Rcpp structures and the structures used in my_func.c. It is currently something like
#include "my_func.h"
#include <Rcpp.h>
// [[Rcpp::export]]
int my_func_r(Rcpp::List x, ...) {
... convert inputs to structure recognised by my_func.h ...
... run my_func.c ...
... put returned objects back into one of the R structure ...
return 0;
}
The problem I have is if I now run
sourceCpp('my_func_r.cpp', verbose=TRUE, rebuild=TRUE)
It complains about missing symbols for functions located in matrix/matrix.c. A workaround is to simply paste all my header and source code from both the my_func and matrix files at the top of my_func_r.cpp.
This however feels a very unsatisfactory solution especially for code maintenance. What is the easiest way to accomplish what I am trying to do?
Quick ones:
This is not really particular to Rcpp per se
You are simply struggling with a more advanced / complicated src/ directory in an R build.
There is official documentation about this in Writing R Extensions, and the questions has come up here on SO before.
You could compile a libmatrix.a first in the subdirectory and link to that. This is doable via a simple src/Makevars but still discouraged. So read on.
But this is a self-inflicted wound. Just copy matrix.h and matrix.c into src/, adjust the include path, and you are done.
As always: Create a package. Don't use sourceCpp() on larger setup. It is not made for that,

Igraph eigenvector centrality Run-Time error c++

I'm writing program on c++ that needs to generate graphs and calculate some measures.I'm working with Visual Studio 2013 and Igraph C library. At this point I can create graphs from custom info and calculate some metrics like betweennes and closeness centrality, but when i try to calculate eigenvector centrality, the program crash and show me this message:
"Run-Time Check Failure #3 - The variable 'tgetv0' is being used without being initialized."
The tgetv0 variable is used inside of dgetv.c from Igraph source.
Here is my code:
void GraphObject::calcEigen()
{
igraph_arpack_options_t options;
igraph_real_t value;
igraph_vector_t weights;
igraph_vector_init(&weights, igraph_ecount(&cGraph)); //cGraph is already created.
igraph_vector_init(&eigenRes, igraph_vcount(&cGraph)); //All ..Res igraph_vector_t are declarated in header
igraph_vector_init(&betweennesRes, 0);
igraph_vector_init(&closenessRes, 0);
igraph_arpack_options_init(&options);
igraph_betweenness(&cGraph, &betweennesRes, igraph_vss_all(), 0, 0, 1);
igraph_closeness(&cGraph, &closenessRes, igraph_vss_all(), IGRAPH_ALL, 0, 1);
igraph_eigenvector_centrality(&cGraph, &eigenRes, &value, 0, 1, &weights, &options);
}
The closeness and betwenness are correctly calculated an "couted" but crash on eigenvector function.
After lot of research on documentation, internet and the debugger i cant't figure which is the problem, especially when I tryed the example code in the documentation http://igraph.org/c/doc/igraph-Structural.html#igraph_eigenvector_centrality (copy/paste) and makes the same. Is this a library or example issue, I a'm missing something?
When I init the weights vector and then I call igraph_null(&weights), it works but the result of all eigenvalues is 1, and this is incorrect result. What I'm doing wrong?
Let us assume that Visual Studio is right and we indeed have a variable named tgetv0 that is being used uninitialized. I scanned igraph's source code and it looks like there are two places where it could indeed be the case. One of them is in src/lapack/dnaupd.c, the other one is in src/lapack/dsaupd.c. Both of these files were converted from Fortran using f2c so it is hard to tell whether the issue was present in the original Fortran code or whether this was introduced during the conversion. Either way, you can probably fix this easily by looking up the lines where tgetv0 is declared in src/lapack/dnaupd.c and src/lapack/dsaupd.c and initializing it to a value of 0. In my version, the lines to change are line 486 in src/lapack/dnaupd.c and line 482 in src/lapack/dsaupd.c.
Please add a comment to confirm whether the solution works for you or not - if it works, I'll commit a patch to the igraph source tree.

Chi-Squared Probability Function in C++

The following code of mine computes the confidence interval using Chi-square's 'quantile' and probability function from Boost.
I am trying to implement this function as to avoid dependency to Boost. Is there any resource where can I find such implementation?
#include <boost/math/distributions/chi_squared.hpp>
#include <boost/cstdint.hpp>
using namespace std;
using boost::math::chi_squared;
using boost::math::quantile;
vector <double> ConfidenceInterval(double x) {
vector <double> ConfInts;
// x is an estimated value in which
// we want to derive the confidence interval.
chi_squared distl(2);
chi_squared distu((x+1)*2);
double alpha = 0.90;
double lower_limit = 0;
if (x != 0) {
chi_squared distl(x*2);
lower_limit = (quantile(distl,((1-alpha)/2)))/2;
}
double upper_limit = (quantile(distu,1-((1-alpha)/2)))/2;
ConfInts.push_back(lower_limit);
ConfInts.push_back(upper_limit);
return ConfInts;
}
If you're looking for source code you can copy/paste, here are some links:
AlgLib
Koders
YMMV...
Have a look at the Gnu Scientific library. Or look in Numerical Recipes. There's also a Java version in Apache Commons Math, which should be straightforward to translate.
I am trying to implement this function as to avoid dependency to Boost.
Another option is to reduce Boost dependencies, but not avoid them. If you reduce the dependency, you might be able to use a Boost folder with say, 200 or 300 source files rather than the entire 1+ GB of material. (Yes, 200 or 300 can be accurate - its what I ended up with when copying out shared_ptr).
To reduce the dependency, use bcp (boost copy) to copy out just the files needed for chi_squared.hpp. The bad thing is, you usually have to build bcp from sources because its not distributed in the ZIPs or TARBALLs.
To find instructions on building bcp, see How to checkout latest stable Boost (not development or bleeding edge)?