common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /** common.h
  2. *
  3. * @copyright
  4. * Copyright (C) 2010-2013, Intel Corporation
  5. * All rights reserved.
  6. *
  7. * @copyright
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name of Intel Corporation nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * @copyright
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
  33. * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. /** @file common.h
  37. *
  38. * @brief Defines common macros and structures used by the Intel Cilk Plus
  39. * runtime.
  40. *
  41. * @ingroup common
  42. */
  43. /** @defgroup common Common Definitions
  44. * Macro, structure, and class definitions used elsewhere in the runtime.
  45. * @{
  46. */
  47. #ifndef INCLUDED_CILK_COMMON
  48. #define INCLUDED_CILK_COMMON
  49. #ifdef __cplusplus
  50. /** Namespace for all Cilk definitions that can be included in user code.
  51. */
  52. namespace cilk {
  53. /** Namespace for definitions that are primarily intended for use
  54. * in other Cilk definitions.
  55. */
  56. namespace internal {}
  57. }
  58. #endif
  59. /** Cilk library version = 1.01
  60. */
  61. #define CILK_LIBRARY_VERSION 102
  62. #ifdef __cplusplus
  63. # include <cassert>
  64. #else
  65. # include <assert.h>
  66. #endif
  67. /**
  68. * Prefix standard library function and type names with __STDNS in order to
  69. * get correct lookup in both C and C++.
  70. */
  71. #ifdef __cplusplus
  72. # define __STDNS std::
  73. #else
  74. # define __STDNS
  75. #endif
  76. /**
  77. * @def CILK_EXPORT
  78. * Define export of runtime functions from shared library.
  79. * Should be exported only from cilkrts*.dll/cilkrts*.so
  80. * @def CILK_EXPORT_DATA
  81. * Define export of runtime data from shared library.
  82. */
  83. #ifdef _WIN32
  84. # ifdef IN_CILK_RUNTIME
  85. # define CILK_EXPORT __declspec(dllexport)
  86. # define CILK_EXPORT_DATA __declspec(dllexport)
  87. # else
  88. # define CILK_EXPORT __declspec(dllimport)
  89. # define CILK_EXPORT_DATA __declspec(dllimport)
  90. # endif /* IN_CILK_RUNTIME */
  91. #elif defined(__CYGWIN__) || defined(__APPLE__) || defined(_DARWIN_C_SOURCE)
  92. # define CILK_EXPORT /* nothing */
  93. # define CILK_EXPORT_DATA /* nothing */
  94. #else /* Unix/gcc */
  95. # if defined(IN_CILK_RUNTIME) && defined(HAVE_ATTRIBUTE_VISIBILITY)
  96. # define CILK_EXPORT __attribute__((visibility("protected")))
  97. # define CILK_EXPORT_DATA __attribute__((visibility("protected")))
  98. # else
  99. # define CILK_EXPORT /* nothing */
  100. # define CILK_EXPORT_DATA /* nothing */
  101. # endif /* IN_CILK_RUNTIME */
  102. #endif /* Unix/gcc */
  103. /**
  104. * @def __CILKRTS_BEGIN_EXTERN_C
  105. * Macro to denote the start of a section in which all names have "C" linkage.
  106. * That is, none of the names are to be mangled.
  107. * @see __CILKRTS_END_EXTERN_C
  108. * @see __CILKRTS_EXTERN_C
  109. *
  110. * @def __CILKRTS_END_EXTERN_C
  111. * Macro to denote the end of a section in which all names have "C" linkage.
  112. * That is, none of the names are to be mangled.
  113. * @see __CILKRTS_BEGIN_EXTERN_C
  114. * @see __CILKRTS_EXTERN_C
  115. *
  116. * @def __CILKRTS_EXTERN_C
  117. * Macro to prefix a single definition which has "C" linkage.
  118. * That is, the defined name is not to be mangled.
  119. * @see __CILKRTS_BEGIN_EXTERN_C
  120. * @see __CILKRTS_END_EXTERN_C
  121. */
  122. #ifdef __cplusplus
  123. # define __CILKRTS_BEGIN_EXTERN_C extern "C" {
  124. # define __CILKRTS_END_EXTERN_C }
  125. # define __CILKRTS_EXTERN_C extern "C"
  126. #else
  127. # define __CILKRTS_BEGIN_EXTERN_C
  128. # define __CILKRTS_END_EXTERN_C
  129. # define __CILKRTS_EXTERN_C
  130. #endif
  131. /**
  132. * OS-independent macro to specify a function which is known to not throw
  133. * an exception.
  134. */
  135. #ifdef __cplusplus
  136. # ifdef _WIN32
  137. # define __CILKRTS_NOTHROW __declspec(nothrow)
  138. # else /* Unix/gcc */
  139. # define __CILKRTS_NOTHROW __attribute__((nothrow))
  140. # endif /* Unix/gcc */
  141. #else
  142. # define __CILKRTS_NOTHROW /* nothing */
  143. #endif /* __cplusplus */
  144. /** Cache alignment. (Good enough for most architectures.)
  145. */
  146. #define __CILKRTS_CACHE_LINE__ 64
  147. /**
  148. * Macro to specify alignment of a data member in a structure.
  149. * Because of the way that gcc’s alignment attribute is defined, @a n must
  150. * be a numeric literal, not just a compile-time constant expression.
  151. */
  152. #ifdef _WIN32
  153. # define CILK_ALIGNAS(n) __declspec(align(n))
  154. #else /* Unix/gcc */
  155. # define CILK_ALIGNAS(n) __attribute__((__aligned__(n)))
  156. #endif
  157. /**
  158. * Macro to specify cache-line alignment of a data member in a structure.
  159. */
  160. #define __CILKRTS_CACHE_ALIGN CILK_ALIGNAS(__CILKRTS_CACHE_LINE__)
  161. /**
  162. * Macro to specify a class as being at least as strictly aligned as some
  163. * type on Windows. gcc does not provide a way of doing this, so on Unix,
  164. * this just specifies the largest natural type alignment. Put the macro
  165. * between the `class` keyword and the class name:
  166. *
  167. * class CILK_ALIGNAS_TYPE(foo) bar { ... };
  168. */
  169. #ifdef _WIN32
  170. # define CILK_ALIGNAS_TYPE(t) __declspec(align(__alignof(t)))
  171. #else /* Unix/gcc */
  172. # define CILK_ALIGNAS_TYPE(t) __attribute__((__aligned__))
  173. #endif
  174. /**
  175. * @def CILK_API(RET_TYPE)
  176. * A function called explicitly by the programmer.
  177. * @def CILK_ABI(RET_TYPE)
  178. * A function called by compiler-generated code.
  179. * @def CILK_ABI_THROWS(RET_TYPE)
  180. * An ABI function that may throw an exception
  181. *
  182. * Even when these are the same definitions, they should be separate macros so
  183. * that they can be easily found in the code.
  184. */
  185. #ifdef _WIN32
  186. # define CILK_API(RET_TYPE) CILK_EXPORT RET_TYPE __CILKRTS_NOTHROW __cdecl
  187. # define CILK_ABI(RET_TYPE) CILK_EXPORT RET_TYPE __CILKRTS_NOTHROW __cdecl
  188. # define CILK_ABI_THROWS(RET_TYPE) CILK_EXPORT RET_TYPE __cdecl
  189. #else
  190. # define CILK_API(RET_TYPE) CILK_EXPORT RET_TYPE __CILKRTS_NOTHROW
  191. # define CILK_ABI(RET_TYPE) CILK_EXPORT RET_TYPE __CILKRTS_NOTHROW
  192. # define CILK_ABI_THROWS(RET_TYPE) CILK_EXPORT RET_TYPE
  193. #endif
  194. /**
  195. * __CILKRTS_ASSERT should be defined for debugging only, otherwise it
  196. * interferes with vectorization. Since NDEBUG is not reliable (it must be
  197. * set by the user), we must use a platform-specific detection of debug mode.
  198. */
  199. #if defined(_WIN32) && defined(_DEBUG)
  200. /* Windows debug */
  201. # define __CILKRTS_ASSERT(e) assert(e)
  202. #elif (! defined(_WIN32)) && ! defined(__OPTIMIZE__)
  203. /* Unix non-optimized */
  204. # define __CILKRTS_ASSERT(e) assert(e)
  205. #elif defined __cplusplus
  206. /* C++ non-debug */
  207. # define __CILKRTS_ASSERT(e) static_cast<void>(0)
  208. #else
  209. /* C non-debug */
  210. # define __CILKRTS_ASSERT(e) ((void) 0)
  211. #endif
  212. /**
  213. * OS-independent macro to specify a function that should be inlined
  214. */
  215. #ifdef __cpluspus
  216. // C++
  217. # define __CILKRTS_INLINE inline
  218. #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  219. // C99
  220. # define __CILKRTS_INLINE static inline
  221. #elif defined(_MSC_VER)
  222. // C89 on Windows
  223. # define __CILKRTS_INLINE __inline
  224. #else
  225. // C89 on GCC-compatible systems
  226. # define __CILKRTS_INLINE extern __inline__
  227. #endif
  228. /**
  229. * Functions marked as CILK_EXPORT_AND_INLINE have both
  230. * inline versions defined in the Cilk API, as well as
  231. * non-inlined versions that are exported (for
  232. * compatibility with previous versions that did not
  233. * inline the functions).
  234. */
  235. #ifdef COMPILING_CILK_API_FUNCTIONS
  236. # define CILK_EXPORT_AND_INLINE CILK_EXPORT
  237. #else
  238. # define CILK_EXPORT_AND_INLINE __CILKRTS_INLINE
  239. #endif
  240. /**
  241. * Try to determine if compiler supports rvalue references.
  242. */
  243. #if defined(__cplusplus) && !defined(__CILKRTS_RVALUE_REFERENCES)
  244. # if __cplusplus >= 201103L // C++11
  245. # define __CILKRTS_RVALUE_REFERENCES 1
  246. # elif defined(__GXX_EXPERIMENTAL_CXX0X__)
  247. # define __CILKRTS_RVALUE_REFERENCES 1
  248. # elif __cplusplus >= 199711L && __cplusplus < 201103L
  249. // Compiler recognizes a language version prior to C++11
  250. # elif __INTEL_COMPILER == 1200 && defined(__STDC_HOSTED__)
  251. // Intel compiler version 12.0
  252. // __cplusplus has a non-standard definition. In the absence of a
  253. // proper definition, look for the C++0x macro, __STDC_HOSTED__.
  254. # define __CILKRTS_RVALUE_REFERENCES 1
  255. # elif __INTEL_COMPILER > 1200 && defined(CHAR16T)
  256. // Intel compiler version >= 12.1
  257. // __cplusplus has a non-standard definition. In the absence of a
  258. // proper definition, look for the Intel macro, CHAR16T
  259. # define __CILKRTS_RVALUE_REFERENCES 1
  260. # endif
  261. #endif
  262. /*
  263. * Include stdint.h to define the standard integer types.
  264. *
  265. * Unfortunately Microsoft doesn't provide stdint.h until Visual Studio 2010,
  266. * so use our own definitions until those are available
  267. */
  268. #if ! defined(_MSC_VER) || (_MSC_VER >= 1600)
  269. # include <stdint.h>
  270. #else
  271. # ifndef __MS_STDINT_TYPES_DEFINED__
  272. # define __MS_STDINT_TYPES_DEFINED__
  273. typedef signed char int8_t;
  274. typedef short int16_t;
  275. typedef int int32_t;
  276. typedef __int64 int64_t;
  277. typedef unsigned char uint8_t;
  278. typedef unsigned short uint16_t;
  279. typedef unsigned int uint32_t;
  280. typedef unsigned __int64 uint64_t;
  281. # endif /* __MS_STDINT_TYPES_DEFINED__ */
  282. #endif /* ! defined(_MSC_VER) || (_MSC_VER >= 1600) */
  283. /**
  284. * @brief Application Binary Interface version of the Cilk runtime library.
  285. *
  286. * The ABI version is determined by the compiler used. An object file
  287. * compiled with a higher ABI version is not compatible with a library that is
  288. * compiled with a lower ABI version. An object file compiled with a lower
  289. * ABI version, however, can be used with a library compiled with a higher ABI
  290. * version unless otherwise stated.
  291. */
  292. #ifndef __CILKRTS_ABI_VERSION
  293. # ifdef IN_CILK_RUNTIME
  294. # define __CILKRTS_ABI_VERSION 1
  295. # elif defined(__INTEL_COMPILER) && (__INTEL_COMPILER <= 1200)
  296. // Intel compilers prior to version 12.1 support only ABI 0
  297. # define __CILKRTS_ABI_VERSION 0
  298. # else
  299. // Non-Intel compiler or Intel compiler after version 12.0.
  300. # define __CILKRTS_ABI_VERSION 1
  301. # endif
  302. #endif
  303. // These structs are exported because the inlining of
  304. // the internal version of API methods require a worker
  305. // structure as parameter.
  306. __CILKRTS_BEGIN_EXTERN_C
  307. /// Worker struct, exported for inlined API methods
  308. /// @ingroup api
  309. struct __cilkrts_worker;
  310. /// Worker struct, exported for inlined API methods
  311. /// @ingroup api
  312. typedef struct __cilkrts_worker __cilkrts_worker;
  313. /// Worker struct pointer, exported for inlined API methods
  314. /// @ingroup api
  315. typedef struct __cilkrts_worker *__cilkrts_worker_ptr;
  316. /// Fetch the worker out of TLS.
  317. CILK_ABI(__cilkrts_worker_ptr) __cilkrts_get_tls_worker(void);
  318. /// void *, defined to work around complaints from the compiler
  319. /// about using __declspec(nothrow) after the "void *" return type
  320. typedef void * __cilkrts_void_ptr;
  321. __CILKRTS_END_EXTERN_C
  322. #if __CILKRTS_ABI_VERSION >= 1
  323. // Pedigree API is available only for compilers that use ABI version >= 1.
  324. /** Pedigree information kept in the worker and stack frame.
  325. * @ingroup api
  326. */
  327. typedef struct __cilkrts_pedigree
  328. {
  329. /** Rank at start of spawn helper. Saved rank for spawning functions */
  330. uint64_t rank;
  331. /** Link to next in chain */
  332. const struct __cilkrts_pedigree *parent;
  333. } __cilkrts_pedigree;
  334. #endif // __CILKRTS_ABI_VERSION >= 1
  335. /// @}
  336. #endif /* INCLUDED_CILK_COMMON */