Is element wise multiplication (%) speed in armadillo depends whether LAPACK/BLAS is installed? Im currently running armadillo without them installed and speed is awful.
Ok here is the simplest code, which takes eternity to calculate
#include <iostream>
#include "conio.h"
#include "armadillo"
using namespace arma;
using namespace std;
int main(int argc, char** argv)
{
int n=250;
mat X=ones(n,n);
mat quan;
for (int xi=1;xi<=256;xi++)
{
quan = exp(X)%exp(X);
}
getch();
return 0;
}
Make sure you have optimisation flags enabled in your compiler settings (eg. in GCC or Clang, use -O2 or -O3). Armadillo makes use of template metaprogramming, and like any C++ template library, this absolutely requires optimisation enabled within the compiler to be effective. For example, this also applies to C++ template libraries such as Boost.
Why are you calculating exp(X) twice? You're not benchmarking elementwise multiplication; you're apparently benchmarking exp(). Also, why are you not using expmat() or expmat_sym()?
Related
I was prepossessing data in C++ using the Armadillo library. The program end product is a ucube, which is a cube filled with unsigned integers. After its run, I want to load the ucube to R to perform some final statistical tests. To do so, I made a C++ function that load the ucube returning an array.
But it does not work!
I got the following warning: "warning: Cube::load(): incorrect header in B.bin" and the program returns a 0x0x0 array.
Trying to find why, I made a toy C++ program, which works fine. It is able to load the cubes without any problem.
#include <iostream>
#include <armadillo>
using namespace arma;
void read_cubes(char const* A, char const* B){
cube C;
ucube D;
C.load(A, arma_binary);
D.load(B, arma_binary);
}
int main(int argc, char** argv){
cube A = randu<cube>(5,5,5);
ucube B = randi<ucube>(5,5,5, distr_param(1, 10));
A.save(argv[1], arma_binary);
B.save(argv[2], arma_binary);
read_cubes(argv[1], argv[2]);
}
But I do not know why, doing the same steps in R does not work. To illustrate, please run the toy program as ./a.out A.bin B.bin. It will yield the Cube<double> A.bin and the Cube<uword> B.bin, which I will mention later.
The problem
If I source the following C++ code with Rcpp::sourceCpp and I try to read the Cube<double> A.bin with read_cube("A.bin") it works, but if I do the same for the Cube<uword> B.bin with read_ucube("B.bin") it does not (I get the warning).
#include <RcppArmadillo.h>
#include <iostream>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::cube read_cube(char const* x){
arma::cube A;
A.load(x, arma::arma_binary);
return A;
}
// [[Rcpp::export]]
arma::ucube read_ucube(char const* x){
arma::ucube B;
B.load(x, arma::arma_binary);
return B;
}
Of course I could cast the Cube<uword> to a Cube<double> before ending the C++ program, but I would like to know why this happen and if it is possible to load a Cube<uword> in RcppArmadillo. Because it should be possible, right?
Unfortunately R still only supports 32 bit integers, so RcppArmadillo forces Armadillo to use 32 bit integers. This is done by defining ARMA_32BIT_WORD before including the armadillo header. See RcppArmadillo's configuration here.
You can apply the same "trick" with your Armadillo programs like so:
#define ARMA_32BIT_WORD
#include <armadillo>
One of the effects is that ucube (Cube<uword>) will use 32 bit unsigned integers.
After doing the above trick, recompile your Armadillo programs and save the ucubes again. They can then be loaded in RcppArmadillo.
Consider the following code which creates a multiprecision floating-point number 'a' by using boost.
How do I use boost library to invoke trigonometric functions?
For example, I hope to calculate sin(a).
#include <iostream>
#include "boost/multiprecision/cpp_bin_float.hpp"
using namespace std;
using namespace boost::multiprecision;
typedef number<backends::cpp_bin_float<24, backends::digit_base_2, void, boost::int16_t, -126, 127>, et_off> float32;
int main (void) {
float32 a("0.5");
return 0;
}
It looks like there is a limitation in the library. When the precision is dropped too low, the sin implementation no longer compiles.
Some intermediate calculations are being done in double precision. The assignment into the result type would be lossy and hence doesn't compile.
Your chosen type actually corresponds to cpp_bin_float_single. That doesn't compile.
As soon as you select cpp_bin_float_double (precision 53 binary digits) or higher, you'll be fine.
I suppose this limitation could be viewed as a bug in some respects. You might report it to the library devs, who will be able to judge whether the related code could use single-precision floats there without hurting the convergence of the sin approximation.
#include <boost/multiprecision/cpp_bin_float.hpp>
#include <iostream>
using namespace std;
using namespace boost::multiprecision;
int main() {
cpp_bin_float_100 a = 1;
cout << setprecision(50) << endl;
cout << sin(a) << endl;
return 0;
}
I've verified digits with Wolfram Mathematica and they are correct:
Running on Widows 7 with MSVS 2010
I am following this tutorial to understand how to use MPIR library for adding two big integers
I understand this library should help me in adding very big numbers as shown in the program below:
#include < stdio.h>
#include < stdlib.h>
#include < gmpxx.h>
#include < iostream>
using namespace std;
void main(int argc, char *argv[])
{
mpz_class answer_a = 111111111111111111111111111111111111111111111111;
mpz_class answer_b = 111111111111111111111111111111111111111111111111;
mpz_class answer_c;
answer_c= answer_b + answer_a ;
cout << answer_c<<"\n";
}
But still I get error C2177: constant too big.
Did I misunderstand MPIR ?
Such constant is (very likely) too big for standard integer types. You should use char * constructor instead:
void mpz_class::mpz_class (const char *s)
For example:
mpz_class answer_a("111111111111111111111111111111111111111111111111");
to make this work you need to include suitable MPIR C++ interface header (notice that <gmpxx.h> is from C++ interface of GNU MP library):
#include <mpirxx.h>
See 12.2 C++ Interface Integers chapter in MPIR documentation for more details.
I am very curious why I can use the math functions in C++ without including the "math.h". I can't find an answer with google search.
Here is the simple code I am executing. Everything is compiling and running.
#include <iostream>
using namespace std;
int main()
{
const float PI = acosf(-1);
cout << PI << endl;
return 0;
}
Any standard header is allowed to include any other standard header.
if you would compile the same with gcc-4.8 it would complain.
Keep in mind that this is not something to rely on if you want your code to be portable and compilable on different versions of the same or different compilers.
I have a server that I want to run, and it uses a cross platform library that only gives me a tick() to call:
int main()
{
Inst inst;
while(true)
{
inst.tick();
}
}
I need to try to lower the cpu usage so that it doesnt constantly take up 1 core.
Is there a simple way to do this without boost?
Thanks
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int main()
{
//5 seconds
auto duration = chrono::duration<float>(5);
this_thread::sleep_for(duration);
return 0;
}
However, even if this code is completely fine, I can't seem to compile it with the provided MinGW compiler from Code::Blocks.