Problem with running mlpack sample program - c++

I have installed mlpack via msys2.
Also I have installed gcc via msys2.
Made a simple program in c++ from the code on mlpack website
// This is an interactive demo, so feel free to change the code and click the 'Run' button.
// This simple program uses the mlpack::neighbor::NeighborSearch object
// to find the nearest neighbor of each point in a dataset using the L1 metric,
// and then print the index of the neighbor and the distance of it to stdout.
#include <C:\msys64\mingw64\include\mlpack\core.hpp>
#include <C:\msys64\mingw64\include\mlpack\methods\neighbor_search\neighbor_search.hpp>
using namespace mlpack;
using namespace mlpack::neighbor; // NeighborSearch and NearestNeighborSort
using namespace mlpack::metric; // ManhattanDistance
int main()
{
// Load the data from data.csv (hard-coded). Use CLI for simple command-line
// parameter handling.
arma::mat data("0.339406815,0.843176636,0.472701471; \
0.212587646,0.351174901,0.81056695; \
0.160147626,0.255047893,0.04072469; \
0.564535197,0.943435462,0.597070812");
data = data.t();
// Use templates to specify that we want a NeighborSearch object which uses
// the Manhattan distance.
NeighborSearch<NearestNeighborSort, ManhattanDistance> nn(data);
// Create the object we will store the nearest neighbors in.
arma::Mat<size_t> neighbors;
arma::mat distances; // We need to store the distance too.
// Compute the neighbors.
nn.Search(1, neighbors, distances);
// Write each neighbor and distance using Log.
for (size_t i = 0; i < neighbors.n_elem; ++i)
{
std::cout << "Nearest neighbor of point " << i << " is point "
<< neighbors[i] << " and the distance is " << distances[i] << "." << std::endl;
}
return 0;
}
Trying to run this program as follows,
g++ nearest-neighbour.cpp -o nearest-neighbour -std=c++11 -larmadillo -l mlpack -lomp
I get the following error while executing the executable.
After installing dependency walker I see the above procedure as flagged in red colour, I dont know what it means.
This time I have used below command to compile,
g++ -std=c++11 nearest_neighbour.cpp -o nearest_neighbour.exe -larmadillo -llapack -fopenmp -lmlpack -lboost_serialization-mt -lopenblas

Related

Problem compiling and linking voxelyze library

I'm rather new in c++, and I've just installed Ubuntu in my windows PC a few weeks ago.
I have to work with a static library called Voxelyze (I downloaded it from Here. There you can see where are the includes, libs and makefile). And I'm trying to compile an example code (I named it testeo.cpp).
My testeo.cpp path is: c/Users/Familia/Desktop/MEMORIA/F/galib247/galib247/examples
And the voxelyze library path is: c/Users/Familia/Desktop/MEMORIA/F/galib247/galib247/examples/Voxelyze-master
This is the code:
#include "Voxelyze.h"
#include <iostream>
#include <stdio.h>
using namespace std;
int main(){
cout << "Testing voxelyze library\n";
CVoxelyze Vx(0.005); //5mm voxels
CVX_Material* pMaterial = Vx.addMaterial(1000000, 1000); //A material with stiffness E=1MPa and density 1000Kg/m^3
CVX_Voxel* Voxel1 = Vx.setVoxel(pMaterial, 0, 0, 0); //Voxel at index x=0, y=0, z=0
CVX_Voxel* Voxel2 = Vx.setVoxel(pMaterial, 1, 0, 0);
CVX_Voxel* Voxel3 = Vx.setVoxel(pMaterial, 2, 0, 0); //Beam extends in the +X direction
Voxel1->external()->setFixedAll(); //Fixes all 6 degrees of freedom with an external condition on Voxel 1
Voxel3->external()->setForce(0, 0, -1); //pulls Voxel 3 downward with 1 Newton of force.
for (int i=0; i<100; i++){
Vx.doTimeStep(); //simulate 100 timesteps.
cout << "Current position: " << (float)Voxel3->position().z << "\n";
}
return 0;
}
This code simply puts three voxels in a row, excerts a downward force in the third one, and then simulates its behavior, and for each timestep simulation it shows its displacement.
Ok, now, in my ubuntu command (after doing sudo apt-get -y update, sudo apt-get -y upgrade and sudo apt-get install build-essential) I firstly go to the voxelyze directory and I do "make clean" and then "make", so that it makes everything that needs to be made:
$ cd /mnt/c/Users/Familia/Desktop/MEMORIA/F/galib247/galib247/examples/Voxelyze-master
$ make clean
$ make
So far so good.
Then, I return to my testeo.cpp path by simply doing cd .. and, using GCC, I try to compile testeo.cpp this way:
$ cd..
$ g++ -Wall -o testeo testeo.cpp -I./Voxelyze-master/include -L./Voxelyze-master/lib/ -lvoxelyze.0.9
But I get the following error message:
testeo.cpp: In function ‘int main()’:
testeo.cpp:15:14: warning: unused variable ‘Voxel2’ [-Wunused-variable]
15 | CVX_Voxel* Voxel2 = Vx.setVoxel(pMaterial, 1, 0, 0);
| ^~~~~~
/usr/bin/ld: ./Voxelyze-master/lib//libvoxelyze.0.9.a(VX_LinearSolver.o): in function `CVX_LinearSolver::CVX_LinearSolver(CVoxelyze*)':
VX_LinearSolver.cpp:(.text+0x123): undefined reference to `pardisoinit'
/usr/bin/ld: ./Voxelyze-master/lib//libvoxelyze.0.9.a(VX_LinearSolver.o): in function `CVX_LinearSolver::solve()':
VX_LinearSolver.cpp:(.text+0x2a13): undefined reference to `pardiso'
/usr/bin/ld: VX_LinearSolver.cpp:(.text+0x2b17): undefined reference to `pardiso'
/usr/bin/ld: VX_LinearSolver.cpp:(.text+0x2c26): undefined reference to `pardiso'
/usr/bin/ld: VX_LinearSolver.cpp:(.text+0x2e30): undefined reference to `pardiso'
collect2: error: ld returned 1 exit status
Well I don't care much about the warning message, but about the "undefined reference to error".
I think it's an internal library error, (or maybe I'm doing something wrong in my linkage, IDK) because it's complaining about some "LinearSolver" class functions and a "pardiso" thing... but I'm not using them in my testeo.cpp code (at least not directly).
Well, I don't know how to make this thing work, and my engineering degree is on the line here hahaha.
I hope you can help me here, I'd thank you a lot. I'm rather new in this C++-Linux world, but whatever additional information you need in order to solve this mystery, I'll try to do my best to help.
Thanks.

Armadillo random number generator only generating zeros (Windows, MSYS2)

The following test program is supposed to generate a vector with 5 random elements, but only contains zeroes when I compile and run it on my machine.
#include <iostream>
#include <armadillo>
using std::cout;
using std::endl;
using arma::vec;
int main()
{
arma::arma_rng::set_seed(1);
vec v = arma::randu<vec>(5);
cout << v << endl;
cout << v(0) << endl;
return 0;
}
Compilation/output
$ g++ main.cpp -o example -std=c++11 -O2 -larmadillo
$ ./example.exe
0
0
0
0
0
0
I'm on Windows 10, using gcc ((Rev1, Built by MSYS2 project) 8.2.1 20181214) and Armadillo (9.200.6) from MSYS2.
Packages (pacman in mingw64 subsystem):
mingw64/mingw-w64-x86_64-armadillo 9.200.6-1
mingw64/mingw-w64-x86_64-gcc-8.3.0-1
Any idea what could cause this?
I have an inkling that this might be because I'm using the MSYS2 version of Armadillo, but I'm not sure, and haven't tested compiling the library myself (yet).
EDIT: I have an inkling that this is related to MSYS somehow, so I opened an issue over here: https://github.com/msys2/MINGW-packages/issues/5019

Exception throw when using Armadillo and qpOASES

I have a quadratic programming optimization problem that I am solving with qpOASES. In there exists a matrix X that I need to precondition, so I am using Armadillo and the routine arma::pinv from there in order to calculate the Moor-Penrose pseudoinverse.
The problem: I write the matrix X in a file , and then I read it in a separate program (say test.cpp) that does not depend in any way to qpOASES. The routine pinv runs fine.
#include <iostream>
#include <fstream>
#include <armadillo>
#include <string>
using namespace std;
using namespace arma;
int main(){
// Read design matrix.
int NRows = 199;
int NFields = 26;
string flname_in = "chol_out_2_data";
mat A (NRows,NFields);
for (int i=0; i < NRows; ++i)
for (int j=0; j < NFields; ++j)
myin >> A(i,j) ;
// Calculate pseudoinverse
mat M;
pinv(M,A); // <========= THIS fails when I use flag: -lqpOASES
}
When I include the same routine in the file where I perform the QP optimization (say true_QP.cpp), I get a runtime error, due to pinv not being able to calculate the pseudo inverse. I've done extensive tests, the file is read in OK and the values are the same.
I've tracked down the problem that is a conflict in the following way: I compiled the program that does not depend in any way on qpOASES (test.cpp - as described above) also with the flag -lqpOASES and then, the code gives run time error.
That is,compile:
g++ test.cpp -o test.xxx -larmadillo
runs fine:
./test.xxx
compile:
g++ test.cpp -o test.xxx -larmadillo -lqpOASES
throws exception (due to failure of calculating pinv):
./test.xxx
Therefore I suspect some conflict - it seems that using -lqpOASES affects some flag in armadillo also? Any ideas? Is there some dependency in LAPACK/BLAS or some flag internally that may change the setup of Armadillo? Thank you for your time.
Here is the documentation for the arma::pinv function:
http://arma.sourceforge.net/docs.html#pinv
I have resolved the issue by calculating pinv from Eigen, instead of Armadillo.
The function definition I used for Eigen, based on this bug report:
http://eigen.tuxfamily.org/bz/show_bug.cgi?id=257
is:
template<typename _Matrix_Type_>
Eigen::MatrixXd pinv(const _Matrix_Type_ &a, double epsilon =std::numeric_limits<double>::epsilon())
{
Eigen::JacobiSVD< _Matrix_Type_ > svd(a ,Eigen::ComputeThinU | Eigen::ComputeThinV);
double tolerance = epsilon * std::max(a.cols(), a.rows()) *svd.singularValues().array().abs()(0);
return
svd.matrixV() * (svd.singularValues().array().abs() > tolerance).select(svd.singularValues().array().inverse(), 0).matrix().asDiagonal() * svd.matrixU().adjoint();
}

Octave: mkoctfile not found when compiling a C++ file

I am trying to use Octave functions in C++. I install Octave-3.8.0 on Mac OS X 10.9.3 and follow the standalone program example on Octave website,
#include <iostream>
#include <octave/oct.h>
int
main (void)
{
std::cout << "Hello Octave world!\n";
int n = 2;
Matrix a_matrix = Matrix (n, n);
for (octave_idx_type i = 0; i < n; i++)
for (octave_idx_type j = 0; j < n; j++)
a_matrix(i,j) = (i + 1) * 10 + (j + 1);
std::cout << a_matrix;
return 0;
}
Then I type
$ mkoctfile --link-stand-alone main.cpp -o standalone
But it shows mkoctfile: command not found. What is the problem?
I also tried to compile the C++ file with g++
$ g++ -I /usr/local/octave/3.8.0/include/octave-3.8.0 main.cpp
but it shows 2 errors as follows.
1) 'config.h' file not found with include; use "quotes" instead.
2) fatal error: 'hdft.h' file not found.
Please help me!
It may be that octave is not registered in your system properly, judging by the shell response.
Try invoking the command from inside the octave interpreter.
Not sure about on MacOS, but on Linux, mkoctfile is not bundled within the default Octave distribution. Instead, it requires a supplementary package, liboctave-dev, that has to be installed in addition to Octave itself.
This is not documented in the Octave web tutorial.

Intel Compiler: What does error "unknown type in IL walk" mean?

I tried the Intel compiler (icpc) for the first time on my project, which was developed using GCC. After a few source files, it hits an error which does not tell me much:
/export/home/i11pcmh235/intel/bin/icpc -o .buildO/distmeasures/AlgebraicDistance.o -c -std=c++11 -Wall -c -fmessage-length=0 -fPIC -fopenmp -O3 -DNDEBUG -I/home/i11/cls/workspace/gtest/include src/distmeasures/AlgebraicDistance.cpp
src/community/PLM.cpp(147): internal error: assertion failed: unknown type in IL walk (shared/cfe/edgcpfe/walk_entry.h, line 1015)
});
^
compilation aborted for src/community/PLM.cpp (code 4)
Can anyone explain it to me?
Are my compiler flags okay? I simply tried using the same ones as for GCC.
The code segment in question looks like this:
#pragma omp atomic read
C = zeta[u];
// TRACE("Processing neighborhood of node " << u << ", which is in cluster " << C);
G.forNeighborsOf(u, [&](node v) {
#pragma omp atomic read
D = zeta[v];
// TRACE("Neighbor " << v << ", which is still in cluster " << zeta[v]);
if (D != C) { // consider only nodes in other clusters (and implicitly only nodes other than u)
double delta = deltaMod(u, C, D);
if (delta > deltaBest) {
deltaBest = delta;
best = D;
}
}
});
Internal error generally means that you've hit a compiler bug. That is, the bug is in the compiler, not your code. IL is compiler-writer speak for "Intermediate Language," the internal representation the compiler operates on to generate your code.
You'll have to see if Intel's fixed it, and/or file a bug report.