Skip to content

Commit

Permalink
add a new dir for the c part and add a job file
Browse files Browse the repository at this point in the history
  • Loading branch information
truncs committed Apr 13, 2012
1 parent b84a8f5 commit 5b1d542
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 0 deletions.
16 changes: 16 additions & 0 deletions hw2/b/job.sh
@@ -0,0 +1,16 @@
#!/bin/bash

#$ -V
#$ -cwd
#$ -q development
#$ -pe 12way 12
#$ -N parallel_bfsb
#$ -o output_merge
#$ -e error_merge
#$ -M adityasarawgioo7@gmail.com
#$ -m be
#$ -l h_rt=01:00:00

export PATH=$PATH:$HOME/cilk/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/cilk/lib
./parallel_bfsb < /work/01905/rezaul/CSE613/HW2/samples/sample-02-in.txt > temp.txt
14 changes: 14 additions & 0 deletions hw2/c/Makefile
@@ -0,0 +1,14 @@
# Makefile for Cilk++ example. See source code for details.

CILKPP = cilk++
LIBARG = -O1 -g -lcilkutil
TARGET = parallel_bfsc
SRC = parallel_bfs.cilk bfs_queue.cilk

all: $(TARGET)

$(TARGET): $(SRC)
$(CILKPP) $(SRC) $(LIBARG) -o $@

clean:
rm -f $(TARGET)~
78 changes: 78 additions & 0 deletions hw2/c/bfs_queue.cilk
@@ -0,0 +1,78 @@
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <cilk.h>
#include "bfs_queue.h"


using namespace std;

BfsQueue :: BfsQueue(int procs, int n ) {

nseg = n;
p = procs;


for (int i = 0; i < p; i++) {
queues.push_back(queue<int>());
}
}

void BfsQueue::enque(int i, int value) {
if (i < p) {
queues[i].push(value);

}
}

void BfsQueue::set_segment_size() {
int size = getSize();
int temp = nseg / size;
seg = temp == 0 ? size : temp;
}

vector<int> * BfsQueue::nextSegment() {

int size =getSize();
m.lock();
if (size > 0) {
for (int i = 0; i <p; i++) {
if (!queues[i].empty()) {

int k = min((int)queues[i].size(), seg);
for (int j = 0; j <= k; j++) {
vector<int> * S = new vector<int>();
S->push_back(queues[i].front());
queues[i].pop();
// TODO remember to free S
m.unlock();
return S;
}


}
}
}

m.unlock();
return NULL;

}


int BfsQueue::getSize() {
int n = 0;

for (int i = 0; i < p; i++) {
n += queues[i].size();
}

return n;
}

28 changes: 28 additions & 0 deletions hw2/c/bfs_queue.h
@@ -0,0 +1,28 @@
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <cilk.h>
#include <cilk_mutex.h>

using namespace std;

class BfsQueue {
vector<queue<int> > queues;
int nseg;
int p;
int seg;
cilk::mutex m;

public:
BfsQueue(int p, int n);
void enque(int i, int value);
vector<int> * nextSegment();
void set_segment_size();
int getSize();
};
148 changes: 148 additions & 0 deletions hw2/c/parallel_bfs.cilk
@@ -0,0 +1,148 @@
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <cilk.h>
#include "bfs_queue.h"

#define P 12
#define NSEG 5

using namespace std;


void parallel_bfs_thread(BfsQueue& qin, BfsQueue& qout, int p, vector<int> & d,
vector<vector<int> > & tau) {

vector<int> * S;
while ((S = qin.nextSegment()) != NULL) {
for (int i = 0; i < S->size(); i++) {
int u = S->at(i);
for (int j = 0; j < tau[u].size(); j++) {

int v = tau[u][j];

if (d[v] == -1) {
d[v] = d[u] + 1;
qout.enque(p, v);
}

}


}

delete(S);

}
}


void parallel_bfs(int source_index, int n, vector<vector<int> > & tau) {

vector<int> d;

for (int i = 0; i <= n; i++ ) {
d.push_back(-1);
}

d[source_index] = 0;

BfsQueue * qin = new BfsQueue(P, NSEG);
BfsQueue * qout = new BfsQueue(P, NSEG);

qin->enque(0, source_index);

while (qin->getSize() != 0) {
qin->set_segment_size();
for (int i = 0 ; i < P - 1; i++) {
cilk_spawn parallel_bfs_thread(*qin, *qout, i, d, tau);
}
parallel_bfs_thread(*qin, *qout, P -1, d, tau);
cilk_sync;

delete(qin);
qin = qout;
qout = new BfsQueue(P, NSEG);
}

long int checksum = 0;
int max_depth = 0;

for (int i = 1; i <= n; i++ ) {
if (max_depth < d[i]){
max_depth = d[i];
}

if (d[i] == -1)
checksum += n;
else
checksum += d[i];
}

cout <<max_depth << " "<< checksum<<endl;


}



int cilk_main(){
int n;
int m;
int r;


string string_n;
getline(cin, string_n);
vector<string> tokens;
istringstream iss(string_n);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(tokens));

n = atoi(tokens[0].c_str());
m = atoi(tokens[1].c_str());
r = atoi(tokens[2].c_str());


vector<vector<int> > tau(n + 1);


for (int i = 0; i < m; i++) {

string temp;
vector<string> tokenized;
getline(cin, temp);

stringstream iss(temp);
copy(istream_iterator<string>(iss),
istream_iterator<string>(),
back_inserter<vector<string> >(tokenized));

int u = atoi(tokenized[0].c_str());
int v = atoi(tokenized[1].c_str());

tau[u].push_back(v);
}

for (int j = 0; j < r ; j++) {
string source_string;
getline(cin, source_string);
int source_index = atoi(source_string.c_str());

parallel_bfs(source_index, n, tau);

}


return 0;


}

0 comments on commit 5b1d542

Please sign in to comment.