From b2d72aa03e690a1f5807a64861269bb6789c9503 Mon Sep 17 00:00:00 2001
From: Prakalp Srivastava <psrivas2@illinois.edu>
Date: Fri, 11 Mar 2016 02:56:34 -0600
Subject: [PATCH] Added a working visc_base version of bfs

---
 .../benchmarks/bfs/src/visc_base/main.cpp     | 48 ++++++++-----------
 1 file changed, 21 insertions(+), 27 deletions(-)

diff --git a/llvm/test/VISC/parboil/benchmarks/bfs/src/visc_base/main.cpp b/llvm/test/VISC/parboil/benchmarks/bfs/src/visc_base/main.cpp
index 134d288f91..f2715a8c0e 100644
--- a/llvm/test/VISC/parboil/benchmarks/bfs/src/visc_base/main.cpp
+++ b/llvm/test/VISC/parboil/benchmarks/bfs/src/visc_base/main.cpp
@@ -27,7 +27,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <math.h>
-//#include <stdatomic.h>
 #include "parboil.h"
 #include "config.h"
 #include <visc.h>
@@ -177,25 +176,16 @@ BFSLeaf(int *q1, size_t bytesq1,
       int cost = cur_edge.y;
       cost += cur_cost;
 
-      // TODO: What to do with atomics
-      // Following is an atomic statement replaced for now to amove forward. Has
-      // to be dealt with.
-      //int orig_cost = atom_min(&g_cost[id],cost);
-      int orig_cost;
-      if(g_cost[id] > cost) {
-        orig_cost = g_cost[id];
-        g_cost[id] = cost;
-      }
-      // Remove till here
+      int orig_cost = __visc__atomic_min(&g_cost[id],cost);
       if(orig_cost > cost){//the node should be visited
         if(g_color[id] > UP_LIMIT){
-         int old_color = __sync_swap(&g_color[id],gray_shade);
+         int old_color = __visc__atomic_xchg(&g_color[id],gray_shade);
           //this guarantees that only one thread will push this node
           //into a queue
           if(old_color != gray_shade) {
             //atomic operation guarantees the correctness
             //even if multiple warps are executing simultaneously
-            int index = __sync_fetch_and_add (local_q_tail,1);
+            int index = __visc__atomic_add(local_q_tail, 1);
             local_q[index] = id;
           }
         }
@@ -209,7 +199,7 @@ BFSLeaf(int *q1, size_t bytesq1,
     int tot_sum = *local_q_tail; 
     //the offset or "shift" of the block-level queue within the grid-level queue
     //is determined by atomic operation
-    *shift = __sync_fetch_and_add (tail,tot_sum);
+    *shift = __visc__atomic_add (tail,tot_sum);
   }
   
   __visc__barrier(); 
@@ -377,7 +367,6 @@ int main( int argc, char** argv)
     exit(-1);
   }
 
-  pb_SwitchToTimer(&timers, pb_TimerID_IO);
   //Read in Graph from a file
   fp = fopen(params->inpFiles[0],"r");
   if(!fp)
@@ -428,9 +417,8 @@ int main( int argc, char** argv)
   cost[source] = 0;
 
   pb_SwitchToTimer(&timers, pb_TimerID_COMPUTE);
-  size_t bytes_graph_nodes, bytes_graph_edges;
-  bytes_graph_nodes = num_of_nodes* sizeof(struct Node);
-  bytes_graph_nodes = num_of_edges* sizeof(struct Edge);
+  size_t bytes_graph_nodes = num_of_nodes* sizeof(struct Node);
+  size_t bytes_graph_edges = num_of_edges* sizeof(struct Edge);
 
   llvm_visc_track_mem(graph_nodes, bytes_graph_nodes);
   llvm_visc_track_mem(graph_edges, bytes_graph_edges);
@@ -446,8 +434,8 @@ int main( int argc, char** argv)
   llvm_visc_track_mem(q2, bytes_cost);
 
   // Scalar variable read/written by both graph and host.
-  int tail;
-  llvm_visc_track_mem(&tail, sizeof(int));
+  int* tail;
+  llvm_visc_track_mem(tail, sizeof(int));
 
   pb_SwitchToTimer(&timers, pb_TimerID_KERNEL);
   int num_of_blocks;
@@ -455,8 +443,10 @@ int main( int argc, char** argv)
 
   // Initializations. Can some of these be done in the graph. That way we can
   // move these arrays completely in the graph
-  tail = h_top;
-  cost[0] = 0;
+  *tail = h_top;
+  // Potential source of inefficiency. 
+  //Entire array would be copied intially
+  cost[0] = zero;
   q1[0] = source;
 
   int num_t;//number of threads
@@ -474,7 +464,7 @@ int main( int argc, char** argv)
           graph_edges, bytes_graph_edges,
           color, bytes_cost,
           cost, bytes_cost,
-          &tail, sizeof(int),
+          tail, sizeof(int),
           num_of_nodes,
           gray, 
           k,
@@ -484,9 +474,11 @@ int main( int argc, char** argv)
 
   do
   {
-    llvm_visc_request_mem(&tail, sizeof(int));
-    num_t = tail;
-    tail = 0;
+    llvm_visc_request_mem(tail, sizeof(int));
+    num_t = *tail;
+    //printf("tail for iteration %d = %d\n",k, num_t);
+    *tail = 0;
+    //tail = 0;
 
     if(num_t == 0){//frontier is empty
       break;
@@ -498,6 +490,8 @@ int main( int argc, char** argv)
     args->grid = num_of_blocks;
     args->block = num_of_threads_per_block;
 
+    args->no_of_nodes = num_t;
+    args->k = k;
 
     if(k%2 == 0){
       args->gray_shade = GRAY0;
@@ -519,7 +513,7 @@ int main( int argc, char** argv)
                                 //0);
     void* bfsDFG = __visc__launch(0, BFS_Root, (void*) args);
     __visc__wait(bfsDFG);
-    k++;
+    // Swap q1 and q2
     // Swap q1 and q2
     int* temp = args->q1;
     args->q1 = args->q2;
-- 
GitLab