Addition of STOP statement changes output value - linux

A value obtained for a (double precision) variable is written to the screen as part of my Fortran program. Following the write statement, the program continues uninterrupted.
However, when I add a stop statement immediately after the write, I get a different (screen printed) value for this variable. The change in values begins at the 6th significant digit.
The code includes:
enertot=energy0(x,y,z,size) !energy0 is a function
write (*,*) 'The initial energy is', enertot
This outputs some (plausible) value for enertot on the screen, and the program goes on.
Now adding the stop:
enertot=energy0(x,y,z,size) !energy0 is a function
write (*,*) 'The initial energy is', enertot
stop
This gives me a different value for enertot.
The same problem happens regardless of the compiler I use (f90/95 compilers). Could it have to do with the machine being very old, operating on an out of date Linux Fedora OS?
Even weirder - when I run the exact same program on my Windows laptop with the Silverfrost compiler, I get a third result for enertot altogether, differing from the previous results starting from the 5th significant digit. In this case, however, addition of the stop doesn't change the printed value at all.
Any thoughts?

Related

ENTER keyword in Basic

I am going over some ancient 1975 Basic code and found what looks like a keyword in the line:
ENTER #P,B2,B1,C$.I know that B2, B1 and C$ are all variables, and though it would not make sense P may be a variable also.Does anyone know what the ENTER keyword is and what these parameters refer to?Thanks in advance.
The ENTER statement is extraordinarily obscure; it does not appear in either the first or third editions of David Lien’s BASIC Handbooks, for example.
By searching on “ENTER #P” I was able to find one BASIC variant that does include it: HP 2000/Access BASIC. This particular manual is dated September 1975, so it fits loosely with the date of the program you’re looking at.
In HP 2000/Access BASIC (pages 2-15 and 11-33 of the manual), the ENTER statement is a variation of INPUT, one that provides no prompt and also stores more information about how and where the input was provided. The significance of the pound sign (#) is that it assigns the user port number to that variable. In your case, the “user port number” is assigned to the variable P.
In your example:
P will be assigned the user port number.
B2 is the number of seconds the user has to respond. If the user does not respond, the value returned will be -256; -257 and -258 are error conditions.
B1 is the number of seconds the user actually took to respond.
C$ is the variable which will be assigned the value the user enters, if they enter it in the appropriate time.
The “user port number” does not appear to be defined in the manual; however, contextually it appears to be the port on which the user is logged into the system. It can be a number “in the range from 0 to 31.” The port number appears to be mainly, or only, used for logging purposes. I don’t see anywhere in the manual where the port number can be targeted by the BASIC program.
From the manual:
The ENTER statement provides the program with more control over the input operation. The statement can limit the amount of time allowed to respond with data, provide the program with the actual time taken to respond, indicate whether the data was acceptable, and return the port number of the user's terminal. The port number can be obtained separately, without involving the user at all, or together with a single data value. Data can also be requested without asking for the port number. If a number sign (#) follows the keyword ENTER, then the variable it precedes is assigned the user port number (an integer in the range 0 to 31). Otherwise (or following the port return variable) the first expression is evaluated and rounded to an integer (which must be in the range 1 to 255) specifying the number of seconds permitted the user to respond. No prompt is printed, the program must notify the user that input is expected by a message in a preceding PRINT statement. The variable following the expression is set to the approximate time, in seconds, that the user took to respond. If the constant was not legal, the time is negated. If the allotted time has elapsed, the value -256 is returned; the values -257 and -258 indicate a transmission problem occurred and the user should be asked to respond again. The last variable in the list is assigned the single value which the user is expected to enter. Unlike the INPUT statement, ENTER does not respond to the return (which completes the user's answer) with a line feed.
Examples:
10 ENTER #P
20 ENTER 255,R,A
30 ENTER #P,T1,R,A(I)
Fortunately P is the variable used for the port number in this example as it is in yours; otherwise, the search would not have found this manual.

How to properly run memory-profiler in Jupyter Notebook

I am trying to run a simple memory profiling in Jupyter Notebook (see Environment below) on macOS Catalina (10.15.2). The code (taken from here) is as follows:
def mess_with_memory():
huge_list = range(200)
del huge_list
print("Complete" )
This is how I call the profiler and the resulting profile (the 'importlib.reload' is not called the first time only for subsequent runs if I change the module):
What I expected to see was the 'Increment' column beginning with '0' and then having increasing then decreasing values line by line just like here. Instead the 'Increment' column starts at a certain value and subsequent values for each line are zero. In the instance shown the value for range is very low but it doesn't matter if I increase it to a very much higher value, the result after a kernel restart is roughly the same. If I don't restart the kernel and rerun repeatedly, the topmost value of the 'Increment' column increases.
I'm guessing this is because I'm running in Jupyter but the references I found here indicate I should be able to do this. Can anyone explain what might be happening or point me to where I can find that out?
Environment:

keep the last value of a variable for the next run

x=0
for i in range(0,9):
x=x+1
When I run the script second time, I want x to start with a value of 9 . I know the code above is not logical. x will get the value of zero. I wrote it to be as clear as I can be. I found a solution by saving x value to a txt file as it is below. But if the txt file is removed, I will lose the last x value. It is not safe. Is there any other way to keep the last x value for the second run?
from pathlib import Path
myf=Path("C:\\Users\\Ozgur\\Anaconda3\\yaz.txt")
x=0
if myf.is_file():
f=open("C:\\Users\\Ozgur\\Anaconda3\\yaz.txt","r")
d=f.read()
x=int(d)
else:
f=open("C:\\Users\\Ozgur\\Anaconda3\\yaz.txt","w")
f.write("0")
deger=0
for i in range(0,9):
x=x+1
f=open("C:\\Users\\Ozgur\\Anaconda3\\yaz.txt","w")
f.write(str(x))
f.close()
print(x)
No.
You can't 100% protect against users deleting data. There are some steps you can take (such as duplicating the data to other places, hiding the file, and setting permissions), but if someone wants to, they can find a way to delete the file, reset the contents to the original value, or do any number of things to manipulate the data, even if it takes one unplugging the hard drive and placing it in a different computer.
This is why error-checking is important, because developers cannot make 100% reliable assumptions that everything in place is there and in the correct state (especially since drives do wear down after long periods of time causing odd effects).
You can use databases, they are less likely to be lost than files. You can read about MySQL using Python
However, this is not the only way you can save the value of x. You can have an environment variable in your operating system. As an example,
import os
os.environ["XVARIABLE"] = "9"
To access this variable later, simply use
print os.environ["XVARIABLE"]
Note: On some platforms, modifying os.environ will not actually modify the system environment either for the current process or child processes.

Variable length messages in Verilog (serial CRC-32)

I'm working with a serial protocol. Messages are of variable length that is known in advance. On both transmission and reception sides, I have the message saved to a shift register that is as long as the longest possible message.
I need to calculate CRC32 of these registers, the same as for Ethernet, as fast as possible. Since messages are variable length (anything from 12 to 64 bits), I chose serial implementation that should run already in parallel with reception/transmission of the message.
I ran into a problem with organization of data before calculation. As specified here , the data needs to be bit-reversed, padded with 32 zeros and complemented before calculation.
Even if I forget the part about running in parallel with receiving or transmitting data, how can I effectively get only my relevant message from max-length register so that I can pad it before calculation? I know that ideas like
newregister[31:0] <= oldregister[X:0] // X is my variable length
don't work. It's also impossible to have the generate for loop clause that I use to bit-reverse the old vector run variable number of times. I could use a counter to serially move data to desired length, but I cannot afford to lose this much time.
Alternatively, is there an operation that would directly give me the padded and complemented result? I do not even have an idea how to start developing such an idea.
Thanks in advance for any insight.
You've misunderstood how to do a serial CRC; the Python question you quote isn't relevant. You only need a 32-bit shift register, with appropriate feedback taps. You'll get a million hits if you do a Google search for "serial crc" or "ethernet crc". There's at least one Xilinx app note that does the whole thing for you. You'll need to be careful to preload the 32-bit register with the correct value, and whether or not you invert the 32-bit data on completion.
EDIT
The first hit on 'xilinx serial crc' is xapp209, which has the basic answer in fig 1. On top of this, you need the taps, the preload value, whether or not to invert the answer, and the value to check against on reception. I'm sure they used to do all this in another app note, but I can't find it at the moment. The basic references are the Ethernet 802.3 spec (3.2.8 Frame check Sequence field, which was p27 in the original book), and the V42 spec (8.1.1.6.2 32-bit frame check sequence, page 311 in the old CCITT Blue Book). Both give the taps. V42 requires a preload to all 1's, invert of completion, and gives the test value on reception. Warren has a (new) chapter in Hacker's Delight, which shows the taps graphically; see his website.
You only need the online generators to check your solution. Be careful, though: they will generally have different preload values, and may or may not invert the result, and may or may not be bit-reversed.
Since X is a viarable, you will need to bit assignments with a for-loop. The for-loop needs to be inside an always block and the for-loop must static unroll (ie the starting index, ending index, and step value must be constants).
for(i=0; i<32; i=i+1) begin
if (i<X)
newregister[i] <= oldregister[i];
else
newregister[i] <= 1'b0; // pad zeros
end

Emulation: Unconditional jumps and PC increment through CPU cycles

I'm writing a simple GB emulator (wow, now that's something new, isn't it), since I'm really taking my first steps in emu.
What i don't seem to understand is how to correctly implement the CPU cycle and unconditional jumps.
Consider the command JP nn (unconditional jump to memory adress pointed out), like JP 1000h, if I have a basic loop of:
increment PC
read opcode
execute command
Then after the JP opcode has been read and the command executed (read 1000h from memory and set PC = 1000h), the PC gets incremented and becomes 1001h, thus resulting in bad emulation.
tl;dr How do you emulate jumps in emulators, so that PC value stays correct, when having cpu loops that increment PC?
The PC should be incremented as an 'atomic' operation every time it is used to return a byte. This means immediate operands as well as opcodes.
In your example, the PC would be used three times, once for the opcode and twice for the two operand bytes. By the time the CPU has fetched the three bytes and is in a position to load the PC, the PC is already pointing to the next instruction opcode after the second operand but, since actually implementing the instruction reloads the PC, it doesn't matter.
Move increment PC to the end of the loop, and have it performed conditionally depending on the opcode?
I know next to nothing about emulation, but two obvious approaches spring to mind.
Instead of hardcoding PC += 1 into the main loop, let the evaluation if each opcode return the next PC value (or the offset, or a flag saying whether to increment it, or etc). Then the difference between jumps and other opcodes (their effect on the program counter) is definable along with everything else about them.
Knowing that the main loop will always increment the PC by 1, just have the implementation of jumps set the PC to target - 1 rather than target.

Resources