Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equal distribution of particles to MPI processors #1231

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion parcels/collection/collectionaos.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from ctypes import c_void_p

import copy
import numpy as np

from parcels.collection.collections import ParticleCollection
Expand Down Expand Up @@ -103,7 +104,17 @@ def __init__(self, pclass, lon, lat, depth, time, lonlatdepth_dtype, pid_orig, p
if mpi_rank == 0:
coords = np.vstack((lon, lat)).transpose()
kmeans = KMeans(n_clusters=mpi_size, random_state=0).fit(coords)
self._pu_indicators = kmeans.labels_
labels = np.linspace(0, mpi_size, lon.size, endpoint=False)
labels = np.floor(labels)
reorderedCoords = copy.copy(labels)
for i in range(0, len(kmeans.cluster_centers_)):
clusterCentre = kmeans.cluster_centers_[i]
distances = map(lambda point: ((point[0]-clusterCentre[0])**2 + (point[1]-clusterCentre[1])**2), coords)
sortedDistanceIdxs = np.argsort(list(distances))
numberToChoose = sum(labels == i)
reorderedCoords[sortedDistanceIdxs[0:numberToChoose]] = i
coords[sortedDistanceIdxs[0:numberToChoose],:] = float('inf')
self._pu_indicators = reorderedCoords
else:
self._pu_indicators = None
self._pu_indicators = mpi_comm.bcast(self._pu_indicators, root=0)
Expand Down
16 changes: 13 additions & 3 deletions parcels/collection/collectionsoa.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ctypes import Structure, POINTER
from bisect import bisect_left
from math import floor

import copy
import numpy as np

from parcels.collection.collections import ParticleCollection
Expand Down Expand Up @@ -101,10 +101,20 @@ def __init__(self, pclass, lon, lat, depth, time, lonlatdepth_dtype, pid_orig, p
if mpi_size > 1:
if partitions is not False:
if (self._pu_indicators is None) or (len(self._pu_indicators) != len(lon)):
if mpi_rank == 0:
if mpi_rank == 0:
coords = np.vstack((lon, lat)).transpose()
kmeans = KMeans(n_clusters=mpi_size, random_state=0).fit(coords)
self._pu_indicators = kmeans.labels_
labels = np.linspace(0, mpi_size, lon.size, endpoint=False)
labels = np.floor(labels)
reorderedCoords = copy.copy(labels)
for i in range(0, len(kmeans.cluster_centers_)):
clusterCentre = kmeans.cluster_centers_[i]
distances = map(lambda point: ((point[0]-clusterCentre[0])**2 + (point[1]-clusterCentre[1])**2), coords)
sortedDistanceIdxs = np.argsort(list(distances))
numberToChoose = sum(labels == i)
reorderedCoords[sortedDistanceIdxs[0:numberToChoose]] = i
coords[sortedDistanceIdxs[0:numberToChoose],:] = float('inf')
self._pu_indicators = reorderedCoords
else:
self._pu_indicators = None
self._pu_indicators = mpi_comm.bcast(self._pu_indicators, root=0)
Expand Down