Update instructions in containers.rst
[gromacs.git] / cmake / ThreadMPI.cmake
blobc0847c429e7f7eabd9e8c5b5cf3daac4f55fcac7
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 path for thread_mpi/atomic.h
43 function(TMPI_TEST_ATOMICS INCDIR)
45     if (NOT DEFINED TMPI_ATOMICS)
46         set(CMAKE_REQUIRED_INCLUDES ${INCDIR})
47         check_cxx_source_compiles("
48 // Set a define that forces a compilation error if this platform
49 // is not yet supported.
50 #define TMPI_CHECK_ATOMICS
52 // Include the portable atomics implementation to test
53 #include \"thread_mpi/atomic.h\"
55 int main(void)
57     int i;
58     void *ptr;
59     tMPI_Atomic_t some_atomic;
60     tMPI_Atomic_ptr_t *some_atomic_ptr = NULL;
61     tMPI_Spinlock_t some_spinlock;
63     /* Make the compiler actually emit code for these functions, so
64        that things like inability to emit inline assembly get
65        tested. It is not expected that the code below can run. */
66     tMPI_Atomic_memory_barrier();
67     tMPI_Atomic_memory_barrier_acq();
68     tMPI_Atomic_memory_barrier_rel();
69     tMPI_Atomic_set(&some_atomic, 0);
70     i   = tMPI_Atomic_get(&some_atomic);
71     ptr = tMPI_Atomic_ptr_get(some_atomic_ptr);
72     tMPI_Atomic_ptr_set(some_atomic_ptr, ptr);
73     tMPI_Atomic_add_return(&some_atomic, 0);
74     tMPI_Atomic_fetch_add(&some_atomic, 0);
75     tMPI_Atomic_cas(&some_atomic, 0, 1);
76     tMPI_Atomic_ptr_cas(some_atomic_ptr, ptr, ptr);
77     tMPI_Atomic_swap(&some_atomic, 0);
78     tMPI_Atomic_ptr_swap(some_atomic_ptr, ptr);
79     tMPI_Spinlock_init(&some_spinlock);
80     tMPI_Spinlock_lock(&some_spinlock);
81     tMPI_Spinlock_trylock(&some_spinlock);
82     tMPI_Spinlock_unlock(&some_spinlock);
83     tMPI_Spinlock_islocked(&some_spinlock);
84     tMPI_Spinlock_wait(&some_spinlock);
85     return 0;
86 }" TEST_ATOMICS)
87         if (TEST_ATOMICS)
88             message(STATUS "Atomic operations found")
89             # If the check fails, we want to be able to check again,
90             # in case the user has been able to fix this without
91             # needing to delete the cache. Thus we only cache
92             # positive results.
93             set(TMPI_ATOMICS ${TEST_ATOMICS} CACHE INTERNAL "Whether atomic operations are found")
94             set(TMPI_ATOMICS_INCDIR ${INCDIR} CACHE INTERNAL "Atomic operations check include dir")
95         else ()
96             message(STATUS "Atomic operations not found")
97             unset(TEST_ATOMICS CACHE)
98         endif()
99     endif()
101 endfunction()
103 try_compile(HAVE_PROCESSOR_NUMBER ${CMAKE_BINARY_DIR} "${CMAKE_SOURCE_DIR}/cmake/TestWinProcNum.cpp")
105 include(FindThreads)
107 if(CMAKE_USE_WIN32_THREADS_INIT AND NOT HAVE_PROCESSOR_NUMBER)
108     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).")
109 endif()
111 if (CMAKE_USE_WIN32_THREADS_INIT AND HAVE_PROCESSOR_NUMBER)
112     set(THREAD_WINDOWS 1)
113     set(THREAD_LIB)
114 elseif (CMAKE_USE_PTHREADS_INIT)
115     check_include_file_cxx(pthread.h    HAVE_PTHREAD_H)
116     set(THREAD_PTHREADS 1)
117     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
118 else()
119     message(FATAL_ERROR "Thread support required")
120 endif ()
122 # Turns on thread_mpi core threading functions.
123 MACRO(TMPI_ENABLE_CORE INCDIR)
124     TMPI_TEST_ATOMICS(${INCDIR})
126 # affinity checks
127     include(CheckFunctionExists)
128     if (THREAD_PTHREADS)
129         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
130         # check for sched_setaffinity
131         check_cxx_source_compiles(
132             "#define _GNU_SOURCE
133 #include <pthread.h>
134 #include <stdlib.h>
135 #include <stdio.h>
136 #include <errno.h>
137     int main(void) { cpu_set_t set;
138         CPU_ZERO(&set);
139         CPU_SET(0, &set);
140         pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
141         return 0;
142     }"
143             PTHREAD_SETAFFINITY
144         )
145         if (PTHREAD_SETAFFINITY)
146             set(HAVE_PTHREAD_SETAFFINITY 1)
147         endif ()
148         set(CMAKE_REQUIRED_LIBRARIES)
149     endif ()
152 # this runs on POSIX systems
153     check_include_file_cxx(unistd.h             HAVE_UNISTD_H)
154     check_include_file_cxx(sched.h              HAVE_SCHED_H)
155     check_include_file_cxx(sys/time.h           HAVE_SYS_TIME_H)
156     check_cxx_symbol_exists(sysconf    unistd.h HAVE_SYSCONF)
157 # this runs on windows
158 #check_include_files(windows.h          HAVE_WINDOWS_H)
159 ENDMACRO(TMPI_ENABLE_CORE)
161 # enable C++ library build.
162 set(TMPI_CXX_LIB 1)
164 # Turns on thread_mpi MPI functions.
165 MACRO(TMPI_ENABLE)
166     if(NOT DEFINED TMPI_ATOMICS)
167         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.")
168     endif()
170     set(TMPI_ENABLED 1)
172 # the spin-waiting option
173     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)
174     mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
175     if (THREAD_MPI_WAIT_FOR_NO_ONE)
176         set(TMPI_WAIT_FOR_NO_ONE 1)
177     else ()
178         set(TMPI_WAIT_FOR_NO_ONE 0)
179     endif ()
181 # the copy buffer option
182     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)
183     mark_as_advanced(THREAD_MPI_COPY_BUFFER)
184     if (THREAD_MPI_COPY_BUFFER)
185         set(TMPI_COPY_BUFFER 1)
186     else ()
187         set(TMPI_COPY_BUFFER 0)
188     endif ()
190 # the profiling option
191     option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
192     mark_as_advanced(THREAD_MPI_PROFILING)
193     if (THREAD_MPI_PROFILING)
194         set(TMPI_PROFILE 1)
195     else ()
196         set(TMPI_PROFILE 0)
197     endif ()
199 # tmpi warnings for testing
200     option(THREAD_MPI_WARNINGS "Turn thread_mpi warnings for testing." OFF)
201     mark_as_advanced(THREAD_MPI_WARNINGS)
202     if (THREAD_MPI_WARNINGS)
203         set(TMPI_WARNINGS 1)
204     else ()
205         set(TMPI_WARNINGS 0)
206     endif ()
207 ENDMACRO(TMPI_ENABLE)
210 MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE SRC_ROOT)
211     set(${SRC_VARIABLE}
212         ${SRC_ROOT}/errhandler.cpp
213         ${SRC_ROOT}/tmpi_malloc.cpp
214         ${SRC_ROOT}/atomic.cpp
215         ${SRC_ROOT}/lock.cpp)
217     if (THREAD_PTHREADS)
218         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/pthreads.cpp)
219     elseif (THREAD_WINDOWS)
220         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/winthreads.cpp)
221     endif ()
223     if (TMPI_CXX_LIB)
224         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/system_error.cpp)
225     endif ()
227     if (TMPI_ENABLED)
228         list(APPEND ${SRC_VARIABLE}
229              ${SRC_ROOT}/alltoall.cpp      ${SRC_ROOT}/p2p_protocol.cpp
230              ${SRC_ROOT}/barrier.cpp       ${SRC_ROOT}/p2p_send_recv.cpp
231              ${SRC_ROOT}/bcast.cpp         ${SRC_ROOT}/p2p_wait.cpp
232              ${SRC_ROOT}/collective.cpp    ${SRC_ROOT}/profile.cpp
233              ${SRC_ROOT}/comm.cpp          ${SRC_ROOT}/reduce.cpp
234              ${SRC_ROOT}/event.cpp         ${SRC_ROOT}/reduce_fast.cpp
235              ${SRC_ROOT}/gather.cpp        ${SRC_ROOT}/scatter.cpp
236              ${SRC_ROOT}/group.cpp         ${SRC_ROOT}/tmpi_init.cpp
237              ${SRC_ROOT}/topology.cpp      ${SRC_ROOT}/list.cpp
238              ${SRC_ROOT}/type.cpp          ${SRC_ROOT}/scan.cpp
239              ${SRC_ROOT}/numa_malloc.cpp   ${SRC_ROOT}/once.cpp)
240     endif()
241 ENDMACRO(TMPI_GET_SOURCE_LIST)