Skip to content

Commit

Permalink
Merge branch 'master' of github.com:shogun-toolbox/shogun
Browse files Browse the repository at this point in the history
  • Loading branch information
Soeren Sonnenburg committed Sep 26, 2011
2 parents f7ee9b8 + 70eff01 commit 0cadbbd
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 30 deletions.
12 changes: 12 additions & 0 deletions examples/undocumented/python_modular/library_time.py
@@ -0,0 +1,12 @@
import time
from modshogun import Time


# measure wall clock time difference
t=Time()
time.sleep(1)
diff=t.cur_time_diff()

# measure CPU time required

cpu_diff=t.cur_runtime_diff_sec()
3 changes: 2 additions & 1 deletion src/configure
Expand Up @@ -150,6 +150,7 @@ COMP_DEFAULT_OPTS="-Wall -O2"
COMP_NO_OPTS="-Wall -O0"
COMPFLAGS_C=-fPIC
COMPFLAGS_CPP=-fPIC
COMPFLAGS_SWIG_CPP="-fPIC -O0"
SEDMI='sed -i'
test "$CFLAGS" && COMPFLAGS_C="$COMPFLAGS_C $CFLAGS"
test "$CXXFLAGS" && COMPFLAGS_CPP="$COMPFLAGS_CPP $CXXFLAGS"
Expand Down Expand Up @@ -411,6 +412,7 @@ EOF

if test "$_debug" = yes ; then
COMP_OPTS="-g $COMP_OPTS";
COMPFLAGS_SWIG_CPP="-g $COMPFLAGS_SWIG_CPP"
fi

echocheck "C Compiler flags"
Expand Down Expand Up @@ -4428,7 +4430,6 @@ write_dot_config()
#enable additional warnings / -Wfloat-equal -W
if test "$_cc_major" -ge "3" 2>/dev/null ; then
COMPFLAGS_C=`echo "$COMPFLAGS_C" | sed -e 's/\(-Wall\)/\1 -Wno-unused-parameter -Wformat -Wformat-security -Wparentheses -Wshadow/'`
COMPFLAGS_SWIG_CPP=`echo "$COMPFLAGS_CPP" | sed -e 's/-Wall//'`
COMPFLAGS_CPP=`echo "$COMPFLAGS_CPP" | sed -e 's/\(-Wall\)/\1 -Wno-unused-parameter -Wformat -Wformat-security -Wparentheses -Wshadow -Wno-deprecated/'`
fi

Expand Down
2 changes: 1 addition & 1 deletion src/shogun/classifier/KNN.cpp
Expand Up @@ -147,7 +147,7 @@ CLabels* CKNN::apply()

//choose the class that got 'outputted' most often
int32_t out_idx=0;
int32_t out_max=0;
float64_t out_max=0;

for (j=0; j<num_classes; j++)
{
Expand Down
130 changes: 116 additions & 14 deletions src/shogun/classifier/svm/SVM_libsvm.cpp
Expand Up @@ -51,6 +51,10 @@
#include <string.h>
#include <stdarg.h>

#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif

namespace shogun
{

Expand Down Expand Up @@ -212,6 +216,21 @@ class QMatrix {
float64_t max_train_time;
};

class LibSVMKernel;

// helper struct for threaded processing
struct Q_THREAD_PARAM
{
int32_t i;
int32_t start;
int32_t end;
Qfloat* data;
float64_t* y;
const LibSVMKernel* q;
};

extern Parallel* sg_parallel;

class LibSVMKernel: public QMatrix {
public:
LibSVMKernel(int32_t l, svm_node * const * x, const svm_parameter& param);
Expand All @@ -225,6 +244,94 @@ class LibSVMKernel: public QMatrix {
if(x_square) CMath::swap(x_square[i],x_square[j]);
}

static void* compute_Q_parallel_helper(void* p)
{
Q_THREAD_PARAM* params= (Q_THREAD_PARAM*) p;
int32_t i=params->i;
int32_t start=params->start;
int32_t end=params->end;
float64_t* y=params->y;
Qfloat* data=params->data;
const LibSVMKernel* q=params->q;

if (y) // two class
{
for(int32_t j=start;j<end;j++)
data[j] = (Qfloat) y[i]*y[j]*q->kernel_function(i,j);
}
else // one class, eps svr
{
for(int32_t j=start;j<end;j++)
data[j] = (Qfloat) q->kernel_function(i,j);
}

return NULL;
}

void compute_Q_parallel(Qfloat* data, float64_t* lab, int32_t i, int32_t start, int32_t len) const
{
int32_t num_threads=sg_parallel->get_num_threads();
if (num_threads < 2)
{
Q_THREAD_PARAM params;
params.i=i;
params.start=start;
params.end=len;
params.y=lab;
params.data=data;
params.q=this;
compute_Q_parallel_helper((void*) &params);
}
else
{
int32_t total_num=(len-start);
pthread_t* threads = SG_MALLOC(pthread_t, num_threads-1);
Q_THREAD_PARAM* params = SG_MALLOC(Q_THREAD_PARAM, num_threads);
int32_t step= total_num/num_threads;

int32_t t;

num_threads--;
for (t=0; t<num_threads; t++)
{
params[t].i=i;
params[t].start=t*step;
params[t].end=(t+1)*step;
params[t].y=lab;
params[t].data=data;
params[t].q=this;

int code=pthread_create(&threads[t], NULL,
compute_Q_parallel_helper, (void*)&params[t]);

if (code != 0)
{
SG_SWARNING("Thread creation failed (thread %d of %d) "
"with error:'%s'\n",t, num_threads, strerror(code));
num_threads=t;
break;
}
}

params[t].i=i;
params[t].start=t*step;
params[t].end=len;
params[t].y=lab;
params[t].data=data;
params[t].q=this;
compute_Q_parallel_helper(&params[t]);

for (t=0; t<num_threads; t++)
{
if (pthread_join(threads[t], NULL) != 0)
SG_SWARNING("pthread_join of thread %d/%d failed\n", t, num_threads);
}

SG_FREE(params);
SG_FREE(threads);
}
}

inline float64_t kernel_function(int32_t i, int32_t j) const
{
return kernel->kernel(x[i]->index,x[j]->index);
Expand Down Expand Up @@ -1239,12 +1346,14 @@ class SVC_QMC: public LibSVMKernel
int32_t start;
if((start = cache->get_data(i,&data,len)) < len)
{
compute_Q_parallel(data, NULL, i, start, len);

for(int32_t j=start;j<len;j++)
{
if (y[i]==y[j])
data[j] = factor*(nr_class-1)*kernel_function(i,j);
data[j] *= (factor*(nr_class-1));
else
data[j] = -factor*kernel_function(i,j);
data[j] *= (-factor);
}
}
return data;
Expand Down Expand Up @@ -1614,10 +1723,8 @@ class SVC_Q: public LibSVMKernel
Qfloat *data;
int32_t start;
if((start = cache->get_data(i,&data,len)) < len)
{
for(int32_t j=start;j<len;j++)
data[j] = (Qfloat) y[i]*y[j]*kernel_function(i,j);
}
compute_Q_parallel(data, y, i, start, len);

return data;
}

Expand Down Expand Up @@ -1664,10 +1771,8 @@ class ONE_CLASS_Q: public LibSVMKernel
Qfloat *data;
int32_t start;
if((start = cache->get_data(i,&data,len)) < len)
{
for(int32_t j=start;j<len;j++)
data[j] = (Qfloat) kernel_function(i,j);
}
compute_Q_parallel(data, NULL, i, start, len);

return data;
}

Expand Down Expand Up @@ -1730,10 +1835,7 @@ class SVR_Q: public LibSVMKernel
Qfloat *data;
int32_t real_i = index[i];
if(cache->get_data(real_i,&data,l) < l)
{
for(int32_t j=0;j<l;j++)
data[j] = (Qfloat)kernel_function(real_i,j);
}
compute_Q_parallel(data, NULL, real_i, 0, l);

// reorder and copy
Qfloat *buf = buffer[next_buffer];
Expand Down
19 changes: 5 additions & 14 deletions src/shogun/kernel/Kernel.h
Expand Up @@ -25,8 +25,6 @@
#include <shogun/kernel/KernelNormalizer.h>

#include <vector>
#include <set>
#include <string>

namespace shogun
{
Expand Down Expand Up @@ -247,18 +245,15 @@ class CKernel : public CSGObject
*
* @return the jth column of the kernel matrix
*/
virtual std::vector<float64_t> get_kernel_col(int32_t j)
virtual SGVector<float64_t> get_kernel_col(int32_t j)
{

std::vector<float64_t> col = std::vector<float64_t>(num_rhs);
SGVector<float64_t> col = SGVector<float64_t>(num_rhs);

for (int32_t i=0; i!=num_rhs; i++)
{
col[i] = kernel(i,j);
}

return col;

}


Expand All @@ -267,18 +262,14 @@ class CKernel : public CSGObject
*
* @return the ith row of the kernel matrix
*/
virtual std::vector<float64_t> get_kernel_row(int32_t i)
virtual SGVector<float64_t> get_kernel_row(int32_t i)
{

std::vector<float64_t> row = std::vector<float64_t>(num_lhs);
SGVector<float64_t> row = SGVector<float64_t>(num_lhs);

for (int32_t j=0; j!=num_lhs; j++)
{
row[j] = kernel(i,j);
}

return row;

}

/** get kernel matrix real
Expand Down Expand Up @@ -923,8 +914,8 @@ class CKernel : public CSGObject


#ifdef USE_SVMLIGHT
/**@ cache kernel evalutations to improve speed */
#ifndef DOXYGEN_SHOULD_SKIP_THIS
/**@ cache kernel evalutations to improve speed */
struct KERNEL_CACHE {
/** index */
int32_t *index;
Expand Down
2 changes: 2 additions & 0 deletions src/shogun/kernel/MultitaskKernelMaskNormalizer.h
Expand Up @@ -13,6 +13,8 @@

#include <shogun/kernel/KernelNormalizer.h>
#include <shogun/kernel/Kernel.h>
#include <set>
#include <string>

namespace shogun
{
Expand Down
10 changes: 10 additions & 0 deletions src/shogun/lib/DataType.h
Expand Up @@ -49,6 +49,11 @@ template<class T> class SGVector
SGVector(const SGVector &orig)
: vector(orig.vector), vlen(orig.vlen), do_free(orig.do_free) { }

/** empty destructor */
virtual ~SGVector()
{
}

/** get vector
* @param src vector to get
* @param own true if should be owned
Expand Down Expand Up @@ -305,6 +310,11 @@ template<class T> class SGMatrix
: matrix(orig.matrix), num_rows(orig.num_rows),
num_cols(orig.num_cols), do_free(orig.do_free) { }

/** empty destructor */
virtual ~SGMatrix()
{
}

/** free matrix */
virtual void free_matrix()
{
Expand Down

0 comments on commit 0cadbbd

Please sign in to comment.