Merge release-4-5-patches into release-4-6
[alexxy/gromacs.git] / cmake / ThreadMPI.cmake
1
2 include(CheckIncludeFiles)
3 include(CheckFunctionExists)
4 #include(CheckCSourceCompiles)
5
6 #option(THREAD_PTHREADS "Use posix threads" ON)
7
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" )
13
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)
23
24 MACRO(TMPI_MAKE_CXX_LIB)
25     set(TMPI_CXX_LIB 1)
26     # the C++ library
27     set(THREAD_MPI_CXX_SRC
28         thread_mpi/system_error.cpp )
29 ENDMACRO(TMPI_MAKE_CXX_LIB)
30
31 include(FindThreads)
32 if (CMAKE_USE_PTHREADS_INIT)
33     check_include_files(pthread.h    HAVE_PTHREAD_H)
34     set(THREAD_PTHREADS 1)
35     #add_definitions(-DTHREAD_PTHREADS)
36     set(THREAD_MPI_SRC 
37         thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
38         thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
39         thread_mpi/bcast.c         thread_mpi/p2p_wait.c
40         thread_mpi/collective.c    thread_mpi/profile.c
41         thread_mpi/comm.c          thread_mpi/pthreads.c
42         thread_mpi/errhandler.c    thread_mpi/reduce.c
43         thread_mpi/event.c         thread_mpi/reduce_fast.c
44         thread_mpi/gather.c        thread_mpi/scatter.c
45         thread_mpi/group.c         thread_mpi/tmpi_init.c
46         thread_mpi/topology.c      thread_mpi/list.c          
47         thread_mpi/type.c          thread_mpi/lock.c
48         thread_mpi/numa_malloc.c   thread_mpi/once.c 
49         thread_mpi/scan.c          thread_mpi/tmpi_malloc.c)
50     set(THREAD_LIB ${CMAKE_THREAD_LIBS_INIT})
51 else (CMAKE_USE_PTHREADS_INIT)
52     if (CMAKE_USE_WIN32_THREADS_INIT)
53         set(THREAD_WINDOWS 1)
54         #add_definitions(-DTHREAD_WINDOWS)
55         set(THREAD_MPI_SRC 
56             thread_mpi/alltoall.c      thread_mpi/p2p_protocol.c
57             thread_mpi/barrier.c       thread_mpi/p2p_send_recv.c
58             thread_mpi/bcast.c         thread_mpi/p2p_wait.c
59             thread_mpi/collective.c    thread_mpi/profile.c
60             thread_mpi/comm.c          
61             thread_mpi/errhandler.c    thread_mpi/reduce.c
62             thread_mpi/event.c         thread_mpi/reduce_fast.c
63             thread_mpi/gather.c        thread_mpi/scatter.c
64             thread_mpi/group.c         thread_mpi/tmpi_init.c
65             thread_mpi/topology.c      thread_mpi/list.c
66             thread_mpi/type.c          thread_mpi/lock.c
67             thread_mpi/winthreads.c    thread_mpi/once.c
68             thread_mpi/numa_malloc.c
69             thread_mpi/scan.c          thread_mpi/tmpi_malloc.c)
70         set(THREAD_LIBRARY )
71     endif (CMAKE_USE_WIN32_THREADS_INIT)
72 endif (CMAKE_USE_PTHREADS_INIT)
73
74
75 # the spin-waiting option
76 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)
77 mark_as_advanced(THREAD_MPI_WAIT_FOR_NO_ONE)
78 if (THREAD_MPI_WAIT_FOR_NO_ONE)
79     add_definitions(-DTMPI_WAIT_FOR_NO_ONE)
80 else (THREAD_MPI_WAIT_FOR_NO_ONE)
81     add_definitions()
82 endif (THREAD_MPI_WAIT_FOR_NO_ONE)
83
84
85 # the copy buffer option
86 option(THREAD_MPI_COPY_BUFFER "Use an intermediate copy buffer for small message sizes, to allow blocking sends to return quickly." ON)
87 mark_as_advanced(THREAD_MPI_COPY_BUFFER)
88 if (THREAD_MPI_COPY_BUFFER)
89     add_definitions()
90 else (THREAD_MPI_COPY_BUFFER)
91     add_definitions(-DTMPI_NO_COPY_BUFFER)
92 endif (THREAD_MPI_COPY_BUFFER)
93
94
95 # the profiling option
96 option(THREAD_MPI_PROFILING "Turn on simple MPI profiling." OFF)
97 mark_as_advanced(THREAD_MPI_PROFILING)
98 if (THREAD_MPI_PROFILING)
99     add_definitions(-DTMPI_PROFILE)
100 else (THREAD_MPI_PROFILING)
101     add_definitions()
102 endif (THREAD_MPI_PROFILING)
103
104 include(CheckCSourceCompiles)
105
106 # option to set affinity 
107 option(THREAD_MPI_SET_AFFINITY "Set thread affinity to a core if number of threads equal to number of hardware threads." ON)
108 mark_as_advanced(THREAD_MPI_SET_AFFINITY)
109 if (THREAD_MPI_SET_AFFINITY)
110     add_definitions(-DTMPI_SET_AFFINITY)
111 else (THREAD_MPI_SET_AFFINITY)
112     add_definitions()
113 endif (THREAD_MPI_SET_AFFINITY)
114
115 include(CheckFunctionExists)
116 if (THREAD_PTHREADS)
117     set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_THREAD_LIBS_INIT})
118     # check for sched_setaffinity
119     check_c_source_compiles(
120         "#define _GNU_SOURCE
121 #include <pthread.h>
122 #include <stdlib.h>
123 #include <stdio.h>
124 #include <errno.h>
125 int main(void) { cpu_set_t set;
126     CPU_ZERO(&set);
127     CPU_SET(0, &set);
128     pthread_setaffinity_np(pthread_self(), sizeof(set), &set);
129     return 0;
130 }"
131         PTHREAD_SETAFFINITY
132     )
133     if (PTHREAD_SETAFFINITY)
134         set(HAVE_PTHREAD_SETAFFINITY 1)
135     endif (PTHREAD_SETAFFINITY)
136 endif (THREAD_PTHREADS)
137
138
139 # this runs on POSIX systems
140 check_include_files(unistd.h        HAVE_UNISTD_H)
141 check_include_files(sched.h         HAVE_SCHED_H)
142 check_include_files(sys/time.h      HAVE_SYS_TIME_H)
143 check_function_exists(sysconf       HAVE_SYSCONF)
144 # this runs on windows
145 #check_include_files(windows.h          HAVE_WINDOWS_H)
146
147
148 test_tmpi_atomics(TMPI_ATOMICS)