Commit ff6f6d65 authored by Martin Reinecke's avatar Martin Reinecke

MPI fixes

parent 063b978c
Pipeline #28155 passed with stage
in 54 minutes and 11 seconds
......@@ -26,6 +26,11 @@ IC = ift.GradientNormController(
calls = 0
def mprint(*args):
if ift.dobj.rank == 0:
print(*args)
def test_rosenbrock_convex(minimizer):
np.random.seed(42)
space = ift.UnstructuredDomain((2,))
......@@ -41,25 +46,21 @@ def test_rosenbrock_convex(minimizer):
@property
def value(self):
x = self.position.val[0]
y = self.position.val[1]
x, y = self.position.to_global_data()
return (self.a-x)*(self.a-x)+self.b*(y-x*x)*(y-x*x)
@property
def gradient(self):
x = self.position.val[0]
y = self.position.val[1]
res = ift.Field.zeros(space)
res.val[0] = -2*(self.a-x)-4*self.b*x*(y-x*x)
res.val[1] = 2*self.b*(y-x*x)
return res
x, y = self.position.to_global_data()
v0 = -2*(self.a-x)-4*self.b*x*(y-x*x)
v1 = 2*self.b*(y-x*x)
return ift.Field.from_global_data(space, np.array([v0, v1]))
@property
def curvature(self):
class RBCurv(ift.EndomorphicOperator):
def __init__(self, loc, a, b):
self._x = loc.val[0]
self._y = loc.val[1]
self._x, self._y = loc.to_global_data()
self.a = a
self.b = b
......@@ -72,15 +73,15 @@ def test_rosenbrock_convex(minimizer):
return self.TIMES
def apply(self, x, mode):
x = x.val
res = ift.Field.zeros(space)
res.val[0] = (2+self.b*8*self._x**2)*x[0]
res.val[0] -= self.b*4*self._x*x[1]
res.val[1] = -self.b*4*self._x*x[0]
res.val[1] += 2*self.b*x[1]
x = x.to_global_data()
v0 = (2+self.b*8*self._x**2)*x[0]
v0 -= self.b*4*self._x*x[1]
v1 = -self.b*4*self._x*x[0]
v1 += 2*self.b*x[1]
global calls
calls += 1
return res
return ift.Field.from_global_data(space,
np.array([v0, v1]))
t1 = ift.GradientNormController(tol_abs_gradnorm=1e-6,
iteration_limit=1000)
t2 = ift.ConjugateGradient(controller=t1)
......@@ -109,27 +110,27 @@ def test_Ndim_rosenbrocklike_convex(minimizer, Ndim=3):
@property
def value(self):
x = self.position.val
x = self.position.to_global_data()
t1 = self.a-x[0]
t2 = x[1:]-x[:-1]**3
return 0.5*t1**2+0.5*self.b*np.dot(t2, t2)
@property
def gradient(self):
res = ift.Field.zeros(space)
x = self.position.val
res = np.zeros(space.shape)
x = self.position.to_global_data()
t1 = self.a-x[0]
t2 = x[1:]-x[:-1]**3
res.val[0] = -t1
res.val[1:] += self.b*t2
res.val[:-1] += -3*self.b*x[:-1]**2*t2
return res
res[0] = -t1
res[1:] += self.b*t2
res[:-1] += -3*self.b*x[:-1]**2*t2
return ift.Field.from_global_data(space, res)
@property
def curvature(self):
class RBCurv(ift.EndomorphicOperator):
def __init__(self, loc, a, b):
self._x = loc.val
self._x = loc.to_global_data()
self.a = a
self.b = b
......@@ -142,16 +143,16 @@ def test_Ndim_rosenbrocklike_convex(minimizer, Ndim=3):
return self.TIMES
def apply(self, z, mode):
y = z.val
res = ift.Field.zeros_like(z)
y = z.to_global_data()
res = np.zeros(space.shape)
x = self._x
res.val[0] = y[0]
res[0] = y[0]
dt2 = y[1:] - 3*x[:-1]**2*y[:-1]
res.val[1:] += self.b*dt2
res.val[:-1] += -self.b*3*x[:-1]**2*dt2
res[1:] += self.b*dt2
res[:-1] += -self.b*3*x[:-1]**2*dt2
global calls
calls += 1
return res
return ift.Field.from_global_data(space, res)
t1 = ift.GradientNormController(tol_abs_gradnorm=1e-6,
iteration_limit=1000)
t2 = ift.ConjugateGradient(controller=t1)
......@@ -166,21 +167,21 @@ def test_Ndim_rosenbrocklike_convex(minimizer, Ndim=3):
def verbose_test(func, minimizer):
print("Testing", minimizer)
mprint("Testing", minimizer)
global calls
calls = 0
E = func(minimizer)
print("Used", calls, "calls.")
print("Final energy:", E.value)
mprint("Used", calls, "calls.")
mprint("Final energy:", E.value)
if __name__ == "__main__":
print("Standard Rosenbrock function\n")
mprint("Standard Rosenbrock function\n")
verbose_test(test_rosenbrock_convex, ift.Yango)
verbose_test(test_rosenbrock_convex, ift.RelaxedNewton)
verbose_test(test_rosenbrock_convex, ift.NonlinearCG)
verbose_test(test_rosenbrock_convex, ift.L_BFGS)
print("\nHigher-dimensional Rosenbrock function\n")
mprint("\nHigher-dimensional Rosenbrock function\n")
verbose_test(test_Ndim_rosenbrocklike_convex, ift.Yango)
verbose_test(test_Ndim_rosenbrocklike_convex, ift.RelaxedNewton)
verbose_test(test_Ndim_rosenbrocklike_convex, ift.NonlinearCG)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment