Fortran limited time for keyboard input [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 6 years ago.
Improve this question
I would like to find a solution for my program to read keyboard input from users for a limited time.
For example, I would like to wait for keyboard input for 5 seconds and after 5 seconds passed, program will continue with or without input (if input is given, it will continue immediately)
I have found some stackoverflow questions asked on C++, however, I couldnt find any solution to solve this problem in fortran.
These are the C++ questions:
1-C++ How to wait for keyboard input for a limited time
2-How to use a timer in C++ to force input within a given time?
Since I dont know fortran very well, I dont prefer to use any other 3rd party libraries, however if you can guide my, I can try them too.
Thank you in advance for your replies.

Fortran is primarily meant for batch numerical computing and as Steve Lionel points out, the language has no features to support this sort of interactive use. Let me make a few suggestions:
Wait indefinitely for user input.
This is the traditional method of getting user input and example code is available near the front of every Fortran textbook. The upside is that it's simple to implement; the downside is that the code effectively hangs until the user responds. Effectively, the code blocks, waiting on user input; there's no signalling or threading system in Fortran to proceed after a time interval has elapsed. On unix systems, I made use of SIGALRM and signal handlers in Perl to implement timeouts but that technique is specific to POSIXy systems and is an example of what can be done with OS libraries, as Steve Lionel noted.
I'm assuming this is the problem you are trying to work around.
Take all user input via an input file and/or command-line switches.
Assume the user knows how your software works and understands how to specify input via the command line or an input file. The Fortran Wiki shows a number of command-line processors which you could use directly. Otherwise, they are good example code for using (modern) Fortran 2003 intrinsics get_command_argument and command_argument_count.
For extended input, you will probably want to use some form of input file. Unfortunately, there's effectively no standard Fortran library which helps with this age-old problem; compare with (for example) Python's ability to read JSON with standard libraries which eliminates the need to invent Yet Another Broken, Proprietary, Unparsable, Garbagey Input File Format (YABPUGIFF). Here is a typical example. Welcome to user interface hell.
In my experience, inventing crappy input file formats and maintaining equally crappy parsers is about 75% of Fortran programming. There are third-party libraries to read and write INI, CSV, JSON, XML, etc. in Fortran. I suggest you investigate those before inventing your own crappy input format.
For programming in general, learn the operating system standard(s) for command line interfaces such as GNU long options and common options such as -v and --verbose for verbose output, -h, --help, and /? for getting help, --usage for showing usage information, etc.
Write the user interface in a different language.
A number of languages interoperate reasonably well with Fortran via the standard module ISO_C_BINDING. I've had good results with Lua, but C and Python work well also.
If you don't need deep access to your code's internals, you can just read input via a wrapper program written in Python, etc., then create the appropriate command line or input file for your Fortran application, then issue a system() or exec() call to actually run your code. I use Python's subprocessor multiprocessing libraries to spawn and watch Fortran executables under both Windows or Linux and have used the Jinja2 template system for generating input for Fortran codes, though any of the popular web-oriented templating frameworks work just as well.
My biggest piece of advice is do not reinvent the wheel; others sorted this out decades ago, follow community standards even though they aren't perfect, and get on with writing interesting code.

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)

c++ Writing a cosole application from which you can call functions defined in it self [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
Hi I'm new to c++ and I want to write a program witch allows me to test different Functions witch are defined in it when it is compiled and running.
I could do a simple switch that receives the case from a cin but then I would have to maintain that for every function i write, plus I'm not sure how to pass on the arguments through that (like I said I'm new)
I come from haskell in wich you can just call whatever funktion you like with custome parameters...(makes testing edge cases and single "Parts" really easy)
How would I do that in c++?
Thx
A possible approach might be to embed some interpreter (like Lua, Guile, etc...) in your C++ application. Then your advanced user would code some script in that language and can call whatever routine you have embedded into (i.e. interfaced or glued with) that interpreter.
(alternatively, write your own interpreter, but that is a lot more work)
Another approach might be to use dynamic loading facilities, but these are operating system specific. On Linux (and POSIX) you would use dlopen(3) and dlsym(3). Be aware of name mangling, so use extern "C" for those C++ functions you want to load at runtime with dlsym (see C++ dlopen mini howto).
A variant could be to generate (with your C++ code) dynamically some C++ source in a temporary file (perhaps from some user input; you want some intermediate AST), fork a compilation of that code into a temporary plugin, and dynamically load that with dlopen, then run it (using raw function pointers obtained with dlsym)
But (in contrast to Haskell or Scheme or Ocaml) there is no REPL in C++, so you need to make one (using the tricks I considered above) if you want some, and that is a difficult task for a C++ newbie. C++ is practically a programming language which should be compiled (practically with a compiler such as GCC or Clang started by some build automation tool like GNU make or ninja). And C++ is a very complex language, difficult to learn and to compile, so all its compilers are rather slow.
BTW, be sure to learn at least C++11.
Perhaps you want some unit testing. There are tools and libraries to help on that (see this answer, and also that list)
I could do a simple switch that recieves the case from a cin
You should consider more fancy stuff. For example, you could at least have some std::map with std::string-s keys and values of some common std::function type then use lambda expressions to fill it.
You could read the latest Dragon Book; it is helpful to learn parsing, interpreting and compiling techniques from it (and they are more relevant to your wish than what you might imagine).
Read also SICP (freely downloadable introduction to programming, using Scheme); it is not about C++ but will help you to relate Haskell with C++ and will improve your thinking about programming.
The correct Answer for myself would be:
I can't do it because the names of the functions dont even exsist anymore. After compiling only their Logic with all nessesary information remains.
--> so you can't call functions by their names in compiled Code unless you want to write your own User Interface / keyword interpreter
thx

C++ and Lua from USB [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
So about 2 weeks ago, I started learning C++ and Lua, and I would like to be able to:
compile C++ code (it would be a nice bonus if i could have a C compiler as well, as that's next on my list of languages to learn)
interpret (is that correct terminology?) Lua, and
do all of this from my usb drive WITHOUT downloading anything from the internet or changing the path variable. (I will mostly be working on school computers.)
As a side note, I have fallen in love with Sublime Text 2 (the portable version of which is already on my usb drive). If it's ABSOLUTELY necessary, I'll make do without it, but I would prefer being able to use it wherever I go.
Please be patient with me, as i have mentioned before, I just started learning how to program, and I have little to no knowledge on how things work. I have seen similar questions, but they never seem to help me much due to my limited knowledge, so PLEASE don't mercilessly close my question like others I have seen on this site.
Thank you in advance!
I recently added a page on Lua WIKI (great source of info) that may help you. It is a tutorial for complete newbies on how to build Lua from the sources using only free and "portable" (in the sense of "can be put on usb drives") tools. It is meant for Windows OS users. Do not forget to check the official getting started page and the main Lua site as well!
The fact that you cannot download anything is quite restrictive (how could you get a free compiler then?). Anyway as greatwolf mentioned TDM-GCC is a great C/C++ compiler for 32 bit x86 PCs. It is also patched to be perfectly "portable": I usually use it from a portable USB HD. The tutorial I mentioned shows you how to download it and "install it".
Note that although your sysadmin at school may have blocked your ability to change the path variable globally, you can set it for individual processes ("launched programs") using simple batch files (aka Windows command shell scripts).
Create a file named "myshell.cmd" with this content:
#set path=%path%;c:\the\path\to\my\app&cmd /K
the part c:\the\path\to\my\app must be the actual path of the directory (folder) where the application executable is placed. When you double-click on myshell.cmd a black box will open (assuming your sysadmin hasn't blocked this feature) where you can invoke the app executables.
For example, if you "installed" the TDM_GCC compiler in c:\myprogs\GCC inside that dir you will find a subdir named bin. That subdir must be put in the path, so your myshell.cmd file will be like this:
#set path=%path%;c:\myprogs\GCC\bin&cmd /K
Then in the "black box" I mentioned you can invoke the compiler typing:
gcc --help
As for your learning path, if you intend to learn both Lua and C or C++, I will advice you to try C instead of C++. C++ has more "high-level" features, but it is huge and although Lua can be used embedded in C++ code (of course this is an advanced topic anyway), it is designed to be directly embedded in a C application (it has an API which conforms to C conventions), so for a beginner probably the path Lua --> C --> C+Lua would be a bit easier. C in itself, although difficult to master, is a rather minimalistic language, so the information to digest about it is not that big.
Not to discourage you, but IMO both C and C++ are not the most suitable languages for absolute beginners (they are plenty of pitfalls and have almost no "safety nets" for beginners). But that's up to you, it heavily depends on your skills, dedication and motivation ;-)
Hope all this helps.
For windows,
Take a look at
http://nuwen.net/mingw.html
You should be able to extract the download to a usb directory. Then you can click on the .bat file open a command prompt with the correct path settings.
As a bonus, it already includes prebuilt boost, which will make your c++ use easier.
For the C/C++ piece would also recommend you start with C. Not for ideological reasons, just that it's a lot simpler if you're trying to work out the basics of compiling/linking etc.
As a first C compiler I would recommend the tiny C compiler
Tiny C Compiler
It's one of the simplest to get your head around that I've seen and you can still build lua libraries etc.
Once you're comfortable with that then progressing to one of the more powerful environments such as gcc under MingW or Visual C++ should be a bit less daunting.
Lua is trivial. Download the binaries, put them on your drive, and configure Sublime Text to invoke them on Lua files.
C/C++ is more complicated only because of the range of options is so vast. I use a 2003 version of Microsoft Visual C++, which covers my needs. I find a copy here.
Keep in mind that C++ is a vastly more complicated superset (non-strict) of C, so you're going to learn most of C in the process of learning C++. IMO, learning C first is better for a whole host of reasons. You'll hear some people argue the opposite, but in this case there's a clincher: Lua is written in C and its API is designed for C. Exposing idiomatic C++ (i.e. objects) to Lua is a big ball of complexity that you just don't need right now, while learning two languages.

How to not erase user assigned values after each run time?

How to develop a software in c++? i know it should be done through a program. But how can i develop it into a software which runs like the dos counter-part of eclipse or media player or any other software. I mean how can I make the program to remember the values the user assigned to its variables without erasing it every time I close the .exe file running the program? Hope you understand my question.
You should store that value outside, in example in XML, JSON, SQLITE. The keyword for today is Database.
The simple example is QT with SQLITE or even simpler reading/writing to file. Here is a basic tutorial aka reference.
Here is the usage of rapidxml (pretty fast)
How to develop a software in c++?
Well the simplest way to get into process is to track some open software applications. There is a lot of tasks marked as Junior Job. Try to implement it.
How to develop a software in c++?
I suggest picking up a good book on C++ and do all included exercises. If you don't understand a particular portion of the language or standard library refer to cppreference.com and StackOverflow. Both have search facilities.
I'm sure there are plenty of tutorials available on the web but I cannot personally recommend any for beginners.
I mean how can I make the program to remember the values the user assigned to its variables
The easiest way without using any third party libraries or OS specific API's is to use std::ifstream and std::ofstream.
Since it appears you are developing for Windows platforms (.exe mentioned) I suggest you also take a look at the registry functions available in the Windows API.
Here is a link which guides one for writing data to a file.
http://www.cplusplus.com/doc/tutorial/files/
If you want to use this data the next time you execute the program,
maybe you want to incorporate an if statement that says if the
file exists and is not empty, then import the data from there into
your program (which means adding lines to your program to also read
in the data from the file).

Matlab vs. Visual C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I'm doing a Windows Application the uses lots of charts. Its practically a dataviewer. I started doing Matlab, because its easier, but I realized it's too slow. I wanted to change to another language. Somebody recommended me Visual C++ or Java. But Im not sure. What language should I use??
In my opinion the speed gain from going to another "faster" language is not as much as refining your algorithm.
The "problem" with MATLAB is that it allows you to do some nasty things, such as resizing your matrix in a tight loop. You should really try to pinpoint your bottlenecks using the following command:
profile on
... run your program
profile off
profile report
This will give you nice information about which function takes how long to execute and which line creates the biggest bottleneck. You can also see how many times a function is called and a M-Lint Code Check Report is included.
These measurements and hints can show you the bottlenecks of your algorithm. if your sure there isn't a way to reduce the callcount/speed of a function using a smarter algorithm. Such as do I really need that big 2d matrix where a smart vector would be large enough, or if I found a artifact, why would I still continue searching for artifacts. You could write the functions you're experiencing the most performance problems with in c/c++ and use it as a function in matlab. You can get a big speedup out of correctly choosing which functions to implement in c/c++. There is an amount of overhead with calling a c/c++ function from MATLAB, or more correctly there is a overhead in c/c++ to get the data from MATLAB, so a function which is called 10000 times will not be the best to implement in c/c++, you'd be better of with the function higher up the callstack.
It depends on what your requirements are.
The advantage of using matlab is that it's strong in numerical calculations. If you don't need that, then there is no advantage to using matlab. In this case, all those languages are okay, and many others (Python, C#, ...) as well. It depends on which language you are most comfortable with.
If you do want the advantages of matlab then:
Try optimizing in matlab. Most optimization techniques are language independent.
There are tools to translate matlab to C automatically. You can then try to compile with all optimizations on. I seriously doubt this will help much, however, especially considering the GUI part.
First and foremost, as other answers have mentioned, you need to profile your code to find out where the bottleneck is. I would check out Doug Hull's blog at The MathWorks, specifically this entry about using the profiler. This will help you find out where all the work is being done in your code.
If the source of the slowdown is associated with data processing, there may be a number of ways to speed things up (vectorizing, writing a mex file, etc.).
If the source of the slowdown is your GUI, this may be even easier to solve. There are a number of blog posts, both from Doug and other MathWorkers, which I've seen that deal with GUI design. There have also been a few questions on SO dealing with it (here's one). If you're dealing with displaying very large data sets, this submission from Jiro Doke on The MathWorks File Exchange may help speed things up.
It's hard to give you more specific advice since I don't know how you are designing your GUI, but if that turns out to be the bottleneck in displaying your data there are many resources to turn to for improving its speed before you go through the hassle of switching to a whole other language.
Don't forget that you can create functions in C++ that can be called from Matlab. And TADA, you have access to both environments !
I would use C#. It is easier than C++ and integrates well with the Windows platform. Just find a free graphing library for it and you're good to go.
There are plenty of other options depending on your preference of language. Eg. Qt with Python or C++.
As far as I know, the most common methodology is to first do the proof of concept or just the main algorithm on Matlab, because of its ease of use and convenience for math calculations, and after that to translate it to a "real" programming language in order to improve the performance. Usually C or C++ act as the "real" language, but in your case, aiming to do a Windows application, perhaps C# will be the best option.
I found that GUI programming in MATLAB can get really nasty if your application gets more complex. BTW MATLAB can also be called from Java easily (and vice versa, current versions basically provide an interactive Java console).
Just as a side note, if you still need the math power of Matlab, you may want to check out Scilab. It's open-source and free, and it has examples of how it can be integrated with other C# or C++. I have created projects on which Scilab was running in the background to perform all the data math operations; and displaying them with C#'s ZedGraph library. Worked like magic!
I suggest you using Java and the JFreeChart (http://www.jfree.org/jfreechart/) library. I found very easy (and fast) developing applications with a lot of charts of different typologies. If you don't need particularly fast performances, you can use Java. I suppose that there are similar libraries for C#, but I'm not sure.
An alternative to Matlab and Scilab is another free software: Octave.
I don't know about Scilab, but Octave syntax is nearly the same as matlab so you can import code with minimal effort.
If you need fancy toolboxes though, Scilab and Octave might let you down, so check this.
You can execute Octave functions in a C++ program:
http://en.wikipedia.org/wiki/GNU_Octave
I do not think that you can call your own m-files functions from your C++ program though. In the past, the Matlab compiler would let users run matlab programs without installing Matlab, but not without installing a huge library (250 MB if I remember correctly). Nevermind if your Matlab program took 20 kB, you had to distribute the huge library.
Please someone edit/comment on the situation today!
It has been a while since I used the GUI "ability" of Matlab, but back then (2005) I found it awful. Ugly, hard to use, very hard to maintain, dependent on user settings of windows parameters.
Please comment or edit on that too, they may have made progress!
If they have not, I believe that Matlab is NOT the way to go for a program that you want to deliver to anyone.
If you can use Visual Studio for doing your GUI, do that. I second the earlier opinions: go with what you're comfortable with.
If you need the Matlab functions, go with what you're comfortable with, that supports Matlab libraries.
First of all Visual C++ is not a language is an IDE for developing applications.
Second... Which languages do you know? You can have several options. Take a look to:
C++ + Qt (Mine preferred option, powerful and easy to understand)
C# + .NET or WPF
Java
If you can tell more information we could find a language that matches your needs.