numaapi.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2016, libnumaapi authors
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to
  5. // deal in the Software without restriction, including without limitation the
  6. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. // sell copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. // IN THE SOFTWARE.
  20. //
  21. // Author: Sergey Sharybin (sergey.vfx@gmail.com)
  22. #ifndef __LIBNUMAAPI_H__
  23. #define __LIBNUMAAPI_H__
  24. #include <stdbool.h>
  25. #include <stddef.h>
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. #define NUMAAPI_VERSION_MAJOR 1
  30. #define NUMAAPI_VERSION_MINOR 0
  31. typedef enum NUMAAPI_Result {
  32. NUMAAPI_SUCCESS = 0,
  33. // NUMA is not available on this platform.
  34. NUMAAPI_NOT_AVAILABLE = 1,
  35. // Generic error, no real details are available,
  36. NUMAAPI_ERROR = 2,
  37. // Error installing atexit() handlers.
  38. NUMAAPI_ERROR_ATEXIT = 3,
  39. } NUMAAPI_Result;
  40. ////////////////////////////////////////////////////////////////////////////////
  41. // Initialization.
  42. // Initialize NUMA API.
  43. //
  44. // This is first call which should be called before any other NUMA functions
  45. // can be used.
  46. NUMAAPI_Result numaAPI_Initialize(void);
  47. // Get string representation of NUMAPIResult.
  48. const char* numaAPI_ResultAsString(NUMAAPI_Result result);
  49. ////////////////////////////////////////////////////////////////////////////////
  50. // Topology query.
  51. // Get number of available nodes.
  52. //
  53. // This is in fact an index of last node plus one and it's not guaranteed
  54. // that all nodes up to this one are available.
  55. int numaAPI_GetNumNodes(void);
  56. // Returns truth if the given node is available for compute.
  57. bool numaAPI_IsNodeAvailable(int node);
  58. // Get number of available processors on a given node.
  59. int numaAPI_GetNumNodeProcessors(int node);
  60. ////////////////////////////////////////////////////////////////////////////////
  61. // Topology helpers.
  62. //
  63. // Those are a bit higher level queries, but is still rather platform-specific
  64. // and generally useful.
  65. // Get number of processors within the NUMA nodes on which current thread is
  66. // set affinity on.
  67. int numaAPI_GetNumCurrentNodesProcessors(void);
  68. ////////////////////////////////////////////////////////////////////////////////
  69. // Affinities.
  70. // Runs the current process and its children on a specific node.
  71. //
  72. // Returns truth if affinity has successfully changed.
  73. //
  74. // NOTE: This function can not change active CPU group. Mainly designed to deal
  75. // with Threadripper 2 topology, to make it possible to gain maximum performance
  76. // for the main application thread.
  77. bool numaAPI_RunProcessOnNode(int node);
  78. // Runs the current thread and its children on a specific node.
  79. //
  80. // Returns truth if affinity has successfully changed.
  81. bool numaAPI_RunThreadOnNode(int node);
  82. ////////////////////////////////////////////////////////////////////////////////
  83. // Memory management.
  84. // Allocate memory on a given node,
  85. void* numaAPI_AllocateOnNode(size_t size, int node);
  86. // Allocate memory in the local memory, closest to the current node.
  87. void* numaAPI_AllocateLocal(size_t size);
  88. // Frees size bytes of memory starting at start.
  89. //
  90. // TODO(sergey): Consider making it regular free() semantic.
  91. void numaAPI_Free(void* start, size_t size);
  92. #ifdef __cplusplus
  93. }
  94. #endif
  95. #endif // __LIBNUMAAPI_H__