Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mpi: Add utility to get number of ranks on a single node #2265

Merged
merged 2 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions devito/mpi/distributed.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,16 @@ def nprocs(self):
else:
return 1

@property
def nprocs_local(self):
if self.comm is not MPI.COMM_NULL:
local_comm = MPI.Comm.Split_type(self.comm, MPI.COMM_TYPE_SHARED)
node_size = local_comm.Get_size()
local_comm.Free()
return node_size
else:
return 1

@property
def topology(self):
return self._topology
Expand Down
31 changes: 26 additions & 5 deletions examples/seismic/preset_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
__all__ = ['demo_model']


def Gardners(vp, normalize=True):
"""
Gardner's relation for vp in km/s
"""
b = 1 / (0.31 * (1e3*vp)**0.25)
if normalize:
b[vp < 1.51] = 1.0
return b


def demo_model(preset, **kwargs):
"""
Utility function to create preset `Model` objects for
Expand Down Expand Up @@ -49,6 +59,7 @@ def demo_model(preset, **kwargs):
vp = kwargs.pop('vp', 1.5)
nlayers = kwargs.pop('nlayers', 3)
fs = kwargs.pop('fs', False)
density = kwargs.pop('density', False)

if preset.lower() in ['constant-elastic']:
# A constant single-layer model in a 2D or 3D domain
Expand Down Expand Up @@ -76,7 +87,8 @@ def demo_model(preset, **kwargs):
if preset.lower() in ['constant-isotropic']:
# A constant single-layer model in a 2D or 3D domain
# with velocity 1.5 km/s.

if density:
kwargs['b'] = 1
return SeismicModel(space_order=space_order, vp=vp, origin=origin, shape=shape,
dtype=dtype, spacing=spacing, nbl=nbl, fs=fs, **kwargs)

Expand All @@ -99,7 +111,8 @@ def demo_model(preset, **kwargs):
phi = None
if len(shape) > 2 and preset.lower() not in ['constant-tti-noazimuth']:
phi = .35

if density:
kwargs['b'] = 1
return SeismicModel(space_order=space_order, vp=vp, origin=origin, shape=shape,
dtype=dtype, spacing=spacing, nbl=nbl, epsilon=epsilon,
delta=delta, theta=theta, phi=phi, bcs="damp", **kwargs)
Expand All @@ -119,6 +132,9 @@ def demo_model(preset, **kwargs):
for i in range(1, nlayers):
v[..., i*int(shape[-1] / nlayers):] = vp_i[i] # Bottom velocity

if density:
kwargs['b'] = Gardners(v)

return SeismicModel(space_order=space_order, vp=v, origin=origin, shape=shape,
dtype=dtype, spacing=spacing, nbl=nbl, bcs="damp",
fs=fs, **kwargs)
Expand All @@ -139,8 +155,7 @@ def demo_model(preset, **kwargs):
v[..., i*int(shape[-1] / nlayers):] = vp_i[i] # Bottom velocity

vs = 0.5 * v[:]
b = 1 / (0.31 * (1e3*v)**0.25)
b[v < 1.51] = 1.0
b = Gardners(v)
vs[v < 1.51] = 0.0

return SeismicModel(space_order=space_order, vp=v, vs=vs, b=b,
Expand Down Expand Up @@ -214,6 +229,9 @@ def demo_model(preset, **kwargs):
if len(shape) > 2 and preset.lower() not in ['layers-tti-noazimuth']:
phi = .25*(v - 1.5)

if density:
kwargs['b'] = Gardners(v)

model = SeismicModel(space_order=space_order, vp=v, origin=origin, shape=shape,
dtype=dtype, spacing=spacing, nbl=nbl, epsilon=epsilon,
delta=delta, theta=theta, phi=phi, bcs="damp",
Expand Down Expand Up @@ -245,6 +263,9 @@ def demo_model(preset, **kwargs):
y, x = np.ogrid[-a:shape[0]-a, -b:shape[1]-b]
v[x*x + y*y <= r*r] = vp

if density:
kwargs['b'] = Gardners(v)

return SeismicModel(space_order=space_order, vp=v, origin=origin, shape=shape,
dtype=dtype, spacing=spacing, nbl=nbl, bcs="damp",
fs=fs, **kwargs)
Expand Down Expand Up @@ -347,7 +368,7 @@ def demo_model(preset, **kwargs):

qp[:] = 3.516*((vp[:]*1000.)**2.2)*10**(-6) # Li's empirical formula

b = 1 / (0.31*(vp[:]*1000.)**0.25) # Gardner's relation
b = Gardners(vp, normalize=False)

return SeismicModel(space_order=space_order, vp=vp, qp=qp, b=b, nbl=nbl,
dtype=dtype, origin=origin, shape=shape,
Expand Down
1 change: 1 addition & 0 deletions tests/test_mpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def test_partitioning(self):
}
assert f.shape == expected[distributor.nprocs][distributor.myrank]
assert f.size_global == 225
assert distributor.nprocs_local == distributor.nprocs

@pytest.mark.parallel(mode=[2, 4])
def test_partitioning_fewer_dims(self):
Expand Down
Loading