Parallel finite-difference calculation in OpenMDAO executes each point in each process - openmdao

I am trying to set up a problem in OpenMDAO and would like to make use of parallel finite difference computations. However, when I call compute_totals() each MPI process actually computes all the perturbed points.
I have made a minimal example that demonstrates the problem. Consider the simple case of a model which can be represented by a matrix multiplication. The Jacobian of this model is simply the matrix of the model. See the code below:
import numpy as np
import time
from openmdao.api import ExplicitComponent, Problem, IndepVarComp, Group
from openmdao.utils.mpi import MPI
rank = 0 if not MPI else MPI.COMM_WORLD.rank
class MatMultComp(ExplicitComponent):
def __init__(self, matrix, **kwargs):
super().__init__(**kwargs)
self.matrix = matrix
def setup(self):
self.add_input('x', val=np.ones(self.matrix.shape[1])))
self.add_output('y', val=np.ones(self.matrix.shape[0])))
def compute(self, inputs, outputs, **kwargs):
outputs['y'] = self.matrix.dot(inputs['x'])
print('{} :: x = {}'.format(rank, np.array_str(inputs['x'])))
class Model(Group):
def setup(self):
matrix = np.arange(25, dtype=float).reshape(5, 5)
self.add_subsystem('ivc', IndepVarComp('x', np.ones(matrix.shape[1])), promotes=['*'])
self.add_subsystem('mat', MatMultComp(matrix), promotes=['*'])
self.approx_totals(step=0.1)
self.num_par_fd = matrix.shape[1]
if __name__ == '__main__':
p = Problem()
p.model = Model()
p.setup()
p.run_model()
t0 = time.time()
jac = p.compute_totals(of=['y'], wrt=['x'], return_format='array')
dt = time.time() - t0
if rank == 0:
print('Took {:2.3f} seconds.'.format(dt))
print('J = ')
print(np.array_str(jac, precision=0))
When I run this code without MPI, I get the following output:
0 :: x = [1. 1. 1. 1. 1.]
0 :: x = [1.1 1. 1. 1. 1. ]
0 :: x = [1. 1.1 1. 1. 1. ]
0 :: x = [1. 1. 1.1 1. 1. ]
0 :: x = [1. 1. 1. 1.1 1. ]
0 :: x = [1. 1. 1. 1. 1.1]
Took 5.008 seconds.
J =
[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]
[20. 21. 22. 23. 24.]]
This is the correct result, and takes about 5 seconds, as expected. Now, when I run this with MPI, using 5 processes, with the command mpirun -np 5 python matmult.py, I get the following output:
0 :: x = [1. 1. 1. 1. 1.]
1 :: x = [1. 1. 1. 1. 1.]
2 :: x = [1. 1. 1. 1. 1.]
3 :: x = [1. 1. 1. 1. 1.]
4 :: x = [1. 1. 1. 1. 1.]
0 :: x = [1.001 1. 1. 1. 1. ]
1 :: x = [1.001 1. 1. 1. 1. ]
2 :: x = [1.001 1. 1. 1. 1. ]
3 :: x = [1.001 1. 1. 1. 1. ]
4 :: x = [1.001 1. 1. 1. 1. ]
3 :: x = [1. 1.001 1. 1. 1. ]
0 :: x = [1. 1.001 1. 1. 1. ]
1 :: x = [1. 1.001 1. 1. 1. ]
2 :: x = [1. 1.001 1. 1. 1. ]
4 :: x = [1. 1.001 1. 1. 1. ]
2 :: x = [1. 1. 1.001 1. 1. ]
3 :: x = [1. 1. 1.001 1. 1. ]
0 :: x = [1. 1. 1.001 1. 1. ]
1 :: x = [1. 1. 1.001 1. 1. ]
4 :: x = [1. 1. 1.001 1. 1. ]
1 :: x = [1. 1. 1. 1.001 1. ]
2 :: x = [1. 1. 1. 1.001 1. ]
3 :: x = [1. 1. 1. 1.001 1. ]
0 :: x = [1. 1. 1. 1.001 1. ]
4 :: x = [1. 1. 1. 1.001 1. ]
0 :: x = [1. 1. 1. 1. 1.001]
1 :: x = [1. 1. 1. 1. 1.001]
2 :: x = [1. 1. 1. 1. 1.001]
3 :: x = [1. 1. 1. 1. 1.001]
4 :: x = [1. 1. 1. 1. 1.001]
Took 5.072 seconds.
J =
[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]
[20. 21. 22. 23. 24.]]
The final result is correct, of course. However, this defies the purpose of using MPI, because each of the 5 processes computed all the perturbed points, and the total execution takes about 5 seconds like before. I expected the following output:
0 :: x = [1. 1. 1. 1. 1.]
1 :: x = [1. 1. 1. 1. 1.]
2 :: x = [1. 1. 1. 1. 1.]
3 :: x = [1. 1. 1. 1. 1.]
4 :: x = [1. 1. 1. 1. 1.]
0 :: x = [1.1 1. 1. 1. 1. ]
1 :: x = [1. 1.1 1. 1. 1. ]
2 :: x = [1. 1. 1.1 1. 1. ]
3 :: x = [1. 1. 1. 1.1 1. ]
4 :: x = [1. 1. 1. 1. 1.1]
Took 1.000 seconds.
J =
[[ 0. 1. 2. 3. 4.]
[ 5. 6. 7. 8. 9.]
[10. 11. 12. 13. 14.]
[15. 16. 17. 18. 19.]
[20. 21. 22. 23. 24.]]
Note that in reality the order in which the processes finish is arbitrary, and the time it took will be a little more than 1 second.
How can I get this to work as expected? Note that I am using OpenMDAO 2.5.0.

There are a few of issues here. The first is that num_par_fd should typically be passed as an __init__ arg to your Group or your Component. Setting it in the Component or Group's setup() function is too late, because OpenMDAO does all of its MPI communicator splitting in the _setup_procs function, which happens before the Component/Group setup call. The same timing issue applies to calling the approx_totals function. It must be called prior to the Problem setup call. Finally, the name of the attribute we use internally to specify the number of parallel FD computations is actually self._num_par_fd and not self.num_par_fd. Setting of the internal _num_par_fd attribute isn't recommended, but if you must, you'll have to set it before Problem setup is called.
Note: this is a heavily edited version of my original answer.

Related

How to convert grid number to coordinates when the rows alternate order

7|8|9
6|5|4
1|2|3
1 -> (1,1)
2 -> (2,1)
3 -> (3,1)
4 -> (3,2)
5 -> (2,2)
6 -> (1,2)
7 -> (1,3)
8 -> (2,3)
9 -> (3,3)
In this grid, the mapping of the numbers to coordinates is shown above.
I'm struggling to come up with a formula where given the number of the grid and the number of rows and columns in the grid, it outputs the coordinates of the grid.
I tried following the logic in this question but in this question, the coordinate system starts from 0 and the rows are not alternating.
If there was no alternating and the numbers were all starting at 0 and not 1, then you could apply Euclidean division directly:
x = n % 3
y = n // 3
where // gives the quotient of the Euclidean division, and % gives the remainder of the Euclidean division.
If there was no alternating, but the numbers all start at 1 instead of 0, then you can fix the above formula by removing 1 from n to make it start at 0, then adding 1 to x and y to make them start at 1:
x = ((n - 1) % 3) + 1
y = ((n - 1) // 3) + 1
Now all we have to change to take the alternating into account is to flip the x values on the right-to-left rows.
y remains unchanged, and x remains unchanged on the left-to-right rows.
The right-to-left rows are the rows with an even y, and you can flip x symmetrically around 2 by removing it from 4:
if y % 2 == 0:
x = 4 - x
Putting it all together in a function and testing it, in python:
def coord(n):
y = ((n-1) // 3) + 1
x = ((n-1) % 3) + 1
if y % 2 == 0: # right-to-left row
x = 4 - x # flip horizontally
return (x, y)
for n in range(1, 9+1):
x, y = coord(n)
print(f'{n} -> ({x},{y})')
Output:
1 -> (1,1)
2 -> (2,1)
3 -> (3,1)
4 -> (3,2)
5 -> (2,2)
6 -> (1,2)
7 -> (1,3)
8 -> (2,3)
9 -> (3,3)
Inverse function
The inverse operation of a Euclidean division is a multiplication and an addition:
if y % 2 == 1:
n = 3 * (y-1) + x
else:
n = 3 * (y-1) + 4 - x

CVXPY violates constraints when it solves SDP

Let's say that I want to solve the following problem.
minimize Tr(CY)
s.t. Y = xxT
x is 0 or 1.
where xxT indicates an outer product of n-1 dimension vector x. C is a n-1 by n-1 square matrix. To convert this problem to a problem with a single matrix variable, I can write down the code as follows by using cvxpy.
import cvxpy as cp
import numpy as np
n = 8
np.random.seed(1)
S = np.zeros(shape=(int(n), int(n)))
S[int(n-1), int(n-1)] = 1
C = np.zeros(shape=(n,n))
C[:n-1, :n-1] = np.random.randn(n-1, n-1)
X = cp.Variable((n,n), PSD=True)
constraints=[]
constraints.append(cp.trace(S # X) == 1)
for i in range(n-1):
Q = np.zeros(shape=(n,n))
Q[i,i] = 1
Q[-1,i] = -0.5
Q[i,-1] = -0.5
const = cp.trace(Q # X) == 0
constraints.append(const)
prob = cp.Problem(cp.Minimize(cp.trace(C # X)),constraints)
prob.solve(solver=cp.MOSEK)
print("X is")
print(X.value)
print("C is")
print(C)
To satisfy the binary constraint that the entries of the vector x should be one or zero, I added some constraints for the matrix variable X.
X = [Y x; xT 1]
Tr(QX) == 0
There are n-1 Q matrices which are forcing the vector x's entries to be 0 or 1.
However, when I ran this simple code, the constraints are violated severely.
Looking forward to see any suggestion or comments on this.

Collatz sequence in Erlang

The problem that I am trying to solve is as follows:
Write an Erlang function named collatz that takes one argument N. You may assume that N is an integer 1 or larger. The function should print the Collatz sequence (one number per line) starting with N. For example, collatz( 4 ) should print 4, 2, 1 (on separate lines). collatz( 6 ) should print 6, 3, 10, 5, 16, 8, 4, 2, 1 (on separate lines).
The collatz function that I have written is working properly but I am having difficulty in printing the output on separate lines. The commented-out part of the code below is my attempt to generate the output on separate lines.
collatz(1) -> [1];
collatz(N) when N rem 2 == 0 ->
[N|collatz(N div 2)];
%[io:format("Collatz is : ~p~n",[N])N|collatz(N div 2)];
collatz(N) ->
[N|collatz(3*N+1)].
%[io:format("Collatz is : ~p~n",[N])N|colla[N|collatz(N div 2)]tz(3*N+1)].
The output that I get when I call for example collatz(5) is [5,16,8,4,2,1]. I want these numbers to be printed out on separate lines.
Instead of running the whole program and printing the result, consider printing each element before each iteration, like
collatz(N) -> io:format("~p~n", [N]), collatz(next_collatz(N)).
You just need to evaluate io:format/2 before prepending N in your list…
collatz(1) ->
io:format("Collatz is : 1~n"),
[1];
collatz(N) when N rem 2 == 0 ->
io:format("Collatz is : ~p~n", [N]),
[N | collatz(N div 2)];
collatz(N) ->
io:format("Collatz is : ~p~n", [N]),
[N | collatz(3 * N + 1)].
1> C = fun C(1,_) -> io:format("1~n") ;
2> % rem is not allowed in a guard, it is why I added it in the parameters
2> C(N,0) -> io:format("~p~n",[N]), NN = N div 2, C(NN, NN rem 2);
3> C(N,_) -> io:format("~p~n",[N]), NN = 3 * N + 1, C(NN, NN rem 2) end.
#Fun<erl_eval.19.97283095>
4> Collatz = fun(N) -> C(N, N rem 2) end.
#Fun<erl_eval.44.97283095>
5> Collatz(5).
5
16
8
4
2
1
ok
6>

On submission it is giving runtime_error

t=int(input())
while t>0 :
c=0
n,h,y1,y2,e = list(map(int, input().split()))
for i in range(n):
x0,x1 = list(map(int, input().split()))
if x0==1 :
if x1 < h-y1:
e -= 1
else :
if y2 < x1 :
e -= 1
if e>0 :
c+=1
else :
break
print(c)
t-=1
It is passing the sample test cases but on submission, it is showing runtime error(NZEC) occurred.
Here is the link to the question: https://www.codechef.com/problems/PIPSQUIK
The problem is that you're reading the inputs and processing them simultaneously. So, a situation can arise in some test cases such that e<=0 but you still have some x0 x1 to read(i.e. i<n-1). In such cases, you'll break the loop because e<=0 and in next iteration of while loop, you'll try to read 5 values n,h,y1,y2,e = list(map(int, input().split())) but you'll receive only 2 values x0 x1 and hence it'll throw a ValueError: not enough values to unpack (expected 5, got 2) and hence it'll not pass all the test cases.
To fix this, just take all inputs first and then process them according to your current logic.
t=int(input())
while t>0 :
c=0
n,h,y1,y2,e = list(map(int, input().split()))
inputs = []
for i in range(n):
inputs.append(list(map(int, input().split())))
for inp in inputs:
x0,x1 = inp
if x0==1 :
if x1 < h-y1:
e -= 1
else :
if y2 < x1 :
e -= 1
if e>0 :
c+=1
else :
break
print(c)
t -= 1

Can this be expressed using Integer Programming or Constraint Programming?

Consider a fixed m by n matrix M, all of whose entries are 0 or 1. The question is whether there exists a non zero vector v, all of whose entries are -1, 0 or 1 for which Mv = 0. For example,
[0 1 1 1]
M_1 = [1 0 1 1]
[1 1 0 1]
In this example, there is no such vector v.
[1 0 0 0]
M_2 = [0 1 0 0]
[0 0 1 0]
In this example, the vector (0,0,0,1) gives M_2v = 0.
I am currently solving this problem by trying all different vectors v.
However, is it possible to express the problem as an integer
programming problem or constraint programming problem so I can use an
existing software package, such as SCIP instead which might be more
efficient.
It would help a little if you also give a positive example, not just a negative.
I might have missed something in the requirement/definitions, but here is a way of doing it in the Constraint Programming (CP) system MiniZinc (http://minizinc.org/). It don't use any specific constraints unique to CP systems - except perhaps for the function syntax, so it should be possible to translate it to other CP or IP systems.
% dimensions
int: rows = 3;
int: cols = 4;
% the matrix
array[1..rows, 1..cols] of int: M = array2d(1..rows,1..cols,
[0, 1, 1, 1,
1, 0, 1, 1,
1, 1, 0, 1,
] );
% function for matrix multiplication: res = matrix x vec
function array[int] of var int: matrix_mult(array[int,int] of var int: m,
array[int] of var int: v) =
let {
array[index_set_2of2(m)] of var int: res; % result
constraint
forall(i in index_set_1of2(m)) (
res[i] = sum(j in index_set_2of2(m)) (
m[i,j]*v[j]
)
)
;
} in res; % return value
solve satisfy;
constraint
% M x v = 0
matrix_mult(M, v) = [0 | j in 1..cols] /\
sum(i in 1..cols) (abs(v[i])) != 0 % non-zero vector
;
output
[
"v: ", show(v), "\n",
"M: ",
]
++
[
if j = 1 then "\n" else " " endif ++
show(M[i,j])
| i in 1..rows, j in 1..cols
];
By changing the definition of "M" to use decision variables with the domain 0..1 instead of constants:
array[1..rows, 1..cols] of var 0..1: M;
then this model yield 18066 different solutions, for example these two:
v: [-1, 1, 1, 1]
M:
1 0 0 1
1 1 0 0
1 0 0 1
----------
v: [-1, 1, 1, 1]
M:
0 0 0 0
1 0 1 0
1 0 0 1
Note: Generating all solutions is probably more common in CP systems than in traditional MIP systems (this is a feature that I really appreciate).

Resources