Parallelization using joblib¶
In [1]:
Copied!
import time
import gmspy as gm
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
import time
import gmspy as gm
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from joblib import Parallel, delayed
You can check the configuration of this computer in advance
In [2]:
Copied!
import psutil
print("the number of physical cores:", psutil.cpu_count(logical=False))
print("the number of logical cores:", psutil.cpu_count())
print("CPU frequency:", psutil.cpu_freq())
import psutil
print("the number of physical cores:", psutil.cpu_count(logical=False))
print("the number of logical cores:", psutil.cpu_count())
print("CPU frequency:", psutil.cpu_freq())
the number of physical cores: 14 the number of logical cores: 20 CPU frequency: scpufreq(current=2300.0, min=0.0, max=2300.0)
Below is the ground motion data built into gmspy, you can also document the PEER database through loadPEER, or you can read the data from text using numpy.loadtxt.
In [3]:
Copied!
ts, acc = gm.load_gm_examples("Kobe")
dt = ts[1] - ts[0]
# You can also use other acc data
ts, acc = gm.load_gm_examples("Kobe")
dt = ts[1] - ts[0]
# You can also use other acc data
Suppose we have 1000 ground motions:
In [4]:
Copied!
all_accs = [acc] * 1000
all_accs = [acc] * 1000
A parallel function needs to be defined, here is the IMs of the ground motions
In [5]:
Copied!
def get_ims(acc, dt):
"""Compute intensity measures for the given acceleration data."""
GM = gm.SeismoGM(dt=dt, acc=acc, unit="g")
ims = GM.get_ims(display_results=False)
pga = ims["PGA"]
pgv = ims["PGV"]
pgd = ims["PGD"]
ia = ims["Ia"] # Arias Intensity
# elastic spectral acceleration for T=1.0s, index 0 is the spectral acceleration
sat1 = GM.get_elas_spec(Ts=1.0, damp_ratio=0.05)[0]
# average spectral acceleration, index 0 is the spectral acceleration
Tavg = np.arange(0.05, 4.05, 0.05)
avgsa = GM.get_avgsavd(Tavg=Tavg)[0]
return pga, pgv, pgd, ia, sat1, avgsa
def get_ims(acc, dt):
"""Compute intensity measures for the given acceleration data."""
GM = gm.SeismoGM(dt=dt, acc=acc, unit="g")
ims = GM.get_ims(display_results=False)
pga = ims["PGA"]
pgv = ims["PGV"]
pgd = ims["PGD"]
ia = ims["Ia"] # Arias Intensity
# elastic spectral acceleration for T=1.0s, index 0 is the spectral acceleration
sat1 = GM.get_elas_spec(Ts=1.0, damp_ratio=0.05)[0]
# average spectral acceleration, index 0 is the spectral acceleration
Tavg = np.arange(0.05, 4.05, 0.05)
avgsa = GM.get_avgsavd(Tavg=Tavg)[0]
return pga, pgv, pgd, ia, sat1, avgsa
In [6]:
Copied!
start_time = time.time()
start_time = time.time()
For more details, please refer to joblib.Parallel.
n_jobs=-1 means using all possible CPU cores.
In [7]:
Copied!
output = Parallel(n_jobs=-1)(delayed(get_ims)(acc, dt=dt) for acc in all_accs)
output = Parallel(n_jobs=-1)(delayed(get_ims)(acc, dt=dt) for acc in all_accs)
In [8]:
Copied!
end_time = time.time()
print(f"Parallel computation took {end_time - start_time:.2f} seconds")
end_time = time.time()
print(f"Parallel computation took {end_time - start_time:.2f} seconds")
Parallel computation took 18.98 seconds
Convert to pandas.DataFrame
In [9]:
Copied!
IMS = pd.DataFrame(output, columns=["PGA", "PGV", "PGD", "IA", "SAT1", "AVGSA"])
IMS.head()
IMS = pd.DataFrame(output, columns=["PGA", "PGV", "PGD", "IA", "SAT1", "AVGSA"])
IMS.head()
Out[9]:
| PGA | PGV | PGD | IA | SAT1 | AVGSA | |
|---|---|---|---|---|---|---|
| 0 | 0.3447 | 27.677934 | 9.693236 | 1.68744 | 0.351312 | 0.155794 |
| 1 | 0.3447 | 27.677934 | 9.693236 | 1.68744 | 0.351312 | 0.155794 |
| 2 | 0.3447 | 27.677934 | 9.693236 | 1.68744 | 0.351312 | 0.155794 |
| 3 | 0.3447 | 27.677934 | 9.693236 | 1.68744 | 0.351312 | 0.155794 |
| 4 | 0.3447 | 27.677934 | 9.693236 | 1.68744 | 0.351312 | 0.155794 |