Skip to content

Commit 4152eff

Browse files
committed
Update dual to use parallelization
1 parent dca56ac commit 4152eff

1 file changed

Lines changed: 25 additions & 19 deletions

File tree

uxarray/grid/dual.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import numpy as np
2-
from numba import njit
2+
from numba import njit, prange
33

44
from uxarray.constants import INT_DTYPE, INT_FILL_VALUE
55

@@ -35,10 +35,17 @@ def construct_dual(grid):
3535
# Get an array with the number of edges for each face
3636
n_edges_mask = node_face_connectivity != INT_FILL_VALUE
3737
n_edges = np.sum(n_edges_mask, axis=1)
38+
max_edges = len(node_face_connectivity[0])
39+
40+
valid_node_indices = np.where(n_edges >= 3)[0]
41+
42+
construct_node_face_connectivity = np.full(
43+
(len(valid_node_indices), max_edges), INT_FILL_VALUE, dtype=INT_DTYPE
44+
)
3845

3946
# Construct and return the faces
4047
new_node_face_connectivity = construct_faces(
41-
n_node,
48+
valid_node_indices,
4249
n_edges,
4350
dual_node_x,
4451
dual_node_y,
@@ -47,14 +54,16 @@ def construct_dual(grid):
4754
node_x,
4855
node_y,
4956
node_z,
57+
construct_node_face_connectivity,
58+
max_edges,
5059
)
5160

5261
return new_node_face_connectivity
5362

5463

55-
@njit(cache=True)
64+
@njit(cache=True, parallel=True)
5665
def construct_faces(
57-
n_node,
66+
valid_node_indices,
5867
n_edges,
5968
dual_node_x,
6069
dual_node_y,
@@ -63,14 +72,16 @@ def construct_faces(
6372
node_x,
6473
node_y,
6574
node_z,
75+
construct_node_face_connectivity,
76+
max_edges,
6677
):
6778
"""Construct the faces of the dual mesh based on a given
6879
node_face_connectivity.
6980
7081
Parameters
7182
----------
72-
n_node: np.ndarray
73-
number of nodes in the primal mesh
83+
valid_node_indices: np.ndarray
84+
number of valid nodes in the primal mesh
7485
n_edges: np.ndarray
7586
array of the number of edges for each dual face
7687
dual_node_x: np.ndarray
@@ -94,28 +105,22 @@ def construct_faces(
94105
node_face_connectivity : ndarray
95106
Constructed node_face_connectivity for the dual mesh
96107
"""
97-
correction = 0
98-
max_edges = len(node_face_connectivity[0])
99-
construct_node_face_connectivity = np.full(
100-
(np.sum(n_edges > 2), max_edges), INT_FILL_VALUE, dtype=INT_DTYPE
101-
)
102-
for i in range(n_node):
103-
# If we have less than 3 edges, we can't construct anything but a line
104-
if n_edges[i] < 3:
105-
correction += 1
106-
continue
108+
n_valid = valid_node_indices.shape[0]
109+
110+
for out_idx in prange(n_valid):
111+
i = valid_node_indices[out_idx]
107112

108113
# Construct temporary face to hold unordered face nodes
109114
temp_face = np.array(
110115
[INT_FILL_VALUE for _ in range(n_edges[i])], dtype=INT_DTYPE
111116
)
112117

113118
# Get a list of the valid non fill value nodes
114-
valid_node_indices = node_face_connectivity[i][0 : n_edges[i]]
119+
connected_faces = node_face_connectivity[i][0 : n_edges[i]]
115120
index = 0
116121

117122
# Connect the face centers around the node to make dual face
118-
for node_idx in valid_node_indices:
123+
for node_idx in connected_faces:
119124
temp_face[index] = node_idx
120125
index += 1
121126

@@ -141,7 +146,8 @@ def construct_faces(
141146
dual_node_z,
142147
max_edges,
143148
)
144-
construct_node_face_connectivity[i - correction] = _face
149+
construct_node_face_connectivity[out_idx] = _face
150+
145151
return construct_node_face_connectivity
146152

147153

0 commit comments

Comments
 (0)