Skip to content

Commit 701b73b

Browse files
authored
Linux/Mac --output-windows. Fix np.fromiter() workaround.
1 parent 073ba09 commit 701b73b

File tree

5 files changed

+20
-16
lines changed

5 files changed

+20
-16
lines changed

demos/np_lpsolver.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,15 @@ async def main():
210210
print(f'max = {mx} / {cd} / {scale} = {mx / cd / scale} in {iteration} iterations')
211211

212212
logging.info('Solution x')
213-
sum_powers = secint.array(np.zeros((N,), dtype='O')) # TODO: compare with fromiter approach below
214-
for i in range(m):
215-
x_powers = pow_list(T[i+1][-1] / N, basis[i], N)
216-
sum_powers += x_powers
217213
coefs = Zp.array([[w_powers[(j * k) % N].value for k in range(N)] for j in range(n)]) ## TODO: vandermonde ff array
214+
sum_powers = np.sum(np.fromiter((pow_list(T[i+1][-1] / N, basis[i], N) for i in range(m)), 'O'))
218215
x = coefs @ sum_powers
219216
Ax_bounded_by_b = mpc.all((A @ x <= b * cd).tolist()) ## TODO: np.all
220217
x_nonnegative = mpc.all((x >= 0).tolist())
221218

222219
logging.info('Dual solution y')
223220
coefs = Zp.array([[w_powers[((n + i) * k) % N].value for k in range(N)] for i in range(m)])
224-
sum_powers = np.sum(np.fromiter((pow_list(T[0][j] / N, cobasis[j], N) for j in range(n)), 'O')) #TODO fix for Numpy 1.22, WSL
221+
sum_powers = np.sum(np.fromiter((pow_list(T[0][j] / N, cobasis[j], N) for j in range(n)), 'O'))
225222
y = coefs @ sum_powers
226223
yA_bounded_by_c = mpc.all((y @ A >= c * cd).tolist())
227224
y_nonnegative = mpc.all((y >= 0).tolist())

demos/np_lpsolverfxp.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,18 +192,14 @@ async def main():
192192
print(f'max = {mx} (error {rel_error:.3%}) in {iteration} iterations')
193193

194194
logging.info('Solution x')
195-
x = secfxp.array(np.zeros((n,), dtype='O'))
196-
for i in range(m):
197-
u = mpc.unit_vector(basis[i], m + n)[:n]
198-
x += T[i+1, -1] * mpc.np_fromlist(u)
195+
x = np.sum(np.fromiter((T[i+1, -1] * mpc.np_fromlist(mpc.unit_vector(basis[i], m + n)[:n]) for i in range(m)), 'O'))
199196
cx = c @ x
200197
Ax = A @ x
201198
Ax_bounded_by_b = mpc.all((Ax <= 1.01 * b + 0.0001).tolist())
202199
x_nonnegative = mpc.all((x >= 0).tolist())
203200

204201
logging.info('Dual solution y')
205202
y = np.sum(np.fromiter((T[0, j] * mpc.np_fromlist(mpc.unit_vector(cobasis[j], m + n)[n:]) for j in range(n)), 'O'))
206-
#TODO: fix issue Numpy 1.20, see mpyc.numpy._fromiter()
207203
yb = y @ b
208204
yA = y @ A
209205
yA_bounded_by_c = mpc.all((yA >= np.where(c > 0, 1/1.01, 1.01) * c - 0.0001).tolist())

mpyc/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ def get_arg_parser():
114114
logging.basicConfig(format='{asctime} {message}', style='{', level=level, stream=sys.stdout)
115115
logging.debug(f'Set logging level to {level}: {logging.getLevelName(level)}')
116116
del ch, level
117+
logging.debug(f'On {sys.platform=}')
117118

118119
# Ensure numpy will not be loaded by mpyc.numpy, if demanded (saving resources).
119120
env_no_numpy = os.getenv('MPYC_NONUMPY') == '1' # check if variable MPYC_NONUMPY is set

mpyc/numpy.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,15 @@ def _item_shape(shape, key):
165165

166166
if np.lib.NumpyVersion(np.__version__) < '1.23.0':
167167
def _fromiter(iterable, dtype, **kwargs):
168-
if np.dtype(dtype) != np.object: # and dtype != 'O':
168+
if np.dtype(dtype) != object:
169169
return np.fromiter(iterable, dtype, **kwargs)
170-
# No dtype=object for np.fromiter(), workaround via np.array():
171-
return np.array(list(iterable), dtype=object)
170+
171+
# No dtype=object for np.fromiter(), workaround via 1D array:
172+
x = list(iterable)
173+
a = np.empty(len(x), dtype=object)
174+
for i in range(len(x)):
175+
a[i] = x[i]
176+
return a
172177

173178
np.fromiter = _fromiter
174179

mpyc/runtime.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3276,7 +3276,6 @@ def hop(a):
32763276
# use default port for each local party
32773277
m = options.M or 1
32783278
if m > 1 and options.index is None:
3279-
import platform
32803279
import subprocess
32813280
# convert sys.flags into command line arguments
32823281
flgmap = {'debug': 'd', 'inspect': 'i', 'interactive': 'i', 'optimize': 'O',
@@ -3292,8 +3291,14 @@ def hop(a):
32923291
xopts = ['-X' + a + ('' if c is True else '=' + c) for a, c in sys._xoptions.items()]
32933292
for i in range(m-1, 0, -1):
32943293
cmd_line = [sys.executable] + flags + xopts + argv + ['-I', str(i)]
3295-
if options.output_windows and platform.platform().startswith('Windows'):
3296-
subprocess.Popen(['start'] + cmd_line, shell=True)
3294+
if options.output_windows and sys.platform.startswith(('win32', 'linux', 'darwin')):
3295+
if sys.platform.startswith('win32'):
3296+
subprocess.Popen(['start'] + cmd_line, shell=True)
3297+
elif sys.platform.startswith('linux'):
3298+
# TODO: check for other common Linux terminals
3299+
subprocess.Popen(['gnome-terminal', '--'] + cmd_line)
3300+
elif sys.platform.startswith('darwin'):
3301+
subprocess.Popen(['open', '-a', 'Terminal', '--args'] + cmd_line)
32973302
elif options.output_file:
32983303
with open(f'party{options.M}_{i}.log', 'a') as f:
32993304
f.write('\n')

0 commit comments

Comments
 (0)