Merge release-4-6 into master
[alexxy/gromacs.git] / cmake / ThreadMPI.cmake
1
2 include(CheckIncludeFiles)
3 include(CheckFunctionExists)
4 #include(CheckCSourceCompiles)
5
6 #option(THREAD_PTHREADS "Use posix threads" ON)
7
8 MACRO(TEST_TMPI_ATOMICS VARIABLE)
9     if (NOT DEFINED TMPI_ATOMICS)
10         try_compile(TEST_ATOMICS "${CMAKE_BINARY_DIR}"
11                 "${CMAKE_SOURCE_DIR}/cmake/TestAtomics.c"
12                 COMPILE_DEFINITIONS "-I${CMAKE_SOURCE_DIR}/src/gromacs/legacyheaders" )
13
14         if (TEST_ATOMICS)
15             message(STATUS "Atomics found")
16             set(${VARIABLE} TRUE CACHE INTERNAL "Whether atomic operations for thread-MPI were found")
17         else (TEST_ATOMICS)
18             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.")
19             set(${VARIABLE} FALSE CACHE INTERNAL "Whether atomic operations for thread-MPI were found")
20         endif(TEST_ATOMICS)
21     endif(NOT DEFINED TMPI_ATOMICS)
22 ENDMACRO(TEST_TMPI_ATOMICS VARIABLE)
23
24 MACRO(TMPI_MAKE_CXX_LIB)
25     set(TMPI_CXX_LIB 1)
26 ENDMACRO(TMPI_MAKE_CXX_LIB)
27
28 MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
29     foreach (_option IN ITEMS ${ARGN})
30         if (_option STREQUAL "CXX")
31             set(TMPI_CXX_LIB 1)
32         elseif (_option STREQUAL "NOMPI")
33             set(TMPI_NO_MPI_LIB 1)
34         else ()
35             message(FATAL_ERROR "Unknown thread_mpi option '${_option}'")
36         endif ()
37     endforeach ()
38     set(${SRC_VARIABLE}
39         thread_mpi/errhandler.c
40         thread_mpi/tmpi_malloc.c)
41     if (THREAD_PTHREADS)
42         list(APPEND ${SRC_VARIABLE} thread_mpi/pthreads.c)
43     elseif (THREAD_WINDOWS)
44         list(APPEND ${SRC_VARIABLE} thread_mpi/winthreads.c)
45     endif (THREAD_PTHREADS)
46     if (TMPI_CXX_LIB)
47         list(APPEND ${SRC_VARIABLE} thread_mpi/system_error.cpp)
48     endif (TMPI_CXX_LIB)
49     if (NOT TMPI_NO_MPI_LIB)
50         list(APPEND ${SRC_VARIABLE}
51              thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
52              thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
53              thread_mpi/bcast.c         thread_mpi/p2p_wait.c
54              thread_mpi/collective.c    thread_mpi/profile.c
55              thread_mpi/comm.c          thread_mpi/reduce.c
56              thread_mpi/event.c         thread_mpi/reduce_fast.c
57              thread_mpi/gather.c        thread_mpi/scatter.c
58              thread_mpi/group.c         thread_mpi/tmpi_init.c
59              thread_mpi/topology.c      thread_mpi/list.c
60              thread_mpi/type.c          thread_mpi/lock.c
61              thread_mpi/numa_malloc.c   thread_mpi/once.c
62              thread_mpi/scan.c)
63     endif()
64 ENDMACRO(TMPI_GET_SOURCE_LIST)
65
66 test_tmpi_atomics(TMPI_ATOMICS)
67
68 include(FindThreads)
69 if (CMAKE_USE_PTHREADS_INIT)
70     check_include_files(pthread.h    HAVE_PTHREAD_H)
71     set(THREAD_PTHREADS 1)
72     #add_definitions(-DTHREAD_PTHREADS)
73     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
74 elseif (CMAKE_USE_WIN32_THREADS_INIT)
75     set(THREAD_WINDOWS 1)
76     #add_definitions(-DTHREAD_WINDOWS)
77     set(THREAD_LIB)
78 else ()
79     message(FATAL_ERROR "Thread support required")
80 endif (CMAKE_USE_PTHREADS_INIT)
81
82
83 # the spin-waiting option
84 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)
85 mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
86 if (THREAD_MPI_WAIT_FOR_NO_ONE)
87     add_definitions(-DTMPI_WAIT_FOR_NO_ONE)
88 else (THREAD_MPI_WAIT_FOR_NO_ONE)
89     add_definitions()
90 endif (THREAD_MPI_WAIT_FOR_NO_ONE)
91
92
93 # the copy buffer option
94 option(THREAD_MPI_COPY_BUFFER "Use an intermediate copy buffer for small message sizes, to allow blocking sends to return quickly." ON)
95 mark_as_advanced(THREAD_MPI_COPY_BUFFER)
96 if (THREAD_MPI_COPY_BUFFER)
97     add_definitions()
98 else (THREAD_MPI_COPY_BUFFER)
99     add_definitions(-DTMPI_NO_COPY_BUFFER)
100 endif (THREAD_MPI_COPY_BUFFER)
101
102
103 # the profiling option
104 option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
105 mark_as_advanced(THREAD_MPI_PROFILING)
106 if (THREAD_MPI_PROFILING)
107     add_definitions(-DTMPI_PROFILE)
108 else (THREAD_MPI_PROFILING)
109     add_definitions()
110 endif (THREAD_MPI_PROFILING)
111
112 include(CheckCSourceCompiles)
113
114 # option to set affinity 
115 option(THREAD_MPI_SET_AFFINITY "Set thread affinity to a core if number of threads equal to number of hardware threads." ON)
116 mark_as_advanced(THREAD_MPI_SET_AFFINITY)
117 if (THREAD_MPI_SET_AFFINITY)
118     add_definitions(-DTMPI_SET_AFFINITY)
119 else (THREAD_MPI_SET_AFFINITY)
120     add_definitions()
121 endif (THREAD_MPI_SET_AFFINITY)
122
123 include(CheckFunctionExists)
124 if (THREAD_PTHREADS)
125     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
126     # check for sched_setaffinity
127     check_c_source_compiles(
128         "#define _GNU_SOURCE
129 #include <pthread.h>
130 #include <stdlib.h>
131 #include <stdio.h>
132 #include <errno.h>
133 int main(void) { cpu_set_t set;
134     CPU_ZERO(&set);
135     CPU_SET(0, &set);
136     pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
137     return 0;
138 }"
139         PTHREAD_SETAFFINITY
140     )
141     if (PTHREAD_SETAFFINITY)
142         set(HAVE_PTHREAD_SETAFFINITY 1)
143     endif (PTHREAD_SETAFFINITY)
144     set(CMAKE_REQUIRED_LIBRARIES)
145 endif (THREAD_PTHREADS)
146
147
148 # this runs on POSIX systems
149 check_include_files(unistd.h        HAVE_UNISTD_H)
150 check_include_files(sched.h         HAVE_SCHED_H)
151 check_include_files(sys/time.h      HAVE_SYS_TIME_H)
152 check_function_exists(sysconf       HAVE_SYSCONF)
153 # this runs on windows
154 #check_include_files(windows.h          HAVE_WINDOWS_H)