Skip to content
Draft
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
729 changes: 729 additions & 0 deletions source/guides/debbuging-performance/cupy-naive.ipynb

Large diffs are not rendered by default.

107 changes: 107 additions & 0 deletions source/guides/debbuging-performance/cupy_optimal.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "13f87ff2-5ce9-4954-918d-be5d3eea36b1",
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"import cupy as cp\n",
"import numpy as np\n",
"import xarray as xr"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1eb0759a-0a92-423d-9c4b-264507e1283a",
"metadata": {},
"outputs": [],
"source": [
"ds = xr.open_dataset(\"air.sig995.2025.nc\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "15b6a0c9-4206-4702-9580-05a1c37b4869",
"metadata": {},
"outputs": [],
"source": [
"t0 = time.perf_counter()\n",
"\n",
"air = ds[\"air\"].cupy.as_cupy()\n",
"T = air.data\n",
"ntime, nlat, nlon = T.shape\n",
"\n",
"# --- Build X = anomalies (time × space) ---\n",
"X = T.reshape(ntime, nlat * nlon)\n",
"X = X - X.mean(axis=0, keepdims=True) # remove 2025 mean at each grid point\n",
"\n",
"# --- EOF via SVD ---\n",
"U, S, Vt = np.linalg.svd(X, full_matrices=False)\n",
"\n",
"# First mode\n",
"eof1 = Vt[0, :].reshape(nlat, nlon) # spatial pattern\n",
"pc1 = U[:, 0] * S[0] # time series\n",
"var_frac1 = (S[0] ** 2) / np.sum(S**2) # variance fraction (0–1)\n",
"\n",
"cp.cuda.Stream.null.synchronize() # wait for cupy to finish the computation\n",
"\n",
"elapsed = time.perf_counter() - t0"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3b965656-ddc6-4117-b025-04ca8b8ce017",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Time for EOF/PC calculation: 1.081 s\n"
]
}
],
"source": [
"print(f\"Time for EOF/PC calculation: {elapsed:.3f} s\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "30e67178-7273-4e36-acde-a80d3de65943",
"metadata": {},
"outputs": [],
"source": [
"# noticed memory usage and utilization"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
14 changes: 14 additions & 0 deletions source/guides/debbuging-performance/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: xarray-example
channels:
- conda-forge
- rapidsai
dependencies:
- python>=3.12
- xarray
- netCDF4
- bottleneck
- numpy
- cupy-xarray
- ipykernel # needed on brev to enable kernel
# - matplotlib
# - cartopy
Loading
Loading