glob_opts.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifndef GLOB_OPTS_H
  2. # define GLOB_OPTS_H
  3. # ifdef __cplusplus
  4. extern "C" {
  5. # endif /* ifdef __cplusplus */
  6. /*
  7. Define OSQP compiler flags
  8. */
  9. // cmake generated compiler flags
  10. #include "osqp_configure.h"
  11. /* DATA CUSTOMIZATIONS (depending on memory manager)----------------------- */
  12. // We do not need memory allocation functions if EMBEDDED is enabled
  13. # ifndef EMBEDDED
  14. /* define custom printfs and memory allocation (e.g. matlab/python) */
  15. # ifdef MATLAB
  16. # include "mex.h"
  17. static void* c_calloc(size_t num, size_t size) {
  18. void *m = mxCalloc(num, size);
  19. mexMakeMemoryPersistent(m);
  20. return m;
  21. }
  22. static void* c_malloc(size_t size) {
  23. void *m = mxMalloc(size);
  24. mexMakeMemoryPersistent(m);
  25. return m;
  26. }
  27. static void* c_realloc(void *ptr, size_t size) {
  28. void *m = mxRealloc(ptr, size);
  29. mexMakeMemoryPersistent(m);
  30. return m;
  31. }
  32. # define c_free mxFree
  33. # elif defined PYTHON
  34. // Define memory allocation for python. Note that in Python 2 memory manager
  35. // Calloc is not implemented
  36. # include <Python.h>
  37. # define c_malloc PyMem_Malloc
  38. # if PY_MAJOR_VERSION >= 3
  39. # define c_calloc PyMem_Calloc
  40. # else /* if PY_MAJOR_VERSION >= 3 */
  41. static void* c_calloc(size_t num, size_t size) {
  42. void *m = PyMem_Malloc(num * size);
  43. memset(m, 0, num * size);
  44. return m;
  45. }
  46. # endif /* if PY_MAJOR_VERSION >= 3 */
  47. # define c_free PyMem_Free
  48. # define c_realloc PyMem_Realloc
  49. # elif !defined OSQP_CUSTOM_MEMORY
  50. /* If no custom memory allocator defined, use
  51. * standard linux functions. Custom memory allocator definitions
  52. * appear in the osqp_configure.h generated file. */
  53. # include <stdlib.h>
  54. # define c_malloc malloc
  55. # define c_calloc calloc
  56. # define c_free free
  57. # define c_realloc realloc
  58. # endif /* ifdef MATLAB */
  59. # endif // end ifndef EMBEDDED
  60. /* Use customized number representation ----------------------------------- */
  61. # ifdef DLONG // long integers
  62. typedef long long c_int; /* for indices */
  63. # else // standard integers
  64. typedef int c_int; /* for indices */
  65. # endif /* ifdef DLONG */
  66. # ifndef DFLOAT // Doubles
  67. typedef double c_float; /* for numerical values */
  68. # else // Floats
  69. typedef float c_float; /* for numerical values */
  70. # endif /* ifndef DFLOAT */
  71. /* Use customized operations */
  72. # ifndef c_absval
  73. # define c_absval(x) (((x) < 0) ? -(x) : (x))
  74. # endif /* ifndef c_absval */
  75. # ifndef c_max
  76. # define c_max(a, b) (((a) > (b)) ? (a) : (b))
  77. # endif /* ifndef c_max */
  78. # ifndef c_min
  79. # define c_min(a, b) (((a) < (b)) ? (a) : (b))
  80. # endif /* ifndef c_min */
  81. // Round x to the nearest multiple of N
  82. # ifndef c_roundmultiple
  83. # define c_roundmultiple(x, N) ((x) + .5 * (N)-c_fmod((x) + .5 * (N), (N)))
  84. # endif /* ifndef c_roundmultiple */
  85. /* Use customized functions ----------------------------------------------- */
  86. # if EMBEDDED != 1
  87. # include <math.h>
  88. # ifndef DFLOAT // Doubles
  89. # define c_sqrt sqrt
  90. # define c_fmod fmod
  91. # else // Floats
  92. # define c_sqrt sqrtf
  93. # define c_fmod fmodf
  94. # endif /* ifndef DFLOAT */
  95. # endif // end EMBEDDED
  96. # ifdef PRINTING
  97. # include <stdio.h>
  98. # include <string.h>
  99. # ifdef MATLAB
  100. # define c_print mexPrintf
  101. // The following trick slows down the performance a lot. Since many solvers
  102. // actually
  103. // call mexPrintf and immediately force print buffer flush
  104. // otherwise messages don't appear until solver termination
  105. // ugly because matlab does not provide a vprintf mex interface
  106. // #include <stdarg.h>
  107. // static int c_print(char *msg, ...)
  108. // {
  109. // va_list argList;
  110. // va_start(argList, msg);
  111. // //message buffer
  112. // int bufferSize = 256;
  113. // char buffer[bufferSize];
  114. // vsnprintf(buffer,bufferSize-1, msg, argList);
  115. // va_end(argList);
  116. // int out = mexPrintf(buffer); //print to matlab display
  117. // mexEvalString("drawnow;"); // flush matlab print buffer
  118. // return out;
  119. // }
  120. # elif defined PYTHON
  121. # include <Python.h>
  122. # define c_print PySys_WriteStdout
  123. # elif defined R_LANG
  124. # include <R_ext/Print.h>
  125. # define c_print Rprintf
  126. # else /* ifdef MATLAB */
  127. # define c_print printf
  128. # endif /* ifdef MATLAB */
  129. /* Print error macro */
  130. # define c_eprint(...) c_print("ERROR in %s: ", __FUNCTION__); c_print(\
  131. __VA_ARGS__); c_print("\n");
  132. # endif /* PRINTING */
  133. # ifdef __cplusplus
  134. }
  135. # endif /* ifdef __cplusplus */
  136. #endif /* ifndef GLOB_OPTS_H */