Skip to main content

Dask Local Example

Demonstrates how to spin up a local Dask cluster and run gradient/RTM jobs using the composable API (Dataset + DevitoPhysics).

Prerequisites: a zarr shot dataset and an initial model in zarr or HDF5 format on disk. Update data_path, dump_dir, and model_path below to point at your data before running.

import os
import matplotlib.pyplot as plt
from distributed import LocalCluster
from dask.distributed import Client

from devitohorizon.data import Dataset
from devitohorizon.model import load_model
from devitohorizon.physics import DevitoPhysics
from devitohorizon.scheduler import create_scheduler

os.environ['DEVITO_LOGGING'] = 'ERROR'
os.environ['OMP_NUM_THREADS'] = '4'
param = {
    'tn': 1200,
    't0': 0.0,
    'f0': 0.010,
    'nshots': 6,
    'shape': (101, 101),
    'spacing': (10.0, 10.0),
    'origin': (0.0, 0.0),
    'nbl': 40,
    'space_order': 8,
    'data_path': 'dask-local',
    'dump_dir': 'dask-local',
    'gradient': {'mute': {'type': 'datum'}},
    'parameters': ('m',),
    'data_format': 'zarr',
}
os.makedirs(param['data_path'], exist_ok=True)
cluster = LocalCluster(n_workers=2, threads_per_worker=1, death_timeout=600)
client = Client(cluster)
scheduler = create_scheduler(client)
print(f"Dashboard: {client.dashboard_link}")
# Load observed shot data from a zarr store.
# Replace param['data_path'] with the path to your zarr shot dataset.
dataset = Dataset.from_zarr(param['data_path'])
print(f'Loaded {len(dataset.shot_ids)} shots from {param["data_path"]}')
# Load the initial velocity model.
# Replace model_path with the path to your starting model (zarr or h5).
model_path = os.path.join(param['dump_dir'], 'initial_model.zarr')
model0 = load_model(model_path, nbl=param['nbl'], space_order=param['space_order'])
param_runtime = dict(param)
param_runtime['model'] = model_path
# Build physics using the composable API
physics = DevitoPhysics(
    nbl=param_runtime['nbl'],
    space_order=param_runtime['space_order'],
    parameters=param_runtime['parameters'],
    model=param_runtime['model'],
    gradient=param_runtime['gradient'],
)
# Inspect a representative shot
shot = dataset[0]
print(f'Shot 0: rec shape={shot.data.shape}, dt={shot.dt} ms')
plt.imshow(shot.data, cmap='seismic', aspect='auto', vmin=-0.05, vmax=0.05)
plt.title('Synthetic data (shot 0)')
# Compute gradient for a single shot using objective API
shot_batch = dataset.subset([0])
misfit, grad_dict = physics.objective(shot_batch, physics.model)
print(f'Shot misfit: {misfit:.6e}')
plt.imshow(grad_dict['m'].T, cmap='seismic', aspect='auto')
plt.title('Shot 0 gradient (m parameter)')
# Run a tiny RTM over a few shots distributed with Dask using physics.image()
shot_ids = [0, 2, 4]
shot_subset = dataset.subset(shot_ids)
scheduler = create_scheduler(client)
stacked = physics.image(shot_subset, scheduler=scheduler)
print(f'RTM computed over {len(shot_ids)} shots')
plt.imshow(stacked.T, cmap='seismic', aspect='auto')
plt.title('Mini RTM result (stacked gradient)')
client.close()
cluster.close()
Back to top