Merge branch 'release-4-6'
[alexxy/gromacs.git] / cmake / ThreadMPI.cmake
index fc9183afc2f1a78240caa8b1fbe34496b9cee505..bcd280ae08d48b7d9d2e31d4a1c8c9a740d73231 100644 (file)
@@ -1,31 +1,53 @@
 
 include(CheckIncludeFiles)
 include(CheckFunctionExists)
-#include(CheckCSourceCompiles)
-
-#option(THREAD_PTHREADS "Use posix threads" ON)
+include(CheckCSourceCompiles)
 
-MACRO(TEST_TMPI_ATOMICS VARIABLE)
+# sets TMPI_ATOMICS to 1 if atomic operations are found, 0 otherwise
+MACRO(TMPI_TEST_ATOMICS)
     if (NOT DEFINED TMPI_ATOMICS)
         try_compile(TEST_ATOMICS "${CMAKE_BINARY_DIR}"
                 "${CMAKE_SOURCE_DIR}/cmake/TestAtomics.c"
                 COMPILE_DEFINITIONS "-I${CMAKE_SOURCE_DIR}/src/gromacs/legacyheaders" )
 
         if (TEST_ATOMICS)
-            message(STATUS "Atomics found")
-            set(${VARIABLE} TRUE CACHE INTERNAL "Whether atomic operations for thread-MPI were found")
+            message(STATUS "Atomic operations found")
         else (TEST_ATOMICS)
-            message(WARNING "Atomic operations not found for this CPU+compiler combination. Thread support will be unbearably slow: disable threads. Atomic operations should work on all but the most obscure CPU+compiler combinations; if your system is not obscure -- like, for example, x86 with gcc --  please contact the developers.")
-            set(${VARIABLE} FALSE CACHE INTERNAL "Whether atomic operations for thread-MPI were found")
+            message(STATUS "Atomic operations not found")
         endif(TEST_ATOMICS)
+        set(TMPI_ATOMICS ${TEST_ATOMICS} CACHE INTERNAL "Whether atomic operations are found")
     endif(NOT DEFINED TMPI_ATOMICS)
-ENDMACRO(TEST_TMPI_ATOMICS VARIABLE)
+ENDMACRO(TMPI_TEST_ATOMICS VARIABLE)
 
-MACRO(TMPI_MAKE_CXX_LIB)
-    set(TMPI_CXX_LIB 1)
-ENDMACRO(TMPI_MAKE_CXX_LIB)
+TMPI_TEST_ATOMICS()
 
-MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
+include(FindThreads)
+if (CMAKE_USE_PTHREADS_INIT)
+    check_include_files(pthread.h    HAVE_PTHREAD_H)
+    set(THREAD_PTHREADS 1)
+    set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
+elseif (CMAKE_USE_WIN32_THREADS_INIT)
+    set(THREAD_WINDOWS 1)
+    set(THREAD_LIB)
+else ()
+    message(FATAL_ERROR "Thread support required")
+endif (CMAKE_USE_PTHREADS_INIT)
+
+# Turns on thread_mpi.
+# options are:
+# CXX: enable C++ library build.
+MACRO(TMPI_ENABLE)
+    # first check whether threads and atomics are available.
+    if(NOT TMPI_ATOMICS)
+        # check again, to allow the user to fix this.
+        unset(TMPI_ATOMICS CACHE)
+        TMPI_TEST_ATOMICS()
+    endif(NOT TMPI_ATOMICS)
+    if(NOT TMPI_ATOMICS)
+        message(WARNING "Atomic operations not found for this CPU+compiler combination. Thread support will be unbearably slow: disable threads. Atomic operations should work on all but the most obscure CPU+compiler combinations; if your system is not obscure -- like, for example, x86 with gcc --  please contact the developers.")
+    endif(NOT TMPI_ATOMICS)
+
+    set(TMPI_ENABLED 1)
     foreach (_option IN ITEMS ${ARGN})
         if (_option STREQUAL "CXX")
             set(TMPI_CXX_LIB 1)
@@ -35,9 +57,87 @@ MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
             message(FATAL_ERROR "Unknown thread_mpi option '${_option}'")
         endif ()
     endforeach ()
+
+    #tmpi_test_atomics(TMPI_ATOMICS)
+
+# the spin-waiting option
+    option(THREAD_MPI_WAIT_FOR_NO_ONE "Use busy waits without yielding to the OS scheduler. Turning this on might improve performance (very) slightly at the cost of very poor performance if the threads are competing for CPU time." OFF)
+    mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
+    if (THREAD_MPI_WAIT_FOR_NO_ONE)
+        set(TMPI_WAIT_FOR_NO_ONE 1)
+    else (THREAD_MPI_WAIT_FOR_NO_ONE)
+        set(TMPI_WAIT_FOR_NO_ONE 0)
+    endif (THREAD_MPI_WAIT_FOR_NO_ONE)
+
+# the copy buffer option
+    option(THREAD_MPI_COPY_BUFFER "Use an intermediate copy buffer for small message sizes, to allow blocking sends to return quickly. Only useful in programs with relatively uncoupled threads (infrequent MPI communication)" OFF)
+    mark_as_advanced(THREAD_MPI_COPY_BUFFER)
+    if (THREAD_MPI_COPY_BUFFER)
+        set(TMPI_COPY_BUFFER 1)
+    else (THREAD_MPI_COPY_BUFFER)
+        set(TMPI_COPY_BUFFER 0)
+    endif (THREAD_MPI_COPY_BUFFER)
+
+# the profiling option
+    option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
+    mark_as_advanced(THREAD_MPI_PROFILING)
+    if (THREAD_MPI_PROFILING)
+        set(TMPI_PROFILE 1)
+    else (THREAD_MPI_PROFILING)
+        set(TMPI_PROFILE 0)
+    endif (THREAD_MPI_PROFILING)
+
+# tmpi warnings for testing
+    option(THREAD_MPI_WARNINGS "Turn thread_mpi warnings for testing." OFF)
+    mark_as_advanced(THREAD_MPI_WARNINGS)
+    if (THREAD_MPI_WARNINGS)
+        set(TMPI_WARNINGS 1)
+    else (THREAD_MPI_WARNINGS)
+        set(TMPI_WARNINGS 0)
+    endif (THREAD_MPI_WARNINGS)
+
+    include(CheckCSourceCompiles)
+
+# affinity checks
+    include(CheckFunctionExists)
+    if (THREAD_PTHREADS)
+        set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
+        # check for sched_setaffinity
+        check_c_source_compiles(
+            "#define _GNU_SOURCE
+#include <pthread.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <errno.h>
+    int main(void) { cpu_set_t set;
+        CPU_ZERO(&set);
+        CPU_SET(0, &set);
+        pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
+        return 0;
+    }"
+            PTHREAD_SETAFFINITY
+        )
+        if (PTHREAD_SETAFFINITY)
+            set(HAVE_PTHREAD_SETAFFINITY 1)
+        endif (PTHREAD_SETAFFINITY)
+        set(CMAKE_REQUIRED_LIBRARIES)
+    endif (THREAD_PTHREADS)
+
+
+# this runs on POSIX systems
+    check_include_files(unistd.h        HAVE_UNISTD_H)
+    check_include_files(sched.h         HAVE_SCHED_H)
+    check_include_files(sys/time.h      HAVE_SYS_TIME_H)
+    check_function_exists(sysconf       HAVE_SYSCONF)
+# this runs on windows
+#check_include_files(windows.h         HAVE_WINDOWS_H)
+ENDMACRO(TMPI_ENABLE)
+
+MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
     set(${SRC_VARIABLE}
         thread_mpi/errhandler.c
-        thread_mpi/tmpi_malloc.c)
+        thread_mpi/tmpi_malloc.c
+        thread_mpi/atomic.c)
     if (THREAD_PTHREADS)
         list(APPEND ${SRC_VARIABLE} thread_mpi/pthreads.c)
     elseif (THREAD_WINDOWS)
@@ -46,7 +146,7 @@ MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
     if (TMPI_CXX_LIB)
         list(APPEND ${SRC_VARIABLE} thread_mpi/system_error.cpp)
     endif (TMPI_CXX_LIB)
-    if (NOT TMPI_NO_MPI_LIB)
+    if (TMPI_ENABLED)
         list(APPEND ${SRC_VARIABLE}
              thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
              thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
@@ -63,92 +163,3 @@ MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
     endif()
 ENDMACRO(TMPI_GET_SOURCE_LIST)
 
-test_tmpi_atomics(TMPI_ATOMICS)
-
-include(FindThreads)
-if (CMAKE_USE_PTHREADS_INIT)
-    check_include_files(pthread.h    HAVE_PTHREAD_H)
-    set(THREAD_PTHREADS 1)
-    #add_definitions(-DTHREAD_PTHREADS)
-    set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
-elseif (CMAKE_USE_WIN32_THREADS_INIT)
-    set(THREAD_WINDOWS 1)
-    #add_definitions(-DTHREAD_WINDOWS)
-    set(THREAD_LIB)
-else ()
-    message(FATAL_ERROR "Thread support required")
-endif (CMAKE_USE_PTHREADS_INIT)
-
-
-# the spin-waiting option
-option(THREAD_MPI_WAIT_FOR_NO_ONE "Use busy waits without yielding to the OS scheduler. Turning this on might improve performance (very) slightly at the cost of very poor performance if the threads are competing for CPU time." OFF)
-mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
-if (THREAD_MPI_WAIT_FOR_NO_ONE)
-    add_definitions(-DTMPI_WAIT_FOR_NO_ONE)
-else (THREAD_MPI_WAIT_FOR_NO_ONE)
-    add_definitions()
-endif (THREAD_MPI_WAIT_FOR_NO_ONE)
-
-
-# the copy buffer option
-option(THREAD_MPI_COPY_BUFFER "Use an intermediate copy buffer for small message sizes, to allow blocking sends to return quickly." ON)
-mark_as_advanced(THREAD_MPI_COPY_BUFFER)
-if (THREAD_MPI_COPY_BUFFER)
-    add_definitions()
-else (THREAD_MPI_COPY_BUFFER)
-    add_definitions(-DTMPI_NO_COPY_BUFFER)
-endif (THREAD_MPI_COPY_BUFFER)
-
-
-# the profiling option
-option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
-mark_as_advanced(THREAD_MPI_PROFILING)
-if (THREAD_MPI_PROFILING)
-    add_definitions(-DTMPI_PROFILE)
-else (THREAD_MPI_PROFILING)
-    add_definitions()
-endif (THREAD_MPI_PROFILING)
-
-include(CheckCSourceCompiles)
-
-# option to set affinity 
-option(THREAD_MPI_SET_AFFINITY "Set thread affinity to a core if number of threads equal to number of hardware threads." ON)
-mark_as_advanced(THREAD_MPI_SET_AFFINITY)
-if (THREAD_MPI_SET_AFFINITY)
-    add_definitions(-DTMPI_SET_AFFINITY)
-else (THREAD_MPI_SET_AFFINITY)
-    add_definitions()
-endif (THREAD_MPI_SET_AFFINITY)
-
-include(CheckFunctionExists)
-if (THREAD_PTHREADS)
-    set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
-    # check for sched_setaffinity
-    check_c_source_compiles(
-        "#define _GNU_SOURCE
-#include <pthread.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <errno.h>
-int main(void) { cpu_set_t set;
-    CPU_ZERO(&set);
-    CPU_SET(0, &set);
-    pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
-    return 0;
-}"
-        PTHREAD_SETAFFINITY
-    )
-    if (PTHREAD_SETAFFINITY)
-        set(HAVE_PTHREAD_SETAFFINITY 1)
-    endif (PTHREAD_SETAFFINITY)
-    set(CMAKE_REQUIRED_LIBRARIES)
-endif (THREAD_PTHREADS)
-
-
-# this runs on POSIX systems
-check_include_files(unistd.h        HAVE_UNISTD_H)
-check_include_files(sched.h         HAVE_SCHED_H)
-check_include_files(sys/time.h      HAVE_SYS_TIME_H)
-check_function_exists(sysconf       HAVE_SYSCONF)
-# this runs on windows
-#check_include_files(windows.h         HAVE_WINDOWS_H)