Compiling with a parallel compiler for use with Ruby? - c

I have a (semi) theoretical question.
Is it possible to compile C code with a parallel specific compiler such as OpenMP, CUDA, etc., to write a C extension to the Ruby language that operates better (theoretically) for distributed machines?

If what you mean is, can you compile parallel C code as Ruby extensions, I think you mostly should be able to do so. For example, a numeric extension to Ruby that does matrix multiplications can call into Intel MKL's DGEMM routine and MKL is parallelised internally with OpenMP.
You should only be careful not to mix in the same program extensions that use different OpenMP runtimes. Take for example GCC, Intel C/C++ compiler, and Oracle's Solaris Studio. Each of them uses has its own implementation of the OpenMP runtime. These implementations are at large incompatible with each other and mixing them in the same executable (either by static linking or by dynamically loading modules at runtime) is a call to disaster.
Another thing to note is whether the Ruby runtime is thread-safe and whether it could be called from a multithreaded code. This remark is not relevant if one does not call the Ruby runtime from inside OpenMP parallel regions.
As there is a single CUDA vendor, one should only be concerned with version mismatches when it comes to CUDA-enabled extensions.

Related

Read math functions from file and calculate

I'm creating program which will read model described by math functions from file into memory. I need to make these functions invokable. Is there any other way to achive it instead of implementing RPN ? Performance is the most important factor.
Maybe something like creating and compiling functions during runtime, after reading model from file ?
CUDA currently only has JIT compilation for device code written in PTX assembly code. So your only "native" JIT option would be to have your code translate the functions into PTX code and compile them.
Realistically, your best option would be to write your front end in Python and use PyCUDA, which includes some very powerful metaprogramming and JIT compilation features, or to use OpenCL, which has native C99 JIT compilation, at the expense of an uglier and more verbose host API and a lack of C++ language support.

Concurrent programing in plain C - Windows / Linux

I have a function, lets say, callMe(args). I would like to be able to call my function 100 times in the same time (concurrent programing). My function is written in plain C. I want to make it concurrent for Windows and Linux.
Is there any built in support for this in standard C library? Or can you provide some advices, how should I do this in Linux/Windows?
Is pthread good for Linux environments for this?
You can't write a concurrent program in pure standard C99. Concurrent programming is not in standard C99; it requires some operating system specific libraries. However, the latest C11 standard (which has very few implementations today, February 2014 - I know none!) is beginning to add support for threads. On Linux systems I would suggest today to stick to pthreads.
You could use a cross-platform library (like glib from GTK) to ease porting between Windows and Linux. On Linux, it is wrapping pthreads.
I don't know Windows, but Glib -and GTK for GUI applications- is rumored to enable source-compatible coding between Windows & Linux
If your computation is embarrassingly data parallel (like some matrix numerical computations are), consider also OpenCL (notably because it can run on GPGPUs) or OpenMP. If it fits in a message passing paradigm, consider MPI
OpenCL, OpenMP, MPI are rumored to enable source-compatible coding for Linux and for Windows.
In C++11 (which is a different language than C) you have thread support, and the GCC 4.8.2 compiler is supporting them quite well. And Qt or Poco or Boost is a cross-platform C++ library, notably wrapping concurrent primitives.
Whatever concurrent mechanism you are using, beware of synchronization issues.
You always have synchronization with concurrency, at least to wait for the results of each concurrent or parallel sub-computation
You have to use different functions for different platforms.
Its similar like different browsers uses different kind of css property names.
So for linux/mac you can use pthread
And for windows you use CreateThread

C libraries are distributed along with compilers or directly by the OS?

As per my understanding, C libraries must be distributed along with compilers. For example, GCC must be distributing it's own C library and Forte must be distributing it's own C library. Is my understanding correct?
But, can a user library compiled with GCC work with Forte C library? If both the C libraries are present in a system, which one will get invoked during run time?
Also, if an application is linking to multiple libraries some compiled with GCC and some with Forte, will libraries compiled with GCC automatically link to the GCC C library and will it behave likewise for Forte.
GCC comes with libgcc which includes helper functions to do things like long division (or even simpler things like multiplication on CPUs with no multiply instruction). It does not require a specific libc implementation. FreeBSD uses a BSD derived one, glibc is very popular on Linux and there are special ones for embedded systems like avr-libc.
Systems can have many libraries installed (libc and other) and the rules for selecting them vary by OS. If you link statically it's entirely determined at compile time. If you link dynamically there are versioning and path rules which come into play. Generally you cannot mix and match at runtime because of bits of the library (from headers) that got compiled into the executable.
The compile products of two compilers should be compatible if they both follow the ABI for the platform. That's the purpose of defining specific register and calling conventions.
As far as Solaris is concerned, you assumption is incorrect. Being the interface between the kernel and the userland, the standard C library is provided with the operating system. That means whatever C compiler you use (Forte/studio or gcc), the same libc is always used. In any case, the rare ports of the Gnu standard C library (glibc) to Solaris are quite limited and probably lacking too much features to be usable. http://csclub.uwaterloo.ca/~dtbartle/opensolaris/
None of the other answers (yet) mentions an important feature that promotes interworking between compilers and libraries - the ABI or Application Binary Interface. On Unix-like machines, there is a well documented ABI, and the C compilers on the system all follow the ABI. This allows a great deal of mix'n'match. Normally, you use the system-provided C library, but you can use a replacement version provided with a compiler, or created separately. And normally, you can use a library compiled by one compiler with programs compiled by other compilers.
Sometimes, one compiler uses a runtime support library for some operations - perhaps 64-bit arithmetic routines on a 32-bit machine. If you use a library built with this compiler as part of a program built with another compiler, you may need to link this library. However, I've not seen that as a problem for a long time - with pure C.
Linking C++ is a different matter. There isn't the same degree of interworking between different C++ compilers - they disagree on details of class layout (vtables, etc) and on how exception handling is done, and so on. You have to work harder to create libraries built with one C++ compiler that can be used by others.
Only few things of the C library are mandatory in the sense that they are not needed for a freestanding environment. It only has to provide what is necessary for the headers
<float.h>, <iso646.h>, <limits.h>, <stdarg.h>, <stdbool.h>, <stddef.h>, and <stdint.h>
These usually don't implement a lot of functions that must be provided.
The other type of environments are called "hosted" environments. As the name indicated they suppose that there is some entity that "hosts" the running program, usually the OS. So usually the C library is provided by that "hosting environment", but as Ben said, on different systems there may even be alternative implementations.
Forte? That's really old.
The preferred compilers and developer tools for Solaris are all contained in Oracle Solaris Studio.
C/C++/Fortran with a debugger, performance analyzer, and IDE based on NetBeans, and lots of libraries.
http://www.oracle.com/technetwork/server-storage/solarisstudio/index.html
It's (still) free, too.
I think there a is a bit of confusion about terms: a library is NOT DLL's or .so: in the real sense of programming languages, Libraries are compiled code the LINKER will merge with our binary (.o). So the linker (or the compiler via some directives...) can manage them, but OS can't, simply is NOT a concept related to OS.
We are used to think OSes are written in C and we can rebuild the OS using gcc/libraries or similar, but C is NOT linux / unix.
We can also have an OS written in Pascal (Mac OS was in this manner many years ago..) AND use libraries with our favorite C compiler, OR have an OS written in ASM (even if not all, as in first Windows version), but we must have C libraries to build an exe.

Is there any concurrency package for C-language?

I know Java and C# both have library package to support concurrency programming. Does anyone know whether or not there is library package for C? Thanks
Qt QThread
pthread
MPI (for computations on multiple computers)
(more)
At the lowest level, you have pthreads, which give you threads, locks, condition variables, etc. It's about as basic as you can get. If your program uses a framework, it might provide its own threading primitives so you don't have to use pthreads directly.
Qt Threading Support
Glib threads (used by GTK)
Boost threads (for C++)
Other packages provide higher-level concurrency operations that may be easier to reason about.
Intel Threading Building Blocks
OpenMP
MPI
QtConcurrent
There is OpenMP which is supported by compilers like icc, msvc and gcc (at least).

How does one write cross-platform parallel programs in C?

I am working on a programming language. Currently it compiles to C. I would like to be able to include parallel programming facilities natively in my language so as to take advantage of multiple cores. Is there a way to write parallel C programs which is cross-platform? I would prefer to stick to straight C so as to maximize the number of platforms on which the language will compile.
Depending on what you want to do, OpenMP might work for you. It is supported by GCC, VC++, ICC and more.
Use a cross-platform threads library, like pthreads.
C has no standard, built-in support for threads or parallel processing.
"Straight C" has no concept of threading, so I'm afraid you're out of luck. You'll need to find some sort of cross-platform supporting thread library or port one to the various platforms you want to use. pthreads are as good a place to start as any, I guess.
GLib library (from the GTK project) has many useful cross-platform facilities, including threading.
If you're looking to eventually target large-scale parallelism, have a look at Charm++ and its underlying portable machine layer Converse. We run efficiently on machines ranging from multicore desktops to clusters, to BlueGene and Cray supercomputers.

Resources