Merge "don't auto-set nvcc host compiler with cmake >2.8.9" into release-4-6
[alexxy/gromacs.git] / cmake / ThreadMPI.cmake
1 #
2 # This file is part of the GROMACS molecular simulation package.
3 #
4 # Copyright (c) 2012, by the GROMACS development team, led by
5 # David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 # others, as listed in the AUTHORS file in the top-level source
7 # directory and at http://www.gromacs.org.
8 #
9 # GROMACS is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public License
11 # as published by the Free Software Foundation; either version 2.1
12 # of the License, or (at your option) any later version.
13 #
14 # GROMACS is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 # Lesser General Public License for more details.
18 #
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with GROMACS; if not, see
21 # http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23 #
24 # If you want to redistribute modifications to GROMACS, please
25 # consider that scientific software is very special. Version
26 # control is crucial - bugs must be traceable. We will be happy to
27 # consider code for inclusion in the official distribution, but
28 # derived work must not be called official GROMACS. Details are found
29 # in the README & COPYING files - if they are missing, get the
30 # official version at http://www.gromacs.org.
31 #
32 # To help us fund GROMACS development, we humbly ask that you cite
33 # the research papers on the package. Check out http://www.gromacs.org.
34 #
35
36 include(CheckIncludeFiles)
37 include(CheckFunctionExists)
38 #include(CheckCSourceCompiles)
39
40 #option(THREAD_PTHREADS "Use posix threads" ON)
41
42 MACRO(TEST_TMPI_ATOMICS VARIABLE)
43     if (NOT DEFINED TMPI_ATOMICS)
44         try_compile(TEST_ATOMICS "${CMAKE_BINARY_DIR}"
45                 "${CMAKE_SOURCE_DIR}/cmake/TestAtomics.c"
46                 COMPILE_DEFINITIONS "-I${CMAKE_SOURCE_DIR}/include" )
47
48         if (TEST_ATOMICS)
49             message(STATUS "Atomics found")
50             set(${VARIABLE} TRUE CACHE INTERNAL "Whether atomic operations for thread-MPI were found")
51         else (TEST_ATOMICS)
52             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.")
53             set(${VARIABLE} FALSE CACHE INTERNAL "Whether atomic operations for thread-MPI were found")
54         endif(TEST_ATOMICS)
55     endif(NOT DEFINED TMPI_ATOMICS)
56 ENDMACRO(TEST_TMPI_ATOMICS VARIABLE)
57
58 MACRO(TMPI_MAKE_CXX_LIB)
59     set(TMPI_CXX_LIB 1)
60 ENDMACRO(TMPI_MAKE_CXX_LIB)
61
62 MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
63     foreach (_option IN ITEMS ${ARGN})
64         if (_option STREQUAL "CXX")
65             set(TMPI_CXX_LIB 1)
66         elseif (_option STREQUAL "NOMPI")
67             set(TMPI_NO_MPI_LIB 1)
68         else ()
69             message(FATAL_ERROR "Unknown thread_mpi option '${_option}'")
70         endif ()
71     endforeach ()
72     set(${SRC_VARIABLE}
73         thread_mpi/errhandler.c
74         thread_mpi/tmpi_malloc.c)
75     if (THREAD_PTHREADS)
76         list(APPEND ${SRC_VARIABLE} thread_mpi/pthreads.c)
77     elseif (THREAD_WINDOWS)
78         list(APPEND ${SRC_VARIABLE} thread_mpi/winthreads.c)
79     endif (THREAD_PTHREADS)
80     if (TMPI_CXX_LIB)
81         list(APPEND ${SRC_VARIABLE} thread_mpi/system_error.cpp)
82     endif (TMPI_CXX_LIB)
83     if (NOT TMPI_NO_MPI_LIB)
84         list(APPEND ${SRC_VARIABLE}
85              thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
86              thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
87              thread_mpi/bcast.c         thread_mpi/p2p_wait.c
88              thread_mpi/collective.c    thread_mpi/profile.c
89              thread_mpi/comm.c          thread_mpi/reduce.c
90              thread_mpi/event.c         thread_mpi/reduce_fast.c
91              thread_mpi/gather.c        thread_mpi/scatter.c
92              thread_mpi/group.c         thread_mpi/tmpi_init.c
93              thread_mpi/topology.c      thread_mpi/list.c
94              thread_mpi/type.c          thread_mpi/lock.c
95              thread_mpi/numa_malloc.c   thread_mpi/once.c
96              thread_mpi/scan.c)
97     endif()
98 ENDMACRO(TMPI_GET_SOURCE_LIST)
99
100 test_tmpi_atomics(TMPI_ATOMICS)
101
102 include(FindThreads)
103 if (CMAKE_USE_PTHREADS_INIT)
104     check_include_files(pthread.h    HAVE_PTHREAD_H)
105     set(THREAD_PTHREADS 1)
106     #add_definitions(-DTHREAD_PTHREADS)
107     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
108 elseif (CMAKE_USE_WIN32_THREADS_INIT)
109     set(THREAD_WINDOWS 1)
110     #add_definitions(-DTHREAD_WINDOWS)
111     set(THREAD_LIB)
112 else ()
113     message(FATAL_ERROR "Thread support required")
114 endif (CMAKE_USE_PTHREADS_INIT)
115
116
117 # the spin-waiting option
118 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)
119 mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
120 if (THREAD_MPI_WAIT_FOR_NO_ONE)
121     add_definitions(-DTMPI_WAIT_FOR_NO_ONE)
122 else (THREAD_MPI_WAIT_FOR_NO_ONE)
123     add_definitions()
124 endif (THREAD_MPI_WAIT_FOR_NO_ONE)
125
126
127 # the copy buffer option
128 option(THREAD_MPI_COPY_BUFFER "Use an intermediate copy buffer for small message sizes, to allow blocking sends to return quickly." ON)
129 mark_as_advanced(THREAD_MPI_COPY_BUFFER)
130 if (THREAD_MPI_COPY_BUFFER)
131     add_definitions()
132 else (THREAD_MPI_COPY_BUFFER)
133     add_definitions(-DTMPI_NO_COPY_BUFFER)
134 endif (THREAD_MPI_COPY_BUFFER)
135
136
137 # the profiling option
138 option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
139 mark_as_advanced(THREAD_MPI_PROFILING)
140 if (THREAD_MPI_PROFILING)
141     add_definitions(-DTMPI_PROFILE)
142 else (THREAD_MPI_PROFILING)
143     add_definitions()
144 endif (THREAD_MPI_PROFILING)
145
146 include(CheckCSourceCompiles)
147
148 # option to set affinity 
149 option(THREAD_MPI_SET_AFFINITY "Set thread affinity to a core if number of threads equal to number of hardware threads." ON)
150 mark_as_advanced(THREAD_MPI_SET_AFFINITY)
151 if (THREAD_MPI_SET_AFFINITY)
152     add_definitions(-DTMPI_SET_AFFINITY)
153 else (THREAD_MPI_SET_AFFINITY)
154     add_definitions()
155 endif (THREAD_MPI_SET_AFFINITY)
156
157 include(CheckFunctionExists)
158 if (THREAD_PTHREADS)
159     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
160     # check for sched_setaffinity
161     check_c_source_compiles(
162         "#define _GNU_SOURCE
163 #include <pthread.h>
164 #include <stdlib.h>
165 #include <stdio.h>
166 #include <errno.h>
167 int main(void) { cpu_set_t set;
168     CPU_ZERO(&set);
169     CPU_SET(0, &set);
170     pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
171     return 0;
172 }"
173         PTHREAD_SETAFFINITY
174     )
175     if (PTHREAD_SETAFFINITY)
176         set(HAVE_PTHREAD_SETAFFINITY 1)
177     endif (PTHREAD_SETAFFINITY)
178     set(CMAKE_REQUIRED_LIBRARIES)
179 endif (THREAD_PTHREADS)
180
181
182 # this runs on POSIX systems
183 check_include_files(unistd.h        HAVE_UNISTD_H)
184 check_include_files(sched.h         HAVE_SCHED_H)
185 check_include_files(sys/time.h      HAVE_SYS_TIME_H)
186 check_function_exists(sysconf       HAVE_SYSCONF)
187 # this runs on windows
188 #check_include_files(windows.h          HAVE_WINDOWS_H)