Add OpenMP support to density estimation tree code
There are two parts to the OpenMP support. First, cmake was instructed to compile with compiler-appropriate OpenMP flags. Second, three OpenMP pragmas were added to dt_utils.cpp to parallelize the cross-validation loop. No non-pragma code changes were necessary.
This commit is contained in:
+5
-5
@@ -250,13 +250,13 @@ endif (MSVC)
|
||||
add_definitions(-DBOOST_TEST_DYN_LINK)
|
||||
|
||||
# We require OpenMP now.
|
||||
#find_package(OpenMP REQUIRED)
|
||||
#if (OPENMP_FOUND)
|
||||
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
|
||||
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
|
||||
find_package(OpenMP REQUIRED)
|
||||
if (OPENMP_FOUND)
|
||||
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}")
|
||||
#endif (OPENMP_FOUND)
|
||||
endif (OPENMP_FOUND)
|
||||
|
||||
# Create a 'distclean' target in case the user is using an in-source build for
|
||||
# some reason.
|
||||
|
||||
@@ -177,6 +177,9 @@ DTree* mlpack::det::Trainer(arma::mat& dataset,
|
||||
regularizationConstants.resize(prunedSequence.size(), 0);
|
||||
|
||||
// Go through each fold.
|
||||
#pragma omp parallel for default(none) \
|
||||
shared(testSize,cvData,prunedSequence,regularizationConstants,dataset) \
|
||||
private(alpha,oldAlpha)
|
||||
for (size_t fold = 0; fold < folds; fold++)
|
||||
{
|
||||
// Break up data into train and test sets.
|
||||
@@ -228,6 +231,7 @@ DTree* mlpack::det::Trainer(arma::mat& dataset,
|
||||
}
|
||||
|
||||
// Update the cv regularization constant.
|
||||
#pragma omp atomic
|
||||
regularizationConstants[i] += 2.0 * cvVal / (double) dataset.n_cols;
|
||||
|
||||
// Determine the new alpha value and prune accordingly.
|
||||
@@ -245,6 +249,7 @@ DTree* mlpack::det::Trainer(arma::mat& dataset,
|
||||
}
|
||||
|
||||
if (prunedSequence.size() > 2)
|
||||
#pragma omp atomic
|
||||
regularizationConstants[prunedSequence.size() - 2] += 2.0 * cvVal /
|
||||
(double) dataset.n_cols;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user