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