Whether I did it right or wrong in 3-address code? - performance

Today, I'm done with my Compiler Construction finals paper at university.
The finals paper included a question that asked me to convert a for loop into 3-address code.
The function it asked me to convert was:
for(i=1;i<=10;i++) x=y+z
So, I did loop unrolling and converted the given statements to the equivalent expression:
x=(y+z)^10
Then, I made 3-address code of the converted code:
Please let me know if it is correct.

Your converted code is wrong.
In the original, x is not dependent on past versions of x making the for loop dead code and loop unrolling useless as #Peter Cordes stated. If your looking for the correct answer, the correct non-optimized answer would be:
(0) i=1
(1) if(i > 10) goto 5
(2) x=y+z
(3) i=i+1
(4) goto 1
(5) end
While, the correct optimized answer would be:
(0) x=y+z
(1) end

Related

Julia - Using JuMP Triangular Matrix Variables in JuMP causes bad type performance

On the next MWE, #code_warntype returns bad performance type, ##1469::JuMP.Containers.SparseAxisArray.
using JuMP, Gurobi
function MWE(n)
m = Model(Gurobi.Optimizer)
#variable(m, x[i=1:n, j=i+1:n], Bin)
#variable(m, y[i=1:n], Bin)
end
#codewarn_type MWE(5)
While the adapted version where j goes from 1 to n instead of i+1 to n is totally great for #codewarn_type.
function MWE_codewarntype_safe(n)
m = Model(Gurobi.Optimizer)
#variable(m, x[i=1:n, j=1:n], Bin)
#variable(m, y[i=1:n], Bin)
end
#codewarn_type MWE(5)
However, I can't allow my model to have nearly twice more variables and more than half unused. I ran both code with larger instances and the performances quickly deteriorate. Does this mean I should ignore what #code_warntype tells? If so, that's not the first time I would have to ignore it and I find it particularly unclear how to understand when #codewarn_type returns are meaningful. Maybe I should ask a more general question about this macro, how to read and understand it?
Hmm. I thought we fixed this. Note that x is a concrete type, so this is just a failure of Julia's inference. It also means that when x is passed to another function (e.g., add_constraint), it will be fast.
Edit: opened an issue to discuss: https://github.com/jump-dev/JuMP.jl/issues/2457
Here's the MWE:
using JuMP
function MWE(n)
model = Model()
#variable(model, x[i=1:n, j=i+1:n])
end
#code_warntype MWE(5)
The question to ask is: is the time difference material? If it's only marginally faster, I would go with the more readable version.

How to save output of for loop operation in matlab

I have a matrix A which has a size of 54x100. For some specific condition I perform an operation on each row of A. I need to save the output of this for loop. I've tried the following but it did not work.
S=zeros(54,100);
for i=1:54;
Ri=A(i,:);
answer=mean(reshape(Ri,5,20),1);
S(i)=answer;
end
Firstly, judging by your question I'd recommend some basic Matlab tutorials like this or just detailed documentation like this.
To actually help you with your issue though; you can do this:
%% Make up A (since I don't know what it actually is)
n = 54; m = 100;
A = randn(n,m); % N x m matrix of random numbers
%% Loop over each row of A
S = cell(n,1);
for j = 1:n;
Rj = A(j,:); % j'th row
answer = mean(reshape(Rj,5,20),1); % some operation
S{j} = answer; % store the answer in cell S
end
The problem was that your answer was not a single number (1x1 matrix) but a vector and so you got a dimension mismatch error. Above I'm putting the answers into a cell object of size n. The result of your operation on j'th row can then be retrieved by calling S{j}.
Also:
Do not using i as an iterator since it also represents the imaginary unit.
Do not hard-code values but reference the existing ones. For example here I referenced n in the for-loop declaration as opposed to just writing for j = 1:54 because otherwise, if I got struck by a fancy to use my code for a 53x100 array it would not work anymore.
When you post your code I reccomend adding a minimal working example - a pece of code which people can just copy and paste into their Matlab (or whatever interpreter of whatever language) and run to reproduce your problem. Here you have not included anything which tells the code what A is, for example.
This is quite a good read in general and should help you in the future

diophantine analysis in maxima

I have defined an extended Euclidean algorithm in Maxima as
ext_euclid(a,b):=block(
[x,y,d,x_old,y_old,d_old],
if b = 0 then return([1,0,a])
else ([x_old,y_old,d_old]:ext_euclid(b,mod(a,b)),
[x,y,d]:[y_old,x_old-quotient(a,b)*y_old,d_old],
return([x,y,d])));
in order to solve linear Diophantine equations of the form a+b=c where gcd(a,b)=1, however if a-b=c I get -1 returned by the algorithm for the divisor but gcd(a,-b) as before. Is my algorithm wrong, or can it be used for a-b=c?
Iain
I don't quite understand what the problem is. Can you please give two examples, one in which the result matches what you expected, and one in which it doesn't (and please say what's your expected result in that case).
EDIT: OP replies: "to solve 5x+7y is 23 ext_euclid (5,7) returns [3,-2,1] where gcd(5,7) is 1 however for 5x-7y is 23 ext_euclid(5,-7) returns [-3,1,-1] but gcd(5,-7) is still 1 this fails Bezout's identity ax+by is gcd(a,b)"
Also if you are trying to implement a particular statement of the algorithm, can you please link to it or copy it here.
OP replies: "code at http://mvngu.wordpress.com/2009/08/25/elementary-number-theory-using-maxima/"
One possible thing to look at: does the mod function behave as you expect it?
OP replies: "mod(5,7) is, mod(5,-7) is -2"

suggestions on constructing nested statements properly

I am having trouble constructing my own nested selection statements (ifs) and repetition statements (for loops, whiles and do-whiles). I can understand what most simple repetition and selection statements are doing and although it takes me a bit longer to process what the nested statements are doing I can still get the general gist of the code (keeping count of the control variables and such). However, the real problem comes down to the construction of these statements, I just can't for the life of me construct my own statements that properly aligns with the pseudo-code.
I'm quite new to programming in general so I don't know if this is an experience thing or I just genuinely lack a very logical mind. It is VERY demoralising when it takes me a about an hour to complete 1 question in a book when I feel like it should just take a fraction of the time.
Can you people give me some pointers on how I can develop proper nested selection and repetition statements?
First of all, you need to understand this:
An if statement defines behavior for when **a decision is made**.
A loop (for, while, do-while) signifies **repetitive (iterative) work being done** (such as counting things).
Once you understand these, the next step, when confronted with a problem, is to try to break that problem up in small, manageable components:
i.e. decisions, that provide you with the possible code paths down the way,
and various work you need to do, much of which will end up being repetitive,
hence the need for one or more loops.
For instance, say we have this simple problem:
Given a positive number N, if N is even, count (display numbers) from
0(zero) to N in steps of 2, if N is odd, count from 0 to N in steps of
1.
First, let's break up this problem.
Step 1: Get the value of N. For this we don't need any decision, simply get it using the preferred method (from file, read console, etc.)
Step 2: Make a decision: is N odd or even?
Step 3: According to the decision made in Step 2, do work (count) - we will iterate from 0 to N, in steps of 1 or 2, depending on N's parity, and display the number at each step.
Now, we code:
//read N
int N;
cin<<N;
//make decision, get the 'step' value
int step=0;
if (N % 2 == 0) step = 2;
else step = 1;
//do the work
for (int i=0; i<=N; i=i+step)
{
cout >> i >> endl;
}
These principles, in my opinion, apply to all programming problems, although of course, usually it is not so simple to discern between concepts.
Actually, the complicated phase is usually the problem break-up. That is where you actually think.
Coding is just translating your thinking so the computer can understand you.

How to picture "for" loop in block representation of algorithm

I have probem / strange question, i got algorithm with few "for" loops and now i need to do block scheme of this algorithm.
I know how to picture "while" loop, but is this acceptable to represent "for" loop using "while" and at this point make difference between souce code and algorithm?. Ofcourse assuming that all "for" loops are right in place and using loop of any other kind would produce unnecessary code which i avoided using "for" loops.
I'm guessing that this is rather impossible (at least I can't imagine this) to simply picture "for" loop, but maybe there is a way (if exists).
Thanks in advance
Here's a flow chart that illustrates a for loop:
The equivalent C code would be
for(i = 2; i <= 6; i = i + 2) {
printf("%d\t", i + 1);
}
I found this and several other examples on one of Tenouk's C Laboratory practice worksheets.
What's a "block scheme"?
If I were drawing it, I might draw a box with "for each x in y" written in it.
If you're drawing a flowchart, there's always a loop with a decision box.
Nassi-Schneiderman diagrams have a loop construct you could use.
The Algorithm for given flow chart :
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Step :01
Start
Step :02
[Variable initialization]
Set counter: i<----K [Where K:Positive Number]
Step :03[Condition Check]
If condition True then Do your task, set i=i+N and go to Step :03 [Where N:Positive Number]
If condition False then go to Step :04
Step:04
Stop

Resources