Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix the mutex/spinlock bugs
  • Loading branch information
Soeren Sonnenburg committed Aug 22, 2011
1 parent 4d7f40a commit a57de60
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
4 changes: 3 additions & 1 deletion src/configure
Expand Up @@ -846,7 +846,7 @@ EOF
if cc_check $pthr
then
echores "yes"
HAVE_PTHREAD='#define USE_SPINLOCKS 1'
USE_SPINLOCKS='#define USE_SPINLOCKS 1'
DEFINES="$DEFINES -DUSE_SPINLOCKS"
else
if test "$_spinlocks" = yes
Expand Down Expand Up @@ -4622,6 +4622,7 @@ $HAVE_DOXYGEN
$HAVE_LAPACK
$HAVE_ARPACK
$HAVE_ATLAS
$HAVE_PTHREAD
$HAVE_ACML
$HAVE_MKL
$HAVE_MVEC
Expand All @@ -4631,6 +4632,7 @@ $HAVE_LOG2
$HAVE_JBLAS
$HAVE_UJMP
$HAVE_RUBY_NARRAY
$USE_SPINLOCKS
$USE_LOGCACHE
$USE_SHORTREAL_KERNELCACHE
$USE_LOGSUMARRAY
Expand Down
18 changes: 9 additions & 9 deletions src/shogun/base/Parallel.h
Expand Up @@ -11,23 +11,23 @@
#ifndef PARALLEL_H__
#define PARALLEL_H__

#include <shogun/lib/common.h>
#include <shogun/lib/config.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>

#ifdef HAVE_PTHREAD
#ifdef USE_SPINLOCKS
#define PTHREAD_LOCK_T pthread_spinlock_t
#define PTHREAD_LOCK_INIT(lock) pthread_spin_init(&lock, 0)
#define PTHREAD_LOCK_DESTROY(lock) pthread_spin_destroy(&lock)
#define PTHREAD_LOCK(lock) pthread_spin_lock(&lock)
#define PTHREAD_UNLOCK(lock) pthread_spin_unlock(&lock)
#define PTHREAD_LOCK_INIT(lock) pthread_spin_init(lock, 0)
#define PTHREAD_LOCK_DESTROY(lock) pthread_spin_destroy(lock)
#define PTHREAD_LOCK(lock) pthread_spin_lock(lock)
#define PTHREAD_UNLOCK(lock) pthread_spin_unlock(lock)
#else
#define PTHREAD_LOCK_T pthread_mutex_t
#define PTHREAD_LOCK_INIT(lock) pthread_mutex_init(&lock, NULL)
#define PTHREAD_LOCK_DESTROY(lock) pthread_mutex_destroy(&lock)
#define PTHREAD_LOCK(lock) pthread_mutex_lock(&lock)
#define PTHREAD_UNLOCK(lock) pthread_mutex_unlock(&lock)
#define PTHREAD_LOCK_INIT(lock) pthread_mutex_init(lock, NULL)
#define PTHREAD_LOCK_DESTROY(lock) pthread_mutex_destroy(lock)
#define PTHREAD_LOCK(lock) pthread_mutex_lock(lock)
#define PTHREAD_UNLOCK(lock) pthread_mutex_unlock(lock)
#endif
#endif

Expand Down
8 changes: 4 additions & 4 deletions src/shogun/preprocessor/HessianLocallyLinearEmbedding.cpp
Expand Up @@ -148,7 +148,7 @@ SGMatrix<float64_t> CHessianLocallyLinearEmbedding::apply_to_feature_matrix(CFea
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_T W_matrix_lock;
pthread_attr_t attr;
PTHREAD_LOCK_INIT(W_matrix_lock);
PTHREAD_LOCK_INIT(&W_matrix_lock);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

Expand Down Expand Up @@ -178,7 +178,7 @@ SGMatrix<float64_t> CHessianLocallyLinearEmbedding::apply_to_feature_matrix(CFea
}
for (t=0; t<num_threads; t++)
pthread_join(threads[t], NULL);
PTHREAD_LOCK_DESTROY(W_matrix_lock);
PTHREAD_LOCK_DESTROY(&W_matrix_lock);
SG_FREE(parameters);
SG_FREE(threads);
#else
Expand Down Expand Up @@ -345,15 +345,15 @@ void* CHessianLocallyLinearEmbedding::run_hessianestimation_thread(void* p)
Pii,m_k,
0.0,q_matrix,m_k);
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(*W_matrix_lock);
PTHREAD_LOCK(W_matrix_lock);
#endif
for (j=0; j<m_k; j++)
{
for (k=0; k<m_k; k++)
W_matrix[N*neighborhood_matrix[k*N+i]+neighborhood_matrix[j*N+i]] += q_matrix[j*m_k+k];
}
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(*W_matrix_lock);
PTHREAD_UNLOCK(W_matrix_lock);
#endif
}
return NULL;
Expand Down
8 changes: 4 additions & 4 deletions src/shogun/preprocessor/LocalTangentSpaceAlignment.cpp
Expand Up @@ -142,7 +142,7 @@ SGMatrix<float64_t> CLocalTangentSpaceAlignment::apply_to_feature_matrix(CFeatur
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_T W_matrix_lock;
pthread_attr_t attr;
PTHREAD_LOCK_INIT(W_matrix_lock);
PTHREAD_LOCK_INIT(&W_matrix_lock);
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

Expand All @@ -168,7 +168,7 @@ SGMatrix<float64_t> CLocalTangentSpaceAlignment::apply_to_feature_matrix(CFeatur
}
for (t=0; t<num_threads; t++)
pthread_join(threads[t], NULL);
PTHREAD_LOCK_DESTROY(W_matrix_lock);
PTHREAD_LOCK_DESTROY(&W_matrix_lock);
SG_FREE(parameters);
SG_FREE(threads);
#else
Expand Down Expand Up @@ -290,7 +290,7 @@ void* CLocalTangentSpaceAlignment::run_ltsa_thread(void* p)

// W[neighbors of i, neighbors of i] = I - GG'
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(*W_matrix_lock);
PTHREAD_LOCK(W_matrix_lock);
#endif
for (j=0; j<m_k; j++)
{
Expand All @@ -300,7 +300,7 @@ void* CLocalTangentSpaceAlignment::run_ltsa_thread(void* p)
W_matrix[N*neighborhood_matrix[k*N+i]+neighborhood_matrix[j*N+i]] -= q_matrix[j*m_k+k];
}
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(*W_matrix_lock);
PTHREAD_UNLOCK(W_matrix_lock);
#endif
}
return NULL;
Expand Down

0 comments on commit a57de60

Please sign in to comment.