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.
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
36 include(CheckIncludeFileCXX)
37 include(CheckCXXSymbolExists)
38 include(CheckCXXSourceCompiles)
40 # sets TMPI_ATOMICS to 1 if atomic operations are found, unset otherwise
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\"
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);
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
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")
96 message(STATUS "Atomic operations not found")
97 unset(TEST_ATOMICS CACHE)
103 try_compile(HAVE_PROCESSOR_NUMBER ${CMAKE_BINARY_DIR} "${CMAKE_SOURCE_DIR}/cmake/TestWinProcNum.cpp")
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).")
111 if (CMAKE_USE_WIN32_THREADS_INIT AND HAVE_PROCESSOR_NUMBER)
112 set(THREAD_WINDOWS 1)
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})
119 message(FATAL_ERROR "Thread support required")
122 # Turns on thread_mpi core threading functions.
123 MACRO(TMPI_ENABLE_CORE INCDIR)
124 TMPI_TEST_ATOMICS(${INCDIR})
127 include(CheckFunctionExists)
129 set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
130 # check for sched_setaffinity
131 check_cxx_source_compiles(
137 int main(void) { cpu_set_t set;
140 pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
145 if (PTHREAD_SETAFFINITY)
146 set(HAVE_PTHREAD_SETAFFINITY 1)
148 set(CMAKE_REQUIRED_LIBRARIES)
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.
164 # Turns on thread_mpi MPI functions.
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.")
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)
178 set(TMPI_WAIT_FOR_NO_ONE 0)
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)
187 set(TMPI_COPY_BUFFER 0)
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)
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)
207 ENDMACRO(TMPI_ENABLE)
210 MACRO(TMPI_GET_SOURCE_LIST SRC_VARIABLE SRC_ROOT)
212 ${SRC_ROOT}/errhandler.cpp
213 ${SRC_ROOT}/tmpi_malloc.cpp
214 ${SRC_ROOT}/atomic.cpp
215 ${SRC_ROOT}/lock.cpp)
218 list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/pthreads.cpp)
219 elseif (THREAD_WINDOWS)
220 list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/winthreads.cpp)
224 list(APPEND ${SRC_VARIABLE} ${SRC_ROOT}/system_error.cpp)
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)
241 ENDMACRO(TMPI_GET_SOURCE_LIST)