[arrayfire] 49/284: converted cpu regions function to asynchronous call
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Sun Feb 7 18:59:18 UTC 2016
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch debian/experimental
in repository arrayfire.
commit 0c72451eb1ac629940a76f6f57890e4cba0d6df0
Author: pradeep <pradeep at arrayfire.com>
Date: Wed Dec 2 16:22:42 2015 -0500
converted cpu regions function to asynchronous call
---
src/backend/cpu/regions.cpp | 157 +++++++++++++++++++++++---------------------
1 file changed, 83 insertions(+), 74 deletions(-)
diff --git a/src/backend/cpu/regions.cpp b/src/backend/cpu/regions.cpp
index b753fb5..f7309c8 100644
--- a/src/backend/cpu/regions.cpp
+++ b/src/backend/cpu/regions.cpp
@@ -17,6 +17,8 @@
#include <map>
#include <set>
#include <algorithm>
+#include <platform.hpp>
+#include <async_queue.hpp>
using af::dim4;
@@ -106,97 +108,104 @@ static void setUnion(LabelNode<T>* x, LabelNode<T>* y)
template<typename T>
Array<T> regions(const Array<char> &in, af_connectivity connectivity)
{
- const dim4 in_dims = in.dims();
+ in.eval();
// Create output placeholder
- Array<T> out = createValueArray(in_dims, (T)0);
-
- const char *in_ptr = in.get();
- T *out_ptr = out.get();
-
- // Map labels
- typedef typename std::map<T, LabelNode<T>* > label_map_t;
- typedef typename label_map_t::iterator label_map_iterator_t;
-
- label_map_t lmap;
-
- // Initial label
- T label = (T)1;
-
- for (int j = 0; j < (int)in_dims[1]; j++) {
- for (int i = 0; i < (int)in_dims[0]; i++) {
- int idx = j * in_dims[0] + i;
- if (in_ptr[idx] != 0) {
- std::vector<T> l;
-
- // Test neighbors
- if (i > 0 && out_ptr[j * (int)in_dims[0] + i-1] > 0)
- l.push_back(out_ptr[j * in_dims[0] + i-1]);
- if (j > 0 && out_ptr[(j-1) * (int)in_dims[0] + i] > 0)
- l.push_back(out_ptr[(j-1) * in_dims[0] + i]);
- if (connectivity == AF_CONNECTIVITY_8 && i > 0 && j > 0 && out_ptr[(j-1) * in_dims[0] + i-1] > 0)
- l.push_back(out_ptr[(j-1) * in_dims[0] + i-1]);
- if (connectivity == AF_CONNECTIVITY_8 && i < (int)in_dims[0] - 1 && j > 0 && out_ptr[(j-1) * in_dims[0] + i+1] != 0)
- l.push_back(out_ptr[(j-1) * in_dims[0] + i+1]);
-
- if (!l.empty()) {
- T minl = l[0];
- for (size_t k = 0; k < l.size(); k++) {
- minl = min(l[k], minl);
- label_map_iterator_t cur_map = lmap.find(l[k]);
- LabelNode<T> *node = cur_map->second;
- // Group labels of the same region under a disjoint set
- for (size_t m = k+1; m < l.size(); m++)
- setUnion(node, lmap.find(l[m])->second);
+ Array<T> out = createValueArray(in.dims(), (T)0);
+ out.eval();
+
+ auto func = [=] (Array<T> out, const Array<char> in, af_connectivity connectivity) {
+ const dim4 in_dims = in.dims();
+ const char *in_ptr = in.get();
+ T *out_ptr = out.get();
+
+ // Map labels
+ typedef typename std::map<T, LabelNode<T>* > label_map_t;
+ typedef typename label_map_t::iterator label_map_iterator_t;
+
+ label_map_t lmap;
+
+ // Initial label
+ T label = (T)1;
+
+ for (int j = 0; j < (int)in_dims[1]; j++) {
+ for (int i = 0; i < (int)in_dims[0]; i++) {
+ int idx = j * in_dims[0] + i;
+ if (in_ptr[idx] != 0) {
+ std::vector<T> l;
+
+ // Test neighbors
+ if (i > 0 && out_ptr[j * (int)in_dims[0] + i-1] > 0)
+ l.push_back(out_ptr[j * in_dims[0] + i-1]);
+ if (j > 0 && out_ptr[(j-1) * (int)in_dims[0] + i] > 0)
+ l.push_back(out_ptr[(j-1) * in_dims[0] + i]);
+ if (connectivity == AF_CONNECTIVITY_8 && i > 0 &&
+ j > 0 && out_ptr[(j-1) * in_dims[0] + i-1] > 0)
+ l.push_back(out_ptr[(j-1) * in_dims[0] + i-1]);
+ if (connectivity == AF_CONNECTIVITY_8 &&
+ i < (int)in_dims[0] - 1 && j > 0 && out_ptr[(j-1) * in_dims[0] + i+1] != 0)
+ l.push_back(out_ptr[(j-1) * in_dims[0] + i+1]);
+
+ if (!l.empty()) {
+ T minl = l[0];
+ for (size_t k = 0; k < l.size(); k++) {
+ minl = min(l[k], minl);
+ label_map_iterator_t cur_map = lmap.find(l[k]);
+ LabelNode<T> *node = cur_map->second;
+ // Group labels of the same region under a disjoint set
+ for (size_t m = k+1; m < l.size(); m++)
+ setUnion(node, lmap.find(l[m])->second);
+ }
+ // Set label to smallest neighbor label
+ out_ptr[idx] = minl;
+ }
+ else {
+ // Insert new label in map
+ LabelNode<T> *node = new LabelNode<T>(label);
+ lmap.insert(std::pair<T, LabelNode<T>* >(label, node));
+ out_ptr[idx] = label++;
}
- // Set label to smallest neighbor label
- out_ptr[idx] = minl;
- }
- else {
- // Insert new label in map
- LabelNode<T> *node = new LabelNode<T>(label);
- lmap.insert(std::pair<T, LabelNode<T>* >(label, node));
- out_ptr[idx] = label++;
}
}
}
- }
- std::set<T> removed;
+ std::set<T> removed;
- for (int j = 0; j < (int)in_dims[1]; j++) {
- for (int i = 0; i < (int)in_dims[0]; i++) {
- int idx = j * (int)in_dims[0] + i;
- if (in_ptr[idx] != 0) {
- T l = out_ptr[idx];
- label_map_iterator_t cur_map = lmap.find(l);
+ for (int j = 0; j < (int)in_dims[1]; j++) {
+ for (int i = 0; i < (int)in_dims[0]; i++) {
+ int idx = j * (int)in_dims[0] + i;
+ if (in_ptr[idx] != 0) {
+ T l = out_ptr[idx];
+ label_map_iterator_t cur_map = lmap.find(l);
- if (cur_map != lmap.end()) {
- LabelNode<T>* node = cur_map->second;
+ if (cur_map != lmap.end()) {
+ LabelNode<T>* node = cur_map->second;
- LabelNode<T>* node_root = find(node);
- out_ptr[idx] = node_root->getMinLabel();
+ LabelNode<T>* node_root = find(node);
+ out_ptr[idx] = node_root->getMinLabel();
- // Mark removed labels (those that are part of a region
- // that contains a smaller label)
- if (node->getMinLabel() < l || node_root->getMinLabel() < l)
- removed.insert(l);
- if (node->getLabel() > node->getMinLabel())
- removed.insert(node->getLabel());
+ // Mark removed labels (those that are part of a region
+ // that contains a smaller label)
+ if (node->getMinLabel() < l || node_root->getMinLabel() < l)
+ removed.insert(l);
+ if (node->getLabel() > node->getMinLabel())
+ removed.insert(node->getLabel());
+ }
}
}
}
- }
- // Calculate final neighbors (ensure final labels are sequential)
- for (int j = 0; j < (int)in_dims[1]; j++) {
- for (int i = 0; i < (int)in_dims[0]; i++) {
- int idx = j * (int)in_dims[0] + i;
- if (out_ptr[idx] > 0) {
- out_ptr[idx] -= distance(removed.begin(), removed.lower_bound(out_ptr[idx]));
+ // Calculate final neighbors (ensure final labels are sequential)
+ for (int j = 0; j < (int)in_dims[1]; j++) {
+ for (int i = 0; i < (int)in_dims[0]; i++) {
+ int idx = j * (int)in_dims[0] + i;
+ if (out_ptr[idx] > 0) {
+ out_ptr[idx] -= distance(removed.begin(), removed.lower_bound(out_ptr[idx]));
+ }
}
}
- }
+ };
+ getQueue().enqueue(func, out, in, connectivity);
return out;
}
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/arrayfire.git
More information about the debian-science-commits
mailing list