c++ - How to pass additional array to Thrust's min_element predicate -
i'm trying use thrust's min_element
reduction find next edge in prim's algorithm. iterate on graph edges. comparison function:
struct compareedge { __host__ /*__device__*/ bool operator()(edge l, edge r) { if (visited[l.u] != visited[l.v] && visited[r.u] != visited[r.v]) { return l.cost < r.cost; } else if (visited[l.u] != visited[l.v]) { return true; } else { return false; } } };
unfortunately code cannot run on device, because use visited
array, mark visited nodes. how can pass array predicate make usable device-executed code?
there number of ways can handled. present 1 approach. please note question how pass arbitrary data set functor, i'm trying show. i'm not trying address question of whether or not proposed functor useful comparison predicate thrust::min_element (which i'm not sure of).
one approach have statically defined array:
__device__ int d_visited[dsize];
then in host code, before using functor, need initialize array:
cudamemcpytosymbol(d_visited, visited, dsize*sizeof(int));
your functor code have modified. since may want functor usable either on host or device, need control code based on this:
struct compareedge { __host__ __device__ bool operator()(edge l, edge r) { #ifdef __cuda_arch__ if (d_visited[l.u] != d_visited[l.v] && d_visited[r.u] != d_visited[r.v]) { return l.cost < r.cost; } else if (d_visited[l.u] != d_visited[l.v]) { return true; } else { return false; } #else if (visited[l.u] != visited[l.v] && visited[r.u] != visited[r.v]) { return l.cost < r.cost; } else if (visited[l.u] != visited[l.v]) { return true; } else { return false; } #endif } };
Comments
Post a Comment