Skip to content

Commit

Permalink
added openmp set num threads in CParallel
Browse files Browse the repository at this point in the history
  • Loading branch information
lambday committed May 31, 2016
1 parent 3659533 commit 8565976
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Expand Up @@ -528,6 +528,7 @@ ENDIF()

FIND_PACKAGE(OpenMP)
if (OPENMP_FOUND)
SET(HAVE_OPENMP 1)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
Expand Down Expand Up @@ -1158,7 +1159,7 @@ IF(ENABLE_TESTING)
IF (NOT TRAVIS_DISABLE_UNIT_TESTS AND EXISTS ${CMAKE_SOURCE_DIR}/tests/unit)
add_subdirectory(${CMAKE_SOURCE_DIR}/tests/unit)
ENDIF()

IF(BUILD_META_EXAMPLES)
IF(NOT TRAVIS_DISABLE_META_INTEGRATION_TESTS AND EXISTS ${CMAKE_SOURCE_DIR}/tests/meta)
add_subdirectory(${CMAKE_SOURCE_DIR}/tests/meta)
Expand Down
26 changes: 20 additions & 6 deletions src/shogun/base/Parallel.cpp
Expand Up @@ -20,19 +20,30 @@
#include <sys/sysctl.h>
#endif

#ifdef HAVE_OPENMP
#include <omp.h>
#endif

using namespace shogun;

Parallel::Parallel()
{
num_threads=get_num_cpus();
m_refcount = new RefCount();
#ifdef HAVE_OPENMP
omp_set_dynamic(0);
omp_set_num_threads(num_threads);
#endif
}

Parallel::Parallel(const Parallel& orig)
{
num_threads=orig.get_num_threads();
m_refcount = new RefCount();
#ifdef HAVE_OPENMP
omp_set_dynamic(0);
omp_set_num_threads(num_threads);
#endif
}

Parallel::~Parallel()
Expand All @@ -43,14 +54,14 @@ Parallel::~Parallel()
int32_t Parallel::get_num_cpus() const
{
#if defined(LINUX) && defined(_SC_NPROCESSORS_ONLN)
return sysconf( _SC_NPROCESSORS_ONLN );
return sysconf( _SC_NPROCESSORS_ONLN );
#elif defined(DARWIN)
int num; /* for calling external lib */
size_t size=sizeof(num);
if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
return num;
int num; /* for calling external lib */
size_t size=sizeof(num);
if (!sysctlbyname("hw.ncpu", &num, &size, NULL, 0))
return num;
#endif
return 1;
return 1;
}

void Parallel::set_num_threads(int32_t n)
Expand All @@ -59,6 +70,9 @@ void Parallel::set_num_threads(int32_t n)
ASSERT(n==1)
#endif
num_threads=n;
#ifdef HAVE_OPENMP
omp_set_num_threads(num_threads);
#endif
}

int32_t Parallel::get_num_threads() const
Expand Down
1 change: 1 addition & 0 deletions src/shogun/lib/config.h.in
Expand Up @@ -29,6 +29,7 @@
#cmakedefine HAVE_NLOPT 1
#cmakedefine USE_LPSOLVE 1
#cmakedefine HAVE_PTHREAD 1
#cmakedefine HAVE_OPENMP 1
#cmakedefine USE_CPLEX 1
#cmakedefine HAVE_COLPACK 1
#cmakedefine HAVE_ARPREC 1
Expand Down
73 changes: 73 additions & 0 deletions tests/unit/base/Parallel_unittest.cc
@@ -0,0 +1,73 @@
/*
* Copyright (c) The Shogun Machine Learning Toolbox
* Written (w) 2016 Soumyajit De
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the Shogun Development Team.
*/

#include <shogun/lib/config.h>
#ifdef HAVE_OPENMP
#include <shogun/base/init.h>
#include <shogun/base/Parallel.h>
#include <gtest/gtest.h>
#include <omp.h>

using namespace shogun;

TEST(Parallel, openmp_get_num_threads)
{
int32_t omp_num_threads=omp_get_num_threads();
ASSERT_EQ(1, omp_num_threads);

int32_t sg_num_threads=get_global_parallel()->get_num_threads();
#pragma omp parallel
{
#pragma omp master
omp_num_threads=omp_get_num_threads();
}

ASSERT_EQ(sg_num_threads, omp_num_threads);
}

TEST(Parallel, openmp_set_num_threads)
{
int32_t orig_num_threads=get_global_parallel()->get_num_threads();
int32_t omp_num_threads=omp_get_num_threads();

int32_t desired_num_threads=10;
get_global_parallel()->set_num_threads(desired_num_threads);

#pragma omp parallel
{
#pragma omp master
omp_num_threads=omp_get_num_threads();
}

ASSERT_EQ(desired_num_threads, omp_num_threads);

get_global_parallel()->set_num_threads(orig_num_threads);
}
#endif // HAVE_OPENMP

0 comments on commit 8565976

Please sign in to comment.