Dulmage-Mendelsohn decomposition in R - c++

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.

Related

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,

cusp::extract_diagonal not found

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.

Call mex Function from cmex SFunction

I have implemented a complex mexFunction using visual studio 2012 and successfully integrated it with Matlab. (lets call it mexFunctionA.mexw32 )
When I run this command in matlab command window, I get the expected results:
mexFunctionA("My1Argument", "My2Argument");
Now , I need to develop a mexFunctionB that calls mexFunctionA; mexFunctionB is a simple as it can be.
The C code I´m trying (inside mexFunctionB.c) is:
#include "mexFunctionA.mexw32"
(...)
static void mdlOutputs(SimStruct *S, int_T tid)
{
mexFunctionA("My1Argument", "My2Argument");
}
(...)
This line of code is not compiling.
The command line I am using is:
mex -v mexFunctionB.c -I'C:\patchToMexFunctionA' -L'C:\patchToMexFunctionA' 'mexFunctionA.mexw32'
So, here are the possible errors:
The #include method is wrong.
The command line for compiling the code is wrong.
It is not possible to do what I am planning to do.
Something else.
Anyone knows how to fix it?
The code you give is non-sensical. .mexw32 files are dynamically linked libraries (i.e. dll's), and in C code #include statements aren't used to include dll's.
Firstly note that as far as your S-Function is concerned mexFunctionA is no different from any other MATLAB function. So the question you should be asking is "how do I call a MATLAB function from within a mex file?".
The answer to that is to use the function mexCallMATLAB.
In short, you need to remove the #include and reformat the call to mexFunctionA to the form required by mexCallMATLAB.

How to use FMOD with C++?

I'm trying to create a simple mp3 player using FMOD:
#include "inc/fmod.h"
int main()
{
FSOUND_Init(44100, 32, 0);
return 0;
}
Trying to compile the program I get the following error:
holle#x300:justmp3$ pwd
/media/daten/Entwicklung/C/justmp3
holle#x300:justmp3$ LD_LIBRARY_PATH=$(pwd)/lib
holle#x300:justmp3$ ls $LD_LIBRARY_PATH
libfmodex-4.34.02.so libfmodexL-4.34.02.so
holle#x300:justmp3$ g++ -o mp3 mp3.cpp
mp3.cpp: In function ‘int main()’:
mp3.cpp:8: error: ‘FSOUND_Init’ was not declared in this scope
What's my mistake? How could I get g++ to compile the program?
FSOUND_Init is an FMOD 3 API function, you are using FMOD Ex so that function doesn't exist. To initialize FMOD Ex you should include "fmod.hpp" and use the functions:
System_Create to create the FMOD system object, then
System::init to initialize, followed by
System::createStream to load your MP3, then
System::playSound to play it.
There are a bunch of useful examples that ship with FMOD that you could use as a reference, especially the playstream example for what you want to achieve. Also there is full documentation in CHM format. If you are porting code from FMOD 3 I would recommend reading the migration guide in the fmodex.chm docs.
You need to include the headers for the library too, add
#include <fmod.h>
at the beginning of your code.