Skip to content

Commit 54221b7

Browse files
authored
Merge pull request #155 from saksham-gera/dev
Added A zeromq based study and also corrected some existing studies
2 parents b98040a + df0d70c commit 54221b7

8 files changed

Lines changed: 836 additions & 58 deletions

File tree

0mq/funbody_zmq.dir/concore2.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import time
2+
import os
3+
from ast import literal_eval
4+
import sys
5+
import re
6+
7+
#if windows, create script to kill this process
8+
# because batch files don't provide easy way to know pid of last command
9+
# ignored for posix!=windows, because "concorepid" is handled by script
10+
# ignored for docker (linux!=windows), because handled by docker stop
11+
if hasattr(sys, 'getwindowsversion'):
12+
with open("concorekill.bat","w") as fpid:
13+
fpid.write("taskkill /F /PID "+str(os.getpid())+"\n")
14+
15+
try:
16+
iport = literal_eval(open("concore.iport").read())
17+
except:
18+
iport = dict()
19+
try:
20+
oport = literal_eval(open("concore.oport").read())
21+
except:
22+
oport = dict()
23+
24+
25+
s = ''
26+
olds = ''
27+
delay = 1
28+
retrycount = 0
29+
inpath = "./in" #must be rel path for local
30+
outpath = "./out"
31+
32+
#9/21/22
33+
try:
34+
sparams = open(inpath+"1/concore.params").read()
35+
if sparams[0] == '"': #windows keeps "" need to remove
36+
sparams = sparams[1:]
37+
sparams = sparams[0:sparams.find('"')]
38+
if sparams != '{':
39+
print("converting sparams: "+sparams)
40+
sparams = "{'"+re.sub(';',",'",re.sub('=',"':",re.sub(' ','',sparams)))+"}"
41+
print("converted sparams: " + sparams)
42+
try:
43+
params = literal_eval(sparams)
44+
except:
45+
print("bad params: "+sparams)
46+
except:
47+
params = dict()
48+
#9/30/22
49+
def tryparam(n,i):
50+
try:
51+
return params[n]
52+
except:
53+
return i
54+
55+
56+
#9/12/21
57+
def default_maxtime(default):
58+
global maxtime
59+
try:
60+
maxtime = literal_eval(open(inpath+"1/concore.maxtime").read())
61+
except:
62+
maxtime = default
63+
default_maxtime(100)
64+
65+
def unchanged():
66+
global olds,s
67+
if olds==s:
68+
s = ''
69+
return True
70+
else:
71+
olds = s
72+
return False
73+
74+
def read(port, name, initstr):
75+
global s,simtime,retrycount
76+
time.sleep(delay)
77+
try:
78+
infile = open(inpath+str(port)+"/"+name);
79+
ins = infile.read()
80+
except:
81+
ins = initstr
82+
while len(ins)==0:
83+
time.sleep(delay)
84+
ins = infile.read()
85+
retrycount += 1
86+
s += ins
87+
inval = literal_eval(ins)
88+
simtime = max(simtime,inval[0])
89+
return inval[1:]
90+
91+
def write(port, name, val, delta=0):
92+
global outpath,simtime
93+
if isinstance(val,str):
94+
time.sleep(2*delay)
95+
elif isinstance(val,list)==False:
96+
print("mywrite must have list or str")
97+
quit()
98+
try:
99+
with open(outpath+str(port)+"/"+name,"w") as outfile:
100+
if isinstance(val,list):
101+
outfile.write(str([simtime+delta]+val))
102+
simtime += delta
103+
else:
104+
outfile.write(val)
105+
except:
106+
print("skipping"+outpath+str(port)+"/"+name);
107+
108+
def initval(simtime_val):
109+
global simtime
110+
val = literal_eval(simtime_val)
111+
simtime = val[0]
112+
return val[1:]
113+

0mq/funbody_zmq.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# funbody2_zmq.py
2+
import zmq
3+
import time
4+
import concore
5+
import concore2
6+
7+
print("funbody using 0mq")
8+
9+
context = zmq.Context()
10+
socket = context.socket(zmq.REP)
11+
socket.bind("tcp://*:2346")
12+
13+
concore.delay = 0.07
14+
concore2.delay = 0.07
15+
concore2.inpath = concore.inpath
16+
concore2.outpath = concore.outpath
17+
concore2.simtime = 0
18+
concore.default_maxtime(100)
19+
init_simtime_u = "[0.0, 0.0, 0.0]"
20+
init_simtime_ym = "[0.0, 0.0, 0.0]"
21+
22+
u = concore.initval(init_simtime_u)
23+
ym = concore2.initval(init_simtime_ym)
24+
25+
while concore2.simtime < concore.maxtime:
26+
msg = socket.recv_json()
27+
if msg["action"] == "fun":
28+
u = msg["params"]["u"]
29+
concore.simtime = u[0]
30+
u = u[1:]
31+
concore.write(concore.oport['U2'], "u", u)
32+
print(u)
33+
old2 = concore2.simtime
34+
while concore2.unchanged() or concore2.simtime <= old2:
35+
ym = concore2.read(concore.iport['Y2'], "ym", init_simtime_ym)
36+
ym_full = [concore2.simtime] + ym
37+
print(f"Replying with {ym_full}")
38+
socket.send_json({"ym": ym_full})
39+
print(f"funbody u={u} ym={ym} time={concore2.simtime}")
40+
else:
41+
print("undefined action: " + str(msg.get("action", "None")))
42+
socket.send_json({"error": "undefined action"})
43+
break
44+
45+
print("retry=" + str(concore.retrycount))

0mq/funcall_zmq.dir/concore2.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import time
2+
import os
3+
from ast import literal_eval
4+
import sys
5+
import re
6+
7+
#if windows, create script to kill this process
8+
# because batch files don't provide easy way to know pid of last command
9+
# ignored for posix!=windows, because "concorepid" is handled by script
10+
# ignored for docker (linux!=windows), because handled by docker stop
11+
if hasattr(sys, 'getwindowsversion'):
12+
with open("concorekill.bat","w") as fpid:
13+
fpid.write("taskkill /F /PID "+str(os.getpid())+"\n")
14+
15+
try:
16+
iport = literal_eval(open("concore.iport").read())
17+
except:
18+
iport = dict()
19+
try:
20+
oport = literal_eval(open("concore.oport").read())
21+
except:
22+
oport = dict()
23+
24+
25+
s = ''
26+
olds = ''
27+
delay = 1
28+
retrycount = 0
29+
inpath = "./in" #must be rel path for local
30+
outpath = "./out"
31+
32+
#9/21/22
33+
try:
34+
sparams = open(inpath+"1/concore.params").read()
35+
if sparams[0] == '"': #windows keeps "" need to remove
36+
sparams = sparams[1:]
37+
sparams = sparams[0:sparams.find('"')]
38+
if sparams != '{':
39+
print("converting sparams: "+sparams)
40+
sparams = "{'"+re.sub(';',",'",re.sub('=',"':",re.sub(' ','',sparams)))+"}"
41+
print("converted sparams: " + sparams)
42+
try:
43+
params = literal_eval(sparams)
44+
except:
45+
print("bad params: "+sparams)
46+
except:
47+
params = dict()
48+
#9/30/22
49+
def tryparam(n,i):
50+
try:
51+
return params[n]
52+
except:
53+
return i
54+
55+
56+
#9/12/21
57+
def default_maxtime(default):
58+
global maxtime
59+
try:
60+
maxtime = literal_eval(open(inpath+"1/concore.maxtime").read())
61+
except:
62+
maxtime = default
63+
default_maxtime(100)
64+
65+
def unchanged():
66+
global olds,s
67+
if olds==s:
68+
s = ''
69+
return True
70+
else:
71+
olds = s
72+
return False
73+
74+
def read(port, name, initstr):
75+
global s,simtime,retrycount
76+
time.sleep(delay)
77+
try:
78+
infile = open(inpath+str(port)+"/"+name);
79+
ins = infile.read()
80+
except:
81+
ins = initstr
82+
while len(ins)==0:
83+
time.sleep(delay)
84+
ins = infile.read()
85+
retrycount += 1
86+
s += ins
87+
inval = literal_eval(ins)
88+
simtime = max(simtime,inval[0])
89+
return inval[1:]
90+
91+
def write(port, name, val, delta=0):
92+
global outpath,simtime
93+
if isinstance(val,str):
94+
time.sleep(2*delay)
95+
elif isinstance(val,list)==False:
96+
print("mywrite must have list or str")
97+
quit()
98+
try:
99+
with open(outpath+str(port)+"/"+name,"w") as outfile:
100+
if isinstance(val,list):
101+
outfile.write(str([simtime+delta]+val))
102+
simtime += delta
103+
else:
104+
outfile.write(val)
105+
except:
106+
print("skipping"+outpath+str(port)+"/"+name);
107+
108+
def initval(simtime_val):
109+
global simtime
110+
val = literal_eval(simtime_val)
111+
simtime = val[0]
112+
return val[1:]
113+

0mq/funcall_zmq.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# funcall2_zmq.py
2+
import zmq
3+
import time
4+
import concore
5+
import concore2
6+
7+
print("funcall using 0mq")
8+
9+
context = zmq.Context()
10+
socket = context.socket(zmq.REQ)
11+
socket.connect("tcp://localhost:2346")
12+
13+
concore.delay = 0.07
14+
concore2.delay = 0.07
15+
concore2.inpath = concore.inpath
16+
concore2.outpath = concore.outpath
17+
concore2.simtime = 0
18+
concore.default_maxtime(100)
19+
init_simtime_u = "[0.0, 0.0, 0.0]"
20+
init_simtime_ym = "[0.0, 0.0, 0.0]"
21+
22+
u = concore.initval(init_simtime_u)
23+
ym = concore2.initval(init_simtime_ym)
24+
25+
while concore2.simtime < concore.maxtime:
26+
while concore.unchanged():
27+
u = concore.read(concore.iport['U'], "u", init_simtime_u)
28+
message = {
29+
"action": "fun",
30+
"params": {"u": [concore.simtime] + u}
31+
}
32+
socket.send_json(message)
33+
response = socket.recv_json()
34+
ym = response["ym"]
35+
concore2.simtime = ym[0]
36+
ym = ym[1:]
37+
concore2.write(concore.oport['Y'], "ym", ym)
38+
print(f"funcall 0mq u={u} ym={ym} time={concore2.simtime}")
39+
print("retry=" + str(concore.retrycount))

0 commit comments

Comments
 (0)