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