-
Notifications
You must be signed in to change notification settings - Fork 95
/
_spatial_mp.py
273 lines (220 loc) · 9.86 KB
/
_spatial_mp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2010-2021 Pyresample developers
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""Multiprocessing versions of KDTree and Proj classes."""
from __future__ import absolute_import
import ctypes
import multiprocessing as mp
import numpy as np
import pyproj
try:
import numexpr as ne
except ImportError:
ne = None
from ._multi_proc import Scheduler, shmem_as_ndarray
# Earth radius
R = 6370997.0
class cKDTree_MP(object):
"""Multiprocessing cKDTree subclass, shared memory."""
def __init__(self, data, leafsize=10, nprocs=2, chunk=None,
schedule='guided'):
"""Prepare shared memory for KDTree operations.
Same as cKDTree.__init__ except that an internal copy of data to shared memory is made.
Extra keyword arguments:
chunk : Minimum chunk size for the load balancer.
schedule: Strategy for balancing work load
('static', 'dynamic' or 'guided').
"""
self.n, self.m = data.shape
# Allocate shared memory for data
self.shmem_data = mp.RawArray(ctypes.c_double, self.n * self.m)
# View shared memory as ndarray, and copy over the data.
# The RawArray objects have information about the dtype and
# buffer size.
_data = shmem_as_ndarray(self.shmem_data).reshape((self.n, self.m))
_data[:, :] = data
# Initialize parent, we must do this last because
# cKDTree stores a reference to the data array. We pass in
# the copy in shared memory rather than the origial data.
self.leafsize = leafsize
self._nprocs = nprocs
self._chunk = chunk
self._schedule = schedule
def query(self, x, k=1, eps=0, p=2, distance_upper_bound=np.inf):
"""Query for points at index 'x' parallelized with multiple processes and shared memory."""
# allocate shared memory for x and result
nx = x.shape[0]
shmem_x = mp.RawArray(ctypes.c_double, nx * self.m)
shmem_d = mp.RawArray(ctypes.c_double, nx * k)
shmem_i = mp.RawArray(ctypes.c_int, nx * k)
# view shared memory as ndarrays
_x = shmem_as_ndarray(shmem_x).reshape((nx, self.m))
if k == 1:
_d = shmem_as_ndarray(shmem_d)
_i = shmem_as_ndarray(shmem_i)
else:
_d = shmem_as_ndarray(shmem_d).reshape((nx, k))
_i = shmem_as_ndarray(shmem_i).reshape((nx, k))
# copy x to shared memory
_x[:] = x
# set up a scheduler to load balance the query
scheduler = Scheduler(nx, self._nprocs, chunk=self._chunk,
schedule=self._schedule)
# query with multiple processes
query_args = [scheduler, self.shmem_data, self.n, self.m,
self.leafsize, shmem_x, nx, shmem_d, shmem_i,
k, eps, p, distance_upper_bound]
_run_jobs(_parallel_query, query_args, self._nprocs)
# return results (private memory)
return _d.copy(), _i.copy()
class Proj_MP:
"""Multi-processing version of the pyproj Proj class."""
def __init__(self, *args, **kwargs):
self._args = args
self._kwargs = kwargs
def __call__(self, data1, data2, inverse=False, radians=False,
errcheck=False, nprocs=2, chunk=None, schedule='guided'):
"""Transform coordinates to coordinates in the current coordinate system."""
grid_shape = data1.shape
n = data1.size
# Create shared memory
shmem_data1 = mp.RawArray(ctypes.c_double, n)
shmem_data2 = mp.RawArray(ctypes.c_double, n)
shmem_res1 = mp.RawArray(ctypes.c_double, n)
shmem_res2 = mp.RawArray(ctypes.c_double, n)
# view shared memory as ndarrays
_data1 = shmem_as_ndarray(shmem_data1)
_data2 = shmem_as_ndarray(shmem_data2)
_res1 = shmem_as_ndarray(shmem_res1)
_res2 = shmem_as_ndarray(shmem_res2)
# copy input data to shared memory
_data1[:] = data1.ravel()
_data2[:] = data2.ravel()
# set up a scheduler to load balance the query
scheduler = Scheduler(n, nprocs, chunk=chunk, schedule=schedule)
# Projection with multiple processes
proj_call_args = [scheduler, shmem_data1, shmem_data2, shmem_res1,
shmem_res2, self._args, self._kwargs, inverse,
radians, errcheck]
_run_jobs(_parallel_proj, proj_call_args, nprocs)
return _res1.copy().reshape(grid_shape), _res2.copy().reshape(grid_shape)
class Cartesian(object):
"""Cartesian coordinates."""
def __init__(self, *args, **kwargs):
pass
def transform_lonlats(self, lons, lats):
"""Transform longitudes and latitues to cartesian coordinates."""
if np.issubdtype(lons.dtype, np.integer):
lons = lons.astype(np.float64)
coords = np.zeros((lons.size, 3), dtype=lons.dtype)
if ne:
deg2rad = np.pi / 180 # noqa: F841
coords[:, 0] = ne.evaluate("R*cos(lats*deg2rad)*cos(lons*deg2rad)")
coords[:, 1] = ne.evaluate("R*cos(lats*deg2rad)*sin(lons*deg2rad)")
coords[:, 2] = ne.evaluate("R*sin(lats*deg2rad)")
else:
coords[:, 0] = R * np.cos(np.deg2rad(lats)) * np.cos(np.deg2rad(lons))
coords[:, 1] = R * np.cos(np.deg2rad(lats)) * np.sin(np.deg2rad(lons))
coords[:, 2] = R * np.sin(np.deg2rad(lats))
return coords
Cartesian_MP = Cartesian
def _run_jobs(target, args, nprocs):
"""Run process pool."""
# return status in shared memory
# access to these values are serialized automatically
ierr = mp.Value(ctypes.c_int, 0)
warn_msg = mp.Array(ctypes.c_char, 1024)
args.extend((ierr, warn_msg))
pool = [mp.Process(target=target, args=args) for n in range(nprocs)]
for p in pool:
p.start()
for p in pool:
p.join()
if ierr.value != 0:
raise RuntimeError('%d errors in worker processes. Last one reported:\n%s' %
(ierr.value, warn_msg.value.decode()))
# This is executed in an external process:
def _parallel_query(scheduler, # scheduler for load balancing
# data needed to reconstruct the kd-tree
data, ndata, ndim, leafsize,
x, nx, d, i, # query data and results
k, eps, p, dub, # auxillary query parameters
ierr, warn_msg): # return values (0 on success)
try:
# View shared memory as ndarrays.
_data = shmem_as_ndarray(data).reshape((ndata, ndim))
_x = shmem_as_ndarray(x).reshape((nx, ndim))
if k == 1:
_d = shmem_as_ndarray(d)
_i = shmem_as_ndarray(i)
else:
_d = shmem_as_ndarray(d).reshape((nx, k))
_i = shmem_as_ndarray(i).reshape((nx, k))
# Reconstruct the kd-tree from the data.
import scipy.spatial as sp
kdtree = sp.cKDTree(_data, leafsize=leafsize)
# Query for nearest neighbours, using slice ranges,
# from the load balancer.
for s in scheduler:
if k == 1:
_d[s], _i[s] = kdtree.query(_x[s, :], k=1, eps=eps, p=p,
distance_upper_bound=dub)
else:
_d[s, :], _i[s, :] = kdtree.query(_x[s, :], k=k, eps=eps, p=p,
distance_upper_bound=dub)
# An error occured, increment the return value ierr.
# Access to ierr is serialized by multiprocessing.
except Exception as e:
ierr.value += 1
warn_msg.value = str(e).encode()
def _parallel_proj(scheduler, data1, data2, res1, res2, proj_args, proj_kwargs,
inverse, radians, errcheck, ierr, warn_msg):
try:
# View shared memory as ndarrays.
_data1 = shmem_as_ndarray(data1)
_data2 = shmem_as_ndarray(data2)
_res1 = shmem_as_ndarray(res1)
_res2 = shmem_as_ndarray(res2)
# Initialise pyproj
proj = pyproj.Proj(*proj_args, **proj_kwargs)
# Reproject data segment
for s in scheduler:
_res1[s], _res2[s] = proj(_data1[s], _data2[s], inverse=inverse,
radians=radians, errcheck=errcheck)
# An error occured, increment the return value ierr.
# Access to ierr is serialized by multiprocessing.
except Exception as e:
ierr.value += 1
warn_msg.value = str(e).encode()
def _parallel_transform(scheduler, lons, lats, n, coords, ierr, warn_msg):
try:
# View shared memory as ndarrays.
_lons = shmem_as_ndarray(lons)
_lats = shmem_as_ndarray(lats)
_coords = shmem_as_ndarray(coords).reshape((n, 3))
# Transform to cartesian coordinates
for s in scheduler:
_coords[s, 0] = R * \
np.cos(np.radians(_lats[s])) * np.cos(np.radians(_lons[s]))
_coords[s, 1] = R * \
np.cos(np.radians(_lats[s])) * np.sin(np.radians(_lons[s]))
_coords[s, 2] = R * np.sin(np.radians(_lats[s]))
# An error occured, increment the return value ierr.
# Access to ierr is serialized by multiprocessing.
except Exception as e:
ierr.value += 1
warn_msg.value = str(e).encode()