Skip to content

Commit

Permalink
Replaced OpenMP with tbb
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanming-hu committed Nov 14, 2017
1 parent 3a71e7c commit 9ac3282
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 54 deletions.
12 changes: 0 additions & 12 deletions cmake/TaichiCXXFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,4 @@ if (TC_USE_MPI)
message("Using MPI")
endif ()

if (TC_USE_OPENMP)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTC_USE_OPENMP")
message("Using OpenMP")
if (APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp=libiomp5")
elseif (MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /openmp")
else ()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")
endif ()
endif ()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
4 changes: 4 additions & 0 deletions include/taichi/math/angular.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
#include <taichi/util.h>
#include <taichi/math/math.h>
#include <taichi/math/vector.h>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wmisleading-indentation"
#include <Eigen/Geometry>
#pragma GCC diagnostic pop
#include <type_traits>

TC_NAMESPACE_BEGIN
Expand Down
4 changes: 4 additions & 0 deletions include/taichi/math/array_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ class ArrayND<2, T> {
}
}

void reset_zero() {
memset(&data[0], 0, sizeof(T) * data.size());
}

bool same_dim(const Array2D<T> &arr) const {
return res == arr.res;
}
Expand Down
4 changes: 4 additions & 0 deletions include/taichi/math/array_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,10 @@ class ArrayND<3, T> {
}
}

void reset_zero() {
memset(&data[0], 0, sizeof(T) * data.size());
}

bool same_dim(const Array3D<T> &arr) const {
return res == arr.res;
}
Expand Down
45 changes: 3 additions & 42 deletions include/taichi/system/threading.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@
#else
// Mac and Linux
#include <unistd.h>
#endif

#ifdef TC_USE_OPENMP

#include <omp.h>

#define TC_MT_OPENMP
#include <tbb/tbb.h>
#endif

TC_NAMESPACE_BEGIN
Expand Down Expand Up @@ -74,41 +68,8 @@ class ThreadedTaskManager {
public:
template <typename T>
void static run(const T &target, int begin, int end, int num_threads) {
#ifdef TC_MT_OPENMP
omp_set_num_threads(num_threads);
#pragma omp parallel for schedule(static)
for (int i = begin; i < end; i++) {
target(i);
}
#else
if (num_threads == 1) {
// Single-threading
for (int i = begin; i < end; i++) {
target(i);
}
} else {
// Multi-threading
std::vector<std::thread *> threads;
std::vector<int> end_points;
for (int i = 0; i < num_threads; i++) {
end_points.push_back(i * (end - begin) / num_threads + begin);
}
end_points.push_back(end);
for (int i = 0; i < num_threads; i++) {
auto func = [&target, i, &end_points]() {
int begin = end_points[i], end = end_points[i + 1];
for (int k = begin; k < end; k++) {
target(k);
}
};
threads.push_back(new std::thread(func));
}
for (int i = 0; i < num_threads; i++) {
threads[i]->join();
delete threads[i];
}
}
#endif
tbb::task_arena limited_arena(num_threads);
limited_arena.execute([&]() { tbb::parallel_for(begin, end, target); });
}

template <typename T>
Expand Down

0 comments on commit 9ac3282

Please sign in to comment.