{ "cells": [ { "cell_type": "markdown", "id": "0d6fecdf-48c0-4745-b802-2117fb3137cf", "metadata": {}, "source": [ "# Basic Computations" ] }, { "cell_type": "markdown", "id": "15a05d43-0bf5-48d3-9c88-6074eed82a04", "metadata": {}, "source": [ "## Overview\n", "### In this tutorial, you learn:\n", "\n", "* Applying basic arithmetic and NumPy functions to xarray DataArrays with CuPy.\n", "* Perform operations across multiple datasets\n", "* Understand two important concepts: broadcasting and alignment.\n", "* Performance of Xarray using Cupy vs. Numpy on different array sizes. \n", "\n", "## Prerequisites\n", "\n", "| Concepts | Importance | Notes |\n", "| --- | --- | --- |\n", "| [Familiarity with NumPy](https://foundations.projectpythia.org/core/numpy.html) | Necessary | |\n", "| [Basics of Cupy](Notebook0_Introduction) | Necessary | |\n", "| [Familiarity with Xarray](https://foundations.projectpythia.org/core/xarray.html) | Necessary | |\n", "\n", "- **Time to learn**: 40 minutes\n", "\n", "\n", "## Introduction " ] }, { "cell_type": "markdown", "id": "77343efb-de6d-423c-b1cd-934c5d6d68e1", "metadata": {}, "source": [ "First, let's import our packages\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "55c72b7d-8899-4e2f-9432-e9cf1531cbdf", "metadata": {}, "outputs": [], "source": [ "## Import NumPy and CuPy\n", "import cupy as cp\n", "import numpy as np\n", "import xarray as xr\n", "import cupy_xarray # Adds .cupy to Xarray objects" ] }, { "cell_type": "markdown", "id": "4ed42841-264a-4eb6-9f82-be9a463be816", "metadata": {}, "source": [ "### Creating Xarray DataArray with CuPy" ] }, { "cell_type": "markdown", "id": "573bb115-0e77-4f86-be9f-9ee6ac1c6f9b", "metadata": {}, "source": [ "In the previous tutorial, we learned how to create a DataArray that wraps a CuPy array:" ] }, { "cell_type": "code", "execution_count": 2, "id": "4b91fc01-9e99-4b01-b700-9b4802b7ef14", "metadata": {}, "outputs": [], "source": [ "arr_gpu = cp.random.rand(10, 10)" ] }, { "cell_type": "code", "execution_count": 3, "id": "431edeb0-661f-4929-a83d-e39a6e753a60", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
<xarray.DataArray (x: 10, y: 10)>\n", "array([[0.64440645, 0.62072123, 0.75168547, 0.41128605, 0.88459028,\n", " 0.47016308, 0.86304331, 0.92990986, 0.0041129 , 0.4666957 ],\n", " [0.56647797, 0.11373418, 0.62628122, 0.78959584, 0.36494045,\n", " 0.13310425, 0.73672578, 0.86921365, 0.05596426, 0.55426342],\n", " [0.4720759 , 0.6481852 , 0.46598961, 0.93751977, 0.97099829,\n", " 0.94932666, 0.54603983, 0.29783205, 0.36190421, 0.44288443],\n", " [0.62394009, 0.14474529, 0.36714822, 0.30050983, 0.44310121,\n", " 0.45300226, 0.84836414, 0.41480516, 0.15972742, 0.30865762],\n", " [0.17974085, 0.43178982, 0.68688623, 0.2870211 , 0.94622374,\n", " 0.05305575, 0.10551911, 0.50202377, 0.32414185, 0.52343633],\n", " [0.57433335, 0.55480641, 0.65053659, 0.84821379, 0.86448478,\n", " 0.4614566 , 0.41249327, 0.04641715, 0.9086778 , 0.55099052],\n", " [0.99359918, 0.19577754, 0.42470934, 0.20198499, 0.49022272,\n", " 0.56950438, 0.55683842, 0.81856686, 0.97131091, 0.73117734],\n", " [0.05195378, 0.09355582, 0.23061675, 0.48168679, 0.20765511,\n", " 0.44548051, 0.54251798, 0.63568233, 0.61946882, 0.48324004],\n", " [0.89803925, 0.89935711, 0.57733868, 0.21010146, 0.15491007,\n", " 0.27044434, 0.14652858, 0.35991027, 0.87969536, 0.57918609],\n", " [0.31083571, 0.29447116, 0.06544057, 0.46585981, 0.0189647 ,\n", " 0.08291839, 0.16705158, 0.53118993, 0.99264236, 0.75636455]])\n", "Dimensions without coordinates: x, y
<xarray.DataArray (space: 3, time: 4)>\n", "array([[ 0, 1, 2, 3],\n", " [ 4, 5, 6, 7],\n", " [ 8, 9, 10, 11]])\n", "Coordinates:\n", " * space (space) <U1 'a' 'b' 'c'\n", " * time (time) int64 0 1 2 3
<xarray.DataArray (space: 2, time: 7)>\n", "array([[ 0, 1, 2, 3, 4, 5, 6],\n", " [ 7, 8, 9, 10, 11, 12, 13]])\n", "Coordinates:\n", " * space (space) <U1 'b' 'd'\n", " * time (time) int64 -2 -1 0 1 2 3 4" ], "text/plain": [ "
<xarray.DataArray (space: 3)>\n", "array([0, 1, 2])\n", "Coordinates:\n", " * space (space) <U1 'a' 'b' 'c'
<xarray.DataArray (time: 4)>\n", "array([0, 1, 2, 3])\n", "Coordinates:\n", " * time (time) int64 0 1 2 3" ], "text/plain": [ "
<xarray.DataArray (space: 3, time: 4)>\n", "array([[0, 0, 0, 0],\n", " [1, 1, 1, 1],\n", " [2, 2, 2, 2]])\n", "Coordinates:\n", " * space (space) <U1 'a' 'b' 'c'\n", " * time (time) int64 0 1 2 3
<xarray.DataArray (space: 3, time: 4)>\n", "array([[0, 1, 2, 3],\n", " [0, 1, 2, 3],\n", " [0, 1, 2, 3]])\n", "Coordinates:\n", " * time (time) int64 0 1 2 3\n", " * space (space) <U1 'a' 'b' 'c'" ], "text/plain": [ "