Merge remote-tracking branch 'gerrit/release-4-5-patches' into release-4-6
[gromacs.git] / cmake / ThreadMPI.cmake
blob8ae8f9423b75c688bd048bb63bbf90357e97ff2a
2 include(CheckIncludeFiles)
3 include(CheckFunctionExists)
4 #include(CheckCSourceCompiles)
6 #option(THREAD_PTHREADS "Use posix threads" ON)
8 MACRO(TEST_TMPI_ATOMICS VARIABLE)
9     if (NOT DEFINED TMPI_ATOMICS)
10         try_compile(TEST_ATOMICS "${CMAKE_BINARY_DIR}"
11                 "${CMAKE_SOURCE_DIR}/cmake/TestAtomics.c"
12                 COMPILE_DEFINITIONS "-I${CMAKE_SOURCE_DIR}/include" )
14         if (TEST_ATOMICS)
15             message(STATUS "Atomics found")
16             set(${VARIABLE} CACHE INTERNAL 1)
17         else (TEST_ATOMICS)
18             message(WARNING "Atomics not found for this compiler+cpu combination. Thread support will be unbearably slow: disable threads. Atomics 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.")
19             set(${VARIABLE} CACHE INTERNAL 0)
20         endif(TEST_ATOMICS)
21     endif(NOT DEFINED TMPI_ATOMICS)
22 ENDMACRO(TEST_TMPI_ATOMICS VARIABLE)
25 include(FindThreads)
26 if (CMAKE_USE_PTHREADS_INIT)
27     check_include_files(pthread.h    HAVE_PTHREAD_H)
28     set(THREAD_PTHREADS 1)
29     #add_definitions(-DTHREAD_PTHREADS)
30     set(THREAD_MPI_SRC 
31         thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
32         thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
33         thread_mpi/bcast.c         thread_mpi/p2p_wait.c
34         thread_mpi/collective.c    thread_mpi/profile.c
35         thread_mpi/comm.c          thread_mpi/pthreads.c
36         thread_mpi/errhandler.c    thread_mpi/reduce.c
37         thread_mpi/event.c         thread_mpi/reduce_fast.c
38         thread_mpi/gather.c        thread_mpi/scatter.c
39         thread_mpi/group.c         thread_mpi/tmpi_init.c
40         thread_mpi/topology.c      thread_mpi/list.c          
41         thread_mpi/type.c          thread_mpi/lock.c
42         thread_mpi/numa_malloc.c   thread_mpi/once.c)
43     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
44 else (CMAKE_USE_PTHREADS_INIT)
45     if (CMAKE_USE_WIN32_THREADS_INIT)
46         set(THREAD_WINDOWS 1)
47         #add_definitions(-DTHREAD_WINDOWS)
48         set(THREAD_MPI_SRC 
49             thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
50             thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
51             thread_mpi/bcast.c         thread_mpi/p2p_wait.c
52             thread_mpi/collective.c    thread_mpi/profile.c
53             thread_mpi/comm.c          
54             thread_mpi/errhandler.c    thread_mpi/reduce.c
55             thread_mpi/event.c         thread_mpi/reduce_fast.c
56             thread_mpi/gather.c        thread_mpi/scatter.c
57             thread_mpi/group.c         thread_mpi/tmpi_init.c
58             thread_mpi/topology.c      thread_mpi/list.c
59             thread_mpi/type.c          thread_mpi/lock.c
60             thread_mpi/winthreads.c    thread_mpi/once.c
61             thread_mpi/numa_malloc.c)
62         set(THREAD_LIBRARY )
63     endif (CMAKE_USE_WIN32_THREADS_INIT)
64 endif (CMAKE_USE_PTHREADS_INIT)
66 # the spin-waiting option
67 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)
68 mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
69 if (THREAD_MPI_WAIT_FOR_NO_ONE)
70     add_definitions(-DTMPI_WAIT_FOR_NO_ONE)
71 else (THREAD_MPI_WAIT_FOR_NO_ONE)
72     add_definitions()
73 endif (THREAD_MPI_WAIT_FOR_NO_ONE)
76 # the copy buffer option
77 option(THREAD_MPI_COPY_BUFFER "Use an intermediate copy buffer for small message sizes, to allow blocking sends to return quickly." ON)
78 mark_as_advanced(THREAD_MPI_COPY_BUFFER)
79 if (THREAD_MPI_COPY_BUFFER)
80     add_definitions()
81 else (THREAD_MPI_COPY_BUFFER)
82     add_definitions(-DTMPI_NO_COPY_BUFFER)
83 endif (THREAD_MPI_COPY_BUFFER)
86 # the profiling option
87 option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
88 mark_as_advanced(THREAD_MPI_PROFILING)
89 if (THREAD_MPI_PROFILING)
90     add_definitions(-DTMPI_PROFILE)
91 else (THREAD_MPI_PROFILING)
92     add_definitions()
93 endif (THREAD_MPI_PROFILING)
95 include(CheckCSourceCompiles)
97 # Windows NUMA allocator
98 if (THREAD_WINDOWS)
99         check_c_source_compiles(
100         "#include <windows.h>
101         int main(void) { PROCESSOR_NUMBER a; return 0; }"
102         HAVE_PROCESSOR_NUMBER)
103         if(HAVE_PROCESSOR_NUMBER)
104             #add_definitions(-DTMPI_WINDOWS_NUMA_API)
105             set(TMPI_WINDOWS_NUMA_API 1)
106         endif(HAVE_PROCESSOR_NUMBER)
107 endif(THREAD_WINDOWS)
109 # option to set affinity 
110 option(THREAD_MPI_SET_AFFINITY "Set thread affinity to a core if number of threads equal to number of hardware threads." ON)
111 mark_as_advanced(THREAD_MPI_SET_AFFINITY)
112 if (THREAD_MPI_SET_AFFINITY)
113     add_definitions(-DTMPI_SET_AFFINITY)
114 else (THREAD_MPI_SET_AFFINITY)
115     add_definitions()
116 endif (THREAD_MPI_SET_AFFINITY)
118 include(CheckFunctionExists)
119 if (THREAD_PTHREADS)
120     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
121     # check for sched_setaffinity
122     check_c_source_compiles(
123         "#define _GNU_SOURCE
124 #include <pthread.h>
125 #include <stdlib.h>
126 #include <stdio.h>
127 #include <errno.h>
128 int main(void) { cpu_set_t set;
129     CPU_ZERO(&set);
130     CPU_SET(0, &set);
131     pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
132     return 0;
134         PTHREAD_SETAFFINITY
135     )
136     if (PTHREAD_SETAFFINITY)
137         set(HAVE_PTHREAD_SETAFFINITY 1)
138     endif (PTHREAD_SETAFFINITY)
139 endif (THREAD_PTHREADS)
142 # this runs on POSIX systems
143 check_include_files(unistd.h        HAVE_UNISTD_H)
144 check_include_files(sched.h         HAVE_SCHED_H)
145 check_include_files(sys/time.h      HAVE_SYS_TIME_H)
146 check_function_exists(sysconf       HAVE_SYSCONF)
147 # this runs on windows
148 #check_include_files(windows.h          HAVE_WINDOWS_H)
151 test_tmpi_atomics(TMPI_ATOMICS)