Remove cycle suppression
[gromacs.git] / cmake / ThreadMPI.cmake
blobdc55cf610a5f9becfe669f0b4d324978e57aeae5
1 # This source code file is part of thread_mpi.
2 # Written by Sander Pronk, Erik Lindahl, and possibly others.
4 # Copyright (c) 2009, Sander Pronk, Erik Lindahl.
5 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 # 1) Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2) Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # 3) Neither the name of the copyright holders nor the
15 # names of its contributors may be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY US ''AS IS'' AND ANY
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL WE BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # If you want to redistribute modifications, please consider that
30 # scientific software is very special. Version control is crucial -
31 # bugs must be traceable. We will be happy to consider code for
32 # inclusion in the official distribution, but derived work should not
33 # be called official thread_mpi. Details are found in the README & COPYING
34 # files.
36 include(CheckIncludeFileCXX)
37 include(CheckCXXSymbolExists)
38 include(CheckCXXSourceCompiles)
40 # sets TMPI_ATOMICS to 1 if atomic operations are found, unset otherwise
41 # Options:
42 # include directory for thread_mpi/atomic.h
43 MACRO(TMPI_TEST_ATOMICS INCDIR)
45     if (NOT DEFINED TMPI_ATOMICS)
46         try_compile(TEST_ATOMICS "${CMAKE_BINARY_DIR}"
47                 "${CMAKE_SOURCE_DIR}/cmake/TestAtomics.cpp"
48                 COMPILE_DEFINITIONS "-I${INCDIR} -DTMPI_ATOMICS")
49         if (TEST_ATOMICS)
50             message(STATUS "Atomic operations found")
51             # If the check fails, we want to be able to check again,
52             # in case the user has been able to fix this without
53             # needing to delete the cache. Thus we only cache
54             # positive results.
55             set(TMPI_ATOMICS ${TEST_ATOMICS} CACHE INTERNAL "Whether atomic operations are found")
56             set(TMPI_ATOMICS_INCDIR ${INCDIR} CACHE INTERNAL "Atomic operations check include dir")
57         else ()
58             message(STATUS "Atomic operations not found")
59             unset(TEST_ATOMICS)
60         endif()
61     endif()
63 ENDMACRO(TMPI_TEST_ATOMICS VARIABLE)
65 try_compile(HAVE_PROCESSOR_NUMBER ${CMAKE_BINARY_DIR} "${CMAKE_SOURCE_DIR}/cmake/TestWinProcNum.cpp")
67 include(FindThreads)
69 if(CMAKE_USE_WIN32_THREADS_INIT AND NOT HAVE_PROCESSOR_NUMBER)
70     message(WARNING "Incomplete Windows Processor Group API. If you want GROMACS to be able to set thread affinity, choose a Mingw distribution with a complete API (e.g. Mingw-w64).")
71 endif()
73 if (CMAKE_USE_WIN32_THREADS_INIT AND HAVE_PROCESSOR_NUMBER)
74     set(THREAD_WINDOWS 1)
75     set(THREAD_LIB)
76 elseif (CMAKE_USE_PTHREADS_INIT)
77     check_include_file_cxx(pthread.h    HAVE_PTHREAD_H)
78     set(THREAD_PTHREADS 1)
79     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
80 else()
81     message(FATAL_ERROR "Thread support required")
82 endif ()
84 # Turns on thread_mpi core threading functions.
85 MACRO(TMPI_ENABLE_CORE INCDIR)
86     TMPI_TEST_ATOMICS(${INCDIR})
88 # affinity checks
89     include(CheckFunctionExists)
90     if (THREAD_PTHREADS)
91         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
92         # check for sched_setaffinity
93         check_cxx_source_compiles(
94             "#define _GNU_SOURCE
95 #include <pthread.h>
96 #include <stdlib.h>
97 #include <stdio.h>
98 #include <errno.h>
99     int main(void) { cpu_set_t set;
100         CPU_ZERO(&set);
101         CPU_SET(0, &set);
102         pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
103         return 0;
104     }"
105             PTHREAD_SETAFFINITY
106         )
107         if (PTHREAD_SETAFFINITY)
108             set(HAVE_PTHREAD_SETAFFINITY 1)
109         endif ()
110         set(CMAKE_REQUIRED_LIBRARIES)
111     endif ()
114 # this runs on POSIX systems
115     check_include_file_cxx(unistd.h             HAVE_UNISTD_H)
116     check_include_file_cxx(sched.h              HAVE_SCHED_H)
117     check_include_file_cxx(sys/time.h           HAVE_SYS_TIME_H)
118     check_cxx_symbol_exists(sysconf    unistd.h HAVE_SYSCONF)
119 # this runs on windows
120 #check_include_files(windows.h          HAVE_WINDOWS_H)
121 ENDMACRO(TMPI_ENABLE_CORE)
123 # enable C++ library build.
124 set(TMPI_CXX_LIB 1)
126 # Turns on thread_mpi MPI functions.
127 MACRO(TMPI_ENABLE)
128     TMPI_TEST_ATOMICS(TMPI_ATOMICS_INCDIR)
129     if(NOT DEFINED TMPI_ATOMICS)
130         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.")
131     endif()
133     set(TMPI_ENABLED 1)
135 # the spin-waiting option
136     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)
137     mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
138     if (THREAD_MPI_WAIT_FOR_NO_ONE)
139         set(TMPI_WAIT_FOR_NO_ONE 1)
140     else ()
141         set(TMPI_WAIT_FOR_NO_ONE 0)
142     endif ()
144 # the copy buffer option
145     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)
146     mark_as_advanced(THREAD_MPI_COPY_BUFFER)
147     if (THREAD_MPI_COPY_BUFFER)
148         set(TMPI_COPY_BUFFER 1)
149     else ()
150         set(TMPI_COPY_BUFFER 0)
151     endif ()
153 # the profiling option
154     option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
155     mark_as_advanced(THREAD_MPI_PROFILING)
156     if (THREAD_MPI_PROFILING)
157         set(TMPI_PROFILE 1)
158     else ()
159         set(TMPI_PROFILE 0)
160     endif ()
162 # tmpi warnings for testing
163     option(THREAD_MPI_WARNINGS "Turn thread_mpi warnings for testing." OFF)
164     mark_as_advanced(THREAD_MPI_WARNINGS)
165     if (THREAD_MPI_WARNINGS)
166         set(TMPI_WARNINGS 1)
167     else ()
168         set(TMPI_WARNINGS 0)
169     endif ()
170 ENDMACRO(TMPI_ENABLE)
173 MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE SRC_ROOT)
174     set(${SRC_VARIABLE}
175         ${SRC_ROOT}/errhandler.cpp
176         ${SRC_ROOT}/tmpi_malloc.cpp
177         ${SRC_ROOT}/atomic.cpp
178         ${SRC_ROOT}/lock.cpp)
180     if (THREAD_PTHREADS)
181         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/pthreads.cpp)
182     elseif (THREAD_WINDOWS)
183         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/winthreads.cpp)
184     endif ()
186     if (TMPI_CXX_LIB)
187         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/system_error.cpp)
188     endif ()
190     if (TMPI_ENABLED)
191         list(APPEND ${SRC_VARIABLE}
192              ${SRC_ROOT}/alltoall.cpp      ${SRC_ROOT}/p2p_protocol.cpp
193              ${SRC_ROOT}/barrier.cpp       ${SRC_ROOT}/p2p_send_recv.cpp
194              ${SRC_ROOT}/bcast.cpp         ${SRC_ROOT}/p2p_wait.cpp
195              ${SRC_ROOT}/collective.cpp    ${SRC_ROOT}/profile.cpp
196              ${SRC_ROOT}/comm.cpp          ${SRC_ROOT}/reduce.cpp
197              ${SRC_ROOT}/event.cpp         ${SRC_ROOT}/reduce_fast.cpp
198              ${SRC_ROOT}/gather.cpp        ${SRC_ROOT}/scatter.cpp
199              ${SRC_ROOT}/group.cpp         ${SRC_ROOT}/tmpi_init.cpp
200              ${SRC_ROOT}/topology.cpp      ${SRC_ROOT}/list.cpp
201              ${SRC_ROOT}/type.cpp          ${SRC_ROOT}/scan.cpp
202              ${SRC_ROOT}/numa_malloc.cpp   ${SRC_ROOT}/once.cpp)
203     endif()
204 ENDMACRO(TMPI_GET_SOURCE_LIST)