-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update src/to_device.jl Co-authored-by: Gabriele Bozzola <gbozzola@caltech.edu> More docs
- Loading branch information
1 parent
654daaa
commit 63c457a
Showing
6 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Frequently asked questions | ||
|
||
## Moving objects between the CPU and GPU. | ||
|
||
Occasionally, it's helpful to move data between the CPU and GPU. ClimaCore | ||
supports this through a method, [`to_device`](@ref ClimaCore.to_device). We also | ||
have a specialized version for converting only to the CPU: [`to_cpu`](@ref ClimaCore.to_cpu). | ||
|
||
For example, to create a CUDA column space from a CPU space, you can use: | ||
|
||
```julia | ||
using ClimaComms | ||
using ClimaCore | ||
using ClimaCore.CommonSpaces | ||
cpu_space = ColumnSpace(; | ||
z_elem = 10, | ||
z_min = 0, | ||
z_max = 10, | ||
staggering = CellCenter() | ||
) | ||
cuda_space = ClimaCore.to_device(ClimaComms.CUDADevice(), cpu_space) | ||
``` | ||
|
||
Similarly, we can convert back with `to_cpu`: | ||
```julia | ||
new_cpu_space = ClimaCore.to_cpu(cuda_space) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Adapt | ||
import ClimaComms | ||
|
||
""" | ||
out = to_device(device, x::Union{ | ||
DataLayouts.AbstractData, | ||
Spaces.AbstractSpace, | ||
Fields.Field, | ||
Fields.FieldVector, | ||
}) | ||
Move `x` to the given `device`. | ||
This is particularly useful to move different types of `Space.AbstractSpace`s, | ||
`Fields.Field`s, and `Fields.FieldVector`s from CPUs to GPUs and viceversa. | ||
If the input is already defined on the target device, returns a copy. | ||
This means that `out === x` will not in general be satisfied. | ||
""" | ||
function to_device( | ||
device::ClimaComms.AbstractDevice, | ||
x::Union{ | ||
DataLayouts.AbstractData, | ||
Spaces.AbstractSpace, | ||
Fields.Field, | ||
Fields.FieldVector, | ||
}, | ||
) | ||
return Adapt.adapt(ClimaComms.array_type(device), x) | ||
end | ||
|
||
to_device(::ClimaComms.CPUMultiThreaded, _) = error("Not supported") | ||
|
||
|
||
""" | ||
out = to_cpu(x::Union{ | ||
DataLayouts.AbstractData, | ||
Spaces.AbstractSpace, | ||
Fields.Field, | ||
Fields.FieldVector, | ||
}) | ||
Move `x` backing data to the CPU. | ||
This is particularly useful for `Space.AbstractSpace`s, | ||
`Fields.Field`s, and `Fields.FieldVector`s. | ||
Returns a copy. | ||
This means that `out === x` will not in general be satisfied. | ||
""" | ||
to_cpu( | ||
x::Union{ | ||
DataLayouts.AbstractData, | ||
Spaces.AbstractSpace, | ||
Fields.Field, | ||
Fields.FieldVector, | ||
}, | ||
) = to_device(ClimaComms.CPUSingleThreaded(), x) |