You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

462 lines
13 KiB

  1. %* glpk06.tex *%
  2. \chapter{Miscellaneous API Routines}
  3. \section{GLPK environment routines}
  4. \subsection{glp\_init\_env --- initialize GLPK environment}
  5. \synopsis
  6. \begin{verbatim}
  7. int glp_init_env(void);
  8. \end{verbatim}
  9. \description
  10. The routine \verb|glp_init_env| initializes the GLPK environment.
  11. Normally the application program does not need to call this routine,
  12. because it is called automatically on the first call to any API
  13. routine.
  14. \returns
  15. \begin{retlist}
  16. 0 & initialization successful;\\
  17. 1 & environment is already initialized;\\
  18. 2 & initialization failed (insufficient memory);\\
  19. 3 & initialization failed (unsupported programming model).\\
  20. \end{retlist}
  21. \subsection{glp\_version --- determine library version}
  22. \synopsis
  23. \begin{verbatim}
  24. const char *glp_version(void);
  25. \end{verbatim}
  26. \returns
  27. The routine \verb|glp_version| returns a pointer to a null-terminated
  28. character string, which specifies the version of the GLPK library in
  29. the form \verb|"X.Y"|, where `\verb|X|' is the major version number,
  30. and `\verb|Y|' is the minor version number, for example, \verb|"4.16"|.
  31. \newpage
  32. \subsection{glp\_free\_env --- free GLPK environment}
  33. \synopsis
  34. \begin{verbatim}
  35. int glp_free_env(void);
  36. \end{verbatim}
  37. \description
  38. The routine \verb|glp_free_env| frees all resources used by GLPK
  39. routines (memory blocks, etc.) which are currently still in use.
  40. Normally the application program does not need to call this routine,
  41. because GLPK routines always free all unused resources. However, if
  42. the application program even has deleted all problem objects, there
  43. will be several memory blocks still allocated for the internal library
  44. needs. For some reasons the application program may want GLPK to free
  45. this memory, in which case it should call \verb|glp_free_env|.
  46. Note that a call to \verb|glp_free_env| invalidates all problem objects
  47. which still exist.
  48. \returns
  49. \begin{retlist}
  50. 0 & termination successful;\\
  51. 1 & environment is inactive (was not initialized).\\
  52. \end{retlist}
  53. \subsection{glp\_printf --- write formatted output to terminal}
  54. \synopsis
  55. \begin{verbatim}
  56. void glp_printf(const char *fmt, ...);
  57. \end{verbatim}
  58. \description
  59. The routine \verb|glp_printf| uses the format control string
  60. \verb|fmt| to format its parameters and writes the formatted output to
  61. the terminal.
  62. This routine is a replacement of the standard C function
  63. \verb|printf| and used by all GLPK routines to perform terminal
  64. output. The application program may use \verb|glp_printf| for the same
  65. purpose that allows controlling its terminal output with the routines
  66. \verb|glp_term_out| and \verb|glp_term_hook|.
  67. \subsection{glp\_vprintf --- write formatted output to terminal}
  68. \synopsis
  69. \begin{verbatim}
  70. void glp_vprintf(const char *fmt, va_list arg);
  71. \end{verbatim}
  72. \description
  73. The routine \verb|glp_vprintf| uses the format control string
  74. \verb|fmt| to format its parameters specified by the list \verb|arg|
  75. and writes the formatted output to the terminal.
  76. This routine is a replacement of the standard C function
  77. \verb|vprintf| and used by all GLPK routines to perform terminal
  78. output. The application program may use \verb|glp_vprintf| for the same
  79. purpose that allows controlling its terminal output with the routines
  80. \verb|glp_term_out| and \verb|glp_term_hook|.
  81. \newpage
  82. \subsection{glp\_term\_out --- enable/disable terminal output}
  83. \synopsis
  84. \begin{verbatim}
  85. int glp_term_out(int flag);
  86. \end{verbatim}
  87. \description
  88. Depending on the parameter flag the routine \verb|glp_term_out| enables
  89. or disables terminal output performed by glpk routines:
  90. \verb|GLP_ON | --- enable terminal output;
  91. \verb|GLP_OFF| --- disable terminal output.
  92. \returns
  93. The routine \verb|glp_term_out| returns the previous value of the
  94. terminal output flag.
  95. \subsection{glp\_term\_hook --- intercept terminal output}
  96. \synopsis
  97. \begin{verbatim}
  98. void glp_term_hook(int (*func)(void *info, const char *s), void *info);
  99. \end{verbatim}
  100. \description
  101. The routine \verb|glp_term_hook| installs the user-defined hook routine
  102. to intercept all terminal output performed by GLPK routines.
  103. %This feature can be used to redirect the terminal output to other
  104. %destination, for example, to a file or a text window.
  105. The parameter {\it func} specifies the user-defined hook routine. It is
  106. called from an internal printing routine, which passes to it two
  107. parameters: {\it info} and {\it s}. The parameter {\it info} is a
  108. transit pointer specified in corresponding call to the routine
  109. \verb|glp_term_hook|; it may be used to pass some additional information
  110. to the hook routine. The parameter {\it s} is a pointer to the null
  111. terminated character string, which is intended to be written to the
  112. terminal. If the hook routine returns zero, the printing routine writes
  113. the string {\it s} to the terminal in a usual way; otherwise, if the
  114. hook routine returns non-zero, no terminal output is performed.
  115. To uninstall the hook routine both parameters {\it func} and {\it info}
  116. should be specified as \verb|NULL|.
  117. \para{Example}
  118. \begin{footnotesize}
  119. \begin{verbatim}
  120. static int hook(void *info, const char *s)
  121. { FILE *foo = info;
  122. fputs(s, foo);
  123. return 1;
  124. }
  125. int main(void)
  126. { FILE *foo;
  127. . . .
  128. glp_term_hook(hook, foo); /* redirect terminal output */
  129. . . .
  130. glp_term_hook(NULL, NULL); /* resume terminal output */
  131. . . .
  132. }
  133. \end{verbatim}
  134. \end{footnotesize}
  135. \newpage
  136. \subsection{glp\_open\_tee --- start copying terminal output}
  137. \synopsis
  138. \begin{verbatim}
  139. int glp_open_tee(const char *fname);
  140. \end{verbatim}
  141. \description
  142. The routine \verb|glp_open_tee| starts copying all the terminal output
  143. to an output text file, whose name is specified by the character string
  144. \verb|fname|.
  145. \returns
  146. \begin{retlist}
  147. 0 & operation successful;\\
  148. 1 & copying terminal output is already active;\\
  149. 2 & unable to create output file.\\
  150. \end{retlist}
  151. \subsection{glp\_close\_tee --- stop copying terminal output}
  152. \synopsis
  153. \begin{verbatim}
  154. int glp_close_tee(void);
  155. \end{verbatim}
  156. \description
  157. The routine \verb|glp_close_tee| stops copying the terminal output to
  158. the output text file previously open by the routine \verb|glp_open_tee|
  159. closing that file.
  160. \returns
  161. \begin{retlist}
  162. 0 & operation successful;\\
  163. 1 & copying terminal output was not started.\\
  164. \end{retlist}
  165. \subsection{glp\_error --- display error message and terminate
  166. execution}
  167. \synopsis
  168. \begin{verbatim}
  169. void glp_error(const char *fmt, ...);
  170. \end{verbatim}
  171. \description
  172. The routine \verb|glp_error| (implemented as a macro) formats its
  173. parameters using the format control string \verb|fmt|, writes the
  174. formatted message to the terminal, and then abnormally terminates the
  175. program.
  176. \newpage
  177. \subsection{glp\_at\_error --- check for error state}
  178. \synopsis
  179. \begin{verbatim}
  180. int glp_at_error(void);
  181. \end{verbatim}
  182. \description
  183. The routine \verb|glp_at_error| checks if the GLPK environment is at
  184. error state, i.~e.~if the call to the routine is (indirectly) made from
  185. the \verb|glp_error| routine via an user-defined hook routine.
  186. This routine can be used, for example, by a custom output handler
  187. (installed with the routine \verb|glp_term_hook|) to determine whether
  188. or not the message to be displayed is an error message.
  189. \returns
  190. If the GLPK environment is at error state, the routine returns
  191. non-zero, otherwise zero.
  192. \subsection{glp\_assert --- check logical condition}
  193. \synopsis
  194. \begin{verbatim}
  195. void glp_assert(int expr);
  196. \end{verbatim}
  197. \description
  198. The routine \verb|glp_assert| (implemented as a macro) checks
  199. a logical condition specified by the expression \verb|expr|. If the
  200. condition is true (non-zero), the routine does nothing; otherwise, if
  201. the condition is false (zero), the routine prints an error message and
  202. abnormally terminates the program.
  203. This routine is a replacement of the standard C function \verb|assert|
  204. and used by all GLPK routines to check program logic. The application
  205. program may use \verb|glp_assert| for the same purpose.
  206. \subsection{glp\_error\_hook --- install hook to intercept abnormal
  207. termination}
  208. \synopsis
  209. \begin{verbatim}
  210. void glp_error_hook(void (*func)(void *info), void *info);
  211. \end{verbatim}
  212. \description
  213. The routine \verb|glp_error_hook| installs a user-defined hook routine
  214. to intercept abnormal termination.
  215. The parameter \verb|func| specifies the user-defined hook routine. It
  216. is called from the routine \verb|glp_error| before the latter calls the
  217. abort function to abnormally terminate the application program because
  218. of fatal error. The parameter \verb|info| is a transit pointer,
  219. specified in the corresponding call to the routine
  220. \verb|glp_error_hook|; it may be used to pass some information to the
  221. hook routine.
  222. To uninstall the hook routine the parameters \verb|func| and \verb|info|
  223. should be specified as \verb|NULL|.
  224. If the hook routine returns, the application program is abnormally
  225. terminated. To prevent abnormal termnation the hook routine may perform
  226. a global jump using the standard function \verb|longjmp|, in which case
  227. the application program {\it must} call the routine \verb|glp_free_env|.
  228. \subsection{glp\_alloc --- allocate memory block}
  229. \synopsis
  230. \begin{verbatim}
  231. void *glp_alloc(int n, int size);
  232. \end{verbatim}
  233. \description
  234. The routine \verb|glp_alloc| dynamically allocates a memory block of
  235. \verb|n|$\times$\verb|size| bytes long. Note that:
  236. 1) the parameters \verb|n| and \verb|size| must be positive;
  237. 2) having been allocated the memory block contains arbitrary data, that
  238. is, it is {\it not} initialized by binary zeros;
  239. 3) if the block cannot be allocated due to insufficient memory, the
  240. routine prints an error message and abnormally terminates the program.
  241. This routine is a replacement of the standard C function \verb|malloc|
  242. and used by GLPK routines for dynamic memory allocation. The
  243. application program may use \verb|glp_alloc| for the same purpose.
  244. \returns
  245. The routine \verb|glp_alloc| returns a pointer to the memory block
  246. allocated. To free this block the routine \verb|glp_free| (not the
  247. standard C function \verb|free|!) should be used.
  248. \subsection{glp\_realloc --- reallocate memory block}
  249. \synopsis
  250. \begin{verbatim}
  251. void *glp_realloc(void *ptr, int n, int size);
  252. \end{verbatim}
  253. \description
  254. The routine \verb|glp_realloc| dynamically reallocates a memory block
  255. pointed to by \verb|ptr|, which was previously allocated by the routine
  256. \verb|glp_alloc| or reallocated by this routine. Note that the pointer
  257. \verb|ptr| must be valid and must not be \verb|NULL|. The new size of
  258. the memory block is \verb|n|$\times$\verb|size| bytes long. Note that:
  259. 1) both parameters \verb|n| and \verb|size| must be positive;
  260. 2) if the block cannot be reallocated due to insufficient memory, the
  261. routine prints an error message and abnormally terminates the program.
  262. This routine is a replacement of the standard C function \verb|realloc|
  263. and used by GLPK routines for dynamic memory allocation. The
  264. application program may use \verb|glp_realloc| for the same purpose.
  265. \returns
  266. The routine \verb|glp_realloc| returns a pointer to the memory block
  267. reallocated. To free this block the routine \verb|glp_free| (not the
  268. standard C function \verb|free|!) should be used.
  269. \subsection{glp\_free --- free memory block}
  270. \synopsis
  271. \begin{verbatim}
  272. void glp_free(void *ptr);
  273. \end{verbatim}
  274. \description
  275. The routine \verb|glp_free| deallocates a memory block pointed to by
  276. \verb|ptr|, which was previously allocated by the routine
  277. \verb|glp_malloc| or reallocated by the routine \verb|glp_realloc|.
  278. Note that the pointer \verb|ptr| must be valid and must not be
  279. \verb|NULL|.
  280. This routine is a replacement of the standard C function \verb|free|
  281. and used by GLPK routines for dynamic memory allocation. The
  282. application program may use \verb|glp_free| for the same purpose.
  283. \subsection{glp\_mem\_usage --- get memory usage information}
  284. \synopsis
  285. \begin{verbatim}
  286. void glp_mem_usage(int *count, int *cpeak, size_t *total, size_t *tpeak);
  287. \end{verbatim}
  288. \description
  289. The routine \verb|glp_mem_usage| reports some information about
  290. utilization of the memory by the routines \verb|glp_malloc|,
  291. \verb|glp_calloc|, and \verb|glp_free|. Information is stored to
  292. locations specified by corresponding parameters (see below). Any
  293. parameter can be specified as \verb|NULL|, in which case corresponding
  294. information is not stored.
  295. \verb|*count| is the number of currently allocated memory blocks.
  296. \verb|*cpeak| is the peak value of \verb|*count| reached since the
  297. initialization of the GLPK library environment.
  298. \verb|*total| is the total amount, in bytes, of currently allocated
  299. memory blocks.
  300. \verb|*tpeak| is the peak value of \verb|*total| reached since the
  301. initialization of the GLPK library envirionment.
  302. \para{Example}
  303. \begin{footnotesize}
  304. \begin{verbatim}
  305. glp_mem_usage(&count, NULL, NULL, NULL);
  306. printf("%d memory block(s) are still allocated\n", count);
  307. \end{verbatim}
  308. \end{footnotesize}
  309. \subsection{glp\_mem\_limit --- set memory usage limit}
  310. \synopsis
  311. \begin{verbatim}
  312. void glp_mem_limit(int limit);
  313. \end{verbatim}
  314. \description
  315. The routine \verb|glp_mem_limit| limits the amount of memory available
  316. for dynamic allocation (with the routines \verb|glp_malloc| and
  317. \verb|glp_calloc|) to \verb|limit| megabytes.
  318. %* eof *%