[Assorted-commits] SF.net SVN: assorted: [350] hash-join/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-10 18:28:51
|
Revision: 350 http://assorted.svn.sourceforge.net/assorted/?rev=350&view=rev Author: yangzhang Date: 2008-02-10 10:28:56 -0800 (Sun, 10 Feb 2008) Log Message: ----------- moved to cpp-commons Modified Paths: -------------- hash-join/trunk/src/Makefile hash-join/trunk/src/hashjoin.cc Removed Paths: ------------- hash-join/trunk/src/method_thread1.h Modified: hash-join/trunk/src/Makefile =================================================================== --- hash-join/trunk/src/Makefile 2008-02-10 18:28:49 UTC (rev 349) +++ hash-join/trunk/src/Makefile 2008-02-10 18:28:56 UTC (rev 350) @@ -1,16 +1,17 @@ TARGET := hashjoin -SRCS := hashjoin.cc +SRCS := hashjoin.cc $(wildcard commons/*.h) ### begin common makefrag -CFLAGS := -Wall -lpthread # -lprofiler +CFLAGS := -I. -Wall -lpthread # -lprofiler CXX = g++ $(CFLAGS) -o $@ $^ -all: opt dbg pg +all: pg dbg: $(TARGET)-dbg opt: $(TARGET)-opt pg: $(TARGET)-pg +goo: $(TARGET)-goo $(TARGET)-pg: $(SRCS) $(CXX) -g -pg Modified: hash-join/trunk/src/hashjoin.cc =================================================================== --- hash-join/trunk/src/hashjoin.cc 2008-02-10 18:28:49 UTC (rev 349) +++ hash-join/trunk/src/hashjoin.cc 2008-02-10 18:28:56 UTC (rev 350) @@ -12,178 +12,19 @@ #include <fcntl.h> #include <pthread.h> -#include "method_thread1.h" +#include <commons/check.h> +#include <commons/files.h> +#include <commons/strings.h> +#include <commons/threads.h> +#include <commons/time.h> // -// C++ Commons :: NUMA -// - -using namespace std; - -// TODO: Figure out how to create an exception with a useful message. -inline void -_check(bool cond, const char *msg, const char *file, int line) -{ - if (!cond) { - throw exception(); - } -} - -#define check(cond) _check(cond, NULL, __FILE__, __LINE__) - -/** - * Similar to assert(), but is not conditionally compiled, so this is safe to - * use as a guard against expected failures (such as checking return codes). - */ -#define checkmsg(cond, msg) \ - bool b = cond; \ - if (!b) _check(b, (msg), __FILE__, __LINE__) - -/** - * Search in p for the nth instance of c and return the character past it. - */ -inline const char * -strchrrep(const char *p, char c, int n) -{ - for (int i = 0; i < n; i++) { - p = strchr(p, c); - check(p); - p++; - } - return p; -} - -/** - * Search in p for the nth instance of c and return the character past it. - */ -inline char * -strchrrep(char *p, char c, int n) -{ - return const_cast<char *>(strchrrep(const_cast<const char *>(p), c, n)); -} - -/** - * Get the current time in milliseconds. - */ -inline long long -current_time_millis() -{ - long long t; - struct timeval tv; - - gettimeofday(&tv, 0); - - t = tv.tv_sec; - t = (t *1000) + (tv.tv_usec/1000); - - return t; -} - -/** - * Convenience class for performing wall-clock benchmarking. - */ -class timer -{ -public: - timer(const string label) : - label(label), start(current_time_millis()), last(start) {} - void print() - { - long long now = current_time_millis(); - cout << label << now - last << endl; - last = now; - } -private: - const string label; - long long start, last; -}; - -/** - * A functor that checks for string equality. Mainly useful as a template - * parameter to the hash data structures in STL extensions. - */ -struct eqstr -{ - bool operator()(const char* s1, const char* s2) const - { - return strcmp(s1, s2) == 0; - } -}; - -/** - * Look for a substring, but without null-termination conventions. - */ -inline char * -unsafe_strstr(char *p, const char *q, const char *lim) -{ - if (lim == 0) { - while (true) { - for (; !(*p == '\0' && *(p+1) == '\0'); p++); - return p; - } - } else { - check(p < lim); - while (true) { - for (; !(*p == '\0' && *(p+1) == '\0') && p < lim; p++); - if (p == lim) return NULL; - return p; - } - } -} - -/** - * Look for a substring, but without null-termination conventions. - */ -inline const char* -unsafe_strstr(const char *p, const char *q, const char *lim) -{ - return unsafe_strstr((char*) p, q, lim); -} - -/** - * Load an entire file into buf and also give us the length of the buffer. - * TODO this probably isn't very safe, since we're demoting an off_t to a - * size_t. Is there a healthier approach? - */ -char * -load_file(const char *path, size_t & len, unsigned int ncpus) { - struct stat sb; - int fd; - - fd = open(path, 0); - check(fd >= 0); - - check(fstat(fd, &sb) == 0); - check(sb.st_size <= 0xffffffff); - - // TODO Why don't we need (static) cast here? Isn't this a lossy cast? - len = sb.st_size; - - char *buf = new char[len + 1]; - check(buf); - - // TODO Use threads to pull data to the correct initial locations? - size_t chunk_len = len / ncpus; - for (unsigned int i = 0; i < ncpus; i++) { - int off = i *chunk_len; - ssize_t status = pread(fd, buf + off, chunk_len, off); - // We read the whole chunk or hit the end. - size_t nread = static_cast<ssize_t>(status); - check(status != -1 && (nread == chunk_len || off + nread == len)); - } - - check(close(fd) == 0); - - buf[len] = '\0'; // don't let strcmp() run off the end - return buf; -} - -// // Hash Join // using namespace std; using namespace __gnu_cxx; +using namespace commons; // TODO use dependency injection! unsigned int ncpus = 1; @@ -361,7 +202,7 @@ // into bucket[i][j]. pthread_t ts[ncpus]; for (unsigned int i = 0; i < ncpus; i++) { - ts[i] = method_thread1(this, &db::partition1, i, buckets[i]); + ts[i] = method_thread(this, &db::partition1, i, buckets[i]); } for (unsigned int i = 0; i < ncpus; i++) { void *value; @@ -501,7 +342,7 @@ pthread_t ts[ncpus]; hmap *hs = new hmap[ncpus]; for (unsigned int i = 0; i < ncpus; i++) { - ts[i] = method_thread1(this, &movdb::build1, i, movbucs, &hs[i]); + ts[i] = method_thread(this, &movdb::build1, i, movbucs, &hs[i]); } for (unsigned int i = 0; i < ncpus; i++) { void *value; @@ -533,7 +374,7 @@ { pthread_t ts[ncpus]; for (unsigned int i = 0; i < ncpus; i++) { - ts[i] = method_thread1(this, &actdb::probe1, i, &hs[i], actbucs); + ts[i] = method_thread(this, &actdb::probe1, i, &hs[i], actbucs); } for (unsigned int i = 0; i < ncpus; i++) { void *value; Deleted: hash-join/trunk/src/method_thread1.h =================================================================== --- hash-join/trunk/src/method_thread1.h 2008-02-10 18:28:49 UTC (rev 349) +++ hash-join/trunk/src/method_thread1.h 2008-02-10 18:28:56 UTC (rev 350) @@ -1,132 +0,0 @@ -#ifndef method_thread_h -#define method_thread_h - -#include <assert.h> -#include <pthread.h> - -// non-rpc-specific utility to start a thread that runs -// an object method. returns a pthread_t on success, and -// zero on error. -template <class C> pthread_t -method_thread1(C *o, void (C::*m)()) -{ - class XXX { - public: - C *o; - void (C::*m)(); - static void *yyy(void *vvv) { - XXX *x = (XXX*)vvv; - C *o = x->o; - void (C::*m)() = x->m; - delete x; - (o->*m)(); - return 0; - } - }; - XXX *x = new XXX; - x->o = o; - x->m = m; - pthread_t th; - if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ - return th; - } - return 0; -} - -template <class C, class A> pthread_t -method_thread1(C *o, void (C::*m)(A), A a) -{ - class XXX { - public: - C *o; - void (C::*m)(A a); - A a; - static void *yyy(void *vvv) { - XXX *x = (XXX*)vvv; - C *o = x->o; - void (C::*m)(A ) = x->m; - A a = x->a; - delete x; - (o->*m)(a); - return 0; - } - }; - XXX *x = new XXX; - x->o = o; - x->m = m; - x->a = a; - pthread_t th; - if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ - return th; - } - return 0; -} - -template <class C, class A1, class A2> pthread_t -method_thread1(C *o, void (C::*m)(A1 , A2 ), A1 a1, A2 a2) -{ - class XXX { - public: - C *o; - void (C::*m)(A1 a1, A2 a2); - A1 a1; - A2 a2; - static void *yyy(void *vvv) { - XXX *x = (XXX*)vvv; - C *o = x->o; - void (C::*m)(A1 , A2 ) = x->m; - A1 a1 = x->a1; - A2 a2 = x->a2; - delete x; - (o->*m)(a1, a2); - return 0; - } - }; - XXX *x = new XXX; - x->o = o; - x->m = m; - x->a1 = a1; - x->a2 = a2; - pthread_t th; - if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ - return th; - } - return 0; -} - -template <class C, class A1, class A2, class A3> pthread_t -method_thread1(C *o, void (C::*m)(A1 , A2, A3), A1 a1, A2 a2, A3 a3) -{ - class XXX { - public: - C *o; - void (C::*m)(A1 a1, A2 a2, A3 a3); - A1 a1; - A2 a2; - A3 a3; - static void *yyy(void *vvv) { - XXX *x = (XXX*)vvv; - C *o = x->o; - void (C::*m)(A1, A2, A3) = x->m; - A1 a1 = x->a1; - A2 a2 = x->a2; - A3 a3 = x->a3; - delete x; - (o->*m)(a1, a2, a3); - return 0; - } - }; - XXX *x = new XXX; - x->o = o; - x->m = m; - x->a1 = a1; - x->a2 = a2; - x->a3 = a3; - pthread_t th; - if(pthread_create(&th, NULL, &XXX::yyy, (void *) x) == 0){ - return th; - } - return 0; -} - -#endif This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |