Cookbook#
We start with importing the CrossPy library, as well as NumPy and CuPy for demostration.
import crosspy as xp
import numpy as np
import cupy as cp
Create arrays#
We create a NumPy array and a CuPy and then merge them into a CrossPy array.
x_cpu = np.array([1, 2, 3])
x_cpu
array([1, 2, 3])
x_gpu = cp.array([4, 5])
x_gpu
array([4, 5])
x_gpu.device
<CUDA Device 0>
x_cross = xp.array([x_cpu, x_gpu], axis=0)
x_cross
array([1, 2, 3])@<CPU 0>; array([4, 5])@<CUDA Device 0>
x_cpu.shape
(3,)
x_gpu.shape
(2,)
x_cross.shape
(5,)
x = xp.array([x_gpu, x_gpu], axis=0)
x
array([4, 5])@<CUDA Device 0>; array([4, 5])@<CUDA Device 0>
x.shape
(4,)
x = xp.array([x_gpu, x_gpu], axis=0)
x
array([4, 5])@<CUDA Device 0>; array([4, 5])@<CUDA Device 0>
x.shape
(4,)
# xp.array([x_cpu, x_cpu.T])
Indexing#
x = x_cross[0] # slicing with a single integer
x
1
x.shape
()
x = x_cross[[0, 3]] # slicing with a list of integers
x
array([1])@<CPU 0>; array([4])@<CUDA Device 0>
x.shape
(2,)
# x = x_cross[0:2] # slicing with Python built-in slices
# x
x.shape
(2,)
from crosspy import cpu, gpu
# a = xp.array(range(6), distribution=[cpu(0), gpu(0), gpu(1)])
# a
# a.device
# from crosspy import PartitionScheme
# partition = PartitionScheme(6, default_device=cpu(0))
# partition[(0, 4, 5)] = cpu(0)
# partition[1:3] = gpu(0)
# partition[3] = gpu(1)
# a = xp.array(range(6), distribution=partition)
# a
# a.device