Skip to content

Commit

Permalink
applyReduce with parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Narayanan Sundaram committed Feb 29, 2016
1 parent bb28bdf commit fb52c23
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
35 changes: 28 additions & 7 deletions src/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ typedef __declspec(align(16)) struct EDGE_T
extern int nthreads;

//const int MAX_PARTS = 512;
template<class T>
void AddFn(T a, T b, T* c, void* vsp) {
*c = a + b ;
}

template <class V, class E=int>
class Graph {
Expand Down Expand Up @@ -94,8 +98,8 @@ class Graph {
int getBlockIdBySrc(int vertexid) const;
int getBlockIdByDst(int vertexid) const;
int getNumberOfVertices() const;
void applyToAllVertices( void (*func)(V& _v));
template<class T> void applyReduceAllVertices(T (*func)(V& _v), T* val);
void applyToAllVertices(void (*ApplyFn)(V, V*, void*), void* param=nullptr);
template<class T> void applyReduceAllVertices(T* val, void (*ApplyFn)(V, T*, void*), void (*ReduceFn)(T,T,T*,void*)=AddFn<T>, void* param=nullptr);
~Graph();
};

Expand Down Expand Up @@ -1356,21 +1360,38 @@ int Graph<V,E>::getNumberOfVertices() const {
}

template<class V, class E>
void Graph<V,E>::applyToAllVertices( void (*func)(V& _v)) {
void Graph<V,E>::applyToAllVertices(void (*ApplyFn)(V, V*, void*), void* param) {
#pragma omp parallel for num_threads(nthreads)
for (int i = 0; i < nvertices; i++) {
func(vertexproperty[i]);
ApplyFn(vertexproperty[i], &vertexproperty[i], param);
}
}

template<class V, class E>
template<class T>
void Graph<V,E>::applyReduceAllVertices(T (*func)(V& _v), T* val) {
void Graph<V,E>::applyReduceAllVertices(T* val, void (*ApplyFn)(V, T*, void*), void (*ReduceFn)(T,T,T*,void*), void* param) {
T sum = *val;
#pragma omp parallel for num_threads(nthreads) reduction(+:sum)

/*for (int i = 0; i < nvertices; i++) {
T tmp;
ApplyFn(vertexproperty[i], &tmp, param);
ReduceFn(sum, tmp, &sum, param);
}*/
T* tmpsum = new T[nthreads*16]; //reduce false sharing
for(int i = 0; i < nthreads; i++) tmpsum[i*16] = sum;
#pragma omp parallel for num_threads(nthreads)
for (int i = 0; i < nvertices; i++) {
sum += func(vertexproperty[i]);
T tmp;
int tid = omp_get_thread_num();
ApplyFn(vertexproperty[i], &tmp, param);
ReduceFn(tmpsum[tid*16], tmp, &tmpsum[tid*16], param);
}

for(int i = 0; i < nthreads; i++) {
ReduceFn(tmpsum[i*16], sum, &sum, param);
}
delete [] tmpsum;

*val = sum;
}

Expand Down
14 changes: 10 additions & 4 deletions src/SSSP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,15 @@ class SSSPwithParent : public GraphProgram<ID_dist, ID_dist, SSSPD> {

extern unsigned long long int edges_traversed;

int reachable_or_not(BFSD2& v) {
void reachable_or_not(BFSD2 v, int* output, void* param=nullptr) {
int reachable = 0;
if (v.distance < MAX_DIST) {
reachable = 1;
}
return reachable;
*output = reachable;
}
void add(int a, int b, int *c, void* param=nullptr) {
*c = a+b;
}

void run_sssp(const char* filename, int nthreads, int v) {
Expand Down Expand Up @@ -245,8 +248,11 @@ void run_sssp(const char* filename, int nthreads, int v) {
if (G.getVertexproperty(i).distance < MAX_DIST) {
reachable_vertices++;
}
}*/
G.applyReduceAllVertices<int>(reachable_or_not, &reachable_vertices);
}
printf("Reachable vertices = %d \n", reachable_vertices);
reachable_vertices = 0;*/
G.applyReduceAllVertices<int>(&reachable_vertices, reachable_or_not, add);

printf("Reachable vertices = %d \n", reachable_vertices);

Expand Down

0 comments on commit fb52c23

Please sign in to comment.