Update copyright statements and change license to LGPL
[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             if (TEST_TMPI_ATOMICS_ONLY)
53                 message(WARNING "Atomic operations not found for this CPU+compiler combination. 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.")
54             else (TEST_TMPI_ATOMICS_ONLY)
55                 message(WARNING "Atomic operations not found for this
56             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.")
57             endif (TEST_TMPI_ATOMICS_ONLY)
58             set(${VARIABLE} FALSE CACHE INTERNAL "Whether atomic operations for thread-MPI were found")
59         endif(TEST_ATOMICS)
60     endif(NOT DEFINED TMPI_ATOMICS)
61 ENDMACRO(TEST_TMPI_ATOMICS VARIABLE)
62
63 MACRO(TMPI_MAKE_CXX_LIB)
64     set(TMPI_CXX_LIB 1)
65 ENDMACRO(TMPI_MAKE_CXX_LIB)
66
67 MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE)
68     foreach (_option IN ITEMS ${ARGN})
69         if (_option STREQUAL "CXX")
70             set(TMPI_CXX_LIB 1)
71         elseif (_option STREQUAL "NOMPI")
72             set(TMPI_NO_MPI_LIB 1)
73         else ()
74             message(FATAL_ERROR "Unknown thread_mpi option '${_option}'")
75         endif ()
76     endforeach ()
77     set(${SRC_VARIABLE}
78         thread_mpi/errhandler.c
79         thread_mpi/tmpi_malloc.c)
80     if (THREAD_PTHREADS)
81         list(APPEND ${SRC_VARIABLE} thread_mpi/pthreads.c)
82     elseif (THREAD_WINDOWS)
83         list(APPEND ${SRC_VARIABLE} thread_mpi/winthreads.c)
84     endif (THREAD_PTHREADS)
85     if (TMPI_CXX_LIB)
86         list(APPEND ${SRC_VARIABLE} thread_mpi/system_error.cpp)
87     endif (TMPI_CXX_LIB)
88     if (NOT TMPI_NO_MPI_LIB)
89         list(APPEND ${SRC_VARIABLE}
90              thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
91              thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
92              thread_mpi/bcast.c         thread_mpi/p2p_wait.c
93              thread_mpi/collective.c    thread_mpi/profile.c
94              thread_mpi/comm.c          thread_mpi/reduce.c
95              thread_mpi/event.c         thread_mpi/reduce_fast.c
96              thread_mpi/gather.c        thread_mpi/scatter.c
97              thread_mpi/group.c         thread_mpi/tmpi_init.c
98              thread_mpi/topology.c      thread_mpi/list.c
99              thread_mpi/type.c          thread_mpi/lock.c
100              thread_mpi/numa_malloc.c   thread_mpi/once.c
101              thread_mpi/scan.c)
102     endif()
103 ENDMACRO(TMPI_GET_SOURCE_LIST)
104
105 test_tmpi_atomics(TMPI_ATOMICS)
106
107 # do we want to only the atomics of tMPI (with GPU + MPI)
108 if(NOT TEST_TMPI_ATOMICS_ONLY)
109 include(FindThreads)
110 if (CMAKE_USE_PTHREADS_INIT)
111     check_include_files(pthread.h    HAVE_PTHREAD_H)
112     set(THREAD_PTHREADS 1)
113     #add_definitions(-DTHREAD_PTHREADS)
114     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
115 elseif (CMAKE_USE_WIN32_THREADS_INIT)
116     set(THREAD_WINDOWS 1)
117     #add_definitions(-DTHREAD_WINDOWS)
118     set(THREAD_LIB)
119 else ()
120     message(FATAL_ERROR "Thread support required")
121 endif (CMAKE_USE_PTHREADS_INIT)
122
123
124 # the spin-waiting option
125 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)
126 mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
127 if (THREAD_MPI_WAIT_FOR_NO_ONE)
128     add_definitions(-DTMPI_WAIT_FOR_NO_ONE)
129 else (THREAD_MPI_WAIT_FOR_NO_ONE)
130     add_definitions()
131 endif (THREAD_MPI_WAIT_FOR_NO_ONE)
132
133
134 # the copy buffer option
135 option(THREAD_MPI_COPY_BUFFER "Use an intermediate copy buffer for small message sizes, to allow blocking sends to return quickly." ON)
136 mark_as_advanced(THREAD_MPI_COPY_BUFFER)
137 if (THREAD_MPI_COPY_BUFFER)
138     add_definitions()
139 else (THREAD_MPI_COPY_BUFFER)
140     add_definitions(-DTMPI_NO_COPY_BUFFER)
141 endif (THREAD_MPI_COPY_BUFFER)
142
143
144 # the profiling option
145 option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
146 mark_as_advanced(THREAD_MPI_PROFILING)
147 if (THREAD_MPI_PROFILING)
148     add_definitions(-DTMPI_PROFILE)
149 else (THREAD_MPI_PROFILING)
150     add_definitions()
151 endif (THREAD_MPI_PROFILING)
152
153 include(CheckCSourceCompiles)
154
155 # option to set affinity 
156 option(THREAD_MPI_SET_AFFINITY "Set thread affinity to a core if number of threads equal to number of hardware threads." ON)
157 mark_as_advanced(THREAD_MPI_SET_AFFINITY)
158 if (THREAD_MPI_SET_AFFINITY)
159     add_definitions(-DTMPI_SET_AFFINITY)
160 else (THREAD_MPI_SET_AFFINITY)
161     add_definitions()
162 endif (THREAD_MPI_SET_AFFINITY)
163
164 include(CheckFunctionExists)
165 if (THREAD_PTHREADS)
166     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
167     # check for sched_setaffinity
168     check_c_source_compiles(
169         "#define _GNU_SOURCE
170 #include <pthread.h>
171 #include <stdlib.h>
172 #include <stdio.h>
173 #include <errno.h>
174 int main(void) { cpu_set_t set;
175     CPU_ZERO(&set);
176     CPU_SET(0, &set);
177     pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
178     return 0;
179 }"
180         PTHREAD_SETAFFINITY
181     )
182     if (PTHREAD_SETAFFINITY)
183         set(HAVE_PTHREAD_SETAFFINITY 1)
184     endif (PTHREAD_SETAFFINITY)
185     set(CMAKE_REQUIRED_LIBRARIES)
186 endif (THREAD_PTHREADS)
187
188
189 # this runs on POSIX systems
190 check_include_files(unistd.h        HAVE_UNISTD_H)
191 check_include_files(sched.h         HAVE_SCHED_H)
192 check_include_files(sys/time.h      HAVE_SYS_TIME_H)
193 check_function_exists(sysconf       HAVE_SYSCONF)
194 # this runs on windows
195 #check_include_files(windows.h          HAVE_WINDOWS_H)
196
197 endif(NOT TEST_TMPI_ATOMICS_ONLY)