d039dba5afd39972591a42db46a18341fcc4fa8f
[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, unset otherwise
41 # Options:
42 # include directory for thread_mpi/atomic.h
43 MACRO(TMPI_TEST_ATOMICS INCDIR)
44
45     if (NOT DEFINED TMPI_ATOMICS)
46         try_compile(TEST_ATOMICS "${CMAKE_BINARY_DIR}"
47                 "${CMAKE_SOURCE_DIR}/cmake/TestAtomics.c"
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()
62
63 ENDMACRO(TMPI_TEST_ATOMICS VARIABLE)
64
65
66 include(FindThreads)
67 if (CMAKE_USE_PTHREADS_INIT)
68     check_include_files(pthread.h    HAVE_PTHREAD_H)
69     set(THREAD_PTHREADS 1)
70     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
71 elseif (CMAKE_USE_WIN32_THREADS_INIT)
72     set(THREAD_WINDOWS 1)
73     set(THREAD_LIB)
74 else()
75     message(FATAL_ERROR "Thread support required")
76 endif ()
77
78 # Turns on thread_mpi core threading functions.
79 MACRO(TMPI_ENABLE_CORE INCDIR)
80     TMPI_TEST_ATOMICS(${INCDIR})
81
82 # affinity checks
83     include(CheckFunctionExists)
84     if (THREAD_PTHREADS)
85         set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
86         # check for sched_setaffinity
87         check_c_source_compiles(
88             "#define _GNU_SOURCE
89 #include <pthread.h>
90 #include <stdlib.h>
91 #include <stdio.h>
92 #include <errno.h>
93     int main(void) { cpu_set_t set;
94         CPU_ZERO(&set);
95         CPU_SET(0, &set);
96         pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
97         return 0;
98     }"
99             PTHREAD_SETAFFINITY
100         )
101         if (PTHREAD_SETAFFINITY)
102             set(HAVE_PTHREAD_SETAFFINITY 1)
103         endif ()
104         set(CMAKE_REQUIRED_LIBRARIES)
105     endif ()
106
107
108 # this runs on POSIX systems
109     check_include_files(unistd.h        HAVE_UNISTD_H)
110     check_include_files(sched.h         HAVE_SCHED_H)
111     check_include_files(sys/time.h      HAVE_SYS_TIME_H)
112     check_function_exists(sysconf       HAVE_SYSCONF)
113 # this runs on windows
114 #check_include_files(windows.h          HAVE_WINDOWS_H)
115 ENDMACRO(TMPI_ENABLE_CORE)
116
117 # enable C++ library build.
118 MACRO(TMPI_ENABLE_CXX)
119     set(TMPI_CXX_LIB 1)
120 ENDMACRO(TMPI_ENABLE_CXX)
121
122 # Turns on thread_mpi MPI functions.
123 MACRO(TMPI_ENABLE)
124     TMPI_TEST_ATOMICS(TMPI_ATOMICS_INCDIR)
125     if(NOT DEFINED TMPI_ATOMICS)
126         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.")
127     endif()
128
129     set(TMPI_ENABLED 1)
130
131 # the spin-waiting option
132     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)
133     mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
134     if (THREAD_MPI_WAIT_FOR_NO_ONE)
135         set(TMPI_WAIT_FOR_NO_ONE 1)
136     else ()
137         set(TMPI_WAIT_FOR_NO_ONE 0)
138     endif ()
139
140 # the copy buffer option
141     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)
142     mark_as_advanced(THREAD_MPI_COPY_BUFFER)
143     if (THREAD_MPI_COPY_BUFFER)
144         set(TMPI_COPY_BUFFER 1)
145     else ()
146         set(TMPI_COPY_BUFFER 0)
147     endif ()
148
149 # the profiling option
150     option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
151     mark_as_advanced(THREAD_MPI_PROFILING)
152     if (THREAD_MPI_PROFILING)
153         set(TMPI_PROFILE 1)
154     else ()
155         set(TMPI_PROFILE 0)
156     endif ()
157
158 # tmpi warnings for testing
159     option(THREAD_MPI_WARNINGS "Turn thread_mpi warnings for testing." OFF)
160     mark_as_advanced(THREAD_MPI_WARNINGS)
161     if (THREAD_MPI_WARNINGS)
162         set(TMPI_WARNINGS 1)
163     else ()
164         set(TMPI_WARNINGS 0)
165     endif ()
166
167     include(CheckCSourceCompiles)
168 ENDMACRO(TMPI_ENABLE)
169
170
171 MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE SRC_ROOT)
172     set(${SRC_VARIABLE}
173         ${SRC_ROOT}/errhandler.c
174         ${SRC_ROOT}/tmpi_malloc.c
175         ${SRC_ROOT}/atomic.c
176         ${SRC_ROOT}/lock.c)
177
178     if (THREAD_PTHREADS)
179         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/pthreads.c)
180     elseif (THREAD_WINDOWS)
181         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/winthreads.c)
182     endif ()
183
184     if (TMPI_CXX_LIB)
185         list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/system_error.cpp)
186     endif ()
187
188     if (TMPI_ENABLED)
189         list(APPEND ${SRC_VARIABLE}
190              ${SRC_ROOT}/alltoall.c      ${SRC_ROOT}/p2p_protocol.c
191              ${SRC_ROOT}/barrier.c       ${SRC_ROOT}/p2p_send_recv.c
192              ${SRC_ROOT}/bcast.c         ${SRC_ROOT}/p2p_wait.c
193              ${SRC_ROOT}/collective.c    ${SRC_ROOT}/profile.c
194              ${SRC_ROOT}/comm.c          ${SRC_ROOT}/reduce.c
195              ${SRC_ROOT}/event.c         ${SRC_ROOT}/reduce_fast.c
196              ${SRC_ROOT}/gather.c        ${SRC_ROOT}/scatter.c
197              ${SRC_ROOT}/group.c         ${SRC_ROOT}/tmpi_init.c
198              ${SRC_ROOT}/topology.c      ${SRC_ROOT}/list.c
199              ${SRC_ROOT}/type.c          ${SRC_ROOT}/scan.c
200              ${SRC_ROOT}/numa_malloc.c   ${SRC_ROOT}/once.c)
201     endif()
202 ENDMACRO(TMPI_GET_SOURCE_LIST)
203