I wrote a program in Processing 3.5.4. It's basic structure is as follows:
int SOMEINITIALSTUFF;
Class[] classArrays = new Class[];
void setup() {
Sets up the simulation to run;
size(1200, 700);
}
void draw() {
background(255, 200, 200);
Runs Simulation;
Collects data;
}
This runs fine. What I would like to do is run this program multiple times to gather some statistics.
I can't figure out how to do this. I want to essentially put this whole code into a loop, and collect the data it creates for each iteration, possibly running it thousands of times. I've tried just that, but it breaks the program. Any suggestions?
You can do this with a couple simple steps, some of which may require some refactoring:
Determine the conditions which define the end of the simulation.
Set un a method which will run only once the simulation is done. We'll call it Reset() to make things easier.
In Reset(), make sure that you reinitialize any global variables which is initialized at their creation. Set them back to their initial value.
In Reset(), run setup().
Let nature follows it's course, your application has been tricked into beginning anew.
Of course, you may want to organize your code so the information you gather won't be erased, whether by saving it to a different file/appending it to a file at every time you run a new simulation, or by preserving it in a global variable which won't be reset. I don't have enough details to elaborate on this front, but I think you'll understand the idea I'm advancing.
Have fun!
Related
I have a sample code :
#include<iostream>
main()
{
int i = 10; //Line 1
std::cout<<" i : "<<i; //Line 2
}
I want to somehow insert another statement (lets say one more std::cout) between Line-1 and Line 2.
Direct way is to change the source code and add required line. But for my source code compilation takes lot of time, so i can't afford to change the code frequently. So i want an automatic way such that i will be able to execute any statement in any function from outside so that upon hitting that function it execute my newly given statement.
I am not sure if this is possible or not. But any direction in writing the original code in such a way that my requirement can be fulfilled would be helpful.
[for debugging prupose]
If you want new C++ code to be executed, it must first be compiled. I don't think you can avoid that. You can however try to reduce how long the compilation takes, through various practices such as using header guards and being selective with headers.
There is a lot you can do in gdb to modify the behaviour of your program when it hits a nonstop breakpoint. The print command can also be used to change values, eg print i=0 actually sets i to zero.
Just remember that all these changes and hacks need to be ported back into the source code and tested again! I have lost many excellent edits over the years doing inline hacks in running code, and then exiting without reviewing the changes.
Reading all the compiled vs interpreted articles it seems like compiled means the machine will run the compiled code directly whereas interpreted, the interpreter will run the code. But how does the interpreter run the code if it's on a machine? Doesn't it still end up having to convert what it's interpreting into machine code and STILL having the machine run it? At the end of the day, everything has to end up being machine code in order for the machine to run it right? It seems like interpreted just means that it's running through the language one line at a time whereas compiled means going thru it all at once. After that, it's pretty much the same right?
Related: How programs written in interpreted languages are executed if they are never translated into machine language?
No, it doesn't need to convert it to a machine code. The instructions merely provide instructions to the interpreter itself, which the interpreter then executes itself.
Consider a really dumb "language" that consists of the following instructions:
add [number]
subtract [number]
divide [number]
multiply [number]
We could implement an "interpreter" like this (written in C#):
public static void ExecuteStatements(List<string> instructions)
{
int result = 0;
foreach (string instruction in instructions)
{
string[] action = instruction.Split(' ');
int number = int.Parse(action[1]);
switch (action[0].Trim().ToLower())
{
case "add":
result += number;
break;
case "subtract":
result -= number;
break;
case "divide":
result /= number;
break;
case "multiply":
result *= number;
break;
}
}
Console.WriteLine("Result: " + result);
}
The ExecuteStatements method will be compiled to machine code. Separately, we have a text file like this:
add 1
subtract 1
add 10
multiply 50
divide 5
The result will be 100. The strings are never actually compiled to anything - they just tell the interpreter what actions to take.
Obviously, this "language" isn't even close to Turing-complete, but the point is that at no point are we somehow "translating" this into machine code - the "interpreter" just takes whatever action is specified.
I actually wrote an interpreter once as part of a Test Automation framework. When someone did a call against the API, we would intercept the call, use reflection on it to determine what the call was and what the parameters were, and serialize the reflection metadata to JSON. We then later deserialized the JSON and used reflection to call whatever methods had been run before with the same parameters. The engine consumed JSON, not machine code, and used that to figure out what calls it should perform; at no point did it create or need any machine code. The critical thing was that, assuming that it had been given a valid script, the engine "knew" how to perform all of the specified actions itself, so it never needed to "offload" any of the instructions to the machine itself.
Here's the key insight: the interpreted code itself is quite literally doing nothing - all it's doing is feeding the interpreter which actions it needs to take. The interpreter already "knows" how to take all of the actions you can perform in the interpreted language, so no additional machine code is required.
As an analogy, think of the code you're interpreting as a recipe and the interpreter as a cook. The recipe specifies actions like "add 1 cup of flour and mix." The cook knows how to follow whatever directions he finds in the recipe and he performs them himself. Strictly speaking, the recipe isn't actually doing anything - it's just sitting there for the cook to read so that the cook can know what actions to take. There's no need for the recipe to actually be a cook in order for the recipe to be completed - it just needs someone who knows how to follow its directions.
TL;DR You don't need to "translate" it into machine code - you just need to have enough information for your interpreter to know what actions to take. A good interpreter already "knows" how to take whatever actions the language could implement, so there's no need to create any additional machine code.
Assume I have a function which is called often, say by an ODE-solver or similar. Is it faster to use a persistent variable than to reallocate it each time?
That is, which function would be faster and what is best practice?
function ret=thisfunction(a,b,c)
A = zeros(3)
foo = 3;
bar = 34;
% ...
% process some in A
% ...
ret = A\c;
end
or
function ret=thatfunction(a,b,c)
persistent A foo bar
if isempty(A);
A=zeros(3);
foo = 3;
bar = 34;
end
% ...
% process some in A
% ...
ret = A\c;
end
Which one is faster can only be proven by test, as it may depend on variable size etc. However, I would say that if it is not required, it is usually also not recommended to use persistent variables.
Therefore I would definately recommend you to use option number one.
Sidenote: You probably want to check whether it exists rather than whether it is empty. Furthermore I don't know what happens to your A when you leave the function scope, if you want to define it as persistent or global you may have to do it one level higher.
When you have a single function such as this to test I have found that it's very easy to setup a parent function, run the function you are testing say, 10 million times and time the results. Then consider the difference in time AND the possible trade off or side effects of using a persistent variable here. It may not be worth it if the difference is a few percent over 10 million calls and you are actually only going to call the function 10 thousand times in application. YMMV.
In regards to best practice, I would dissuade you from using persitent variables in this manner, for two reasons.
Persitent variables can be cleared externally, e.g. running clear('thatfunction') from any other function that has "thatfunction" on the path would reset your persitent variables in "thatfunction". As such, it's possible that they'll be unwittingly reset elsewhere. This may not be a problem for you in this context, but if you want to keep results between function calls (which is the primary point of persitent variables) this can cause you headaches.
Also, if you modify them, you'll have to remember to clear them when you're done running in-order to reset your workspace to a clean state. Otherwise if you (or someone else) runs your program again without clearing your persitent variable(s) first, the results from the previous run. This isn't an issue if they're read-only, but you cannot enforce that they will be.
I am wondering if any of you guys know why my performance is terrible;
What I am trying to achieve;
Generate 2.2 million files. To create each file, 2-5 databasecalls are needed on average.
The server I am working on has 24 cores and 190GB of RAM.
I divided the files I need to generate in 24 batches.
Whey I use following code, I get lousy performance. The generation process takes over an hour.
Parrallel.ForEach(batches, batch =>
{
using (var ctx = new MyContext())
{
for each(var file in batch.Files)
{
GenerateFile(file);
}
}
});
However, when I make sure that my program receives a parameter so the progam knows which batch to generate so I don't need to use the parallel functionality. If I execute the program for each batch with the following .bat file;
START CaMaakEiBericht.exe \B1
START CaMaakEiBericht.exe \B2
...
START CaMaakEiBericht.exe \B24
It runs amazingly fast! The total generation process takes less than 15 minutes!
This batch file also makes sure that each core has a cpu usage around 90%. When I use the Parallel approach, I only get 30-40% usage.
Does someone have a logical explanation for this? I was pleased with this project because I finally had the possibility to use the .NET 4 Parallel library in combination with EF but unfortunately, it kinda disappointed me :-)
I personally have a slight suspision that EF is the bottleneck here... Does it cache some stuff internally which imposes some locks when multiple processes are fetching data?
Enlighten me :-)
I can't speak as to why your other EXE file works well, but I can offer a suggestion for the code that you present.
You mentioned that you split your work up into 24 batches, then you used ForEach over the list of batches. With this setup, it would seem that each of our 24 cores can be working on 1 file at a time. My guess is that is your bottleneck.
Each core could be doing a lot more if you let it. Try something like this:
Parallel.ForEach(batches, batch =>
{
Parallel.ForEach(batch.Files, file =>
{
using (var ctx = new MyContext())
{
GenerateFile(file);
}
}
});
Or you could just get rid of the batches entirely and give it the full list of files. The task parallel library will take care of using multiple cores for you.
Parallel.ForEach(Files, file =>
{
using (var ctx = new MyContext())
{
GenerateFile(file);
}
});
You probably already know this, but keep in mind that the context is not thread safe, so you have to create a new one inside the inner-most Parallel.ForEach structure.
I have a Simulink model that uses an embedded MATLAB function for a block, and I haven't been able to figure out how to move data between the embedded MATLAB block and a GUI in real-time (i.e. while the model is running). I tried to implement a "to workspace" block in my model but I don't know how to correctly use it.
Does anyone know how to move data from a Simulink block into a GUI in real-time?
Non-real-time solution:
If you want to set parameters in a GUI, simulate a model with those parameters, and then display the simulation output in the GUI, there is a good tutorial on blinkdagger.com. One solution they describe is using the SIMSET function to define which workspace the Simulink model interacts with. You should be able to supersede the base workspace so that data is instead sent to and from the workspace of the GUI functions that are calling the Simulink model.
Real-time solution
As suggested by MikeT, you can use a RuntimeObject. You first have to use the get_param function to get the RuntimeObject from the block:
rto = get_param(obj,'RuntimeObject');
Where obj is either a block pathname or a block-object handle. You can get the pathname of the most recently selected block using the GCB function (in which case you can replace obj with gcb). You can then get the block's output with the following:
blockData = rto.OutputPort(1).Data
One additional caveat from the documentation:
To ensure the Data field contains the
correct block output, turn off the
Signal storage reuse option (see
Signal storage reuse) on the
Optimization pane in the Configuration Parameters dialog box.
You would likely end up with a loop or a timer routine running in your GUI that would continuously get the output data from the RuntimeObject for as long as the simulation is running. The documentation also states:
A run-time object exists only while
the model containing the block is
running or paused. If the model is
stopped, get_param returns an empty
handle. When you stop or pause a
model, all existing handles for
run-time objects become empty.
Your loop or timer routine would thus have to keep checking first that the RuntimeObject exists, and either stop (if it doesn't) or get the data from it (if it does). I'm unsure of exactly how to check for existence of a RuntimeObject, but I believe you would either check if the object is empty or if the BlockHandle property of the object is empty:
isempty(rto) % Check if the RuntimeObject is empty
%OR
isempty(rto.BlockHandle) % Check if the BlockHandle property is empty
From your responses, I'm guessing you want to see the results while the simulation is running, is that correct? The blinkdagger.com tutorial lets you view the results of a simulation after it is done, but not while it is running. Do you basically want to embed something like a scope block into your GUI?
There's a few ways to do this, the best is probably using the EML block's runtime object. If you use this, you should be able to look at the output of the EML block while it is running.