max/msp: random choice from coll depending on its length - random

A varying number of lists are stored in a coll object. Then a series of bangs is provided to a random integer generator. With each bang the generated integer will decide which list will go out of the coll. Obviously, this number must be between 1 and the varying length of the coll. This means that the RND generator has to take into account any changed length of the coll before generating the integer. So, I guess we have a circuit like the following:
A loadbang-ed message ‘length’ must enter the coll object to make it output its length.
This length number must get into the right inlet of a ‘between’ (RND gen.) object to set its maximum.
A bang in the left input of the ‘between’ object will generate a random integer.
The integer will go back into the coll and make it output the corresponding stored list.
The output list will be sent to an iteration mechanism that will read and output its atoms one by one.
When the last atom of the current list is output, a new bang will be sent to the ‘between’ object so that a new list will be picked and the process will be repeated (stages #3 – 6).
The problem with the above process is that it stumbles on the functionality of the coll object: a coll outputs both its length and any stored data from the same outlet (why, I wonder?), but in processes like the above, the length of the coll must be output only once (in the beginning of the session) and it should only feed the ‘between’ object (it should never reach the iteration mechanism, since it is irrelevant). Therefore the routes of the coll's length and its data must be different, although they are all integers and they are all coming out of the same outlet.
Any ideas please?

Have you investigated the grab object? It's wiring is a bit counterintuitive, but it's a useful object for many things, including acting as a 'circuit breaker' in the kind of situation you're describing.
Setting up a grab object enables you to direct a message to an object's inlet, and then collect only the output associated with that message, without affecting any other patchcords that may be connected to the same outlet.

I posted a reply with a solution I knocked up for you but a moderator deleted it.
So I've taken a screen shot, and hopefully that helps.
Essentially you can have loads of different messages (adding entries, selecting entries, deleting, renumbering...) go into coll, and just the ones coming out of coll go through the 1-in-2-out switch.
The default setting for the switch would be to route the output of coll to a multislider, or message box or where ever.
However in the case you want to capture the length output from the coll, use:
[trigger 0 length 1] - 0 and 1 go to the left input of the switch, and 'length' goes to the coll.
first send the switch a 1 to route the coll output to the place you need it to go
then send the coll the length prompt - it'll pass out the second output of the switch
then close the gate with a 0 - routing coll's output back to it's normal place
Any time you need the length updated (like after adding or deleting an entry) you just send that trigger module a bang.

I solved this over the last few days by using a gswitch2 and a trigger object to open and close it either side of the message: [t 0 length 1]
The coll is plugged into the gswitch so it's messages are routed to the length functions just for that moment they're needed, and otherwise routed to the objects that need the coll contents.
-exeterdown

Related

How to check if user input fits in variable?

I'm trying to write a simple program to calculate a function with Fortran95/03 which needs as input a number(x) and gets as output a number(y).
The user input is a real :: input and the read call looks like
read (*,*, iostat=stat) input
if(stat > 0) then
print *, "False input, use numbers!"
The iostat helps me to check if the input was a number or a letter.
My problem is that if I enter a very big number, like 1000000000000, the program crashes with the error message "bufferoverflow". I know that I can make the real bigger than a 4 byte variable, but I also can make the input number bigger, so this does not solve the problem.
The main question is, is it possible to prevent the program crashing because of user input?
Checking the values of the user's input is a very basic technique which must be employed in all kinds of software which interacts with someone else than just the author. It is used in all programming languages.
You can just use a simple condition
if (input > input_max) then
print *, "Input value too large, try again"
cycle ! or return stop or set some flag or whatever
end if
Don't forget the value may be also too small!
It is important to understand where does the crash come from. It certainly does not come from just from inputting a large number but using the number in a bad way, for example, allocating an array which is too large or by making a calculation which triggers a floating point exception.
Read the input as a string, then validate the string input, then use an internal read to convert the validated string into a REAL.
There are many aspects of processor dependent behaviour to input and output, as a general principle if you want robustness then you need to do much of the leg work yourself. For example, if malformed input for a real is provided, then there is no requirement that a processor identify that as an error condition and return a non-zero IOSTAT code.
List directed input offers further challenges, in that it has a number of surprising features that may trip you and your users up.

why is the length of block after the block

I'm extracting data from a binary file and see that the length of the binary data block comes after the block itself (the character chunks within the block have length first then 00 and then the information)
what is the purpose of the the block? is it for error checking?
Couple of examples:
The length of block was unknown when write operation began. Consider audio stream from microphone which we want to write as single block. It is not feasible to buffer it in RAM because it may be huge. That's why after we received EOF, we append effective size of block to the file. (Alternative way would be to reserve couple of bytes for length field in the beginning of block and then, after EOF, to write length there. But this requires more IO.)
Database WALs (write-ahead logs) may use such scheme. Consider that user starts transaction and makes lots of changes. Every change is appended as single record (block) to WAL. If user decides to rollback transaction, it is easy now to go backwards and then to chop off all records which were added as part of transaction user wants to rollback.
It is common for binary files to carry two blocks of metainformation: one block in the beginning (e.g. creation date, hostname) and another one in the end (e.g. statistics and checksum). When application opens existing binary file, it first wants to load these two blocks to make decisions about memory allocation and the like. This is much easier to load last block if its length is stored in the very end of file rather then scanning file from the beginning.

COMPARE AND WRITE on umapped block?

The COMPARE AND WRITE command description in the SBC-4 doesn't say anything about the case when the range of logical blocks to be replaced contains unmapped blocks.
What's the common practice to deal with this case on the target side? Should a target assume that the verification step is always successful in the case when an initiator asks to replace unmapped blocks with something meaningful?
When a block is unmapped it is equal to all zeroes. You should perform all your commands that read or compare data with this block as it was just zeroes.

MPI file view and IO

I am writing an MPI program that needs to read a part of a file into memory, one piece at a time, with each piece going to an available process. I am therefore using a shared filepointer. The first part of the file is a header which I want to read to read and distribute to all processes, I have managed to do this by reading it on the master process and broadcasting it to all other processes.
The next part of the file is a long (in theory up to several gigabytes) array of float triples. I want to set the fileview for all the processes so that it starts at the beginning of this array, and each process should be able to see the whole array. Furthermore, and this is my real problem, I do not want the processes to see beyond this array, so that after they encounter the last set of 3 floats they report EOF. So in practice each process just sees one long 3-float array and nothing else.
After the header has been read this is my code:
MPI_Datatype particle_type;
MPI_Type_contiguous(3,MPI_FLOAT,&particle_type);
MPI_Type_commit(&particle_type);
MPI_Offset cur_file_pos;
MPI_File_get_position_shared(fh,&cur_file_pos);
MPI_File_set_view(fh, cur_file_pos, particle_type, particle_type, (char *) "native", MPI_INFO_NULL); /* fh is the file-handle from MPI_File_open */
As I understand, this simply skips the header, but the file view does not stop after the array, it continues into the next part of the file which I am not interested in. Can anyone help me with this simple problem? I have not been able to find any thorough explanations (with examples) of file views anywhere.
Unfortunately, MPI_File_set_view won't do this for you; once you go beyond the filetype the filetype repeats. While MPI_File_set_view will allow you to partition the view of the file between processes, it won't let you "truncate" the view of the file like this.
If you're using the shared file pointer, presumably the simplest thing to do is to loop until the new position == number of particles (once the view is set, the file pointer is in units of etypes).

How to check whether the FrontEnd considers evaluation still running?

Is there a way to check programmatically whether the FrontEnd considers evaluation still running?
Or even better: is there a way to check whether the FrontEnd has some pending inputs to be sent to the kernel?
P.S. This question has arisen from previous question.
EDIT
When evaluating a Cell in the FrontEnd we usually create a queue of inputs for the kernel.
I need a function that will return True if the FrontEnd has sent to the kernel the last input of the queue of inputs from the EvaluationNotebook[]. Or in other words I need a function that returns True if this current input is the last input of the queue of inputs generated by the FrontEnd.
This should work. Of course, you have to run it in a different kernel than the one that is performing the evaluation you want to check for.
NotebookEvaluatingQ[nb_] := (
SelectionMove[nb, All, Notebook];
Or ## Map["Evaluating" /. # &, Developer`CellInformation[nb]]
)
Obviously, it's best to set things up before hand using a tool like Monitor. For example,
Monitor[
Do[Pause[6], {i, 10}],
i]
will allow you to observe the progress of the index variable i. If you haven't set things up before hand, you might be able to do something using the "Interrupt Evaluation" button under the Evaluation menu. For example, try the following:
Do[Pause[6], {i, 10}]
Now, wait six or more seconds and then select "Interrupt Evaluation". You can then examine the state of i to see how far along it is. You resume evaluation using Continue under "Debugger Controls".

Resources