Multitasking in Fortran [closed] - fortran

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
How can I do multi-tasking and inter-process communication in Fortran?

The main standards to read up on are OpenMP (shared memory multi-threading) and MPI (message passing). Both work well with Fortran (as well as other languages) and you will find a lot of information online.
OpenMP defines a simple way of programming concurrent (parallel) processing in Fortran/C/C++. The process must reside in a same computer (node).
OpenMP 3.0 recent introduces $OMP TASK directive which in principle should allow multitasking the way multithreading is usually done (that is, each thread does its own task). For OpenMP, see this tutorial:
https://computing.llnl.gov/tutorials/openMP/
or specs in http://www.openmp.org/
I won't address interprocess communication (IPC) since I am not familiar with this. I believe you can do POSIX function calls if that what you want. If your compiler supports some Fortran 2003 constructs (e.g. gfortran >= 4.4) then you can use the nice C-Fortran interoperability provided by ISO_C_BINDING standard module. Then with proper care you can call posix functions that can provide IPC functionalities. That's my 2c.

Fortran2008 also has coarrays, which allows distributed-memory computing from within the language itself, and do concurrent, which allows for functionality similar to an OpenMP parallel do loop. Right now, only the newest intel compiler fully supports these, and g95 has partial support; however, they are actively being worked on by the other compiler vendors, including gfortran.

You do concurrency in Fortran in the same way you would do this in any other language: Spawn a pthread, use OpenMP, use MPI, fork() ... whatever suits your need best.
Systems APIs are often in C (cf. POSIX and Windows API), but interacting with C is a fact of life, regardless of which programming language you use.
The "do concurrent" contruct in Fortran 2008 still does not have a lot of compiler support, even in 2015.

Related

How to utilise all cores for C++ program [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I translated a Java program into C++, and I was getting very slow performance from C++ as compared to Java. Looking at the activity monitor, I saw that the Java program, which I ran first, was using over 90% of the CPU, while the C++ program was using around 30%.
It appears that the JVM is able to harness the full power of the CPU without user intervention. What changes should I make to use the CPU better for the C++ case?
Read a good C++ programming book then see this C++ reference. Read also the documentation of your C++ compiler (perhaps GCC).
Read also some operating system textbook.
Consider using frameworks such as POCO or Qt or Wt.
With a multi-core processor, you might use C++ threads. You'll need to synchronize them, e.g. using mutex and condition variables.
On GNU Linux, you could read advanced linux programming and use the syscalls(2) related to process creation and management (e..g. fork(2), execve(2), waitpid(2)...) and profiling (see time(7), profil(3), gprof(1), perf(1)).
On Windows, you need to use the WinAPI.
I translated a Java program into C++
In general, that is a bad idea. Learn your programming language and your operating system, and design your software for it.
Take inspiration from existing open source software on github or gitlab (e.g. RefPerSys, Firefox, Tensorflow, OpenJDK, Clang static analyzer, Fish ...)
Java implementations (e.g. JVMs) have some garbage collection and some class loader.
C++ implementations do not have any garbage collection. So read the GC handbook. Notice that circular references are hard to handle efficiently in C++, and this could explain your performance issues.
On Linux/x86-64, you might load plugins with dlopen(3) with dlsym(3) (or generate machine code at runtime using asmjit or libgccjit)

Is it true that a C++ compiler is written multiple times for different platforms (Linux, windows etc) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
As far as I know C++ is an ISO standard, so they provide some sort of standards and list of features to be implemented for the coming release.
Is it the case that every platform owner will go and write their own implementation for those standards?
Or is there any core compiler code which is implemented once and then every other platform will write wrappers around it?
Or do they write their own C++ compiler from scratch?
Yes and no. Compiler basically consists of two parts: parser (a.k.a front-end) and code generator (a.k.a. back-end). Parser is responsible of recogninizing C++ grammar. Code generator constructs machine code for target platform (hardware type and operating system) based on information it gets from parser. While parser is platform independent, code generator part is tied to target platform. So, in order to support a new platform, one can reuse existing parser part, but has to write new code generator part.
Basically what ISO standards set is set of rules that should be followed by the compiler vendors.
But these are the standards for implementation and not the actual implementation.
Every major hardware vendor knows how to use its own hardware best
This includes aspects like
1) ABI support - This include things like binary formats, system calls and other interfaces
2) Shared Libraries.
3) Architecture Support.
So Microsoft, IBM, Intel, Oracle, and HP all have their own C++ compilers, which create optimal code on their latest hardware.
Standards, however do provide the draft that has to be purchased
https://isocpp.org/std/the-standard
The following table presents compiler support for new C++ features. These include C++11, C++14, C++17, and later accepted revisions to the standard, as well as various technical specifications.
http://en.cppreference.com/w/cpp/compiler_support
Likely once they where created platform specific, put simple a windows exe or com file wont run on linux, however later versions can be compiled trough older versions.

Is it possible to make C++ platform independent by making it run inside a VM just like in Java? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Given that Java is highly portable and it is not having serious overheads, can't C++ be made platform independent?
Yes, it is perfectly possible. For example, you can compile C++ to JavaScript ( see https://softwareengineering.stackexchange.com/questions/197940/how-to-run-c-code-in-browser-using-asm-js ) or to CLI byte code ( https://en.wikipedia.org/wiki/C%2B%2B/CLI ) to run on Windows or Linux, or various other targets.
None of these currently performs as well as native C++, and most lack direct access to operating system resources. So the portability comes at some cost, and usually if you wanted to pay the cost of targeting web browsers or CLI, you have languages better suited to those platforms.
In reality, the method of code execution (whether the code is compiled, interpreted, run by VM, etc) is more a property of the implementation, and not the language.
when people say C++ is a compiled language and that JavaScript is an interpreted language, that does not necessarily mean that you can't write a compiler that translates JavaScript code to machine code on your hardware of choice, but rather what is a common way to provide implementation for said language.
In practice, C++ is used because of its efficiency and close to the metal features that is a good choice for performance critical tasks like embedded systems programming, systems programming, graphics, etc, so getting C++ to run in a VM would defeat its purpose.
kinda like buying a fillet Mignon and cooking it in the microwave.
Java compiles to an intermediate platform-independent byte code that is then interpreted at runtime by platform-specific JVMs. That is what allows Java to be portable. Each type of JVM is tailored to run the byte code on the platform's particular hardware architecture.
C/C++ compiles to native machine code that runs directly on the CPU (or as directly as the OS will allow). So no, you cannot compile C/C++ in a platform-independent manner. You have to use platform-specific compilers to compile C/C++ code for each hardware architecture that you want to run your code on.

GPU programming in Standard C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I know that you can program an OS by putting your code in the first 512 bytes of the RAM and things like that, but how do you program a GPU? Is it possible in standard C++?
I've heard of CUDA, but I would like to go by standards. Does Adobe Photoshop also use CUDA?
For all intents and purposes, there is no way to program a GPU without making use of some libraries, whether OpenCL, CUDA, or otherwise. While it is technically possible to drive the GPU directly (given that the GPU drivers are doing so!), documentation on how GPUs work is very difficult to come by, especially regarding advanced features such as those required for computation, and it's almost certainly not something you want to get involved with.
CUDA is a proprietary Nvidia language and software. It won't work on AMD graphics cards, or on recent Intel or AMD processors having an integrated GPU.
OpenCL is an industry standard, available on Nvidia and AMD graphics cards (and recent processors with integrated GPU).
However, pure standard C++ don't give you access to graphics cards or GPGPUs. You'll need an operating system and some libraries.
Notice that the small codes (called kernels) running on the GPU are not coded in fully standard C++, but in a small C-like dialect (also called OpenCL). There are some limitations (no recursive function, no function pointers, etc...) because of the weird nature of GPGPUs. The programming model is not as general as in C++.
There is also OpenACC, a set of pragmas for C++ compilers.
CUDA is the standard for NVidia cards. Alternatively OpenCL is also an option for a bit more cross platform.
http://en.wikipedia.org/wiki/OpenCL
Coding a GPU is fundamentally different to coding a CPU in that you have hundreds to well in the thousands of concurrent threads hence the different way of calling functions/kernels on the GPU.
NOTE: There is no way you can run your own code on the GPU without compiling it for the GPU therefore a standard C++ compiler will only be able to call functions in libraries.
It is surely possible up do everything in C++, but you will need to use some external libraries.
You might want to consider OpenCL instead of CUDA.
CUDA is a proprietary NVidia technology, while OpenCL works on more hardware and platforms.

Best operating system abstraction? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm looking for something to abstract the standard operating system functionality in C/C++: span/kill a thread, send/receive a message, start/stop a timer, maybe even memory management, although I can probably handle that myself with my own buffer pool.
I want to to be able to develop and unit test on Linux/windows and then recompile the c/c++ code for various target O/Ses (for embedded systems: eCos, FreeRTOS, VxWorks, etc)
Something as "light" as possible would be best, hopefully just a library, maybe even a collection of macros.
Have you looked at the Boost library? It has threads, timers, memory management, and a signals library.
The library is not a small download, but most of the library components are header-only implementations (though the OS abstraction libraries tend to have to be linked), and you only have to use what you need.
I keep a (long) list of OS Abstraction Libraries. Hope it helps.
Why don't you directly call only POSIX functions (POSIX1 seems to fill all your needs) and install a POSIX layer above non-compliant operating system (to be read as Microsoft Windows)?
I think boost is worth to look into; it can provide you an os abstraction, but also a compiler independence, and much, much more. It does require C++ of course. Other options: Posix.
In your list:
eCos, VxWorks, Linux : good posix support, so you can use this.
freertos: see link
Windows lacks good posix support out of the box (see wikipedia Posix)
If cygwin is ok for you, you can propably use it. If you need to mix with Visual Studio, a library like boost seems more interesting (you'd abstract away from it)