Linking Intel MKL with Armadillo C++ Matrix Library - c++

I have been having a number of issues with linking Intel MKL with Armadillo C++.
Im trying to do this on OSX.
I have installed Intel parallel_studio_xe_2016.3.068 as root so that its available across the whole system.
Here is my example code that I'm trying to run:
example.cpp
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main(int argc, const char * argv[]) {
// mkl_set_num_threads(4);
mat A = randu<mat>(1,2);
mat B = randu<mat>(2,1);
mat D;
double C = 0;
while (C == 0){
cout << A*B;
}
return 0;
}
I downloaded the armadillo 7.300.1 tar from sourceforge, unpacked it ran the following commands in terminal:
cd armadillo-7.300.1
cmake .
make
And then to compile my cpp file I used the link line advisor on intel site to get all the details I needed.
icpc -DMKL_ILP64 -qopenmp -I/opt/intel/mkl/include -I~/GithubRepos/PMIWork/armadillo-7.300.1/include -DARMA_DONT_USE_WRAPPER -L/opt/intel/mkl/lib -Wl,-rpath,/opt/intel/mkl/lib -lmkl_intel_ilp64 -lmkl_core -lmkl_intel_thread -lpthread -lm -ldl example.cpp -o example
However when I run the resulting executable I get a segmentation fault. If I remove the cout << then the code complies, runs but it only uses 1 single core. I thought that MKL is suppose to automatically scale to the whole CPU.
Would really appreciate any help with this.

Related

How to use std::thread on Windows with MinGW with POSIX threads

I have been trying to compile and run the following very simple bit of code
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
I am compiling on Windows 10 with MinGW 6.3.0 (target x86_64-w64-mingw32) and the thread model (obtained using g++ -v) is POSIX.
I am not getting any compilation error with g++ -Wall -g test.cpp -o test.exe, however I am getting a runtime error when trying to run the exe (entry point of _ZNSt6thread15_M_start_threadESt10unique_ptrINS_3_StateESt14default_deleteIS1_EEPFvve cannot be found).
I also tried compiling with the -pthreador -lpthread flags, but I am getting the same runtime error.
Obviously this seems to be related to the use of std::thread, but I didn't get how to fix this. Am I missing a compilation flag or additional library enabling POSIX thread support on Windows?
Edit: I managed to get it working by changing the compilation command to
g++ -Wall -g test.cpp -o test.exe -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,Bdynamic
I tried your code after making the necessary fixes:
#include <thread>
#include <iostream>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
std::cout << i;
}
}
int main()
{
thread t(thread_c);
t.join();
system("pause");
return 0;
}
and it worked fine.
The build command was the same as yours:
g++ -Wall -g test.cpp -o test.exe
The output was:
012345678910Press any key to continue . . .
But I did use MinGW-w64 GCC 12.1.0 (fro https://winlibs.com/) instead of the very old version 6.3.0.
Maybe the MinGW-w64 (or was it still the old MinGW) library you were using was simply too old...

How to include <numbers> header file and use std::numbers

running on version 11.1.0 of gcc and g++. Every time I run this code I run into issues it says std::numbers was not declared. I tried running g++ randomCodeWhileReading.cpp -o main-std=c++20 within my terminal (im running ubuntu linux) and still no change. Here is the code in question:
#include <iostream>
#include <numbers>
int main()
{
const long double pi {0};
const long double pi2 {0};
pi = std::numbers::pi_v<long double>;
pi2 = std::numbers::pi_v<long double>;
std::cout << pi << std::endl << pi2;
}
Just wanted to see the numbers module in action nothing else. (is it even called a module or is it a header file?)
EDIT 10/6/21:
The modifying a constant variable has been fixed. However, this code still wont run on my computer. Namely, the #include <numbers> does not seem to work on my machine it throws an error even when using -std=c++20. I am running gcc and g++ version 11.1 See error below:
gcc ex2_03.cpp -o -std=c++20
ex2_03.cpp: In function ‘int main()’:
ex2_03.cpp:22:65: error: ‘std::numbers’ has not been declared
22 | const double pond_diameter {2.0 * std::sqrt(pond_area/ std::numbers::pi)}; //find diameter by finding radius & multiplying by 2
|
however I was unable to replicate using godbolt.org (similar program not the same but uses as well). Clearly, it seems that this is an issue with my machine. How would I go about fixing this?
EDIT 10/8/21:
I ran the code again using more flags and changing -std=c++20 to -std=c++2a this was what was returned:
chris#chris-Aspire-E5-576G:~/Desktop/programming/c++/Learning$ ls
ex2_02 HelloWorld randomCodeWhileReading textbookExample1
ex2_02.cpp HelloWorld.cpp randomCodeWhileReading.cpp textbookExample1.cpp
ex2_02.o HelloWorld.o randomCodeWhileReading.o textbookExample1.o
ex2_03 main textbookDebug textbookOutputNameAndAge.cpp
ex2_03.cpp outputNameAndAge textbookDebug.cpp
ex2_03.o outputNameAndAge.o textbookDebug.o
chris#chris-Aspire-E5-576G:~/Desktop/programming/c++/Learning$ g++ -g -Wall -pedantic -std=c++2a -o randomCodeWhileReading.cpp
g++: fatal error: no input files
compilation terminated.
added the ls output to show I was in the correct directory.
EDIT 10/8/21 v2:
I used the following command and did not receive an error.
g++ randomCodeWhileReading.cpp -o main -std=c++20
Now just confused where the output went. By #nate's responses I assume it was sent to main? Just wanted to see a cout using std::numbers::pi
EDIT 10/8/21 v3:
All clear nate explained program can be ran by using ./main
EDIT 10/8/21 v4:
... I repeated the earlier command and got a error:
g++ randomCodeWhileReading.cpp -o main -std=c++20
cc1plus: fatal error: randomCodeWhileReading.cpp: No such file or directory
compilation terminated.
can someone explain what went wrong this time? (I am still in the same directory). After using ls it seems that the file is no longer in the directory seems to be deleted?
EDIT 10/8/21 v5:
I think the file got deleted when I was explaining the error to a friend and the wrong ways I was running the command lol. All good :D !
You need to compile with the extra flag -std=c++20.
Moreover, there is an error in your code: pi and pi2 are declared const, hence you cannot modify them after they are initialized. Use this instead:
#include <iostream>
#include <numbers>
int main()
{
const long double pi = std::numbers::pi_v<long double>;
const long double pi2 = std::numbers::pi_v<long double>;
std::cout << pi << std::endl << pi2;
}
Please try this code and this "compile" command on your machine with your version of g++:
/*
* TEST ENVIRONMENT: MSYS2 (Windows 10)
* COMPILER:
* g++ --version
* g++.exe (Rev5, Built by MSYS2 project) 10.3.0
* BUILD COMMAND:
* g++ -g -Wall -pedantic -std=c++2a -o x x.cpp
* <= NOTE: the correct command for C++ 20 compatibility in g++ 10.x is "-std=c++2a"
* RUN COMMAND:
* ./x
* 2pi=6.28319
*/
#include <iostream>
#include <numbers>
int main()
{
auto two_pi = 2*std::numbers::pi;
std::cout << "2pi=" << two_pi << '\n';
}
If it works, great. If it's the same, try "-std=c++20" and/or "-std=gnu++20". Look here for details: https://gcc.gnu.org/projects/cxx-status.html
See also:
https://stackoverflow.com/a/67453352/421195
https://stackoverflow.com/a/67406788/421195
Definitely "Update" your post to report back what happens. Be sure to copy/paste your commands and any error messages EXACTLY.
'Hope that helps.

Trivial Eigen3 Tensor program does not build without -On

I'm trying to build a write of software with the Tensor module provided as unsupported from eigen3. I've written a simple piece of code that will build with a simple application of VectorXd (just printing it to stdout), and will also build with an analogous application of Tensor in place of the VectorXd, but WILL NOT build when I do not throw an optimization flag (-On). Note that my build is from within a conda enviromnent that is using conda-forge compilers, so the g++ in what follows is the g++ obtained from conda forge for ubuntu. It says its name in the error messages following, if that is perceived to be the issue.
I have a feeling this is not about the program I'm trying to write, but just in case I've included an mwe.cpp that seems to produce the error. The code follows:
#include <eigen3/Eigen/Dense>
#include <eigen3/unsupported/Eigen/CXX11/Tensor>
#include <iostream>
using namespace Eigen;
using namespace std;
int main(int argc, char const *argv[])
{
VectorXd v(6);
v << 1, 2, 3, 4, 5, 6;
cout << v.cwiseSqrt() << "\n";
Tensor<double, 1> t(6);
for (auto i=0; i<v.size(); i++){
t(i) = v(i);
}
cout << "\n";
for (auto i=0; i<t.size(); i++){
cout << t(i) << " ";
}
cout << "\n";
return 0;
}
If the above code is compiled without any optimizations, like:
g++ -I ~/miniconda3/envs/myenv/include/ mwe.cpp -o mwe
I get the following compiler error:
/home/myname/miniconda3/envs/myenv/bin/../lib/gcc/x86_64-conda_cos6-linux-gnu/7.3.0/../../../../x86_64-conda_cos6-linux-gnu/bin/ld: /tmp/cc2q8gj4.o: in function `Eigen::internal::(anonymous namespace)::get_random_seed()':
mwe.cpp:(.text+0x15): undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status
If instead I ask for 'n' optimization level, like the following:
g++ -I ~/miniconda3/envs/loos/include/ -On mwe.cpp -o mwe
The program builds without complaint and I get expected output:
$ ./mwe
1
1.41421
1.73205
2
2.23607
2.44949
1 2 3 4 5 6
I have no clue why this little program, or the real program I'm trying to write, would be trying to get a random seed for anything. Any advice would be appreciated. The reason why I would like to build without optimization is so that debugging is easier. I actually thought all this was being caused by debug flags, but I realized that my build tool's debug setting didn't ask for optimization and narrowed that down to the apparent cause. If I throw -g -O1 I do not see the error.
Obviously, if one were to comment out all the code that has to do with the Tensor module, that is everthing in main above 'return' and below the cwiseSqrt() line, and also the include statement, the code builds and produces expected output.
Technically, this is a linker error (g++ calls the compiler as well as the linker, depending on the command line arguments). And you get linker-errors if an externally defined function is called from somewhere, even if the code is never reached.
When compiling with optimizations enabled, g++ will optimize away uncalled functions (outside the global namespace), thus you get no linker errors. You may want to try -Og instead of -O1 for better debugging experience.
The following code should produce similar behavior:
int foo(); // externally defined
namespace { // anonymous namespace
// defined inside this module, but never called
int bar() {
return foo();
}
}
int main() {
// if you un-comment this line, the
// optimized version will fail as well:
// ::bar();
}
According to man clock_gettime you need to link with -lrt if your glibc version is older than 2.17 -- maybe that is the case for your setup:
g++ -I ~/miniconda3/envs/myenv/include/ mwe.cpp -o mwe -lrt

Compilation steps for a c++ file with Rcpp header file

I am trying this code on gedit and compiling by g++ compiler on terminal.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double sumC(NumericVector x) {
int n = x.size();
double total = 0;
for(int i = 0; i < n; ++i) {
total += x[i];
}
return total;
}
// [[Rcpp::export]]
double meanC(NumericVector x) {
return sumC(x) / x.size();
}
Error occurred for the header file.
fatal error: Rcpp.h: No such file or directory
I have compiled like this: g++ -I /usr/ r1.cpp -o c0 -L /usr/ -lRcpp
Also i tried :g++ -I /usr/lib/R/site-library/Rcpp/include/ r1.cpp -o c0 -L /usr/lib/R/site-library/Rcpp/libs/ -lRcpp . THen got error like fatal
error: R.h: No such file or directory #include <R.h>
Locations:
locate Rcpp.h:/usr/lib/R/site-library/Rcpp/include/Rcpp.h
locate R.h:/usr/share/R/include/R.h
I have tried with make file also.
My make file:
all:
g++ rcpp.cpp -o obj
compile:
I have attached all the depending header files in a single folder. Still getting the errors for Rcpp.
Any one knows how to compile this through terminal?
You can compile this file with
g++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -c rcpp.cpp -o rcpp.o
However, I do not understand why you want to do this. In order to make such C++ functions callable from R, several additional steps are necessary:
C++ wrapper functions that translate to an interface based on R's SEXP.
R wrapper functions that call the C++ wrapper functions via .Call().
Linking of all the object files into a dynamic library that R can load.
Loading the library and the R wrapper functions into R.
All this is automated via sourceCpp() or when using Rcpp::compileAttributes() in the context of packages using Rcpp, c.f. the vignettes on attributes and packages.

MinGW: building a dynamic library with opencv

A want to build a simple dll, that shows an image:
#include <opencv2\core\core.hpp>
#include<opencv2\highgui\highgui.hpp>
using namespace cv;
void showimagedata(uchar * dataBuffer,int width,int height)
{
Mat image(Size(width, height), CV_8UC1, dataBuffer, Mat::AUTO_STEP);
imshow("image",image);
}
How can I compile it with MinGW?
I tried the following:
g++ -c -fPIC showimg.cpp -o showimg.o -I"C:\opencv\build-mingw\install\include" -L"C:\opencv\build-mingw\install\x86\mingw\lib" -lopencv_core247 -lopencv_highgui247
g++ -shared -Wl,-soname,libshowimg.so.1 -o libshowimg.so.1.0.1 showimg.o
but it seems that the linker cant find the symbols.
Filenames and paths are correct, but I'm not certain about the naming conventions.
The two libs are called: libopencv_core247.dll.a libopencv_highgui247.dll.a