Commit 131a81b6 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

renamed kd_buildArg() to kd_initArg(), changed interface and memory handling.

parent aaa05e54
......@@ -10,7 +10,7 @@
changed *min to min[KD_MAX_DIM]
changed *max to max[KD_MAX_DIM]
_compPoints: compare index if points[axis] are equal
replace qsortR by libc:qsort (speedup 25%)
20171102: renamed kd_buildArg() to kd_initArg(), changed interface and memory handling
*/
#ifndef KDTREE_H_
#define KDTREE_H_
......@@ -156,11 +156,8 @@ void kd_freeNode(kdNode * node);
struct kdNode *kd_allocNode(struct kd_point *points, size_t pivot,
kdata_t *min, kdata_t *max, int dim, int axis);
void kd_destroyTree(struct kdNode *node);
struct kd_thread_data *kd_buildArg(struct kd_point *points,
size_t nPoints,
kdata_t *min, kdata_t *max,
int depth, int max_threads,
int dim);
void kd_initArg(struct kd_thread_data *d, struct kd_point *points, size_t nPoints,
kdata_t *min, kdata_t *max, int depth, int max_threads, int dim);
struct kdNode *kd_buildTree(struct kd_point *points, size_t nPoints,
kdata_t *min, kdata_t *max, int dim, int max_threads);
void *kd_doBuildTree(void *threadarg);
......
......@@ -73,10 +73,10 @@ struct kdNode *
kd_buildTree(struct kd_point *points, size_t nPoints,
kdata_t *min, kdata_t *max, int dim, int max_threads)
{
struct kd_thread_data *my_data = kd_buildArg(points, nPoints, min, max, 0, max_threads, dim);
struct kdNode *tree = (kdNode *)kd_doBuildTree(my_data);
free(my_data);
return tree;
struct kd_thread_data my_data;
kd_initArg(&my_data, points, nPoints, min, max, 0, max_threads, dim);
struct kdNode *tree = (kdNode *)kd_doBuildTree(&my_data);
return tree;
}
......
......@@ -91,7 +91,7 @@ void *kd_doBuildTree(void *threadarg)
struct kdNode *node;
pthread_t threads[2];
pthread_attr_t attr;
struct kd_thread_data *argleft, *argright;
struct kd_thread_data argleft, argright;
struct kd_thread_data *my_data = (struct kd_thread_data *) threadarg;
struct kd_point *points = my_data->points;
......@@ -127,18 +127,14 @@ void *kd_doBuildTree(void *threadarg)
memcpy(tmpMinLeft, min, dim * sizeof(kdata_t));
memcpy(tmpMaxLeft, max, dim * sizeof(kdata_t));
tmpMaxLeft[sortaxis] = node->location[sortaxis];
argleft = kd_buildArg(points, pivot, tmpMinLeft, tmpMaxLeft,
depth + 1, max_threads / 2, dim);
if (!argleft) {
kd_destroyTree(node);
return NULL;
}
kd_initArg(&argleft, points, pivot, tmpMinLeft, tmpMaxLeft,
depth + 1, max_threads / 2, dim);
if (max_threads > 1) {
pthread_create(&threads[0], &attr, kd_doBuildTree, (void *) argleft);
pthread_create(&threads[0], &attr, kd_doBuildTree, (void *) &argleft);
} else {
node->left = (kdNode *)kd_doBuildTree((void *) argleft);
free(argleft);
node->left = (kdNode *)kd_doBuildTree((void *) &argleft);
//free(argleft);
if (!node->left) {
kd_destroyTree(node);
return NULL;
......@@ -148,19 +144,14 @@ void *kd_doBuildTree(void *threadarg)
memcpy(tmpMinRight, min, dim * sizeof(kdata_t));
memcpy(tmpMaxRight, max, dim * sizeof(kdata_t));
tmpMinRight[sortaxis] = node->location[sortaxis];
argright = kd_buildArg(&points[pivot], nPoints - pivot,
tmpMinRight, tmpMaxRight, depth + 1,
max_threads / 2, dim);
if (!argright) {
kd_destroyTree(node);
return NULL;
}
kd_initArg(&argright, &points[pivot], nPoints - pivot, tmpMinRight, tmpMaxRight,
depth + 1, max_threads / 2, dim);
if (max_threads > 1) {
pthread_create(&threads[1], &attr, kd_doBuildTree, (void *) argright);
pthread_create(&threads[1], &attr, kd_doBuildTree, (void *) &argright);
} else {
node->right = (kdNode *)kd_doBuildTree((void *) argright);
free(argright);
node->right = (kdNode *)kd_doBuildTree((void *) &argright);
//free(argright);
if (!node->right) {
kd_destroyTree(node);
return NULL;
......@@ -169,9 +160,9 @@ void *kd_doBuildTree(void *threadarg)
if (max_threads > 1) {
pthread_join(threads[0], (void **) (&node->left));
free(argleft);
// free(argleft);
pthread_join(threads[1], (void **) (&node->right));
free(argright);
// free(argright);
if (!node->left || !node->right) {
kd_destroyTree(node);
return NULL;
......@@ -190,17 +181,9 @@ kd_freeNode(kdNode *node)
}
struct kd_thread_data *
kd_buildArg(struct kd_point *points,
size_t nPoints,
kdata_t *min, kdata_t *max,
int depth, int max_threads, int dim)
void kd_initArg(struct kd_thread_data *d, struct kd_point *points, size_t nPoints,
kdata_t *min, kdata_t *max, int depth, int max_threads, int dim)
{
struct kd_thread_data *d;
if ((d = (kd_thread_data *)kd_malloc(sizeof(kd_thread_data), "kd_thread_data")) == NULL)
return NULL;
d->points = points;
d->nPoints = nPoints;
memcpy(d->min, min, dim*sizeof(kdata_t));
......@@ -208,10 +191,9 @@ kd_buildArg(struct kd_point *points,
d->depth = depth;
d->max_threads = max_threads;
d->dim = dim;
return d;
}
//#define TEST_BIGMEM 1
#ifdef TEST_BIGMEM
size_t num_nodes = 0;
......
......@@ -131,10 +131,10 @@ struct kdNode *
kd_sph_buildTree(struct kd_point *points, size_t nPoints,
kdata_t *min, kdata_t *max, int max_threads)
{
struct kd_thread_data *my_data = kd_buildArg(points, nPoints, min, max, 0,max_threads, 2);
struct kdNode *tree = (kdNode *)kd_doBuildTree(my_data);
free(my_data);
return tree;
struct kd_thread_data my_data;
kd_initArg(&my_data, points, nPoints, min, max, 0,max_threads, 2);
struct kdNode *tree = (kdNode *)kd_doBuildTree(&my_data);
return tree;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment