Concurrent programming: synchronization of three theads using semaphores - concurrency

We have three processes, which run concurrently and consist of the following statements:
P
Q
R
p1 p2 p3
q1 q2 q3
r1 r2 r3
Synchronize P, Q and R, fullfilling all of the conditions
simulatenously:
p1 must execute before q2
q1 must execute before r2
r1 must execute before p2
r3 must execute after p2 and q2
First I tried this:
pCompleted = Semaphore(0)
qCompleted = Semaphore(0)
rCompleted = Semaphore(0)
bothCompleted = Semaphore(0)
P:
Q:
R:
p1 pCompleted.signal() rCompleted.wait() p2 bothCompleted.signal() p3
q1 qCompleted.signal() pCompleted.wait() q2 bothCompleted.signal() q3
r1 rCompleted.signal() qCompleted.wait() r2 bothCompleted.wait() bothCompleted.wait() r3
It is needless to say this is extremely ineffective, maybe even wrong.
I think I can drop at least one semaphore without causing a deadlock:
P:
Q:
R:
p1 pCompleted.signal() rCompleted.wait() p2 pCompleted.signal() p3
q1 qCompleted.signal() pCompleted.wait() q2 qCompleted.signal() q3
r1 rCompleted.signal() qCompleted.wait() r2 pCompleted.wait() qCompleted.wait() r3
Still, I highly doubt this is a good solution.
Could somebody suggest an optimal solution?

I think this can be achieved quite easily with four CountDownLatches. For example,
CountDownLatch p1q2 = new CountDownLatch(1);
controls the synchronization between threads P and Q for the first bullet. Q waits on p1q2 before entering q2, and P countdowns p1q2 after exiting p1. And so on.
Note that the r3p2q2 would have to wait for two countdowns.
(And, obviously, you also need a thread D, with statements d1 and d2, where d2 must execute after r2 ;) )

Related

IF/OR Function Excel 2010

I have been searching for an answer and can't seem to find one. I have multiple OR conditions within an IF statement.
What I need is:
If L3 OR M3 OR N3 or O3 OR P3 OR Q3 >0 then X3=Past
If R3 >0 then X3=Present
If S3 OR T3 or U3 or V3 > 0 then X3=Future
The problem is the following are also true:
If L3 OR M3 OR N3 or O3 OR P3 OR Q3 OR R3 > 0 then X3=Past, Present
If L3 OR M3 OR N3 or O3 OR P3 OR Q3 OR S3 OR T3 or U3 or V3 >0 then X3=Past, Future
If L3 OR M3 OR N3 or O3 OR P3 OR Q3 OR R3 OR S3 OR T3 or U3 or V3 >0 then X3=Past, Present, Future
This is my non-working code thus far:
=(IF(OR($L3>0, $M3>0, $N3>0, $O3>0, $P3>0, $Q3>0),"Past"), IF(OR($R3=0),”Present”),IF(OR($S105>0,$T105>0,$U105>0,$V105>0),"Future" ,NoDate)))
Any help is greatly appreciated!!
I think the correct answer is
=IF(OR($L3>0,$M3>0,$N3>0,$O3>0,$P3>0,$Q3>0),"Past",IF(OR($R3=0),"Present",IF(OR($S105>0,$T105>0,$U105>0,$V105>0),"Future","NoDate")))
Check your parentesis, quote "NoDate".
The trick is
=IF(conditions,"Past",IF(conditions,"Present",IF(conditions,"Future","NoDate")))

Simplifying equations in Sympy using inequalities

I am trying in Sympy to use a standard engineering method of simplifying an equation, where you know one variable is much larger or smaller than another. For instance, given the equation
C1*R2*s+C1*R2+R1+R2
and knowing that
R1 >> R2
the equation can be simplified to
C1*R2*s+C1*R2+R1.
What you'd normally do by hand is divide by R1, giving
C1*R2*s/R1+C1*R2/R1+1+R2/R1
then anyplace you see R2/R1 by itself you can set it to zero and then multiply by R1. I've not been able to figure how this would be done in Sympy. Obviously it's easy to do the division step, but I haven't been able to figure out how to do the search and replace step- just using subs gives you
R1
which isn't the right answer. factor, expand, collect, don't seem to get me anywhere.
Using replace instead of subs works here.
C1, C2, R1, R2 = sp.symbols('C1, C2, R1, R2', real = True)
s = sp.symbols('s')
expr = C1*R2*s+C1*R2+R1+R2
print('original expression:', expr)
expr_approx = (R1 * ((expr/R1).expand().replace(R2/R1,0))).simplify()
print('approximate expression:', expr_approx)
original expression: C1*R2*s + C1*R2 + R1 + R2
approximate expression: C1*R2*s + C1*R2 + R1

Right Runge Kutta 4th method approach?

I have this runge kutta code. However, one mentioned my approach is wrong. And I couldn't really understand why from him, so anyone here, who could give a hint on why this way is wrong?
Vector3d r = P.GetAcceleration();
Vector3d s = P.GetAcceleration() + 0.5*m_dDeltaT*r;
Vector3d t = P.GetAcceleration() + 0.5*m_dDeltaT*s;
Vector3d u = P.GetAcceleration() + m_dDeltaT*t;
P.Velocity += m_dDeltaT * (r + 2.0 * (s + t) + u) / 6.0);
====EDIT====
Vector3d are storing the coordinates, x, y, z.
The GetAcceleration returns the acceleration for each x, y, and z.
You have some acceleration function
a(p,q) where p=(x,y,z) and q=(vx,vy,vz)
Your order 1 system that can be solved via RK4 is
dotp = q
dotq = a(p,q)
The stages of the RK method involve an offset of the state vector(s)
k1p = q
k1q = a(p,q)
p1 = p + 0.5*dt*k1p
q1 = q + 0.5*dt*k1q
k2p = q1
k2q = a(p1,q1)
p2 = p + 0.5*dt*k2p
q2 = p + 0.5*dt*k2q
k3p = q2
k3q = a(p2,q2)
etc. You can either adjust the state vectors of the point P for each step, saving the original coordinates, or use a temporary copy of P to compute k2, k3, k4.
You haven't defined your methods, but the thing that's jumping out at me is you're mixing your results with your inputs. Since Runge-Kutta is a method for calculating y_(n+1) = y_n + hsum(b_ik_i), I would expect your solution to keep your _n terms on the right, and your (n+1) terms on the left. This is NOT what you're doing. Instead, s(n+1) is dependent on r_(n+1) instead of on r_n, t_(n+1) on s_(n+1), and so on. This smells of an error where you attempted to limit the number of variables being used.
With that in mind, can you indicate the actual intermediate values of the calculations your program generates and compare them with the intended intermediate values?

about pointers in c++

In the following questions, p1,p2, abd p3 are pointers to intergers. In the BEGINNING of each question *p1 and *p2 will both have the value 10.
after executing the following statements, what is the value of *p1 and *p2
p3 = NULL
p3 = p1
p1 = p2
p2 = p1
The way I did it i ended up getting 10 for both the same way they started.
p3 = NULL
p3 = p1 ;now p3 is set to 10
p1 = p2 ;now p1 is set to 10
p2 = p1 ;now p2 is set to 10
but I don't think I'm understanding it correctly.
The last line sets p2 to p1, which was just set to p2; so both p1 and p2 point to the original target of p2, which contains the value 10.
Don't confuse pointers to what they point to. *p1 and *p2 have value 10, that is, both p1 and p2 point to memory locations that have 10 written in it (although the memory locations may not be the same). It doesn't mean that p1 and p2 point to the same memory location!
Line by line:
p3 = p1; // p3 points to the memory location of p1, *p3 = *p1 = 10
p1 = p2; // p1 points to the memory location of p2, *p1 = *p2 = 10
p2 = p1; // p2 points to the memory location of p1, *p2 = *p1 = 10
So at the end of the day, you end up with: p3 taking the value of the original p1, then p1 taking the value of the original p2, and p2 being left unchanged.
This is really like playing a shell game.
So lets start with that.. here is the state after each line:
p1=orig1, p2=orig2, p3=orig3
p3 = NULL
p1=orig1, p2=orig2, p3=NULL
p3 = p1 (currently set to orig1)
p1=orig1, p2=orig2, p3=orig1
p1 = p2 (currently set to orig2)
p1=orig2, p2=orig2, p3=orig1
p2 = p1 (currently set to orig2)
p1=orig2, p2=orig2, p3=orig1
And as it said.. the original value of *p2 and *p1 is 10, so they will all be 10.

"initial values are not feasible" error message while running structural equation model in Stata?

I am working on a structural equation model (sem) model with 47 observed variables and 6 latent variables, of which 5 observed variables are endogenous and one latent variable is endogenous. Data has no missing values and sample size is 4,634.
I ran sem in Stata using the following command:
sem (I -> i1 i2 i3 i4 i5_1) ///
(N -> n1 n2 n3 n4) ///
(S -> s1 s2 s3 s4 s5 s6 s7 s8 s9) ///
(T -> t1 t2 t3 t4) ///
(SES -> se1 se2 se3 se4 se5 se6 se7 se8 se9 se10 ///
se11 se12 se13 se14 se15 se16 se17 se18 se19 se20) ///
(CS -> c1 c2 c3 c4 c5) ///
(CS <- I N S T SES)
It returned the following error message:
initial values are not feasible
Why am I receiving this message? How can I deal with this error?
I would start by looking at each measurement model separately, and see if there are problems there. i.e.:
sem ( i1 i2 i3 i4 i5_1 <- I)
sem ( i1 i2 i3 i4 i5_1 <- N)
etc.
My guess would be that the model for SES might prove to be the problem.
Edit:
Based on your comment we now know that the measurement models in isolation converge. Next step would be to check each of the measurement models to see whether they make sense: Do each of the loading have the expected sign? Are there loading that are unexpectedly large or small? If you see that, you need to figure out why that is the case. This just requires staring at your data, looking at graphs and correlation tables.
If there is no problem with your measurement model, then the next step would be to look at the structural part. Obviously you cannot do the same trick as with the measurement model, that is, you cannot estimate the structural part without the measurement models. The structural contains latent variables, and it is the measurement models that define what they are. So without measurement models, the structural model is not identified.
What I would do instead is simplify your model, and than add complication till you run into problems. For example I might start with:
sem (I -> i1 i2 i3 i4 i5_1) ///
(CS -> c1 c2 c3 c4 c5) ///
(CS <- I)
Than continue with:
sem (I -> i1 i2 i3 i4 i5_1) ///
(N -> n1 n2 n3 n4) ///
(CS -> c1 c2 c3 c4 c5) ///
(CS <- I N)
etc.
That way you can find which latent variable causes trouble. My first move would be to look at the measurement model of that variable and look at the scale of that variable. By default sem "borrows" the scale of one of the observed variables by setting the loading for that variable to 1. Is that variable in some sense "weird"? Similarly I would also look at the scale for your endogenous latent variable CS. If they are weird, you can choose to constrain the loading of another variable with a more reasonable scale to 1, or you can "standardize" your latent variable by constraining the variance of the latent variable to be 1.