Merge branch 'release-4-6' into master
[alexxy/gromacs.git] / cmake / ThreadMPI.cmake
1 # This source code file is part of thread_mpi.
2 # Written by Sander Pronk, Erik Lindahl, and possibly others.
3 #
4 # Copyright (c) 2009, Sander Pronk, Erik Lindahl.
5 # All rights reserved.
6 #
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.
17 #
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.
28 #
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.
35
36 include(CheckIncludeFiles)
37 include(CheckFunctionExists)
38 include(CheckCSourceCompiles)
39
40 # sets TMPI_ATOMICS to 1 if atomic operations are found, 0 otherwise
41 # Options:
42 # include directory for thread_mpi/atomic.h
43 MACRO(TMPI_TEST_ATOMICS INCDIR)
44     if (NOT DEFINED TMPI_ATOMICS)
45         try_compile(TEST_ATOMICS "${CMAKE_BINARY_DIR}"
46                 "${CMAKE_SOURCE_DIR}/cmake/TestAtomics.c"
47                 COMPILE_DEFINITIONS "-I${INCDIR}")
48         if (TEST_ATOMICS)
49             message(STATUS "Atomic operations found")
50         else (TEST_ATOMICS)
51             message(STATUS "Atomic operations not found")
52         endif(TEST_ATOMICS)
53         set(TMPI_ATOMICS ${TEST_ATOMICS} CACHE INTERNAL "Whether atomic operations are found")
54         set(TMPI_ATOMICS_INCDIR ${INCDIR} CACHE INTERNAL "Atomic operations check include dir")
55     endif(NOT DEFINED TMPI_ATOMICS)
56 ENDMACRO(TMPI_TEST_ATOMICS VARIABLE)
57
58
59 include(FindThreads)
60 if (CMAKE_USE_PTHREADS_INIT)
61     check_include_files(pthread.h    HAVE_PTHREAD_H)
62     set(THREAD_PTHREADS 1)
63     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
64 elseif (CMAKE_USE_WIN32_THREADS_INIT)
65     set(THREAD_WINDOWS 1)
66     set(THREAD_LIB)
67 else ()
68     message(FATAL_ERROR "Thread support required")
69 endif (CMAKE_USE_PTHREADS_INIT)
70
71 # Turns on thread_mpi core threading functions.
72 # options are:
73 MACRO(TMPI_ENABLE_CORE INCDIR)
74     TMPI_TEST_ATOMICS(${INCDIR})
75 ENDMACRO(TMPI_ENABLE_CORE)
76
77 # enable C++ library build.
78 MACRO(TMPI_ENABLE_CXX)
79     set(TMPI_CXX_LIB 1)
80 ENDMACRO(TMPI_ENABLE_CXX)
81
82 # Turns on thread_mpi MPI functions.
83 MACRO(TMPI_ENABLE)
84     # first check whether threads and atomics are available.
85     if(NOT TMPI_ATOMICS)
86         # check again, to allow the user to fix this.
87         unset(TMPI_ATOMICS CACHE)
88         TMPI_TEST_ATOMICS(${TMPI_ATOMICS_INCDIR})
89     endif(NOT TMPI_ATOMICS)
90     if(NOT TMPI_ATOMICS)
91         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.")
92     endif(NOT TMPI_ATOMICS)
93
94     set(TMPI_ENABLED 1)
95
96 # the spin-waiting option
97     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)
98     mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
99     if (THREAD_MPI_WAIT_FOR_NO_ONE)
100         set(TMPI_WAIT_FOR_NO_ONE 1)
101     else (THREAD_MPI_WAIT_FOR_NO_ONE)
102         set(TMPI_WAIT_FOR_NO_ONE 0)
103     endif (THREAD_MPI_WAIT_FOR_NO_ONE)
104
105 # the copy buffer option
106     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)
107     mark_as_advanced(THREAD_MPI_COPY_BUFFER)
108     if (THREAD_MPI_COPY_BUFFER)
109         set(TMPI_COPY_BUFFER 1)
110     else (THREAD_MPI_COPY_BUFFER)
111         set(TMPI_COPY_BUFFER 0)
112     endif (THREAD_MPI_COPY_BUFFER)
113
114 # the profiling option
115     option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
116     mark_as_advanced(THREAD_MPI_PROFILING)
117     if (THREAD_MPI_PROFILING)
118         set(TMPI_PROFILE 1)
119     else (THREAD_MPI_PROFILING)
120         set(TMPI_PROFILE 0)
121     endif (THREAD_MPI_PROFILING)
122
123 # tmpi warnings for testing
124     option(THREAD_MPI_WARNINGS "Turn thread_mpi warnings for testing." OFF)
125     mark_as_advanced(THREAD_MPI_WARNINGS)
126     if (THREAD_MPI_WARNINGS)
127         set(TMPI_WARNINGS 1)
128     else (THREAD_MPI_WARNINGS)
129         set(TMPI_WARNINGS 0)
130     endif (THREAD_MPI_WARNINGS)
131
132     include(CheckCSourceCompiles)
133
134 # affinity checks
135     include(CheckFunctionExists)
136     if (THREAD_PTHREADS)
137         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
138         # check for sched_setaffinity
139         check_c_source_compiles(
140             "#define _GNU_SOURCE
141 #include <pthread.h>
142 #include <stdlib.h>
143 #include <stdio.h>
144 #include <errno.h>
145     int main(void) { cpu_set_t set;
146         CPU_ZERO(&set);
147         CPU_SET(0, &set);
148         pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
149         return 0;
150     }"
151             PTHREAD_SETAFFINITY
152         )
153         if (PTHREAD_SETAFFINITY)
154             set(HAVE_PTHREAD_SETAFFINITY 1)
155         endif (PTHREAD_SETAFFINITY)
156         set(CMAKE_REQUIRED_LIBRARIES)
157     endif (THREAD_PTHREADS)
158
159
160 # this runs on POSIX systems
161     check_include_files(unistd.h        HAVE_UNISTD_H)
162     check_include_files(sched.h         HAVE_SCHED_H)
163     check_include_files(sys/time.h      HAVE_SYS_TIME_H)
164     check_function_exists(sysconf       HAVE_SYSCONF)
165 # this runs on windows
166 #check_include_files(windows.h          HAVE_WINDOWS_H)
167 ENDMACRO(TMPI_ENABLE)
168
169
170 MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
171     set(${SRC_VARIABLE}
172         thread_mpi/errhandler.c
173         thread_mpi/tmpi_malloc.c
174         thread_mpi/atomic.c)
175     if (THREAD_PTHREADS)
176         list(APPEND ${SRC_VARIABLE} thread_mpi/pthreads.c)
177     elseif (THREAD_WINDOWS)
178         list(APPEND ${SRC_VARIABLE} thread_mpi/winthreads.c)
179     endif (THREAD_PTHREADS)
180     if (TMPI_CXX_LIB)
181         list(APPEND ${SRC_VARIABLE} thread_mpi/system_error.cpp)
182     endif (TMPI_CXX_LIB)
183     if (TMPI_ENABLED)
184         list(APPEND ${SRC_VARIABLE}
185              thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
186              thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
187              thread_mpi/bcast.c         thread_mpi/p2p_wait.c
188              thread_mpi/collective.c    thread_mpi/profile.c
189              thread_mpi/comm.c          thread_mpi/reduce.c
190              thread_mpi/event.c         thread_mpi/reduce_fast.c
191              thread_mpi/gather.c        thread_mpi/scatter.c
192              thread_mpi/group.c         thread_mpi/tmpi_init.c
193              thread_mpi/topology.c      thread_mpi/list.c
194              thread_mpi/type.c          thread_mpi/lock.c
195              thread_mpi/numa_malloc.c   thread_mpi/once.c
196              thread_mpi/scan.c)
197     endif()
198 ENDMACRO(TMPI_GET_SOURCE_LIST)
199