Verilog dataflow delay model - verilog

I am reading about Verilog data-flow programming.
I have learned about delays in data-flow model but now I have some misunderstandings about it. I found that in data-flow model, we have rejection delay model. In other words, for assign #2 c= a | b, changes can be rejected.
My question is when will the input changes be rejected?
I am sure that when a or b change so that c expected value changes, then we should start the 2 unit delay again!
My question is, do we need to start the delay again when a or b change but expected c doesn't change?
For example in a | b "a" is 0 and "b" is 1 and after sometimes we will change "a" to 1. Is there any need to reject previous time and start the 2 unit delay again for seeing the 1 in output? (note that expected c will not change because our operation is |).

The simulator will evaluate the LHS (left hand side) of the expressions first, then apply changes the variable on the RHS (right-hand-side). Rejection (or filtering) is determined by the results of the LHS expression.
To visualize this, add an intermediate step between a | b.
assign ab = a | b;
assign #2 c = ab;
Run it thought the simulate to generate a waveform. An example output:
0 5 10 15 20 25
| | | | | |
_ _ __
a ___/ \__________/ \______/
__ _____
b _______/\___/ \______/
_ __ _ _____
ab ___/ \_/\___/ \/ \___/
_ _____ ___
c xx___/ \______/ \___/
First 2 time steps of c are unknown because there is no data for ab before time 0. The pulse on ab starting at times 7 and 15 are filtered out since they are less then 2 time steps. All other transitions in c is a shift by 2 in time ab.
There is no rejection time when a goes high at time 25 because the intermediate step (ab) does not have a transition. The simulator will do its own intermediate step, evaluating a change on a | b before deciding what action should be performed in c.

Related

How to find overlapping subproblem in coin change problem of this recursive code i can't find one

You are working at the cash counter at a fun-fair, and you have different types of coins available to you in infinite quantities. The value of each coin is already given. Can you determine the number of ways of making change for a particular number of units using the given types of coins?
counter = 0
def helper(n,c):
global counter
if n == 0:
counter += 1
return
if len(c) == 0:
return
else:
if n >= c[0]:
helper(n - c[0], c)
helper(n,c[1:])
def getWays(n, c):
helper(n,c)
print(counter)
return counter ```
#the helper function takes n and c
#where
#n is amount whose change is to be made
#c is a list of available coins
Let n be the amount of currency units to return as change. You wish to find N(n), the number of possible ways to return change.
One easy solution would be to first choose the "first" coin you give (let's say it has value c), then notice that N(n) is the sum of all the values N(n-c) for every possible c. Since this appears to be a recursive problem, we need some base cases. Typically, we'll have N(1) = 1 (one coin of value one).
Let's do an example: 3 can be returned as "1 plus 1 plus 1" or as "2 plus 1" (assuming coins of value one and two exist). Therefore, N(3)=2.
However, if we apply the previous algorithm, it will compute N(3) to be 3.
+------------+-------------+------------+
| First coin | Second coin | Third coin |
+------------+-------------+------------+
| 2 | 1 | |
+------------+-------------+------------+
| | 2 | |
| 1 +-------------+------------+
| | 1 | 1 |
+------------+-------------+------------+
Indeed, notice that returning 3 units as "2 plus 1" or as "1 plus 2" is counted as two different solutions by our algorithm, whereas they are the same.
We therefore need to apply an additional restriction to avoid such duplicates. One possible solution is to order the coins (for example by decreasing value). We then impose the following restriction: if at a given step we returned a coin of value c0, then at the next step, we may only return coins of value c0 or less.
This leads to the following induction relation (noting c0 the value of the coin returned in the last step): N(n) is the sum of all the values of N(n-c) for all possible values of c less than or equal to c0.
Happy coding :)

Excel: Help me use COUNTIFS to find A or B, but NOT A and B

I have this questionnaire that has a score for answers in 3 rows (3 questionnaires). I need to find the ones that have either Q1 >=30 or Q2 >12 or Q3 <=33. I've been googling and trying solutions but failed when compared by counting manually.
See screenshot (rows AN / AT / BC would be the answers for Q1 / Q2 / Q3):
On rows BD/BE you can see my manual count, which was a PITA and is prone to error.
This formula helped me count ALL values including Q1+Q2+Q3:
=COUNTIFS(AN4:AN147;">=30";AT4:AT147;">=12";BC4:BC147;">=0"; BC4:BC147;"<=33")
What I need the formula for is to find instances where AN or AT or BC has a given value. For AN it's >=30, for AT it's >=12 and for BC it's <=33. For example, if one responder has 31(!), 10, 40, then he should be counted as "1" (or just counted), if one responder has 31(!), 15(!), 41; then he should also be counted as "1"), if one responder has 25), 10, 41; then he shouldn't be counted.
This is a burnout syndrome scale called Maslach Burnout Inventory, what I'm trying to get here are those at risk of burnout (must have 1 or 2 of those 3 sub-questionnaires altered, but not all 3 as those individuals have burnout syndrome)
Here's a sample using markdown generator as requested:
| AN | AT | BC | | |
|---- |---- |---- |--- |--- |
| 14 | 11 | 41 | | |
| 14 | 4 | 43 | | |
| 50 | 9 | 41 | | |
| 38 | 16 | 20 | | |
edit: can't get to display it properly, hmm.
edit2: got it
=SUMPRODUCT(N(((AN>=30)+(AT>=12)+((BC>=0)*(BC<=33)))={1,2}))
where AN, BC and AT are named ranges that refer to the obvious.
In your original formula, you also tested that BC>0 so I included it above.
However, if that test is not necessary, the formula can be shortened to:
=SUMPRODUCT(N(((AN>=30)+(AT>=12)+(BC<=33))={1,2}))
The Formula Evaluation tool can help you figure out what is going on.
And in the screen shot below, the byRow column is not required. It is only there to demonstrate the results of the test on each line, for learning purposes.
Each equality test returns an array of {TRUE,FALSE} depending on the results.
In Excel TRUE=1 and FALSE=0.
Summing the arrays results in an array of {0,1,2,3} depending on how many matches there are in each row.
We then compare those results to see if they are equal to 1 or 2 again returning another {TRUE, FALSE} array.
The N function changes the {TRUE,FALSE} into {1,0} and SUMPRODUCT adds the all up.
One could use the SUM function but then you have to remember that, with the array formula, you have to confirm the formula by holding down ctrl-shift while hitting enter to get the correct result.
Here is one way of doing it :
=SUMPRODUCT( ( ( (AN4:AN7>=30)+(AT4:AT7>12)+(BC4:BC7<=33) )=1)
+( ( (AN4:AN7>=30)+(AT4:AT7>12)+(BC4:BC7<=33) )=2) )
#Ron Rosenfeld's formula is a shorter and more elegant way of accomplishing the same thing.
Anyway if you consider some dummy data like that in the screenshot below which shows rows which satisfy 0,1,2 and 3 of the criteria respectively, my formula works as follows:
Row 4:
(FALSE+FALSE+FALSE = 1) => 0+0+0 = 1 => 0=1 => FALSE
(FALSE+FALSE+FALSE = 2) => 0+0+0 = 2 => 0=2 => FALSE
FALSE+FALSE => 0
Row 5:
(TRUE +FALSE+FALSE = 1) => 1+0+0 = 1 => 1=1 => TRUE
(FALSE+FALSE+FALSE = 2) => 0+0+0 = 2 => 0=2 => FALSE
TRUE+FALSE => 1
Row 6:
(TRUE +TRUE +FALSE = 1) => 1+1+0 = 1 => 2=1 => FALSE
(TRUE +TRUE +FALSE = 2) => 1+1+0 = 2 => 2=2 => TRUE
FALSE+TRUE => 1
Row 7:
(TRUE +TRUE +TRUE = 1) => 1+1+1 = 1 => 3=1 => FALSE
(TRUE +TRUE +TRUE = 2) => 1+1+1 = 2 => 3=2 => FALSE
FALSE+FALSE => 0
Sumproduct adds up the four totals.
There is another way of doing it (perhaps more for interest than practical use, but doesn't involve pseudo-array formulas):
=COUNTIF(AN4:AN7,">=30")+COUNTIF(AT4:AT7,">=12")+COUNTIF(BC4:BC7,"<=33")
-COUNTIFS(AN4:AN7,">=30",AT4:AT7,">=12")-COUNTIFS(AN4:AN7,">=30",BC4:BC7,"<=33")-COUNTIFS(AN4:AN7,">=12",BC4:BC7,"<=33")
The first line of the formula just adds up the individual questions that satisfy one of the criteria. But then in the case where two criteria are satisfied, we would have double counting. So we have to subtract one for each of those pairs. What about the case when three criteria are satisfied? We would end up subtracting one three times, which brings us back to zero, so that is OK.
Looking at the Venn diagram might help:
Note there is some evidence here that even multiple sumifs may be faster than sumproducts for this type of problem, but only relevant if you have a lot of data.

Replace/Update Value in Column Using If statement

Consider this example. There are two columns one called grade with a number 1-10 and another called status that says pass or fail. Originally, any number 1-6 resulted in fail and everything from 7-10 said pass.
Grade | Status
1 | Fail
2 | Fail
3 | Fail
4 | Fail
5 | Fail
6 | Fail
7 | Pass
8 | Pass
9 | Pass
10 | Pass
Now I lowered the passing grade to a 6. I want to replace every fail in the status column with pass if it has a grade of 6. How would I do this using an if statement in Excel?
It sounds as if you're looking for a simple IF statement for your worksheet. If so, try this:
=IF(A2<6,"Fail","Pass")
Take this formula and drag it down your range, adjusting where necessary.
Below are some screens I threw together to show the formula a bit better.
This is how I assumed you had your original worksheet set up:
And this is how I set it up based on your request:

Excel grouping by prioritized ruleset

I wonder why nobody has asked this, but how do I classify (ordinal) entries in a table according to a prioritized ruleset / tree? (possibly with naked excel and not a nested if cascade)
Minimal example (only 3 of 11 or more features shown)
Name | IsCool | IsNerdy | HasChild
Joe | 1 | 1 | 1
Charliese | 1 | 0 | 1
Peter | 1 | 0 | 0
Jonas | 0 | 0 | 0
Rules
Priority | IsCool | IsNerdy | HasChild | => Group
1. | 1 | 1 | ignore | A (at least cool&nerdy)
2. | ignore | ignore | 1 | B (not A, but has a child)
3. | 1 | 0 | 0 | C (only cool)
4. | ignore | ignore | ignore | D (everything else)
stop after first match
yielding:
Name | IsCool | IsNerdy | HasChild | Group
Joe | 1 | 1 | 1 | A
Charliese | 1 | 0 | 1 | B
Peter | 1 | 0 | 0 | C
Jonas | 0 | 0 | 0 | D
You can convert a "ruleset" into all possible combinations of the attributes (IsCool, IsNerdy, HasChild, etc) by treating "Ignore" as 0 (zero) or 1 (unity).
So, the first rule in the questions ruleset would be replaced by two rules.
IsCool ¦ IsNerdy ¦ HasChild¦ Group
1 ¦ 1 ¦ 0 ¦ A
1 ¦ 1 ¦ 1 ¦ A
Although there are only 8 possibilities across the three attributes, this approach can lead to more than 8 rules. For example in the question's ruleset, a person with the data tuple (IsCool, IsNerdy, HasChild) given by (1,1,1) would match to both Group A and Group B when the "ignore"'s in the rulset are expanded in this way. To eliminate the ambiguity this causes, it is also necessary to apply the priorities: the match to Group A has higher priority than that for B so the lookup table would include (1,1,1,A) as a row but exclude (1,1,1,B).
With a larger ruleset involving more attributes, the task of constructing the required VLOOKUPtable from the table of rules will not be without its problems, particularly if a non-manual approach is desired and just Excel is to be used rather Excel in conjunction with VBA.
An alternative approach not involving VBA which treats the ruleset as data is as follows.
Formalising the notation introduced above, a rule involving n attributes can be expressed as
(r[1],r[2],...,r[n],G)
where r[i] (i=1,...,n) can take values of 0, 1 or "ignore" and G represents the group (one of A, B, C or D in the question's example).
An instance of data can similarly be represented as
(d[1],d[2],...,d[n])
where d[i] (i=1,...,n) takes values of 0 or 1 (but not "ignore")
The rule is matched if
r[i] = "ignore" OR "d[i] = r[i]" for each i from 1 to n
There is a pretty obvious way implementing this in Excel as
=AND(OR(r[1]="ignore",d[1]=r[1]),OR(r[2]="ignore",d[2]=r[2]),...,OR(r[n]="ignore",d[n]=r[n]))
where, of course, the relevant cell references are used in place of the d[i] and r[i] placeholders shown above and the appropriate number of OR's are nested inside of the AND to replace the ... shorthand.
The pseudo-formula above has a value of either TRUE or FALSE with the former indicating the data instance matches to the rule and the latter that it does not.
However, this is not the end of the story as the rules still need to be applied in priority order.
So, extending the notation further, assume that the rules are listed in priority order (rule 1 has a higher priority than rule 2, which has a higher priority than rule 3, etc)
If cell C[k] holds the result of the applying the k'th rule to an instance of the data then modifying the above pseudo-formula so that C[k] now has the formula
=IF(OR(C[1],...,C[k-1]),FALSE,AND(...))
ensures that the k'th rule can be matched only if no earlier (and therefore higher priority) rule is matched. (Here the third part of the IF is the AND formula previously noted.)
The screengrab below shows the approach in action for the question's example.
The cells in blue are formulae. Those for TRUE/FALSE values in the second table implement the pseudo-formula discussed above. For example, cellF13 shows the result of applying rule 1 to the data instance (0,0,0) and has the following formula
=AND(OR($C$5="Ignore",$C$5=$C13),OR($D$5="Ignore",$D$5=$D13),OR($E$5="Ignore",$E$5=$E13))
NB: no IF needs to be wrapped around this formula because there is no rule with a higher priority than rule 1.
The formula for cell I13 shows the result of applying rule 4 to the same data instance and needs to take account of the higher priorities accorded to rules 1, 2 and 3. The formula in this cell is
=IF(OR($F13:H13),FALSE,AND(OR($C$8="Ignore",$C$8=$C13),OR($D$8="Ignore",$D$8=$D13),OR($E$8="Ignore",$E$8=$E13)))
The formulae in cells G13 and H13 are similar to that of I13 (left as an exercise).
By design, there can be at most one TRUE value in each row and, if the ruleset is sound, there should be exactly one such value. The formulae in the final column of the second table make this asssumption about the ruleset and simply pick out the relevant value from the final column of the first table corresponding to whichever rule shows as TRUE.
The formula in cell J13 is
=INDEX(F$5:F$8,SUMPRODUCT(1*(F13:I13),F$12:I$12))
The formulae in range F13:J13 were simply copied down the rows of the table to cells F14:J20
You could do this by creating a key in your data (e.g. Joe = "111", Charliese = "101", etc.), and then it's just a vlookup against your ruleset which contains all possible combinations of the key.

thread in single processor system

these two thread run concurrently in a shared memory (all variables are
shared between the two threads):
Thread A
for (i=0; i<5; i++) {
x = x + 1;
}
Thread B
for (j=0; j<5; j++) {
x = x + 2;
}
Assume a single-processor system
a- Give a concise proof why x≤15 when both threads have completed.
b- Suppose we replace x = x+2 in Thread B with x = x-1 what will be the value of X.
I do not understand this question and I google it and I found answer but I can not get it
I wanna some explanation for it.
If the threading works perfectly, the highest value 'x' can have is 15. This all depends on the scheduler of the operating system.
Note that I am assuming the initial value of x is 0!
Lets say that Thread A and Thread B are serialized.
The value of x after Thread A is complete will be 5.
i | x
-------
0 | 1
1 | 2
2 | 3
3 | 4
4 | 5
The value of x going into Thread B will be 5, resulting x to be a final value of 15
i | x
-------
0 | 7
1 | 9
2 | 11
3 | 13
4 | 15
Now, things typically don't happen this way, and a thread will both read an initial value of x and do their addition, then write their modified value back into memory. The following can happen.
Thread A reads the value 'x' as 0
Thread B reads the value 'x' as 0
Thread A adds 1 to x making its local copy of x, 1
Thread B adds 2 to x making its local copy of x, 2
Thread A writes its modified value of x as 1
Thread B writes its modified value of x as 2 (overwriting Thread A's modification)
Therefore, x will be no more than 15, but depending on the scheduler, will be less!
a)
Instruction x=x+1 is not single instruction at low level and It consist of sequence of read x, then add 1 to x and then update x's memory . Hence it may be happen two threads read same value of x.
Suppose, if two threads read same value of x variable then update x and write back to x this cause x < 15.
b)
For the same reason value of x may be between 0 to 5 if your instruction is x=x-1.
Try this you will learn more!
compile your c code with -S option it will create a compiled assemble code for your program. Then you can understand x = x + 1 is not a single instruction. And switching between threads can be possible before completion of x = x + 1 instruction hence its not atomic.

Resources