From 8deb53cba1ce4a312792cf7ab002f09b4ef57d3a Mon Sep 17 00:00:00 2001
From: Carlos Mastalli <carlos.mastalli@laas.fr>
Date: Mon, 9 Sep 2019 13:17:28 +0200
Subject: [PATCH] [cmake] Added option in cmake for setting the number of
 threads

---
 CMakeLists.txt                | 19 ++++++++++++++-----
 benchmark/unicycle.cpp        |  2 +-
 src/core/optctrl/shooting.cpp |  2 +-
 3 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4d863aec..ea70ff26 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -37,7 +37,7 @@ ENDIF()
 SET(CXX_DISABLE_WERROR True)
 
 # Create different building options
-OPTION(ENABLE_VECTORIZATION "Enable vectorization and futhers processor-related optimizations" ON)
+OPTION(ENABLE_VECTORIZATION "Enable vectorization and futhers processor-related optimizations" OFF)
 OPTION(BUILD_PYTHON_INTERFACE "Build the python binding" ON)
 OPTION(BUILD_UNIT_TESTS "Build the unitary tests" ON)
 OPTION(BUILD_BENCHMARK "Build the benchmark" ON)
@@ -58,16 +58,25 @@ ADD_OPTIONAL_DEPENDENCY("quadprog")
 ADD_OPTIONAL_DEPENDENCY("scipy")
 
 
-OPTION(BUILD_WITH_MULTITHREADING "Build the library with the OpenMP support (required OpenMP)" ON)
+OPTION(BUILD_WITH_MULTITHREADS "Build the library with the OpenMP support (required OpenMP)" OFF)
+IF(BUILD_WITH_MULTITHREADS)
+  SET(BUILD_WITH_NTHREADS "4" CACHE STRING "Number of threads")
+  string(REGEX MATCH "^[0-9]+$" BUILD_WITH_NTHREADS  ${BUILD_WITH_NTHREADS})
+  IF(NOT BUILD_WITH_NTHREADS MATCHES "^[0-9]+$")
+    SET(BUILD_WITH_NTHREADS 4)
+    MESSAGE("Warning: the number of threads have to be an interger value, set to ${BUILD_WITH_NTHREADS}")
+  ENDIF()
+ENDIF()
 
-#OpenMP
-if(BUILD_WITH_MULTITHREADING)
+# Add OpenMP
+if(BUILD_WITH_MULTITHREADS)
   find_package(OpenMP)
 ENDIF()
 if(OPENMP_FOUND)
   SET(CMAKE_CXX_FLAGS "-fopenmp")
   ADD_DEFINITIONS(-DWITH_MULTITHREADING)
-  LIST(APPEND CFLAGS_DEPENDENCIES "-DWITH_MULTITHREADING")
+  ADD_DEFINITIONS(-DNTHREADS=${BUILD_WITH_NTHREADS})
+  LIST(APPEND CFLAGS_DEPENDENCIES "-DWITH_MULTITHREADING", "DNTHREADS")
 ENDIF()
 
 
diff --git a/benchmark/unicycle.cpp b/benchmark/unicycle.cpp
index 87bc5c2c..5be6f986 100644
--- a/benchmark/unicycle.cpp
+++ b/benchmark/unicycle.cpp
@@ -10,7 +10,7 @@
 int main() {
   bool CALLBACKS = false;
   unsigned int N = 200;  // number of nodes
-  unsigned int T = 10;   // number of trials
+  unsigned int T = 5e3;   // number of trials
   unsigned int MAXITER = 1;
   using namespace crocoddyl;
 
diff --git a/src/core/optctrl/shooting.cpp b/src/core/optctrl/shooting.cpp
index aaf79b15..04ef09e4 100644
--- a/src/core/optctrl/shooting.cpp
+++ b/src/core/optctrl/shooting.cpp
@@ -10,7 +10,7 @@
 #include <iostream>
 #ifdef WITH_MULTITHREADING
 #include <omp.h>
-#define NUM_THREADS 4
+#define NUM_THREADS NTHREADS
 #endif  // WITH_MULTITHREADING
 
 namespace crocoddyl {
-- 
GitLab