Skip to content

Commit 47e6371

Browse files
committed
try to make arrays F_CONTIGUOUS
1 parent b63d534 commit 47e6371

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

experiments/ClimaEarth/Manifest-v1.11.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.11.6"
44
manifest_format = "2.0"
5-
project_hash = "bf902fbe4cdfe69d6bdc0c99b12a9c00c5aa28ee"
5+
project_hash = "4aefcc1c61d8db91f115838c0af8a131cde73b99"
66

77
[[deps.ADTypes]]
88
git-tree-sha1 = "27cecae79e5cc9935255f90c53bb831cc3c870d7"

experiments/ClimaEarth/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ NCDatasets = "85f8d34a-cbdd-5861-8df4-14fed0d494ab"
2727
Oceananigans = "9e8cae18-63c1-5223-a75c-80ca9d6e9a09"
2828
Poppler_jll = "9c32591e-4766-534b-9725-b71a8799265b"
2929
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
30+
PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
3031
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
3132
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
3233
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"

experiments/ClimaEarth/components/ocean/oceananigans.jl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Thermodynamics as TD
88
import ClimaOcean.EN4: download_dataset
99
using KernelAbstractions: @kernel, @index, @inbounds
1010
using XESMF # to load Oceananigans regridding extension
11+
using PythonCall
1112

1213
OceananigansXESMFExt = Base.get_extension(OC, :OceananigansXESMFExt).OceananigansXESMFExt;
1314

@@ -204,9 +205,20 @@ function construct_remappers(grid, boundary_space)
204205

205206
coords_climacore = Dict("lat" => climacore_lat, "lon" => climacore_lon)
206207

208+
# Make arrays F-contiguous (column major) using `np.asfortranarray` to reduce memory footprint and
209+
# prevent segfaults since XESMF works more efficiently with F-contiguous arrays.
210+
np = pyimport("numpy")
211+
coords_oc_contiguous =
212+
Dict(k => pyconvert(Array, np.asfortranarray(v)) for (k, v) in coords_oc)
213+
coords_climacore_contiguous =
214+
Dict(k => pyconvert(Array, np.asfortranarray(v)) for (k, v) in coords_climacore)
215+
207216
# Construct the XESMF regridder object
208-
regridder_oceananigans_to_climacore =
209-
XESMF.Regridder(coords_oc, coords_climacore; method = "bilinear")
217+
regridder_oceananigans_to_climacore = XESMF.Regridder(
218+
coords_oc_contiguous,
219+
coords_climacore_contiguous;
220+
method = "bilinear",
221+
)
210222

211223
# Allocate space for source an destination vectors to use as intermediate storage
212224
src_vec_oc = Array(vec(OC.Field{OC.Center, OC.Center, Nothing}(grid))) # 2D field on Center/Center
@@ -223,10 +235,17 @@ function construct_remappers(grid, boundary_space)
223235
)
224236

225237
## Remapper: Cubed sphere nodes to Oceananigans grid `Center, Center`
238+
# For a TripolarGrid, latitude and longitude are already 2D arrays,
239+
# so we broadcast over them directly.
226240
long_oc = OC.λnodes(grid, OC.Center(), OC.Center(), OC.Center())
227241
lat_oc = OC.φnodes(grid, OC.Center(), OC.Center(), OC.Center())
228-
long_oc = reshape(long_oc, length(long_oc), 1)
229-
lat_oc = reshape(lat_oc, 1, length(lat_oc))
242+
243+
# For a LatitudeLongitudeGrid, latitude and longitude are 1D collections,
244+
# so we reshape them to 2D arrays.
245+
if grid isa OC.LatitudeLongitudeGrid
246+
long_oc = reshape(long_oc, length(long_oc), 1)
247+
lat_oc = reshape(lat_oc, 1, length(lat_oc))
248+
end
230249
target_points_oc = @. CC.Geometry.LatLongPoint(lat_oc, long_oc)
231250

232251
# Construct the ClimaCore remapper object

0 commit comments

Comments
 (0)