[yt-svn] commit/yt: 4 new changesets
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Wed Jun 1 11:23:43 PDT 2016
4 new commits in yt:
https://bitbucket.org/yt_analysis/yt/commits/d35990cd0b35/
Changeset: d35990cd0b35
Branch: yt
User: MatthewTurk
Date: 2016-05-19 01:38:27+00:00
Summary: First pass at using memoryviews in AMRKDTree
Affected #: 2 files
diff -r de4aaa4344b2ffd78d9e4411074b9b398f5e6ad4 -r d35990cd0b359dc798703bcbebc1a58053679469 yt/utilities/lib/amr_kdtools.pxd
--- a/yt/utilities/lib/amr_kdtools.pxd
+++ b/yt/utilities/lib/amr_kdtools.pxd
@@ -35,6 +35,6 @@
cdef Split * split
cdef int level
-cdef int point_in_node(Node node, np.ndarray[np.float64_t, ndim=1] point)
-cdef Node _find_node(Node node, np.float64_t *point)
+cdef int point_in_node(Node node, np.float64_t[:] point)
+cdef Node _find_node(Node node, np.float64_t[:] point)
cdef int _kd_is_leaf(Node node)
diff -r de4aaa4344b2ffd78d9e4411074b9b398f5e6ad4 -r d35990cd0b359dc798703bcbebc1a58053679469 yt/utilities/lib/amr_kdtools.pyx
--- a/yt/utilities/lib/amr_kdtools.pyx
+++ b/yt/utilities/lib/amr_kdtools.pyx
@@ -34,8 +34,8 @@
Node parent,
Node left,
Node right,
- np.ndarray[np.float64_t, ndim=1] left_edge,
- np.ndarray[np.float64_t, ndim=1] right_edge,
+ np.float64_t[:] left_edge,
+ np.float64_t[:] right_edge,
int grid,
np.int64_t node_id):
self.dirty = False
@@ -77,12 +77,12 @@
def get_right_edge(self):
return get_right_edge(self)
- def set_left_edge(self, np.ndarray[np.float64_t, ndim=1] left_edge):
+ def set_left_edge(self, np.float64_t[:] left_edge):
cdef int i
for i in range(3):
self.left_edge[i] = left_edge[i]
- def set_right_edge(self, np.ndarray[np.float64_t, ndim=1] right_edge):
+ def set_right_edge(self, np.float64_t[:] right_edge):
cdef int i
for i in range(3):
self.right_edge[i] = right_edge[i]
@@ -155,8 +155,8 @@
@cython.wraparound(False)
@cython.cdivision(True)
cdef add_grid(Node node,
- np.float64_t *gle,
- np.float64_t *gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int gid,
int rank,
int size):
@@ -179,8 +179,8 @@
return
def add_pygrid(Node node,
- np.ndarray[np.float64_t, ndim=1] gle,
- np.ndarray[np.float64_t, ndim=1] gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int gid,
int rank,
int size):
@@ -189,23 +189,21 @@
The entire purpose of this function is to move everything from ndarrays
to internal C pointers.
"""
- pgles = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- pgres = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ pgles = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
+ pgres = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
cdef int j
for j in range(3):
pgles[j] = gle[j]
pgres[j] = gre[j]
add_grid(node, pgles, pgres, gid, rank, size)
- free(pgles)
- free(pgres)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef insert_grid(Node node,
- np.float64_t *gle,
- np.float64_t *gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int grid_id,
int rank,
int size):
@@ -239,47 +237,11 @@
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
-def add_pygrids(Node node,
- int ngrids,
- np.ndarray[np.float64_t, ndim=2] gles,
- np.ndarray[np.float64_t, ndim=2] gres,
- np.ndarray[np.int64_t, ndim=1] gids,
- int rank,
- int size):
- """
- The entire purpose of this function is to move everything from ndarrays
- to internal C pointers.
- """
- pgles = <np.float64_t **> malloc(ngrids * sizeof(np.float64_t*))
- pgres = <np.float64_t **> malloc(ngrids * sizeof(np.float64_t*))
- pgids = <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
- for i in range(ngrids):
- pgles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- pgres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- pgids[i] = gids[i]
- for j in range(3):
- pgles[i][j] = gles[i, j]
- pgres[i][j] = gres[i, j]
-
- add_grids(node, ngrids, pgles, pgres, pgids, rank, size)
-
- for i in range(ngrids):
- free(pgles[i])
- free(pgres[i])
- free(pgles)
- free(pgres)
- free(pgids)
-
-
-
- at cython.boundscheck(False)
- at cython.wraparound(False)
- at cython.cdivision(True)
cdef add_grids(Node node,
int ngrids,
- np.float64_t **gles,
- np.float64_t **gres,
- np.int64_t *gids,
+ np.float64_t[:,:] gles,
+ np.float64_t[:,:] gres,
+ np.int64_t[:] gids,
int rank,
int size):
cdef int i, j, nless, ngreater
@@ -290,44 +252,38 @@
insert_grids(node, ngrids, gles, gres, gids, rank, size)
return
- less_ids= <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
- greater_ids = <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
+ less_ids= <np.int64_t[:ngrids] > alloca(ngrids * sizeof(np.int64_t))
+ greater_ids = <np.int64_t[:ngrids] > alloca(ngrids * sizeof(np.int64_t))
nless = 0
ngreater = 0
for i in range(ngrids):
- if gles[i][node.split.dim] < node.split.pos:
+ if gles[i, node.split.dim] < node.split.pos:
less_ids[nless] = i
nless += 1
- if gres[i][node.split.dim] > node.split.pos:
+ if gres[i, node.split.dim] > node.split.pos:
greater_ids[ngreater] = i
ngreater += 1
#print 'nless: %i' % nless
#print 'ngreater: %i' % ngreater
- less_gles = <np.float64_t **> malloc(nless * sizeof(np.float64_t*))
- less_gres = <np.float64_t **> malloc(nless * sizeof(np.float64_t*))
- l_ids = <np.int64_t *> malloc(nless * sizeof(np.int64_t))
- for i in range(nless):
- less_gles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- less_gres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ less_gles = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
+ less_gres = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
+ l_ids = <np.int64_t[:nless] > alloca(nless * sizeof(np.int64_t))
- greater_gles = <np.float64_t **> malloc(ngreater * sizeof(np.float64_t*))
- greater_gres = <np.float64_t **> malloc(ngreater * sizeof(np.float64_t*))
- g_ids = <np.int64_t *> malloc(ngreater * sizeof(np.int64_t))
- for i in range(ngreater):
- greater_gles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- greater_gres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ greater_gles = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
+ greater_gres = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
+ g_ids = <np.int64_t[:ngreater] > alloca(ngreater * sizeof(np.int64_t))
cdef int index
for i in range(nless):
index = less_ids[i]
l_ids[i] = gids[index]
for j in range(3):
- less_gles[i][j] = gles[index][j]
- less_gres[i][j] = gres[index][j]
+ less_gles[i,j] = gles[index,j]
+ less_gres[i,j] = gres[index,j]
if nless > 0:
add_grids(node.left, nless, less_gles, less_gres,
@@ -337,28 +293,13 @@
index = greater_ids[i]
g_ids[i] = gids[index]
for j in range(3):
- greater_gles[i][j] = gles[index][j]
- greater_gres[i][j] = gres[index][j]
+ greater_gles[i,j] = gles[index,j]
+ greater_gres[i,j] = gres[index,j]
if ngreater > 0:
add_grids(node.right, ngreater, greater_gles, greater_gres,
g_ids, rank, size)
- for i in range(nless):
- free(less_gles[i])
- free(less_gres[i])
- free(less_gles)
- free(less_gres)
- free(less_ids)
- free(l_ids)
- for i in range(ngreater):
- free(greater_gles[i])
- free(greater_gres[i])
- free(greater_gles)
- free(greater_gres)
- free(greater_ids)
- free(g_ids)
-
return
@cython.boundscheck(False)
@@ -374,9 +315,9 @@
@cython.cdivision(True)
cdef void insert_grids(Node node,
int ngrids,
- np.float64_t **gles,
- np.float64_t **gres,
- np.int64_t *gids,
+ np.float64_t[:,:] gles,
+ np.float64_t[:,:] gres,
+ np.int64_t[:] gids,
int rank,
int size):
@@ -388,12 +329,12 @@
if ngrids == 1:
# If we should continue to split based on parallelism, do so!
if should_i_split(node, rank, size):
- geo_split(node, gles[0], gres[0], gids[0], rank, size)
+ geo_split(node, gles[0,:], gres[0,:], gids[0], rank, size)
return
for i in range(3):
- contained *= gles[0][i] <= node.left_edge[i]
- contained *= gres[0][i] >= node.right_edge[i]
+ contained *= gles[0,i] <= node.left_edge[i]
+ contained *= gres[0,i] >= node.right_edge[i]
if contained == 1:
# print 'Node fully contained, setting to grid: %i' % gids[0]
@@ -413,23 +354,20 @@
@cython.wraparound(False)
@cython.cdivision(True)
cdef split_grid(Node node,
- np.float64_t *gle,
- np.float64_t *gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int gid,
int rank,
int size):
cdef int j
- data = <np.float64_t ***> alloca(sizeof(np.float64_t**))
- data[0] = <np.float64_t **> alloca(2 * sizeof(np.float64_t*))
- for j in range(2):
- data[0][j] = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
+ data = <np.float64_t[:1,:2,:3] > alloca(sizeof(np.float64_t**))
for j in range(3):
- data[0][0][j] = gle[j]
- data[0][1][j] = gre[j]
+ data[0,0,j] = gle[j]
+ data[0,1,j] = gre[j]
- less_ids = <np.uint8_t *> alloca(1 * sizeof(np.uint8_t))
- greater_ids = <np.uint8_t *> alloca(1 * sizeof(np.uint8_t))
+ less_ids = <np.uint8_t[:1] > alloca(1 * sizeof(np.uint8_t))
+ greater_ids = <np.uint8_t[:1] > alloca(1 * sizeof(np.uint8_t))
best_dim, split_pos, nless, ngreater = \
kdtree_get_choices(1, data, node.left_edge, node.right_edge,
@@ -466,11 +404,11 @@
@cython.wraparound(False)
@cython.cdivision(True)
cdef kdtree_get_choices(int n_grids,
- np.float64_t ***data,
- np.float64_t *l_corner,
- np.float64_t *r_corner,
- np.uint8_t *less_ids,
- np.uint8_t *greater_ids,
+ np.float64_t[:,:,:] data,
+ np.float64_t[:] l_corner,
+ np.float64_t[:] r_corner,
+ np.uint8_t[:] less_ids,
+ np.uint8_t[:] greater_ids,
):
cdef int i, j, k, dim, n_unique, best_dim, my_split
cdef np.float64_t split
@@ -514,7 +452,7 @@
free(uniquedims)
return -1, 0, 0, 0
# I recognize how lame this is.
- cdef np.ndarray[np.float64_t, ndim=1] tarr = np.empty(my_max, dtype='float64')
+ cdef np.float64_t[:] tarr = np.empty(my_max, dtype='float64')
for i in range(my_max):
# print "Setting tarr: ", i, uniquedims[best_dim][i]
tarr[i] = uniquedims[best_dim][i]
@@ -545,25 +483,23 @@
#@cython.cdivision(True)
cdef int split_grids(Node node,
int ngrids,
- np.float64_t **gles,
- np.float64_t **gres,
- np.int64_t *gids,
+ np.float64_t[:,:] gles,
+ np.float64_t[:,:] gres,
+ np.int64_t[:] gids,
int rank,
int size):
# Find a Split
cdef int i, j
- data = <np.float64_t ***> malloc(ngrids * sizeof(np.float64_t**))
+ data = <np.float64_t[:ngrids,:2,:3]> alloca(ngrids * 2 * 3 * sizeof(np.float64_t))
+
for i in range(ngrids):
- data[i] = <np.float64_t **> malloc(2 * sizeof(np.float64_t*))
- for j in range(2):
- data[i][j] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
for j in range(3):
- data[i][0][j] = gles[i][j]
- data[i][1][j] = gres[i][j]
+ data[i,0,j] = gles[i,j]
+ data[i,1,j] = gres[i,j]
- less_ids = <np.uint8_t *> malloc(ngrids * sizeof(np.uint8_t))
- greater_ids = <np.uint8_t *> malloc(ngrids * sizeof(np.uint8_t))
+ less_ids = <np.uint8_t[:ngrids]> alloca(ngrids * sizeof(np.uint8_t))
+ greater_ids = <np.uint8_t[:ngrids]> alloca(ngrids * sizeof(np.uint8_t))
best_dim, split_pos, nless, ngreater = \
kdtree_get_choices(ngrids, data, node.left_edge, node.right_edge,
@@ -583,8 +519,8 @@
# Create a Split
divide(node, split)
- less_index = <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
- greater_index = <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
+ less_index = <np.int64_t[:ngrids]> alloca(ngrids * sizeof(np.int64_t))
+ greater_index = <np.int64_t[:ngrids]> alloca(ngrids * sizeof(np.int64_t))
nless = 0
ngreater = 0
@@ -597,27 +533,21 @@
greater_index[ngreater] = i
ngreater += 1
- less_gles = <np.float64_t **> malloc(nless * sizeof(np.float64_t*))
- less_gres = <np.float64_t **> malloc(nless * sizeof(np.float64_t*))
- l_ids = <np.int64_t *> malloc(nless * sizeof(np.int64_t))
- for i in range(nless):
- less_gles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- less_gres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ less_gles = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
+ less_gres = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
+ l_ids = <np.int64_t[:nless] > alloca(nless * sizeof(np.int64_t))
- greater_gles = <np.float64_t **> malloc(ngreater * sizeof(np.float64_t*))
- greater_gres = <np.float64_t **> malloc(ngreater * sizeof(np.float64_t*))
- g_ids = <np.int64_t *> malloc(ngreater * sizeof(np.int64_t))
- for i in range(ngreater):
- greater_gles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- greater_gres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ greater_gles = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
+ greater_gres = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
+ g_ids = <np.int64_t[:ngreater] > alloca(ngreater * sizeof(np.int64_t))
cdef int index
for i in range(nless):
index = less_index[i]
l_ids[i] = gids[index]
for j in range(3):
- less_gles[i][j] = gles[index][j]
- less_gres[i][j] = gres[index][j]
+ less_gles[i,j] = gles[index,j]
+ less_gres[i,j] = gres[index,j]
if nless > 0:
# Populate Left Node
@@ -629,8 +559,8 @@
index = greater_index[i]
g_ids[i] = gids[index]
for j in range(3):
- greater_gles[i][j] = gles[index][j]
- greater_gres[i][j] = gres[index][j]
+ greater_gles[i,j] = gles[index,j]
+ greater_gres[i,j] = gres[index,j]
if ngreater > 0:
# Populate Right Node
@@ -638,34 +568,11 @@
insert_grids(node.right, ngreater, greater_gles, greater_gres,
g_ids, rank, size)
- for i in range(nless):
- free(less_gles[i])
- free(less_gres[i])
- free(less_gles)
- free(less_gres)
- free(less_ids)
- free(less_index)
- free(l_ids)
- for i in range(ngreater):
- free(greater_gles[i])
- free(greater_gres[i])
- free(greater_gles)
- free(greater_gres)
- free(greater_ids)
- free(greater_index)
- free(g_ids)
-
- for i in range(ngrids):
- for j in range(2):
- free(data[i][j])
- free(data[i])
- free(data)
-
return 0
cdef geo_split(Node node,
- np.float64_t *gle,
- np.float64_t *gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int grid_id,
int rank,
int size):
@@ -681,10 +588,10 @@
new_pos = (gre[big_dim] + gle[big_dim])/2.
- lnew_gle = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
- lnew_gre = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
- rnew_gle = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
- rnew_gre = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
+ lnew_gle = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
+ lnew_gre = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
+ rnew_gle = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
+ rnew_gre = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
for j in range(3):
lnew_gle[j] = gle[j]
@@ -716,8 +623,8 @@
# Create a Split
node.split = split
- cdef np.ndarray[np.float64_t, ndim=1] le = np.zeros(3, dtype='float64')
- cdef np.ndarray[np.float64_t, ndim=1] re = np.zeros(3, dtype='float64')
+ cdef np.float64_t[:] le = np.empty(3, dtype='float64')
+ cdef np.float64_t[:] re = np.empty(3, dtype='float64')
cdef int i
for i in range(3):
@@ -907,7 +814,7 @@
return current, previous
cdef int point_in_node(Node node,
- np.ndarray[np.float64_t, ndim=1] point):
+ np.float64_t[:] point):
cdef int i
cdef int inside = 1
for i in range(3):
@@ -915,7 +822,7 @@
inside *= node.right_edge[i] > point[i]
return inside
-cdef Node _find_node(Node node, np.float64_t *point):
+cdef Node _find_node(Node node, np.float64_t[:] point):
while _kd_is_leaf(node) == 0:
if point[node.split.dim] < node.split.pos:
node = node.left
@@ -924,10 +831,10 @@
return node
def find_node(Node node,
- np.ndarray[np.float64_t, ndim=1] point):
+ np.float64_t[:] point):
"""
Find the AMRKDTree node enclosing a position
"""
assert(point_in_node(node, point))
- return _find_node(node, <np.float64_t *> point.data)
+ return _find_node(node, point)
https://bitbucket.org/yt_analysis/yt/commits/1a51100e858a/
Changeset: 1a51100e858a
Branch: yt
User: MatthewTurk
Date: 2016-05-19 13:55:43+00:00
Summary: Fixing some issues with add_grids importing and zero-size memoryviews.
Affected #: 2 files
diff -r d35990cd0b359dc798703bcbebc1a58053679469 -r 1a51100e858ab6b49f40eb99619fd3baab699af1 yt/utilities/amr_kdtree/amr_kdtree.py
--- a/yt/utilities/amr_kdtree/amr_kdtree.py
+++ b/yt/utilities/amr_kdtree/amr_kdtree.py
@@ -25,7 +25,7 @@
scatter_image
from yt.utilities.lib.amr_kdtools import \
Node, \
- add_pygrids, \
+ add_grids, \
find_node, \
kd_is_leaf, \
set_dirty, \
@@ -95,7 +95,7 @@
gles = np.array([g.LeftEdge for g in grids])
gres = np.array([g.RightEdge for g in grids])
gids = np.array([g.id for g in grids], dtype="int64")
- add_pygrids(self.trunk, gids.size, gles, gres, gids,
+ add_grids(self.trunk, gids.size, gles, gres, gids,
self.comm_rank, self.comm_size)
del gles, gres, gids, grids
diff -r d35990cd0b359dc798703bcbebc1a58053679469 -r 1a51100e858ab6b49f40eb99619fd3baab699af1 yt/utilities/lib/amr_kdtools.pyx
--- a/yt/utilities/lib/amr_kdtools.pyx
+++ b/yt/utilities/lib/amr_kdtools.pyx
@@ -237,14 +237,16 @@
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
-cdef add_grids(Node node,
+cpdef add_grids(Node node,
int ngrids,
np.float64_t[:,:] gles,
np.float64_t[:,:] gres,
np.int64_t[:] gids,
int rank,
int size):
- cdef int i, j, nless, ngreater
+ cdef int i, j, nless, ngreater, index
+ cdef np.float64_t[:,:] less_gles, less_gres, greater_gles, greater_gres
+ cdef np.int64_t[:] l_ids, g_ids
if not should_i_build(node, rank, size):
return
@@ -269,34 +271,33 @@
#print 'nless: %i' % nless
#print 'ngreater: %i' % ngreater
- less_gles = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
- less_gres = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
- l_ids = <np.int64_t[:nless] > alloca(nless * sizeof(np.int64_t))
+ if nless > 0:
+ less_gles = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
+ less_gres = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
+ l_ids = <np.int64_t[:nless] > alloca(nless * sizeof(np.int64_t))
- greater_gles = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
- greater_gres = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
- g_ids = <np.int64_t[:ngreater] > alloca(ngreater * sizeof(np.int64_t))
+ for i in range(nless):
+ index = less_ids[i]
+ l_ids[i] = gids[index]
+ for j in range(3):
+ less_gles[i,j] = gles[index,j]
+ less_gres[i,j] = gres[index,j]
- cdef int index
- for i in range(nless):
- index = less_ids[i]
- l_ids[i] = gids[index]
- for j in range(3):
- less_gles[i,j] = gles[index,j]
- less_gres[i,j] = gres[index,j]
-
- if nless > 0:
add_grids(node.left, nless, less_gles, less_gres,
l_ids, rank, size)
- for i in range(ngreater):
- index = greater_ids[i]
- g_ids[i] = gids[index]
- for j in range(3):
- greater_gles[i,j] = gles[index,j]
- greater_gres[i,j] = gres[index,j]
+ if ngreater > 0:
+ greater_gles = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
+ greater_gres = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
+ g_ids = <np.int64_t[:ngreater] > alloca(ngreater * sizeof(np.int64_t))
- if ngreater > 0:
+ for i in range(ngreater):
+ index = greater_ids[i]
+ g_ids[i] = gids[index]
+ for j in range(3):
+ greater_gles[i,j] = gles[index,j]
+ greater_gres[i,j] = gres[index,j]
+
add_grids(node.right, ngreater, greater_gles, greater_gres,
g_ids, rank, size)
@@ -452,7 +453,7 @@
free(uniquedims)
return -1, 0, 0, 0
# I recognize how lame this is.
- cdef np.float64_t[:] tarr = np.empty(my_max, dtype='float64')
+ cdef np.ndarray[np.float64_t, ndim=1] tarr = np.empty(my_max, dtype='float64')
for i in range(my_max):
# print "Setting tarr: ", i, uniquedims[best_dim][i]
tarr[i] = uniquedims[best_dim][i]
@@ -489,7 +490,10 @@
int rank,
int size):
# Find a Split
- cdef int i, j
+ cdef int i, j, index
+ cdef np.float64_t[:,:] less_gles, less_gres, greater_gles, greater_gres
+ cdef np.int64_t[:] l_ids, g_ids
+ if ngrids == 0: return 0
data = <np.float64_t[:ngrids,:2,:3]> alloca(ngrids * 2 * 3 * sizeof(np.float64_t))
@@ -533,36 +537,35 @@
greater_index[ngreater] = i
ngreater += 1
- less_gles = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
- less_gres = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
- l_ids = <np.int64_t[:nless] > alloca(nless * sizeof(np.int64_t))
+ if nless > 0:
+ less_gles = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
+ less_gres = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
+ l_ids = <np.int64_t[:nless] > alloca(nless * sizeof(np.int64_t))
- greater_gles = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
- greater_gres = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
- g_ids = <np.int64_t[:ngreater] > alloca(ngreater * sizeof(np.int64_t))
+ for i in range(nless):
+ index = less_index[i]
+ l_ids[i] = gids[index]
+ for j in range(3):
+ less_gles[i,j] = gles[index,j]
+ less_gres[i,j] = gres[index,j]
- cdef int index
- for i in range(nless):
- index = less_index[i]
- l_ids[i] = gids[index]
- for j in range(3):
- less_gles[i,j] = gles[index,j]
- less_gres[i,j] = gres[index,j]
-
- if nless > 0:
# Populate Left Node
#print 'Inserting left node', node.left_edge, node.right_edge
insert_grids(node.left, nless, less_gles, less_gres,
l_ids, rank, size)
- for i in range(ngreater):
- index = greater_index[i]
- g_ids[i] = gids[index]
- for j in range(3):
- greater_gles[i,j] = gles[index,j]
- greater_gres[i,j] = gres[index,j]
+ if ngreater > 0:
+ greater_gles = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
+ greater_gres = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
+ g_ids = <np.int64_t[:ngreater] > alloca(ngreater * sizeof(np.int64_t))
- if ngreater > 0:
+ for i in range(ngreater):
+ index = greater_index[i]
+ g_ids[i] = gids[index]
+ for j in range(3):
+ greater_gles[i,j] = gles[index,j]
+ greater_gres[i,j] = gres[index,j]
+
# Populate Right Node
#print 'Inserting right node', node.left_edge, node.right_edge
insert_grids(node.right, ngreater, greater_gles, greater_gres,
https://bitbucket.org/yt_analysis/yt/commits/4598672884af/
Changeset: 4598672884af
Branch: yt
User: MatthewTurk
Date: 2016-05-19 15:32:28+00:00
Summary: Switch to reference-counted cython arrays from alloca.
Affected #: 1 file
diff -r 1a51100e858ab6b49f40eb99619fd3baab699af1 -r 4598672884af864f742e31f1b2d5fcdc640a6e1c yt/utilities/lib/amr_kdtools.pyx
--- a/yt/utilities/lib/amr_kdtools.pyx
+++ b/yt/utilities/lib/amr_kdtools.pyx
@@ -17,11 +17,7 @@
cimport numpy as np
cimport cython
from libc.stdlib cimport malloc, free
-
-cdef extern from "platform_dep.h":
- # NOTE that size_t might not be int
- void *alloca(int)
-
+from cython.view cimport array as cvarray
DEF Nch = 4
@@ -189,8 +185,8 @@
The entire purpose of this function is to move everything from ndarrays
to internal C pointers.
"""
- pgles = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
- pgres = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
+ cdef np.float64_t[:] pgles = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
+ cdef np.float64_t[:] pgres = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
cdef int j
for j in range(3):
pgles[j] = gle[j]
@@ -254,8 +250,8 @@
insert_grids(node, ngrids, gles, gres, gids, rank, size)
return
- less_ids= <np.int64_t[:ngrids] > alloca(ngrids * sizeof(np.int64_t))
- greater_ids = <np.int64_t[:ngrids] > alloca(ngrids * sizeof(np.int64_t))
+ less_ids = cvarray(format="q", shape=(ngrids,), itemsize=sizeof(np.int64_t))
+ greater_ids = cvarray(format="q", shape=(ngrids,), itemsize=sizeof(np.int64_t))
nless = 0
ngreater = 0
@@ -272,9 +268,9 @@
#print 'ngreater: %i' % ngreater
if nless > 0:
- less_gles = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
- less_gres = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
- l_ids = <np.int64_t[:nless] > alloca(nless * sizeof(np.int64_t))
+ less_gles = cvarray(format="d", shape=(nless,3), itemsize=sizeof(np.float64_t))
+ less_gres = cvarray(format="d", shape=(nless,3), itemsize=sizeof(np.float64_t))
+ l_ids = cvarray(format="q", shape=(nless,), itemsize=sizeof(np.int64_t))
for i in range(nless):
index = less_ids[i]
@@ -287,9 +283,9 @@
l_ids, rank, size)
if ngreater > 0:
- greater_gles = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
- greater_gres = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
- g_ids = <np.int64_t[:ngreater] > alloca(ngreater * sizeof(np.int64_t))
+ greater_gles = cvarray(format="d", shape=(ngreater,3), itemsize=sizeof(np.float64_t))
+ greater_gres = cvarray(format="d", shape=(ngreater,3), itemsize=sizeof(np.float64_t))
+ g_ids = cvarray(format="q", shape=(ngreater,), itemsize=sizeof(np.int64_t))
for i in range(ngreater):
index = greater_ids[i]
@@ -362,13 +358,14 @@
int size):
cdef int j
- data = <np.float64_t[:1,:2,:3] > alloca(sizeof(np.float64_t**))
+ cdef np.uint8_t[:] less_ids, greater_ids
+ data = cvarray(format="d", shape=(1,2,3), itemsize=sizeof(np.float64_t))
for j in range(3):
data[0,0,j] = gle[j]
data[0,1,j] = gre[j]
- less_ids = <np.uint8_t[:1] > alloca(1 * sizeof(np.uint8_t))
- greater_ids = <np.uint8_t[:1] > alloca(1 * sizeof(np.uint8_t))
+ less_ids = cvarray(format="B", shape=(1,), itemsize=sizeof(np.uint8_t))
+ greater_ids = cvarray(format="B", shape=(1,), itemsize=sizeof(np.uint8_t))
best_dim, split_pos, nless, ngreater = \
kdtree_get_choices(1, data, node.left_edge, node.right_edge,
@@ -413,12 +410,9 @@
):
cdef int i, j, k, dim, n_unique, best_dim, my_split
cdef np.float64_t split
- cdef np.float64_t **uniquedims
- cdef np.float64_t *uniques
- uniquedims = <np.float64_t **> malloc(3 * sizeof(np.float64_t*))
- for i in range(3):
- uniquedims[i] = <np.float64_t *> \
- malloc(2*n_grids * sizeof(np.float64_t))
+ cdef np.float64_t[:,:] uniquedims
+ cdef np.float64_t[:] uniques
+ uniquedims = cvarray(format="d", shape=(3, 2*n_grids), itemsize=sizeof(np.float64_t))
my_max = 0
my_split = 0
best_dim = -1
@@ -448,9 +442,6 @@
my_max = n_unique
my_split = (n_unique-1)/2
if best_dim == -1:
- for i in range(3):
- free(uniquedims[i])
- free(uniquedims)
return -1, 0, 0, 0
# I recognize how lame this is.
cdef np.ndarray[np.float64_t, ndim=1] tarr = np.empty(my_max, dtype='float64')
@@ -472,10 +463,6 @@
else:
greater_ids[i] = 0
- for i in range(3):
- free(uniquedims[i])
- free(uniquedims)
-
# Return out unique values
return best_dim, split, nless, ngreater
@@ -495,15 +482,15 @@
cdef np.int64_t[:] l_ids, g_ids
if ngrids == 0: return 0
- data = <np.float64_t[:ngrids,:2,:3]> alloca(ngrids * 2 * 3 * sizeof(np.float64_t))
+ data = cvarray(format="d", shape=(ngrids,2,3), itemsize=sizeof(np.float64_t))
for i in range(ngrids):
for j in range(3):
data[i,0,j] = gles[i,j]
data[i,1,j] = gres[i,j]
- less_ids = <np.uint8_t[:ngrids]> alloca(ngrids * sizeof(np.uint8_t))
- greater_ids = <np.uint8_t[:ngrids]> alloca(ngrids * sizeof(np.uint8_t))
+ less_ids = cvarray(format="B", shape=(ngrids,), itemsize=sizeof(np.uint8_t))
+ greater_ids = cvarray(format="B", shape=(ngrids,), itemsize=sizeof(np.uint8_t))
best_dim, split_pos, nless, ngreater = \
kdtree_get_choices(ngrids, data, node.left_edge, node.right_edge,
@@ -523,8 +510,8 @@
# Create a Split
divide(node, split)
- less_index = <np.int64_t[:ngrids]> alloca(ngrids * sizeof(np.int64_t))
- greater_index = <np.int64_t[:ngrids]> alloca(ngrids * sizeof(np.int64_t))
+ less_index = cvarray(format="q", shape=(ngrids,), itemsize=sizeof(np.int64_t))
+ greater_index = cvarray(format="q", shape=(ngrids,), itemsize=sizeof(np.int64_t))
nless = 0
ngreater = 0
@@ -538,9 +525,9 @@
ngreater += 1
if nless > 0:
- less_gles = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
- less_gres = <np.float64_t[:nless,:3]> alloca(3*nless * sizeof(np.float64_t))
- l_ids = <np.int64_t[:nless] > alloca(nless * sizeof(np.int64_t))
+ less_gles = cvarray(format="d", shape=(nless,3), itemsize=sizeof(np.float64_t))
+ less_gres = cvarray(format="d", shape=(nless,3), itemsize=sizeof(np.float64_t))
+ l_ids = cvarray(format="q", shape=(nless,), itemsize=sizeof(np.int64_t))
for i in range(nless):
index = less_index[i]
@@ -555,9 +542,9 @@
l_ids, rank, size)
if ngreater > 0:
- greater_gles = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
- greater_gres = <np.float64_t[:ngreater,:3]> alloca(3*ngreater * sizeof(np.float64_t))
- g_ids = <np.int64_t[:ngreater] > alloca(ngreater * sizeof(np.int64_t))
+ greater_gles = cvarray(format="d", shape=(ngreater,3), itemsize=sizeof(np.float64_t))
+ greater_gres = cvarray(format="d", shape=(ngreater,3), itemsize=sizeof(np.float64_t))
+ g_ids = cvarray(format="q", shape=(ngreater,), itemsize=sizeof(np.int64_t))
for i in range(ngreater):
index = greater_index[i]
@@ -591,10 +578,10 @@
new_pos = (gre[big_dim] + gle[big_dim])/2.
- lnew_gle = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
- lnew_gre = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
- rnew_gle = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
- rnew_gre = <np.float64_t[:3]> alloca(3 * sizeof(np.float64_t))
+ lnew_gle = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
+ lnew_gre = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
+ rnew_gle = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
+ rnew_gre = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
for j in range(3):
lnew_gle[j] = gle[j]
https://bitbucket.org/yt_analysis/yt/commits/6873d6aa7e0a/
Changeset: 6873d6aa7e0a
Branch: yt
User: ngoldbaum
Date: 2016-06-01 18:23:34+00:00
Summary: Merged in MatthewTurk/yt (pull request #2185)
Switch to memoryviews in AMRKDTree
Affected #: 3 files
diff -r b8a09cd382dd34f386ce3634e7f78df3f5d9401d -r 6873d6aa7e0a9ac68941b52692b8287a25869160 yt/utilities/amr_kdtree/amr_kdtree.py
--- a/yt/utilities/amr_kdtree/amr_kdtree.py
+++ b/yt/utilities/amr_kdtree/amr_kdtree.py
@@ -25,7 +25,7 @@
scatter_image
from yt.utilities.lib.amr_kdtools import \
Node, \
- add_pygrids, \
+ add_grids, \
find_node, \
kd_is_leaf, \
set_dirty, \
@@ -95,7 +95,7 @@
gles = np.array([g.LeftEdge for g in grids])
gres = np.array([g.RightEdge for g in grids])
gids = np.array([g.id for g in grids], dtype="int64")
- add_pygrids(self.trunk, gids.size, gles, gres, gids,
+ add_grids(self.trunk, gids.size, gles, gres, gids,
self.comm_rank, self.comm_size)
del gles, gres, gids, grids
diff -r b8a09cd382dd34f386ce3634e7f78df3f5d9401d -r 6873d6aa7e0a9ac68941b52692b8287a25869160 yt/utilities/lib/amr_kdtools.pxd
--- a/yt/utilities/lib/amr_kdtools.pxd
+++ b/yt/utilities/lib/amr_kdtools.pxd
@@ -35,6 +35,6 @@
cdef Split * split
cdef int level
-cdef int point_in_node(Node node, np.ndarray[np.float64_t, ndim=1] point)
-cdef Node _find_node(Node node, np.float64_t *point)
+cdef int point_in_node(Node node, np.float64_t[:] point)
+cdef Node _find_node(Node node, np.float64_t[:] point)
cdef int _kd_is_leaf(Node node)
diff -r b8a09cd382dd34f386ce3634e7f78df3f5d9401d -r 6873d6aa7e0a9ac68941b52692b8287a25869160 yt/utilities/lib/amr_kdtools.pyx
--- a/yt/utilities/lib/amr_kdtools.pyx
+++ b/yt/utilities/lib/amr_kdtools.pyx
@@ -17,11 +17,7 @@
cimport numpy as np
cimport cython
from libc.stdlib cimport malloc, free
-
-cdef extern from "platform_dep.h":
- # NOTE that size_t might not be int
- void *alloca(int)
-
+from cython.view cimport array as cvarray
DEF Nch = 4
@@ -34,8 +30,8 @@
Node parent,
Node left,
Node right,
- np.ndarray[np.float64_t, ndim=1] left_edge,
- np.ndarray[np.float64_t, ndim=1] right_edge,
+ np.float64_t[:] left_edge,
+ np.float64_t[:] right_edge,
int grid,
np.int64_t node_id):
self.dirty = False
@@ -77,12 +73,12 @@
def get_right_edge(self):
return get_right_edge(self)
- def set_left_edge(self, np.ndarray[np.float64_t, ndim=1] left_edge):
+ def set_left_edge(self, np.float64_t[:] left_edge):
cdef int i
for i in range(3):
self.left_edge[i] = left_edge[i]
- def set_right_edge(self, np.ndarray[np.float64_t, ndim=1] right_edge):
+ def set_right_edge(self, np.float64_t[:] right_edge):
cdef int i
for i in range(3):
self.right_edge[i] = right_edge[i]
@@ -155,8 +151,8 @@
@cython.wraparound(False)
@cython.cdivision(True)
cdef add_grid(Node node,
- np.float64_t *gle,
- np.float64_t *gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int gid,
int rank,
int size):
@@ -179,8 +175,8 @@
return
def add_pygrid(Node node,
- np.ndarray[np.float64_t, ndim=1] gle,
- np.ndarray[np.float64_t, ndim=1] gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int gid,
int rank,
int size):
@@ -189,23 +185,21 @@
The entire purpose of this function is to move everything from ndarrays
to internal C pointers.
"""
- pgles = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- pgres = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ cdef np.float64_t[:] pgles = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
+ cdef np.float64_t[:] pgres = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
cdef int j
for j in range(3):
pgles[j] = gle[j]
pgres[j] = gre[j]
add_grid(node, pgles, pgres, gid, rank, size)
- free(pgles)
- free(pgres)
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
cdef insert_grid(Node node,
- np.float64_t *gle,
- np.float64_t *gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int grid_id,
int rank,
int size):
@@ -239,50 +233,16 @@
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.cdivision(True)
-def add_pygrids(Node node,
+cpdef add_grids(Node node,
int ngrids,
- np.ndarray[np.float64_t, ndim=2] gles,
- np.ndarray[np.float64_t, ndim=2] gres,
- np.ndarray[np.int64_t, ndim=1] gids,
+ np.float64_t[:,:] gles,
+ np.float64_t[:,:] gres,
+ np.int64_t[:] gids,
int rank,
int size):
- """
- The entire purpose of this function is to move everything from ndarrays
- to internal C pointers.
- """
- pgles = <np.float64_t **> malloc(ngrids * sizeof(np.float64_t*))
- pgres = <np.float64_t **> malloc(ngrids * sizeof(np.float64_t*))
- pgids = <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
- for i in range(ngrids):
- pgles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- pgres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- pgids[i] = gids[i]
- for j in range(3):
- pgles[i][j] = gles[i, j]
- pgres[i][j] = gres[i, j]
-
- add_grids(node, ngrids, pgles, pgres, pgids, rank, size)
-
- for i in range(ngrids):
- free(pgles[i])
- free(pgres[i])
- free(pgles)
- free(pgres)
- free(pgids)
-
-
-
- at cython.boundscheck(False)
- at cython.wraparound(False)
- at cython.cdivision(True)
-cdef add_grids(Node node,
- int ngrids,
- np.float64_t **gles,
- np.float64_t **gres,
- np.int64_t *gids,
- int rank,
- int size):
- cdef int i, j, nless, ngreater
+ cdef int i, j, nless, ngreater, index
+ cdef np.float64_t[:,:] less_gles, less_gres, greater_gles, greater_gres
+ cdef np.int64_t[:] l_ids, g_ids
if not should_i_build(node, rank, size):
return
@@ -290,75 +250,53 @@
insert_grids(node, ngrids, gles, gres, gids, rank, size)
return
- less_ids= <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
- greater_ids = <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
+ less_ids = cvarray(format="q", shape=(ngrids,), itemsize=sizeof(np.int64_t))
+ greater_ids = cvarray(format="q", shape=(ngrids,), itemsize=sizeof(np.int64_t))
nless = 0
ngreater = 0
for i in range(ngrids):
- if gles[i][node.split.dim] < node.split.pos:
+ if gles[i, node.split.dim] < node.split.pos:
less_ids[nless] = i
nless += 1
- if gres[i][node.split.dim] > node.split.pos:
+ if gres[i, node.split.dim] > node.split.pos:
greater_ids[ngreater] = i
ngreater += 1
#print 'nless: %i' % nless
#print 'ngreater: %i' % ngreater
- less_gles = <np.float64_t **> malloc(nless * sizeof(np.float64_t*))
- less_gres = <np.float64_t **> malloc(nless * sizeof(np.float64_t*))
- l_ids = <np.int64_t *> malloc(nless * sizeof(np.int64_t))
- for i in range(nless):
- less_gles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- less_gres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ if nless > 0:
+ less_gles = cvarray(format="d", shape=(nless,3), itemsize=sizeof(np.float64_t))
+ less_gres = cvarray(format="d", shape=(nless,3), itemsize=sizeof(np.float64_t))
+ l_ids = cvarray(format="q", shape=(nless,), itemsize=sizeof(np.int64_t))
- greater_gles = <np.float64_t **> malloc(ngreater * sizeof(np.float64_t*))
- greater_gres = <np.float64_t **> malloc(ngreater * sizeof(np.float64_t*))
- g_ids = <np.int64_t *> malloc(ngreater * sizeof(np.int64_t))
- for i in range(ngreater):
- greater_gles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- greater_gres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ for i in range(nless):
+ index = less_ids[i]
+ l_ids[i] = gids[index]
+ for j in range(3):
+ less_gles[i,j] = gles[index,j]
+ less_gres[i,j] = gres[index,j]
- cdef int index
- for i in range(nless):
- index = less_ids[i]
- l_ids[i] = gids[index]
- for j in range(3):
- less_gles[i][j] = gles[index][j]
- less_gres[i][j] = gres[index][j]
-
- if nless > 0:
add_grids(node.left, nless, less_gles, less_gres,
l_ids, rank, size)
- for i in range(ngreater):
- index = greater_ids[i]
- g_ids[i] = gids[index]
- for j in range(3):
- greater_gles[i][j] = gles[index][j]
- greater_gres[i][j] = gres[index][j]
+ if ngreater > 0:
+ greater_gles = cvarray(format="d", shape=(ngreater,3), itemsize=sizeof(np.float64_t))
+ greater_gres = cvarray(format="d", shape=(ngreater,3), itemsize=sizeof(np.float64_t))
+ g_ids = cvarray(format="q", shape=(ngreater,), itemsize=sizeof(np.int64_t))
- if ngreater > 0:
+ for i in range(ngreater):
+ index = greater_ids[i]
+ g_ids[i] = gids[index]
+ for j in range(3):
+ greater_gles[i,j] = gles[index,j]
+ greater_gres[i,j] = gres[index,j]
+
add_grids(node.right, ngreater, greater_gles, greater_gres,
g_ids, rank, size)
- for i in range(nless):
- free(less_gles[i])
- free(less_gres[i])
- free(less_gles)
- free(less_gres)
- free(less_ids)
- free(l_ids)
- for i in range(ngreater):
- free(greater_gles[i])
- free(greater_gres[i])
- free(greater_gles)
- free(greater_gres)
- free(greater_ids)
- free(g_ids)
-
return
@cython.boundscheck(False)
@@ -374,9 +312,9 @@
@cython.cdivision(True)
cdef void insert_grids(Node node,
int ngrids,
- np.float64_t **gles,
- np.float64_t **gres,
- np.int64_t *gids,
+ np.float64_t[:,:] gles,
+ np.float64_t[:,:] gres,
+ np.int64_t[:] gids,
int rank,
int size):
@@ -388,12 +326,12 @@
if ngrids == 1:
# If we should continue to split based on parallelism, do so!
if should_i_split(node, rank, size):
- geo_split(node, gles[0], gres[0], gids[0], rank, size)
+ geo_split(node, gles[0,:], gres[0,:], gids[0], rank, size)
return
for i in range(3):
- contained *= gles[0][i] <= node.left_edge[i]
- contained *= gres[0][i] >= node.right_edge[i]
+ contained *= gles[0,i] <= node.left_edge[i]
+ contained *= gres[0,i] >= node.right_edge[i]
if contained == 1:
# print 'Node fully contained, setting to grid: %i' % gids[0]
@@ -413,23 +351,21 @@
@cython.wraparound(False)
@cython.cdivision(True)
cdef split_grid(Node node,
- np.float64_t *gle,
- np.float64_t *gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int gid,
int rank,
int size):
cdef int j
- data = <np.float64_t ***> alloca(sizeof(np.float64_t**))
- data[0] = <np.float64_t **> alloca(2 * sizeof(np.float64_t*))
- for j in range(2):
- data[0][j] = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
+ cdef np.uint8_t[:] less_ids, greater_ids
+ data = cvarray(format="d", shape=(1,2,3), itemsize=sizeof(np.float64_t))
for j in range(3):
- data[0][0][j] = gle[j]
- data[0][1][j] = gre[j]
+ data[0,0,j] = gle[j]
+ data[0,1,j] = gre[j]
- less_ids = <np.uint8_t *> alloca(1 * sizeof(np.uint8_t))
- greater_ids = <np.uint8_t *> alloca(1 * sizeof(np.uint8_t))
+ less_ids = cvarray(format="B", shape=(1,), itemsize=sizeof(np.uint8_t))
+ greater_ids = cvarray(format="B", shape=(1,), itemsize=sizeof(np.uint8_t))
best_dim, split_pos, nless, ngreater = \
kdtree_get_choices(1, data, node.left_edge, node.right_edge,
@@ -466,20 +402,17 @@
@cython.wraparound(False)
@cython.cdivision(True)
cdef kdtree_get_choices(int n_grids,
- np.float64_t ***data,
- np.float64_t *l_corner,
- np.float64_t *r_corner,
- np.uint8_t *less_ids,
- np.uint8_t *greater_ids,
+ np.float64_t[:,:,:] data,
+ np.float64_t[:] l_corner,
+ np.float64_t[:] r_corner,
+ np.uint8_t[:] less_ids,
+ np.uint8_t[:] greater_ids,
):
cdef int i, j, k, dim, n_unique, best_dim, my_split
cdef np.float64_t split
- cdef np.float64_t **uniquedims
- cdef np.float64_t *uniques
- uniquedims = <np.float64_t **> malloc(3 * sizeof(np.float64_t*))
- for i in range(3):
- uniquedims[i] = <np.float64_t *> \
- malloc(2*n_grids * sizeof(np.float64_t))
+ cdef np.float64_t[:,:] uniquedims
+ cdef np.float64_t[:] uniques
+ uniquedims = cvarray(format="d", shape=(3, 2*n_grids), itemsize=sizeof(np.float64_t))
my_max = 0
my_split = 0
best_dim = -1
@@ -509,9 +442,6 @@
my_max = n_unique
my_split = (n_unique-1)/2
if best_dim == -1:
- for i in range(3):
- free(uniquedims[i])
- free(uniquedims)
return -1, 0, 0, 0
# I recognize how lame this is.
cdef np.ndarray[np.float64_t, ndim=1] tarr = np.empty(my_max, dtype='float64')
@@ -533,10 +463,6 @@
else:
greater_ids[i] = 0
- for i in range(3):
- free(uniquedims[i])
- free(uniquedims)
-
# Return out unique values
return best_dim, split, nless, ngreater
@@ -545,25 +471,26 @@
#@cython.cdivision(True)
cdef int split_grids(Node node,
int ngrids,
- np.float64_t **gles,
- np.float64_t **gres,
- np.int64_t *gids,
+ np.float64_t[:,:] gles,
+ np.float64_t[:,:] gres,
+ np.int64_t[:] gids,
int rank,
int size):
# Find a Split
- cdef int i, j
+ cdef int i, j, index
+ cdef np.float64_t[:,:] less_gles, less_gres, greater_gles, greater_gres
+ cdef np.int64_t[:] l_ids, g_ids
+ if ngrids == 0: return 0
- data = <np.float64_t ***> malloc(ngrids * sizeof(np.float64_t**))
+ data = cvarray(format="d", shape=(ngrids,2,3), itemsize=sizeof(np.float64_t))
+
for i in range(ngrids):
- data[i] = <np.float64_t **> malloc(2 * sizeof(np.float64_t*))
- for j in range(2):
- data[i][j] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
for j in range(3):
- data[i][0][j] = gles[i][j]
- data[i][1][j] = gres[i][j]
+ data[i,0,j] = gles[i,j]
+ data[i,1,j] = gres[i,j]
- less_ids = <np.uint8_t *> malloc(ngrids * sizeof(np.uint8_t))
- greater_ids = <np.uint8_t *> malloc(ngrids * sizeof(np.uint8_t))
+ less_ids = cvarray(format="B", shape=(ngrids,), itemsize=sizeof(np.uint8_t))
+ greater_ids = cvarray(format="B", shape=(ngrids,), itemsize=sizeof(np.uint8_t))
best_dim, split_pos, nless, ngreater = \
kdtree_get_choices(ngrids, data, node.left_edge, node.right_edge,
@@ -583,8 +510,8 @@
# Create a Split
divide(node, split)
- less_index = <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
- greater_index = <np.int64_t *> malloc(ngrids * sizeof(np.int64_t))
+ less_index = cvarray(format="q", shape=(ngrids,), itemsize=sizeof(np.int64_t))
+ greater_index = cvarray(format="q", shape=(ngrids,), itemsize=sizeof(np.int64_t))
nless = 0
ngreater = 0
@@ -597,75 +524,45 @@
greater_index[ngreater] = i
ngreater += 1
- less_gles = <np.float64_t **> malloc(nless * sizeof(np.float64_t*))
- less_gres = <np.float64_t **> malloc(nless * sizeof(np.float64_t*))
- l_ids = <np.int64_t *> malloc(nless * sizeof(np.int64_t))
- for i in range(nless):
- less_gles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- less_gres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ if nless > 0:
+ less_gles = cvarray(format="d", shape=(nless,3), itemsize=sizeof(np.float64_t))
+ less_gres = cvarray(format="d", shape=(nless,3), itemsize=sizeof(np.float64_t))
+ l_ids = cvarray(format="q", shape=(nless,), itemsize=sizeof(np.int64_t))
- greater_gles = <np.float64_t **> malloc(ngreater * sizeof(np.float64_t*))
- greater_gres = <np.float64_t **> malloc(ngreater * sizeof(np.float64_t*))
- g_ids = <np.int64_t *> malloc(ngreater * sizeof(np.int64_t))
- for i in range(ngreater):
- greater_gles[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
- greater_gres[i] = <np.float64_t *> malloc(3 * sizeof(np.float64_t))
+ for i in range(nless):
+ index = less_index[i]
+ l_ids[i] = gids[index]
+ for j in range(3):
+ less_gles[i,j] = gles[index,j]
+ less_gres[i,j] = gres[index,j]
- cdef int index
- for i in range(nless):
- index = less_index[i]
- l_ids[i] = gids[index]
- for j in range(3):
- less_gles[i][j] = gles[index][j]
- less_gres[i][j] = gres[index][j]
-
- if nless > 0:
# Populate Left Node
#print 'Inserting left node', node.left_edge, node.right_edge
insert_grids(node.left, nless, less_gles, less_gres,
l_ids, rank, size)
- for i in range(ngreater):
- index = greater_index[i]
- g_ids[i] = gids[index]
- for j in range(3):
- greater_gles[i][j] = gles[index][j]
- greater_gres[i][j] = gres[index][j]
+ if ngreater > 0:
+ greater_gles = cvarray(format="d", shape=(ngreater,3), itemsize=sizeof(np.float64_t))
+ greater_gres = cvarray(format="d", shape=(ngreater,3), itemsize=sizeof(np.float64_t))
+ g_ids = cvarray(format="q", shape=(ngreater,), itemsize=sizeof(np.int64_t))
- if ngreater > 0:
+ for i in range(ngreater):
+ index = greater_index[i]
+ g_ids[i] = gids[index]
+ for j in range(3):
+ greater_gles[i,j] = gles[index,j]
+ greater_gres[i,j] = gres[index,j]
+
# Populate Right Node
#print 'Inserting right node', node.left_edge, node.right_edge
insert_grids(node.right, ngreater, greater_gles, greater_gres,
g_ids, rank, size)
- for i in range(nless):
- free(less_gles[i])
- free(less_gres[i])
- free(less_gles)
- free(less_gres)
- free(less_ids)
- free(less_index)
- free(l_ids)
- for i in range(ngreater):
- free(greater_gles[i])
- free(greater_gres[i])
- free(greater_gles)
- free(greater_gres)
- free(greater_ids)
- free(greater_index)
- free(g_ids)
-
- for i in range(ngrids):
- for j in range(2):
- free(data[i][j])
- free(data[i])
- free(data)
-
return 0
cdef geo_split(Node node,
- np.float64_t *gle,
- np.float64_t *gre,
+ np.float64_t[:] gle,
+ np.float64_t[:] gre,
int grid_id,
int rank,
int size):
@@ -681,10 +578,10 @@
new_pos = (gre[big_dim] + gle[big_dim])/2.
- lnew_gle = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
- lnew_gre = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
- rnew_gle = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
- rnew_gre = <np.float64_t *> alloca(3 * sizeof(np.float64_t))
+ lnew_gle = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
+ lnew_gre = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
+ rnew_gle = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
+ rnew_gre = cvarray(format="d", shape=(3,), itemsize=sizeof(np.float64_t))
for j in range(3):
lnew_gle[j] = gle[j]
@@ -716,8 +613,8 @@
# Create a Split
node.split = split
- cdef np.ndarray[np.float64_t, ndim=1] le = np.zeros(3, dtype='float64')
- cdef np.ndarray[np.float64_t, ndim=1] re = np.zeros(3, dtype='float64')
+ cdef np.float64_t[:] le = np.empty(3, dtype='float64')
+ cdef np.float64_t[:] re = np.empty(3, dtype='float64')
cdef int i
for i in range(3):
@@ -907,7 +804,7 @@
return current, previous
cdef int point_in_node(Node node,
- np.ndarray[np.float64_t, ndim=1] point):
+ np.float64_t[:] point):
cdef int i
cdef int inside = 1
for i in range(3):
@@ -915,7 +812,7 @@
inside *= node.right_edge[i] > point[i]
return inside
-cdef Node _find_node(Node node, np.float64_t *point):
+cdef Node _find_node(Node node, np.float64_t[:] point):
while _kd_is_leaf(node) == 0:
if point[node.split.dim] < node.split.pos:
node = node.left
@@ -924,10 +821,10 @@
return node
def find_node(Node node,
- np.ndarray[np.float64_t, ndim=1] point):
+ np.float64_t[:] point):
"""
Find the AMRKDTree node enclosing a position
"""
assert(point_in_node(node, point))
- return _find_node(node, <np.float64_t *> point.data)
+ return _find_node(node, point)
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list