I am using the GNU scientific library and I was wondering what was the differences between those two function to copy a vector to another :
gsl_blas_dcopy and gsl_vector_memcpy
do you have any idea which one would be the fastest ?
In the GSL manual, section 8.3.6, it says
However, it is useful to have a small number of utility functions
which do not require the full blas code. The following functions fall
into this category.
int gsl_vector_memcpy
So both are basically the same. If you already need BLAS functionality, use gsl_blas_dcopy.
Rumors say a BLAS implementation for you specific CPU might be the fastest.
Related
I'm trying to build a portable project that uses a library and that library uses GMP. I am wondering if it is possible to use a statement like #include "gmpxx.h" with GMP's code in my project's directory.
Yes, as Marc Glisse pointed out, if performance is not so important to you, and you are not calculating with very very large numbers, GMP has a small footprint library-within-the-library version called "mini-gmp" which is almost fully compatible with the GMP interface, comprising calculations on natural numbers (mpn), integers (mpz), and rationals (mpq), see here and here for details. Almost all issues with mini-gmp have been fixed.
You don't ever install mini-gmp, basically you #include "mini-gmp.c" in your code (and #include "mini-mpq.c" if rational numbers required) and you are ready to go. Or just compile mini-gmp.c as a separate compilation unit and use the declarations in mini-gmp.h. Also, there's a test suite in the directory you may run with make.
Note, though, that if your application is mission-critical you may run into problems because all microarchitecture optimizations and code for arithmetic on asymptotically large numbers are not included in mini-gmp.
In the most recent magma linear algebra library (version 1.6.1), http://icl.cs.utk.edu/magma/software/, in the testing code exercising dgemm functionality (source code: testing_dgemm.cpp), there was a call to functions magma_dgemm and magmablas_dgemm. Can someone clarify the difference between the two? Which one is more general (not tied to just GPU)?
Wirawan
An inspection of the source code reveals that magmablas_Xgemm is actually a C function that launches an appropriate gemm kernel on the GPU. Thus magmablas_Xgemm is a GPU-specific routine. On the other hand, magma_Xgemm is intended to be accelerator-agnostic routine that (currently) can be used for either GPU (NVIDIA/AMD, ...) or MIC.
Ref files, relative to MAGMA source directory (the CUDA edition):
./magmablas/dgemm_fermi.cu
./interface_cuda/blas_d.cpp
So, basically MAGMA includes gemm, both magma_gemm that wraps cublasgemm, and magmablas_*gemm that is Magma's open-source implementation.
I know that some functions like sin cos min max memcpy may be treated not as normal functions but may be replaced by built-in functions (which may be more optimal than merely inline function calls, when the replacement is (an) actual processor instruction(s), such as directly calling the FSIN instruction for standard sin function when compiled for an x86 with a floating point unit).
The question I would like to use power of built-in functions (in C/C++ mostly in mingw/gcc maybe other compiler) but I do not want to link to libc, the Standard C Library).
Is it possible to use builtins with not linking to libc?
Are they any command line flags needed to optimize those symbols as a built-ins?
(Related to previous, but rephrased)
Will they be automatically recognized by name, or are compiler flag(s) necessary to enable usage of built-ins?
#randomusername has already explained the usage of the __builtin_ prefix for many common Standard C Library functions. I recommend using #define to make the change, while keeping your code clean.
#include <math.h>
#define cos __builtin_cos
#define sin __builtin_sin
#define printf __builtin_printf
...
printf("Distance is %f\n", cos(M_PI/4.0) * 7);
...
No Standard C Library
Now to not use the Standard C Library, which means not linking to it, or including the typical startup and exit code stubs, well, with GCC that is possible with the -nostdlib which is equivalent to -nostartfiles and -nodefaultlibs.
The issue is that you then have to replace all the library functions you would normally use, including system calls (or their wrappers / macros from glibc) for any kernel based functions.
I don't know of a portable or robust method that works across processors or even necessarily different families (sysenter vs. syscall (instruction) vs. int 0x80 for various 32 and 64-bit x86 processors). There is issues with ELF Auxiliary Vectors (Elf32_auxv_t) and vDSO (virtual ELF dynamic shared object) that may be possible to address and create a portable solution, I don't know.
Entry Point
I believe all GCC environments use the same default entry point, which is the label/function _start. This is normally included in the "Startup files" and then calls the traditional C/C++ entry point of main. So you would need to replace it with a minimal stub of your own (which can be in C).
Program termination
I don't know how to replace _exit(rc) or similar function required to correct terminate the program, in a portable fashion. For example in a Linux environment it needs to make a system call to the kernel function SYS_exit (aka __NR_exit or sys_exit)
void _start(void) {
int rc;
/* Get command line arguments if necessary */
rc = main(0, NULL);
your_exit_replacement(rc);
}
Alternatives
Normally user processes i.e. application programs, as opposed to Operating System kernels or drivers, accept the overhead of linking the Startup Files and the necessary overhead to enable dynamic linking to the Startard C Library, as memory is considered cheap and readily available that for any real (actually does something) application the memory saving is not worthwhile. In embedded domain, where it is not as acceptable to just assume plenty of memory is available, the alternative is the use a minimal libc replacement. For Linux there are several available (e.g. musl, uClibc, dietlibc), I don't know if there is one available for mingw or Windows-compatible open source replacements (ReactOS, and Wine).
Further
For further information, from a Linux platform point of view, there is a nice introduction "Hello from a libc-free world!" Part 1 and Part 2 by Jessica McKellar blogging at Oracle. There are also a number of related questions, and some (partial in some cases) answers here at stackoverflow about using -nostdlib in various circumstances.
Where to go from here depends on your goals: education, embedded, tiny program (Linux ELF executable) or Windows PE executable competitions.
Microsoft Windows
There are various articles for a Microsoft Windows environment dealing with .COM and .EXE executables, and Windows PE but using Microsoft's Visual Studio environment or assembly typically. The "classics" are Matt Pietrek's Under the Hood column "Reduce EXE and DLL Size with LIBCTINY.LIB" (January 2001 issue of MSDN Magazine) and "Remove Fatty Deposits from Your Applications Using Our 32-Bit Liposuction Tools" from October 1996 Microsoft Systems Journal. Another article, but I haven't read myself, that appears to have include explanations is "Reducing Executable Size".
Lets say you wanted to replace the function cos, all you have to do is replace every occurance of cos in your code with __builtin_cos. The same goes for any other function that you can replace with the compiler's version. Just prepend __builtin_ to the name.
For more information consult the gcc manual.
I need a C++ algebra library in order to use in my project. At the beginning I thought I can write one, but then I realized I was unsuccessfully trying to reinvent the wheel and wasting my precious time.
For arithmetic issues I found GMP Library (you know, for unlimited arithmetic calculations), and tools for other kind of tasks (standard C++ library seemed quite enough for pseudo-random number generation). However, I couldn't find a suitable one for algebraic works.
There are linear algebra libraries (such as Armadillo) but I'm not sure I need such a library. I want to summarize my needs.
#include <string>
#include <somelibrary.h>
int main(){
std::string str = "3*x^3+2*x^2+x+sqrt(x)*x^(1/3)";
algebraic_expression* exp = new algebraic_expression(str);
}
I want to have a tree from such an expression. Lets say it will return a std::vector or C style array with some information. For example (considering the example above) exp[0] will be "3*x^3", or maybe exp[0]["base"]="x".And why do I need this? Actually I can do the similar things by means of using RegEx, but sometimes I cannot handle it for example 3*x^0 is simply 3, I cannot print 3*x^0 because it is meaningless I want to have 3 (just like 3*x^1 is 3*x). Or (3-3)*5*2 will return 0, etc...
Thank you for your help.
You should look for 'CAS' (Computer Algebra System) . I can suggest you two:
Ginac http://www.ginac.de/
Giac: http://www-fourier.ujf-grenoble.fr/~parisse/giac.html
An example program with Giac: http://www-fourier.ujf-grenoble.fr/~parisse/giac_us.html#First%20example
Giac also comes with a GUI application called XCAS. It's a very powerful tool you should give it a try.
I've used LAPACK for my Master thesis program for 3D algebra calculations.
link : http://www.netlib.org/lapack/ link of c++ version of
library : http://www.netlib.org/lapack++/
I'm trying to adjust some mathematical code I've written to allow for arbitrary functions, but I only seem to be able to do so by pre-defining them at compile time, which seems clunky. I'm currently using function pointers, but as far as I can see the same problem would arise with functors. To provide a simplistic example, for forward-difference differentiation the code used is:
double xsquared(double x) {
return x*x;
}
double expx(double x) {
return exp(x);
}
double forward(double x, double h, double (*af)(double)) {
double answer = (af(x+h)-af(x))/h;
return answer;
}
Where either of the first two functions can be passed as the third argument. What I would like to do, however, is pass user input (in valid C++) rather than having to set up the functions beforehand. Any help would be greatly appreciated!
Historically the kind of functionality you're asking for has not been available in C++. The usual workaround is to embed an interpreter for a language other than C++ (Lua and Python for example are specifically designed for being integrated into C/C++ apps to allow scripting of them), or to create a new language specific to your application with your own parser, compiler, etc. However, that's changing.
Clang is a new open source compiler that's having its development by Apple that leverages LLVM. Clang is designed from the ground up to be usable not only as a compiler but also as a C++ library that you can embed into your applications. I haven't tried it myself, but you should be able to do what you want with Clang -- you'd link it as a library and ask it to compile code your users input into the application.
You might try checking out how the ClamAV team already did this, so that new virus definitions can be written in C.
As for other compilers, I know that GCC recently added support for plugins. It maybe possible to leverage that to bridge GCC and your app, but because GCC wasn't designed for being used as a library from the beginning it might be more difficult. I'm not aware of any other compilers that have a similar ability.
As C++ is a fully compiled language, you cannot really transform user input into code unless you write your own compiler or interpreter. But in this example, it can be possible to build a simple interpreter for a Domain Specific Language which would be mathematical formulae. All depends on what you want to do.
You could always take the user's input and run it through your compiler, then executing the resulting binary. This of course would have security risks as they could execute any arbitrary code.
Probably easier is to devise a minimalist language that lets users define simple functions, parsing them in C++ to execute the proper code.
The best solution is to use an embedded language like lua or python for this type of task. See e.g. Selecting An Embedded Language for suggestions.
You may use tiny C compiler as library (libtcc).
It allows you to compile arbitrary code in run-time and load it, but it is only works for C not C++.
Generally the only way is following:
Pass the code to compiler and create shared object or DLL
Load this Shared object or DLL
Use function from this shared object.
C++, unlike some other languages like Perl, isn't capable of doing runtime interpretation of itself.
Your only option here would be to allow the user to compile small shared libraries that could be dynamically-loaded by your application at runtime.
Well, there are two things you can do:
Take full advantage of boost/C++0x lambda's and to define functions at runtime.
If only mathematical formula's are needed, libraries like muParser are designed to turn a string into bytecode, which can be seen as defining a function at runtime.
While it seems like a blow off, there are a lot of people out there who have written equation parsers and interpreters for c++ and c, many commercial, many flawed, and all as different as faces in a crowd. One place to start is the college guys writing infix to postfix translators. Some of these systems use paranthetical grouping followed by putting the items on a stack like you would find in the old HP STL library. I spent 30 seconds and found this one:
http://www.speqmath.com/tutorials/expression_parser_cpp/index.html
possible search string:"gcc 'equation parser' infix to postfix"