Skip to content

Commit

Permalink
Make use of openmp locks instead of pthread ones
Browse files Browse the repository at this point in the history
  • Loading branch information
lisitsyn committed Jul 12, 2012
1 parent 6cc5b15 commit 1d1648f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 62 deletions.
36 changes: 9 additions & 27 deletions src/shogun/base/SGObject.cpp
Expand Up @@ -124,9 +124,7 @@ CSGObject::~CSGObject()
{
SG_GCDEBUG("SGObject destroyed (%p)\n", this);

#ifdef HAVE_PTHREAD
PTHREAD_LOCK_DESTROY(&m_ref_lock);
#endif
omp_destroy_lock(&m_ref_lock);
unset_global_objects();
delete m_parameters;
delete m_model_selection_parameters;
Expand All @@ -137,51 +135,37 @@ CSGObject::~CSGObject()

int32_t CSGObject::ref()
{
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
omp_set_lock(&m_ref_lock);
++m_refcount;
int32_t count=m_refcount;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
omp_unset_lock(&m_ref_lock);
SG_GCDEBUG("ref() refcount %ld obj %s (%p) increased\n", count, this->get_name(), this);
return m_refcount;
}

int32_t CSGObject::ref_count()
{
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
omp_set_lock(&m_ref_lock);
int32_t count=m_refcount;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
omp_unset_lock(&m_ref_lock);
SG_GCDEBUG("ref_count(): refcount %d, obj %s (%p)\n", count, this->get_name(), this);
return count;
}

int32_t CSGObject::unref()
{
#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
omp_set_lock(&m_ref_lock);
if (m_refcount==0 || --m_refcount==0)
{
SG_GCDEBUG("unref() refcount %ld, obj %s (%p) destroying\n", m_refcount, this->get_name(), this);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
omp_unset_lock(&m_ref_lock);
delete this;
return 0;
}
else
{
SG_GCDEBUG("unref() refcount %ld obj %s (%p) decreased\n", m_refcount, this->get_name(), this);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_ref_lock);
#endif //HAVE_PTHREAD
omp_unset_lock(&m_ref_lock);
return m_refcount;
}
}
Expand Down Expand Up @@ -1050,9 +1034,7 @@ extern CMap<void*, shogun::MemoryBlock>* sg_mallocs;

void CSGObject::init()
{
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_INIT(&m_ref_lock);
#endif
omp_init_lock(&m_ref_lock);

#ifdef TRACE_MEMORY_ALLOCS
if (sg_mallocs)
Expand Down
9 changes: 2 additions & 7 deletions src/shogun/base/SGObject.h
Expand Up @@ -21,9 +21,7 @@
#include <shogun/base/Parallel.h>
#include <shogun/base/Version.h>

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

/** \namespace shogun
* @brief all of classes and functions are contained in the shogun namespace
Expand Down Expand Up @@ -457,10 +455,7 @@ class CSGObject
bool m_save_post_called;

int32_t m_refcount;

#ifdef HAVE_PTHREAD
PTHREAD_LOCK_T m_ref_lock;
#endif //HAVE_PTHREAD
omp_lock_t m_ref_lock;
};
}
#endif // __SGOBJECT_H__
38 changes: 10 additions & 28 deletions src/shogun/lib/SGReferencedData.h
Expand Up @@ -12,16 +12,12 @@
#include <shogun/io/SGIO.h>
#include <shogun/base/Parallel.h>

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

struct refcount_t
{
int32_t rc;
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_T lock;
#endif
omp_lock_t lock;
};

namespace shogun
Expand All @@ -36,7 +32,7 @@ class SGReferencedData
if (ref_counting)
{
m_refcount = SG_CALLOC(refcount_t, 1);
PTHREAD_LOCK_INIT(&m_refcount->lock);
omp_init_lock(&m_refcount->lock);
}

ref();
Expand Down Expand Up @@ -80,13 +76,9 @@ class SGReferencedData
if (m_refcount == NULL)
return -1;

#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_refcount->lock);
#endif
omp_set_lock(&m_refcount->lock);
int32_t c = m_refcount->rc;
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_refcount->lock);
#endif
omp_unset_lock(&m_refcount->lock);

#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref_count(): refcount %d, data %p\n", c, this);
Expand All @@ -111,13 +103,9 @@ class SGReferencedData
return -1;
}

#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_refcount->lock);
#endif
omp_set_lock(&m_refcount->lock);
int32_t c = ++(m_refcount->rc);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_refcount->lock);
#endif
omp_unset_lock(&m_refcount->lock);
#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("ref() refcount %ld data %p increased\n", c, this);
#endif
Expand All @@ -138,22 +126,16 @@ class SGReferencedData
return -1;
}

#ifdef HAVE_PTHREAD
PTHREAD_LOCK(&m_refcount->lock);
#endif
omp_set_lock(&m_refcount->lock);
int32_t c = --(m_refcount->rc);
#ifdef HAVE_PTHREAD
PTHREAD_UNLOCK(&m_refcount->lock);
#endif
omp_unset_lock(&m_refcount->lock);
if (c<=0)
{
#ifdef DEBUG_SGVECTOR
SG_SGCDEBUG("unref() refcount %d data %p destroying\n", c, this);
#endif
free_data();
#ifdef HAVE_PTHREAD
PTHREAD_LOCK_DESTROY(&m_refcount->lock);
#endif
omp_destroy_lock(&m_refcount->lock);
SG_FREE(m_refcount);
m_refcount=NULL;
return 0;
Expand Down

0 comments on commit 1d1648f

Please sign in to comment.