1 /** @file generic.c
2 ** @brief Generic - Definition
3 ** @author Andrea Vedaldi
4 **/
5
6 /*
7 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
8 Copyright (C) 2013 Andrea Vedaldi.
9 All rights reserved.
10
11 This file is part of the VLFeat library and is made available under
12 the terms of the BSD license (see the COPYING file).
13 */
14
15 /**
16 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
17 @mainpage Vision Lab Features Library (VLFeat)
18 @version __VLFEAT_VERSION__
19 @author The VLFeat Team
20 @par Copyright © 2012-14 The VLFeat Authors
21 @par Copyright © 2007-11 Andrea Vedaldi and Brian Fulkerson
22 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
23
24 The VLFeat C library implements common computer
25 vision algorithms, with a special focus on visual features, as used
26 in state-of-the-art object recognition and image
27 matching applications.
28
29 VLFeat strives to be clutter-free, simple, portable, and well documented.
30
31 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
32 @section main-contents Contents
33 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
34
35 - **Visual feature detectors and descriptors**
36 - @subpage sift
37 - @subpage dsift
38 - @subpage mser
39 - @subpage covdet
40 - @subpage scalespace
41 - @subpage hog
42 - @subpage fisher
43 - @subpage vlad
44 - @subpage liop
45 - @subpage lbp
46
47 - **Clustering and indexing**
48 - @subpage kmeans
49 - @subpage ikmeans.h "Integer K-means (IKM)"
50 - @subpage hikmeans.h "Hierarchical Integer K-means (HIKM)"
51 - @subpage gmm
52 - @subpage aib
53 - @subpage kdtree
54
55 - **Segmentation**
56 - @subpage slic
57 - @subpage quickshift
58
59 - **Statistical methods**
60 - @subpage aib
61 - @subpage homkermap
62 - @subpage svm
63
64 - **Utilities**
65 - @subpage random
66 - @subpage mathop
67 - @subpage stringop.h "String operations"
68 - @subpage imopv.h "Image operations"
69 - @subpage pgm.h "PGM image format"
70 - @subpage heap-def.h "Generic heap object (priority queue)"
71 - @subpage rodrigues.h "Rodrigues formula"
72 - @subpage mexutils.h "MATLAB MEX helper functions"
73 - @subpage getopt_long.h "Drop-in @c getopt_long replacement"
74
75 - **General information**
76 - @subpage conventions
77 - @subpage generic
78 - @subpage portability
79 - @ref resources
80 - @subpage objects
81 - @ref threads
82 - @subpage matlab
83 - @subpage metaprogram
84
85 - @subpage dev
86 - @subpage glossary
87 **/
88
89 /**
90 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
91 @page resources Memory and resource handling
92 @author Andrea Vedaldi
93 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
94
95 Some VLFeat functions return pointers to memory blocks or
96 objects. Only ::vl_malloc, ::vl_calloc, ::vl_realloc and functions
97 whose name contains either the keywords @c new or @c copy transfer the
98 ownership of the memory block or object to the caller. The caller must
99 dispose explicitly of all the resources it owns (by calling ::vl_free
100 for a memory block, or the appropriate deletion function for an
101 object).
102
103 The memory allocation functions can be customized by
104 ::vl_set_alloc_func (which sets the implementations of ::vl_malloc,
105 ::vl_realloc, ::vl_calloc and ::vl_free). Remapping the memory
106 allocation functions can be done only if there are no currently
107 allocated VLFeat memory blocks or objects -- thus typically at the
108 very beginning of a program. The memory allocation functions are a
109 global property, shared by all threads.
110
111 VLFeat uses three rules that simplify handling exceptions when used in
112 combination which certain environment such as MATLAB.
113
114 - The library allocates local memory only through the re-programmable
115 ::vl_malloc, ::vl_calloc, and ::vl_realloc functions.
116
117 - The only resource referenced by VLFeat objects is memory (for
118 instance, it is illegal for an object to reference an open file).
119 Other resources such as files or threads may be allocated within a
120 VLFeat function call, but they are all released before the function
121 ends, or their ownership is directly transferred to the caller.
122
123 - The global library state is an exception. It cannot reference any
124 local object created by the caller and uses the standard C memory
125 allocation functions.
126
127 In this way, the VLFeat local state can be reset at any time simply by
128 disposing of all the memory allocated by the library so far. The
129 latter can be done easily by mapping the memory allocation functions
130 to implementations that track the memory blocks allocated, and then
131 disposing of all such blocks. Since the global state does not
132 reference any local object nor uses the remapped memory functions, it
133 is unaffected by such an operation; conversely, since no VLFeat object
134 references anything but memory, this guarantees that all allocated
135 resources are properly disposed (avoiding leaking resource). This is
136 used extensively in the design of MATLAB MEX files (see @ref
137 matlab).
138 **/
139
140 /**
141 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
142 @page conventions Conventions
143 @author Andrea Vedaldi
144 @tableofcontents
145 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
146
147 This page summarizes some of the conventions used by the library.
148
149 @section conventions-storage Matrix and image storage conventions
150
151 If not otherwise specified, matrices in VLFeat are stored in memory in
152 <em>column major</em> order. Given a matrix $[A_{ij}] \in \real^{m
153 \times n}$, this amounts of enumerating the elements one column per
154 time: $A_{11}, A_{21}, \dots, A_{m1}, A_{12}, \dots, A_{mn}$. This
155 convention is compatible with Fortran, MATLAB, and popular numerical
156 libraries.
157
158 Matrices are often used in the library to pack a number data vectors
159 $\bx_1,\dots,\bx_n \in \real^m$ of equal dimension together. These are
160 normally stored as the columns of the matrix:
161
162 \[
163 X = \begin{bmatrix} \bx_1, \dots, \bx_n \end{bmatrix},
164 \qquad
165 X \in \real_{m\times n}
166 \]
167
168 In this manner, consecutive elements of each data vector $\bx_i$ is
169 stored in consecutive memory locations, improving memory access
170 locality in most algorithms.
171
172 Images $I(x,y)$ are stored instead in <em>row-major</em> order,
173 i.e. one row after the other. Note that an image can be naturally
174 identified as a matrix $I_{yx}$, where the vertical coordinate $y$
175 indexes the rows and the horizontal coordinate $x$ the columns. The
176 image convention amounts to storing this matrix in row-major rather
177 than column-major order, which is in conflict with the rule given
178 above. The reason for this choice is that most image processing and
179 graphical libraries assume this convention; it is, however,
180 <em>not</em> the same as MATLAB's.
181
182 **/
183
184 /**
185 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
186 @page objects Objects
187 @author Andrea Vedaldi
188 @tableofcontents
189 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
190
191 Many VLFeat algorithms are available in the form of *objects*. The C
192 language, used by VLFeat, does not support objects explicitly. Here an
193 object is intended a C structure along with a number of functions (the
194 object member functions or methods) operating on it. Ideally, the
195 object data structure is kept opaque to the user, for example by
196 defining it in the @c .c implementation files which are not accessible
197 to the library user.
198
199 Object names are capitalized and start with the <code>Vl</code> prefix
200 (for example @c VlExampleObject). Object methods are lowercase and
201 start with the <code>vl_<object_name>_</code> suffix
202 (e.g. @c vl_example_object_new).
203
204 <!-- ------------------------------------------------------------ -->
205 @section objects-lifecycle Object lifecycle
206 <!-- ------------------------------------------------------------ -->
207
208 Conceptually, an object undergoes four phases during its lifecycle:
209 allocation, initialization, finalization, and deallocation:
210
211 - **Allocation.** The memory to hold the object structure is allocated.
212 This is usually done by calling a memory allocation function such as
213 ::vl_calloc to reserve an object of the required size @c
214 sizeof(VlExampleObject). Alternatively, the object can simply by
215 allocated on the stack by declaring a local variable of type
216 VlExampleObject.
217 - **Initialization.** The object is initialized by assigning a value to
218 its data members and potentially allocating a number of resources,
219 including other objects or memory buffers. Initialization is
220 done by methods containing the @c init keyword, e.g. @c
221 vl_example_object_init. Several such methods may be provided.
222 - **Finalization.** Initialization is undone by finalization, whose main
223 purpose is to release any resource allocated and still owned by the
224 object. Finalization is done by the @c vl_example_object_finalize
225 method.
226 - **Deallocation.** The memory holding the object structure is
227 disposed of, for example by calling ::vl_free or automatically when
228 the corresponding local variable is popped from the stack.
229
230 In practice, most VlFeat object are supposed to be created on the
231 heap. To this end, allocation/initialization and
232 finalization/deallocation are combined into two operations:
233
234 - **Creating a new object.** This allocates a new object on the heap
235 and initializes it, combining allocation and initialization in a
236 single operation. It is done by methods containing the @c new keyword,
237 e.g. @c vl_example_object_new.
238 - **Deleting an object.** This disposes of an object created by a @c
239 new method, combining finalization and deallocation, for example
240 @c vl_example_object_delete.
241
242 <!-- ------------------------------------------------------------ -->
243 @section objects-getters-setters Getters and setters
244 <!-- ------------------------------------------------------------ -->
245
246 Most objects contain a number of methods to get (getters) and set
247 (setters) properties. These should contain the @c get and @c set
248 keywords in their name, for example
249
250 @code
251 double x = vl_example_object_get_property () ;
252 vl_example_object_set_property(x) ;
253 @endcode
254 **/
255
256 /**
257 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
258 @page matlab MATLAB integration
259 @author Andrea Vedaldi
260 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
261
262 The VLFeat C library is designed to integrate seamlessly with MATLAB.
263 Binary compatibility is simplified by the use of the C language
264 (rather than C++). In addition, the library design follows certain
265 restrictions that make it compatible with the MATLAB MEX interface.
266
267 The main issue in calling a library function from a MATLAB MEX
268 function is that MATLAB can abort the execution of the MEX function
269 at any point, either due to an error, or directly upon a user request
270 (Ctrl-C) (empirically, however, a MEX function seems to be
271 incorruptible only during the invocation of certain functions of the
272 MEX API such as @c mexErrMsgTxt).
273
274 When a MEX function is interrupted, resources (memory blocks or
275 objects) whose ownership was transferred from VLFeat to the MEX
276 function may be leaked. Notice that interrupting a MEX function would
277 similarly leak any memory block allocated within the MEX function. To
278 solve this issue, MATLAB provides his own memory manager (@c
279 mxMalloc, @c mxRealloc, ...). When a MEX file is interrupted or ends,
280 all memory blocks allocated by using one of such functions are
281 released, preventing leakage.
282
283 In order to integrate VLFeat with this model in the most seamless
284 way, VLFeat memory allocation functions (::vl_malloc, ::vl_realloc,
285 ::vl_calloc) are mapped to the corresponding MEX memory allocation
286 functions. Such functions automatically dispose of all the memory
287 allocated by a MEX function when the function ends (even because of
288 an exception). Because of the restrictions of the library design
289 illustrated in @ref resources, this operation is safe and
290 correctly dispose of VLFeat local state. As a consequence, it is
291 possible to call @c mexErrMsgTxt at any point in the MEX function
292 without worrying about leaking resources.
293
294 This however comes at the price of some limitations. Beyond the
295 restrictions illustrated in @ref resources, here we note that no
296 VLFeat local resource (memory blocks or objects) can persist across
297 MEX file invocations. This implies that any result produced by a
298 VLFeat MEX function must be converted back to a MATLAB object such as
299 a vector or a structure. In particular, there is no direct way of
300 creating an object within a MEX file, returning it to MATLAB, and
301 passing it again to another MEX file.
302 **/
303
304 /**
305 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
306 @page metaprogram Preprocessor metaprogramming
307 @author Andrea Vedaldi
308 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
309
310 Part of VLFeat code uses a simple form of preprocessor metaprogramming.
311 This technique is used, similarly to C++ templates, to instantiate
312 multiple version of a given algorithm for different data types
313 (e.g. @c float and @c double).
314
315 In most cases preprocessor metaprogramming is invisible to the library
316 user, as it is used only internally.
317 **/
318
319 /**
320 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
321 @page glossary Glossary
322 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
323
324 - <b>Column-major.</b> A <em>M x N </em> matrix <em>A</em> is
325 stacked with column-major order as the sequence \f$(A_{11}, A_{21},
326 \dots, A_{12}, \dots)\f$. More in general, when stacking a multi
327 dimensional array this indicates that the first index is the one
328 varying most quickly, with the other followed in the natural order.
329 - <b>Opaque structure.</b> A structure is opaque if the user is not supposed
330 to access its member directly, but through appropriate interface functions.
331 Opaque structures are commonly used to define objects.
332 - <b>Row-major.</b> A <em>M x N </em> matrix <em>A</em> is
333 stacked with row-major order as the sequence \f$(A_{11}, A_{12},
334 \dots, A_{21}, \dots)\f$. More in general, when stacking a multi
335 dimensional array this indicates that the last index is the one
336 varying most quickly, with the other followed in reverse order.
337 - <b>Feature frame.</b> A <em>feature frame</em> is the geometrical
338 description of a visual features. For instance, the frame of
339 a @ref sift.h "SIFT feature" is oriented disk and the frame of
340 @ref mser.h "MSER feature" is either a compact and connected set or
341 a disk.
342 - <b>Feature descriptor.</b> A <em>feature descriptor</em> is a quantity
343 (usually a vector) which describes compactly the appearance of an
344 image region (usually corresponding to a feature frame).
345 **/
346
347 /**
348
349 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
350 @page dev Developing the library
351 @tableofcontents
352 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
353
354 This page contains information useful to the developer of VLFeat.
355
356 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
357 @section dev-copy Copyright
358 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
359
360 A short copyright notice is added at the beginning of each file. For
361 example:
362
363 <pre>
364 Copyright (C) 2013 Milan Sulc
365 Copyright (C) 2012 Daniele Perrone.
366 Copyright (C) 2011-13 Andrea Vedaldi.
367 All rights reserved.
368
369 This file is part of the VLFeat library and is made available under
370 the terms of the BSD license (see the COPYING file).
371 </pre>
372
373 The copyright of each file is assigned to the authors of the file.
374 Every author making a substantial contribution to a file should
375 note its copyright by adding a line to the copyright list with the year
376 of the modification. Year ranges are acceptable. Lines are never
377 deleted, only appended, or potentially modified to list
378 more years.
379
380 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
381 @section dev-style Coding style
382 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
383
384 <ul>
385
386 <li><b>Look at existing code before you start.</b> The general rule
387 is: try to match the style of the existing code as much as
388 possible.</li>
389
390 <li><b>No white spaces at the end of lines.</b> White spaces introduce
391 invisible changes in the code that are however picked up by control
392 version systems such as Git.</li>
393
394 <li><b>Descriptive variable names.</b> Most variable names start with
395 a lower case letter and are capitalized, e.g., @c numElements. Only
396 the following abbreviations are considered acceptable: @c num. The @c
397 dimension of a vector is the number of elements it contains (for other
398 objects that could be a @c size, a @c length, or a @c
399 numElements). For multi-dimensional arrays, @c dimensions could
400 indicate the array with each of the @c numDimensions dimensions.</li>
401
402 <li><b>Short variable names.</b> For indexes in short for loops it is
403 fine to use short index names such as @c i, @c j, and @c k. For example:
404 <pre>
405 for (i = 0 ; i < numEntries ; ++i) values[i] ++ ;
406 </pre>
407 is considered acceptable.</li>
408
409 <li><b>Function arguments.</b> VLFeat functions that operate on an
410 object (member functions) should be passed the object address as first
411 argument; this argument should be called @c self. For example:
412 <pre>
413 void vl_object_do_something(VlObject *self) ;
414 </pre>
415 Multi-dimensional arrays should be specified first by their address,
416 and then by their dimensions. For example
417 <pre>
418 void vl_use_array (float * array, vl_size numColumns, vl_size numRows) ; // good
419 void vl_use_array (vl_size numColumns, vl_size numRows, float * array) ; // bad
420 </pre>
421 Arguments that are used as outputs should be specified first (closer to
422 the left-hand side of an expression). For example
423 <pre>
424 void vl_sum_numbers (float * output, float input1, float input2) ; // good
425 void vl_sum_numbers (float input1, float input2, float * output) ; // bad
426 </pre>
427 These rules can be combined. For example
428 <pre>
429 void vl_object_sum_to_array (VlObject * self, float * outArray,
430 vl_size numColumns, vl_size numRows, float * inArray) ; // good
431 </pre>
432 Note that in this case no dimension for @c inArray is specified as it
433 is assumed that @c numColumns and @c numRows are the dimensions of
434 both arrays.
435 </li>
436 </ul>
437
438 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
439 @subsection dev-style-matlab MATLAB coding style
440 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
441
442 <ul>
443 <li><b>Help messages.</b> Each @c .m file should include a standard
444 help comment block (accessible from MATLAB @c help() command).
445 The first line of the block has a space, the name of the function,
446 4 spaces, and a brief command description. The body of the help
447 message is indented with 4 spaces. For example
448 @code
449 % VL_FUNCTION An example function
450 % VL_FUNCTION() does nothing.
451 @endcode
452 The content HELP message itself should follow MATLAB default style.
453 For example, rather than giving a list of formal input and output
454 arguments as often done, one simply shows how to use the function, explaining
455 along the way the different ways the function can be called and
456 the format of the parameters.
457 </li>
458 </ul>
459
460 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
461 @section dev-doc Documenting the code
462 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
463
464 The VLFeat C library code contains its own in documentation <a
465 href='http://www.stack.nl/~dimitri/doxygen/'>Doxygen</a> format. The
466 documentation consists in generic pages, such as the @ref index
467 "index" and the page you are reading, and documentations for each
468 library module, usually corresponding to a certain header file.
469
470 - **Inline comments.** Inline Doxygen comments are discouraged except
471 in the documentation of data members of structures. They start with
472 a capital letter and end with a period. For example:
473 @code
474 struct VlExampleStructure {
475 int aMember ; /\*\*< A useful data member.
476 }
477 @endcode
478
479 - **Brief comments.** Brief Doxygen comments starts by a capital
480 and end with a period. The documentation of all functions start
481 with a brief comment.
482
483 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
484 @subsection devl-doc-modules Documenting the library modules
485 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
486
487 A library module groups a number of data types and functions that
488 implement a certain functionality of VLFeat. The documentation of a
489 library module is generally organized as follows:
490
491 1. A page introducing the module and including a getting started
492 section (3.g. @ref svm-starting) containing a short tutorial to
493 quickly familiarize the user with the module (e.g. @ref svm).
494 2. One or more pages of detailed technical background discussing the
495 algorithms implemented. These sections are used not just as part of
496 the C API, but also as documentation for other APIs such as MATLAB
497 (e.g. @ref svm-fundamentals).
498 3. One or more pages with the structure and function documentation
499 (e.g. @ref svm.h).
500
501 More in detail, consider a module called <em>Example Module</em>. Then one would
502 typically have:
503
504 <ul>
505 <li>A header or declaration file @c example-module.h. Such a file has an
506 heading of the type:
507
508 @verbinclude example-module-doc.h
509
510 This comment block contains a file directive, causing the file to be
511 included in the documentation, a brief directive, specifying a short
512 description of what the file is, and a list of authors. A
513 (non-Doxygen) comment block with a short the copyright notice follows.
514 The brief directive should include a <code>@@ref</code> directive to point
515 to the main documentation page describing the module, if there is one.
516 </li>
517
518 <li> An implementation or definition file @c example-module.c. This file
519 has an heading of the type:
520
521 @verbinclude example-module-doc.c
522
523 This is similar to the declaration file, except for the content of the
524 brief comment.
525 </li>
526 </ul>
527
528 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
529 @subsection devl-doc-functions Documenting functions
530 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
531
532 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
533 @subsection devl-doc-structures Documenting structures
534 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
535
536 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
537 @subsection devl-doc-structures Documenting objects
538 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
539
540 As seen in @ref objects, VLFeat treats certain structures with
541 an object-like semantics. Usually, a module defines exactly one such
542 objects. In this case, the object member functions should be grouped
543 (by using Doxygen grouping functionality) as
544
545 - **Construct and destroy** for the @c vl_object_new, @c
546 vl_object_delete and similar member functions.
547 - **Set parameters** for setter functions.
548 - **Retrieve parameters and data** for getter functions.
549 - **Process data** for functions processing data.
550
551 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
552 @subsection devl-doc-bib Bibliographic references
553 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
554
555 Since version 0.9.14, the VLFeat C library documentation makes use of
556 a proper bibliographic reference in BibTeX format (see the file @c
557 docsrc/vlfeat.bib). Doxygen uses this file when it sees instances of
558 the <code>@@cite{xyz}</code> command. Here @c xyz is a BibTeX
559 key. For example, @c vlfeat.bib file contains the entry:
560
561 <pre>
562 @@inproceedings{martin97the-det-curve,
563 Author = {A. Martin and G. Doddington and T. Kamm and M. Ordowski and M. Przybocki},
564 Booktitle = {Proc. Conf. on Speech Communication and Technology},
565 Title = {The {DET} curve in assessment of detection task performance},
566 Year = {1997}}
567 </pre>
568
569 For example, the Doxygen directive
570 <code>@@cite{martin97the-det-curve}</code> generates the output
571 @cite{martin97the-det-curve}, which is a link to the corresponding
572 entry in the bibliography.
573
574 **/
575
576 /**
577
578 @file generic.h
579
580 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
581 @page generic General support functionalities
582 @author Andrea Vedaldi
583 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
584
585 VLFeat contains several support functionalities addressing the C
586 preprocessors, using multiple threads (including parallel computations),
587 handling errors, allocating memory, etc. These are described in
588 the following pages:
589
590 - @subpage resources
591 - @subpage threads
592 - @subpage misc
593 **/
594
595 /**
596
597 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
598 @page misc Preprocssor, library state, etc.
599 @author Andrea Vedaldi
600 @tableofcontents
601 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
602
603 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
604 @section misc-preproc C preprocessor helpers
605 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
606
607 VLFeat provides a few C preprocessor macros of general
608 utility. These include stringification (::VL_STRINGIFY,
609 ::VL_XSTRINGIFY) and concatenation (::VL_CAT, ::VL_XCAT) of symbols.
610
611 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
612 @section misc-state VLFeat state and configuration parameters
613 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
614
615 VLFeat has some global configuration parameters that can
616 changed. Changing the configuration is thread unsave
617 (@ref threads). Use ::vl_set_simd_enabled to toggle the use of
618 a SIMD unit (Intel SSE code), ::vl_set_alloc_func to change
619 the memory allocation functions, and ::vl_set_printf_func
620 to change the logging function.
621
622 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
623 @section misc-error Error handling
624 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
625
626 Some VLFeat functions signal errors in a way similar to the
627 standard C library. In case of error, a VLFeat function
628 may return an error code directly,
629 or an invalid result (for instance a negative file descriptor or a null
630 pointer). Then ::vl_get_last_error and ::vl_get_last_error_message can be used
631 to retrieve further details about the error (these functions should be
632 used right after an error has occurred, before any other VLFeat call).
633
634 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
635 @section misc-memory Memory allocation
636 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
637
638 VLFeat uses the ::vl_malloc, ::vl_realloc, ::vl_calloc and ::vl_free
639 functions to allocate memory. Normally these functions are mapped to
640 the underlying standard C library implementations. However
641 ::vl_set_alloc_func can be used to map them to other
642 implementations. For instance, in MATALB MEX files these functions
643 are mapped to the MATLAB equivalent which has a garbage collection
644 mechanism to cope with interruptions during execution.
645
646 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
647 @section misc-logging Logging
648 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
649
650 VLFeat uses the macros ::VL_PRINT and ::VL_PRINTF to print progress
651 or debug informations. These functions are normally mapped to the @c
652 printf function of the underlying standard C library. However
653 ::vl_set_printf_func can be used to map it to a different
654 implementation. For instance, in MATLAB MEX files this function is
655 mapped to @c mexPrintf. Setting the function to @c NULL disables
656 logging.
657
658 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
659 @section misc-time Measuring time
660 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
661
662 VLFeat provides ::vl_tic and ::vl_toc as an easy way of measuring
663 elapsed time.
664
665 **/
666
667 /**
668 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
669 @page threads Threading
670 @tableofcontents
671 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
672
673 VLFeat supports for threaded computations can be used to take advantage
674 of multi-core architectures. Threading support includes:
675
676 - Supporting using VLFeat functions and objects from multiple threads
677 simultaneously. This is discussed in @ref threads-multiple.
678 - Using multiple cores to accelerate computations. This is
679 discussed in @ref threads-parallel.
680
681 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
682 @section threads-multiple Using VLFeat from multiple threads
683 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
684
685 VLFeat can be used from multiple threads simultaneously if proper
686 rules are followed.
687
688 - <b>A VLFeat object instance is accessed only from one thread at any
689 given time.</b> Functions operating on objects (member functions)
690 are conditionally thread safe: the same function may be called
691 simultaneously from multiple threads provided that it operates on
692 different, independent objects. However, modifying the same object
693 from multiple threads (using the same or different member functions)
694 is possible only from one thread at any given time, and should
695 therefore be synchronized. Certain VLFeat objects may contain
696 features specific to simplify multi-threaded operations
697 (e.g. ::VlKDForest).
698 - <b>Thread-safe global functions are used.</b> These include
699 thread-specific operations such as retrieving the last error by
700 ::vl_get_last_error and obtaining the thread-specific random number
701 generator instance by ::vl_get_rand. In these cases, the functions
702 operate on thread-specific data that VLFeat creates and
703 maintains. Note in particular that each thread has an independent
704 default random number generator (as returned by
705 ::vl_get_rand). VLFeat objects that involve using random numbers
706 will typically use the random number generator of the thread
707 currently accessing the object (although an object-specific
708 generator can be often be specified instead).
709 - <b>Any other global function is considered non-thread safe and is
710 accessed exclusively by one thread at a time.</b> A small number of
711 operations are non-reentrant <em>and</em> affect all threads
712 simultaneously. These are restricted to changing certain global
713 configuration parameters, such as the memory allocation functions by
714 ::vl_set_alloc_func. These operations are <em>not</em> thread safe
715 and are preferably executed before multiple threads start to operate
716 with the library.
717
718 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
719 @section threads-parallel Parallel computations
720 <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
721
722 VLFeat uses OpenMP to implement parallel computations. Generally, this
723 means that multiple cores are uses appropriately and transparently,
724 provided that other multi-threaded parts of the application use OpenMP
725 and that VLFeat and the application link to the same OpenMP library.
726 If finer control is required, read on.
727
728 VLFeat functions avoids affecting OpenMP global state, including the
729 desired number of computational threads, in order to minimize side
730 effects to the linked application (e.g. MATLAB). Instead, VLFeat
731 duplicates a few OpenMP control parameters when needed (this approach
732 is similar to the method used by other libraries such as Intel MKL).
733
734 The maximum number of threads available to the application can be
735 obtained by ::vl_get_thread_limit (for OpenMP version 3.0 and
736 greater). This limit is controlled by the OpenMP library (the function
737 is a wrapper around @c omp_get_thread_limit), which in turn may
738 determined that based on the number of computational cores or the
739 value of the @c OMP_THREAD_LIMIT variable when the program is
740 launched. This value is an upper bound on the number of computation
741 threads that can be used at any time.
742
743 The maximum number of computational thread that VLFeat should use is
744 set by ::vl_set_num_threads() and retrieved by ::vl_get_max_threads().
745 This number is a target value as well as an upper bound to the number
746 of threads used by VLFeat. This value is stored in the VLFeat private
747 state and is not necessarily equal to the corresponding OpenMP state
748 variable retrieved by calling @c omp_get_max_threads(). @c
749 vl_set_num_threads(1) disables the use of multiple threads and @c
750 vl_set_num_threads(0) uses the value returned by the OpenMP call @c
751 omp_get_max_threads(). The latter value is controlled, for example, by
752 calling @c omp_set_num_threads() in the application. Note that:
753
754 - @c vl_set_num_threads(0) determines the number of treads using @c
755 omp_get_max_threads() *when it is called*. Subsequent calls to @c
756 omp_set_num_threads() will therefore *not* affect the number of
757 threads used by VLFeat.
758 - @c vl_set_num_threads(vl_get_thread_limit()) causes VLFeat use all
759 the available threads, regardless on the number of threads set
760 within the application by calls to @c omp_set_num_threads().
761 - OpenMP may still dynamically decide to use a smaller number of
762 threads in any specific parallel computation.
763
764 @sa http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_userguide_win/GUID-C2295BC8-DD22-466B-94C9-5FAA79D4F56D.htm
765 http://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_userguide_win/index.htm#GUID-DEEF0363-2B34-4BAB-87FA-A75DBE842040.htm
766 http://software.intel.com/sites/products/documentation/hpc/mkl/lin/MKL_UG_managing_performance/Using_Additional_Threading_Control.htm
767
768 **/
769
770 #include "generic.h"
771
772 #include <assert.h>
773 #include <stdlib.h>
774 #include <stdio.h>
775 #include <stdarg.h>
776 #include <math.h>
777
778 #if defined(VL_OS_WIN)
779 #include <Windows.h>
780 #else
781 #include <unistd.h>
782 #endif
783
784 #if ! defined(VL_DISABLE_THREADS) && defined(VL_THREADS_POSIX)
785 #include <pthread.h>
786 #endif
787
788 #if defined(_OPENMP)
789 #include <omp.h>
790 #endif
791
792 /* ---------------------------------------------------------------- */
793 /* Global and thread states */
794 /* ---------------------------------------------------------------- */
795
796 /* Thread state */
797 typedef struct _VlThreadState
798 {
799 /* errors */
800 int lastError ;
801 char lastErrorMessage [VL_ERR_MSG_LEN] ;
802
803 /* random number generator */
804 VlRand rand ;
805
806 /* time */
807 #if defined(VL_OS_WIN)
808 LARGE_INTEGER ticFreq ;
809 LARGE_INTEGER ticMark ;
810 #else
811 clock_t ticMark ;
812 #endif
813 } VlThreadState ;
814
815 /* Gobal state */
816 typedef struct _VlState
817 {
818 /* The thread state uses either a mutex (POSIX)
819 or a critical section (Win) */
820 #if defined(VL_DISABLE_THREADS)
821 VlThreadState * threadState ;
822 #else
823 #if defined(VL_THREADS_POSIX)
824 pthread_key_t threadKey ;
825 pthread_mutex_t mutex ;
826 pthread_t mutexOwner ;
827 pthread_cond_t mutexCondition ;
828 size_t mutexCount ;
829 #elif defined(VL_THREADS_WIN)
830 DWORD tlsIndex ;
831 CRITICAL_SECTION mutex ;
832 #endif
833 #endif /* VL_DISABLE_THREADS */
834
835 /* Configurable functions */
836 int (*printf_func) (char const * format, ...) ;
837 void *(*malloc_func) (size_t) ;
838 void *(*realloc_func) (void*,size_t) ;
839 void *(*calloc_func) (size_t, size_t) ;
840 void (*free_func) (void*) ;
841
842 #if defined(VL_ARCH_IX86) || defined(VL_ARCH_X64) || defined(VL_ARCH_IA64)
843 VlX86CpuInfo cpuInfo ;
844 #endif
845 vl_size numCPUs ;
846 vl_bool simdEnabled ;
847 vl_size numThreads ;
848 } VlState ;
849
850 /* Global state instance */
851 VlState _vl_state ;
852
853 /* ----------------------------------------------------------------- */
854 VL_INLINE VlState * vl_get_state () ;
855 VL_INLINE VlThreadState * vl_get_thread_specific_state () ;
856 static void vl_lock_state (void) ;
857 static void vl_unlock_state (void) ;
858 static VlThreadState * vl_thread_specific_state_new (void) ;
859 static void vl_thread_specific_state_delete (VlThreadState * self) ;
860
861 /** @brief Get VLFeat version string
862 ** @return the library version string.
863 **/
864
865 char const *
vl_get_version_string()866 vl_get_version_string ()
867 {
868 return VL_VERSION_STRING ;
869 }
870
871 /** @brief Get VLFeat configuration string.
872 ** @return a new configuration string.
873 **
874 ** The function returns a new string containing a human readable
875 ** description of the library configuration.
876 **/
877
878 char *
vl_configuration_to_string_copy()879 vl_configuration_to_string_copy ()
880 {
881 char * string = 0 ;
882 int length = 0 ;
883 char * staticString = vl_static_configuration_to_string_copy() ;
884 char * cpuString =
885 #if defined(VL_ARCH_IX86) || defined(VL_ARCH_X64) || defined(VL_ARCH_IA64)
886 _vl_x86cpu_info_to_string_copy(&vl_get_state()->cpuInfo) ;
887 #else
888 "Generic CPU" ;
889 #endif
890 #if defined(DEBUG)
891 int const debug = 1 ;
892 #else
893 int const debug = 0 ;
894 #endif
895
896 while (string == 0) {
897 if (length > 0) {
898 string = vl_malloc(sizeof(char) * length) ;
899 if (string == NULL) break ;
900 }
901 length = snprintf(string, length,
902 "VLFeat version %s\n"
903 " Static config: %s\n"
904 " %" VL_FMT_SIZE " CPU(s): %s\n"
905 #if defined(_OPENMP)
906 " OpenMP: max threads: %d (library: %" VL_FMT_SIZE ")\n"
907 #endif
908 " Debug: %s\n",
909 vl_get_version_string (),
910 staticString,
911 vl_get_num_cpus(), cpuString,
912 #if defined(_OPENMP)
913 omp_get_max_threads(), vl_get_max_threads(),
914 #endif
915 VL_YESNO(debug)) ;
916 length += 1 ;
917 }
918
919 if (staticString) vl_free(staticString) ;
920 if (cpuString) vl_free(cpuString) ;
921 return string ;
922 }
923
924 /** @internal @brief A printf that does not do anything */
925 static int
do_nothing_printf(char const * format VL_UNUSED,...)926 do_nothing_printf (char const* format VL_UNUSED, ...)
927 {
928 return 0 ;
929 }
930
931 /** @internal
932 ** @brief Lock VLFeat state
933 **
934 ** The function locks VLFeat global state mutex.
935 **
936 ** The mutex is recursive: locking multiple times from the same thread
937 ** is a valid operations, but requires an equivalent number
938 ** of calls to ::vl_unlock_state.
939 **
940 ** @sa ::vl_unlock_state
941 **/
942
943 static void
vl_lock_state(void)944 vl_lock_state (void)
945 {
946 #if ! defined(VL_DISABLE_THREADS)
947 #if defined(VL_THREADS_POSIX)
948 VlState * state = vl_get_state () ;
949 pthread_t thisThread = pthread_self () ;
950 pthread_mutex_lock (&state->mutex) ;
951 if (state->mutexCount >= 1 &&
952 pthread_equal (state->mutexOwner, thisThread)) {
953 state->mutexCount ++ ;
954 } else {
955 while (state->mutexCount >= 1) {
956 pthread_cond_wait (&state->mutexCondition, &state->mutex) ;
957 }
958 state->mutexOwner = thisThread ;
959 state->mutexCount = 1 ;
960 }
961 pthread_mutex_unlock (&state->mutex) ;
962 #elif defined(VL_THREADS_WIN)
963 EnterCriticalSection (&vl_get_state()->mutex) ;
964 #endif
965 #endif
966 }
967
968 /** @internal
969 ** @brief Unlock VLFeat state
970 **
971 ** The function unlocks VLFeat global state mutex.
972 **
973 ** @sa ::vl_lock_state
974 **/
975
976 static void
vl_unlock_state(void)977 vl_unlock_state (void)
978 {
979 #if ! defined(VL_DISABLE_THREADS)
980 #if defined(VL_THREADS_POSIX)
981 VlState * state = vl_get_state () ;
982 pthread_mutex_lock (&state->mutex) ;
983 -- state->mutexCount ;
984 if (state->mutexCount == 0) {
985 pthread_cond_signal (&state->mutexCondition) ;
986 }
987 pthread_mutex_unlock (&state->mutex) ;
988 #elif defined(VL_THREADS_WIN)
989 LeaveCriticalSection (&vl_get_state()->mutex) ;
990 #endif
991 #endif
992 }
993
994 /** @internal
995 ** @brief Return VLFeat global state
996 **
997 ** The function returns a pointer to VLFeat global state.
998 **
999 ** @return pointer to the global state structure.
1000 **/
1001
1002 VL_INLINE VlState *
vl_get_state(void)1003 vl_get_state (void)
1004 {
1005 return &_vl_state ;
1006 }
1007
1008 /** @internal@brief Get VLFeat thread state
1009 ** @return pointer to the thread state structure.
1010 **
1011 ** The function returns a pointer to VLFeat thread state.
1012 **/
1013
1014 VL_INLINE VlThreadState *
vl_get_thread_specific_state(void)1015 vl_get_thread_specific_state (void)
1016 {
1017 #ifdef VL_DISABLE_THREADS
1018 return vl_get_state()->threadState ;
1019 #else
1020 VlState * state ;
1021 VlThreadState * threadState ;
1022
1023 vl_lock_state() ;
1024 state = vl_get_state() ;
1025
1026 #if defined(VL_THREADS_POSIX)
1027 threadState = (VlThreadState *) pthread_getspecific(state->threadKey) ;
1028 #elif defined(VL_THREADS_WIN)
1029 threadState = (VlThreadState *) TlsGetValue(state->tlsIndex) ;
1030 #endif
1031
1032 if (! threadState) {
1033 threadState = vl_thread_specific_state_new () ;
1034 }
1035
1036 #if defined(VL_THREADS_POSIX)
1037 pthread_setspecific(state->threadKey, threadState) ;
1038 #elif defined(VL_THREADS_WIN)
1039 TlsSetValue(state->tlsIndex, threadState) ;
1040 #endif
1041
1042 vl_unlock_state() ;
1043 return threadState ;
1044 #endif
1045 }
1046
1047 /* ---------------------------------------------------------------- */
1048 /** @brief Get the number of CPU cores of the host
1049 ** @return number of CPU cores.
1050 **/
1051
1052 vl_size
vl_get_num_cpus(void)1053 vl_get_num_cpus (void)
1054 {
1055 return vl_get_state()->numCPUs ;
1056 }
1057
1058 /** @fn ::vl_set_simd_enabled(vl_bool)
1059 ** @brief Toggle usage of SIMD instructions
1060 ** @param x @c true if SIMD instructions are used.
1061 **
1062 ** Notice that SIMD instructions are used only if the CPU model
1063 ** supports them. Note also that data alignment may restrict the use
1064 ** of such instructions.
1065 **
1066 ** @see ::vl_cpu_has_sse2(), ::vl_cpu_has_sse3(), etc.
1067 **/
1068
1069 void
vl_set_simd_enabled(vl_bool x)1070 vl_set_simd_enabled (vl_bool x)
1071 {
1072 vl_get_state()->simdEnabled = x ;
1073 }
1074
1075 /** @brief Are SIMD instructons enabled?
1076 ** @return @c true if SIMD instructions are enabled.
1077 **/
1078
1079 vl_bool
vl_get_simd_enabled(void)1080 vl_get_simd_enabled (void)
1081 {
1082 return vl_get_state()->simdEnabled ;
1083 }
1084
1085 /** @brief Check for AVX instruction set
1086 ** @return @c true if AVX is present.
1087 **/
1088
1089 vl_bool
vl_cpu_has_avx(void)1090 vl_cpu_has_avx (void)
1091 {
1092 #if defined(VL_ARCH_IX86) || defined(VL_ARCH_X64) || defined(VL_ARCH_IA64)
1093 return vl_get_state()->cpuInfo.hasAVX ;
1094 #else
1095 return VL_FALSE ;
1096 #endif
1097 }
1098
1099 /** @brief Check for SSE3 instruction set
1100 ** @return @c true if SSE3 is present.
1101 **/
1102
1103 vl_bool
vl_cpu_has_sse3(void)1104 vl_cpu_has_sse3 (void)
1105 {
1106 #if defined(VL_ARCH_IX86) || defined(VL_ARCH_X64) || defined(VL_ARCH_IA64)
1107 return vl_get_state()->cpuInfo.hasSSE3 ;
1108 #else
1109 return VL_FALSE ;
1110 #endif
1111 }
1112
1113 /** @brief Check for SSE2 instruction set
1114 ** @return @c true if SSE2 is present.
1115 **/
1116
1117 vl_bool
vl_cpu_has_sse2(void)1118 vl_cpu_has_sse2 (void)
1119 {
1120 #if defined(VL_ARCH_IX86) || defined(VL_ARCH_X64) || defined(VL_ARCH_IA64)
1121 return vl_get_state()->cpuInfo.hasSSE2 ;
1122 #else
1123 return VL_FALSE ;
1124 #endif
1125 }
1126
1127 /* ---------------------------------------------------------------- */
1128
1129 /** @brief Get the number of computational threads available to the application
1130 ** @return number of threads.
1131 **
1132 ** This function wraps the OpenMP function @c
1133 ** omp_get_thread_limit(). If VLFeat was compiled without OpenMP
1134 ** support, this function returns 1. If VLFeat was compiled with
1135 ** OpenMP prior to version 3.0 (2008/05), it returns 0.
1136 **
1137 ** @sa @ref threads-parallel
1138 **/
1139
1140 vl_size
vl_get_thread_limit(void)1141 vl_get_thread_limit (void)
1142 {
1143 #if defined(_OPENMP)
1144 #if _OPENMP >= 200805
1145 /* OpenMP version >= 3.0 */
1146 return omp_get_thread_limit() ;
1147 #else
1148 return 0 ;
1149 #endif
1150 #else
1151 return 1 ;
1152 #endif
1153 }
1154
1155 /** @brief Get the maximum number of computational threads used by VLFeat.
1156 ** @return number of threads.
1157 **
1158 ** This function returns the maximum number of thread used by
1159 ** VLFeat. VLFeat will try to use this number of computational
1160 ** threads and never exceed it.
1161 **
1162 ** This is similar to the OpenMP function @c omp_get_max_threads();
1163 ** however, it reads a parameter private to VLFeat which is
1164 ** independent of the value used by the OpenMP library.
1165 **
1166 ** If VLFeat was compiled without OpenMP support, this function
1167 ** returns 1.
1168 **
1169 ** @sa vl_set_num_threads(), @ref threads-parallel
1170 **/
1171
1172 vl_size
vl_get_max_threads(void)1173 vl_get_max_threads (void)
1174 {
1175 #if defined(_OPENMP)
1176 return vl_get_state()->numThreads ;
1177 #else
1178 return 1 ;
1179 #endif
1180 }
1181
1182 /** @brief Set the maximum number of threads used by VLFeat.
1183 ** @param numThreads number of threads to use.
1184 **
1185 ** This function sets the maximum number of computational threads
1186 ** that will be used by VLFeat. VLFeat may in practice use fewer
1187 ** threads (for example because @a numThreads is larger than the
1188 ** number of computational cores in the host, or because the number
1189 ** of threads exceeds the limit available to the application).
1190 **
1191 ** If @c numThreads is set to 0, then VLFeat sets the number of
1192 ** threads to the OpenMP current maximum, obtained by calling @c
1193 ** omp_get_max_threads().
1194 **
1195 ** This function is similar to @c omp_set_num_threads() but changes a
1196 ** parameter internal to VLFeat rather than affecting OpenMP global
1197 ** state.
1198 **
1199 ** If VLFeat was compiled without, this function does nothing.
1200 **
1201 ** @sa vl_get_max_threads(), @ref threads-parallel
1202 **/
1203
1204 #if defined(_OPENMP)
1205 void
vl_set_num_threads(vl_size numThreads)1206 vl_set_num_threads (vl_size numThreads)
1207 {
1208 if (numThreads == 0) {
1209 numThreads = omp_get_max_threads() ;
1210 }
1211 vl_get_state()->numThreads = numThreads ;
1212 }
1213 #else
1214 void
vl_set_num_threads(vl_size numThreads VL_UNUSED)1215 vl_set_num_threads (vl_size numThreads VL_UNUSED) { }
1216 #endif
1217
1218 /* ---------------------------------------------------------------- */
1219 /** @brief Set last VLFeat error
1220 ** @param error error code.
1221 ** @param errorMessage error message format string.
1222 ** @param ... format string arguments.
1223 ** @return error code.
1224 **
1225 ** The function sets the code and optionally the error message
1226 ** of the last encountered error. @a errorMessage is the message
1227 ** format. It uses the @c printf convention and is followed by
1228 ** the format arguments. The maximum length of the error message is
1229 ** given by ::VL_ERR_MSG_LEN (longer messages are truncated).
1230 **
1231 ** Passing @c NULL as @a errorMessage
1232 ** sets the error message to the empty string.
1233 **/
1234
1235 int
vl_set_last_error(int error,char const * errorMessage,...)1236 vl_set_last_error (int error, char const * errorMessage, ...)
1237 {
1238 VlThreadState * state = vl_get_thread_specific_state() ;
1239 va_list args;
1240 va_start(args, errorMessage) ;
1241 if (errorMessage) {
1242 #ifdef VL_COMPILER_LCC
1243 vsprintf(state->lastErrorMessage, errorMessage, args) ;
1244 #else
1245 vsnprintf(state->lastErrorMessage,
1246 sizeof(state->lastErrorMessage)/sizeof(char),
1247 errorMessage, args) ;
1248 #endif
1249 } else {
1250 state->lastErrorMessage[0] = 0 ;
1251 }
1252 state->lastError = error ;
1253 va_end(args) ;
1254 return error ;
1255 }
1256
1257 /** @brief Get the code of the last error
1258 ** @return error code.
1259 ** @sa ::vl_get_last_error_message.
1260 **/
1261
1262 int
vl_get_last_error(void)1263 vl_get_last_error (void) {
1264 return vl_get_thread_specific_state()->lastError ;
1265 }
1266
1267 /** @brief Get the last error message
1268 ** @return pointer to the error message.
1269 ** @sa ::vl_get_last_error.
1270 **/
1271
1272 char const *
vl_get_last_error_message(void)1273 vl_get_last_error_message (void)
1274 {
1275 return vl_get_thread_specific_state()->lastErrorMessage ;
1276 }
1277
1278 /* ---------------------------------------------------------------- */
1279 /** @brief Set memory allocation functions
1280 ** @param malloc_func pointer to @c malloc.
1281 ** @param realloc_func pointer to @c realloc.
1282 ** @param calloc_func pointer to @c calloc.
1283 ** @param free_func pointer to @c free.
1284 **/
1285
1286 void
vl_set_alloc_func(void * (* malloc_func)(size_t),void * (* realloc_func)(void *,size_t),void * (* calloc_func)(size_t,size_t),void (* free_func)(void *))1287 vl_set_alloc_func (void *(*malloc_func) (size_t),
1288 void *(*realloc_func) (void*, size_t),
1289 void *(*calloc_func) (size_t, size_t),
1290 void (*free_func) (void*))
1291 {
1292 VlState * state ;
1293 vl_lock_state () ;
1294 state = vl_get_state() ;
1295 state->malloc_func = malloc_func ;
1296 state->realloc_func = realloc_func ;
1297 state->calloc_func = calloc_func ;
1298 state->free_func = free_func ;
1299 vl_unlock_state () ;
1300 }
1301
1302 /** @brief Allocate a memory block
1303 ** @param n size in bytes of the new block.
1304 ** @return pointer to the allocated block.
1305 **
1306 ** This function allocates a memory block of the specified size.
1307 ** The synopsis is the same as the POSIX @c malloc function.
1308 **/
1309
1310 void *
vl_malloc(size_t n)1311 vl_malloc (size_t n)
1312 {
1313 return (vl_get_state()->malloc_func)(n) ;
1314 //return (memalign)(32,n) ;
1315 }
1316
1317
1318 /** @brief Reallocate a memory block
1319 ** @param ptr pointer to a memory block previously allocated.
1320 ** @param n size in bytes of the new block.
1321 ** @return pointer to the new block.
1322 **
1323 ** This function reallocates a memory block to change its size.
1324 ** The synopsis is the same as the POSIX @c realloc function.
1325 **/
1326
1327 void *
vl_realloc(void * ptr,size_t n)1328 vl_realloc (void* ptr, size_t n)
1329 {
1330 return (vl_get_state()->realloc_func)(ptr, n) ;
1331 }
1332
1333 /** @brief Free and clear a memory block
1334 ** @param n number of items to allocate.
1335 ** @param size size in bytes of an item.
1336 ** @return pointer to the new block.
1337 **
1338 ** This function allocates and clears a memory block.
1339 ** The synopsis is the same as the POSIX @c calloc function.
1340 **/
1341
1342 void *
vl_calloc(size_t n,size_t size)1343 vl_calloc (size_t n, size_t size)
1344 {
1345 return (vl_get_state()->calloc_func)(n, size) ;
1346 }
1347
1348 /** @brief Free a memory block
1349 ** @param ptr pointer to the memory block.
1350 **
1351 ** This function frees a memory block allocated by ::vl_malloc,
1352 ** ::vl_calloc, or ::vl_realloc. The synopsis is the same as the POSIX
1353 ** @c malloc function.
1354 **/
1355
1356 void
vl_free(void * ptr)1357 vl_free (void *ptr)
1358 {
1359 (vl_get_state()->free_func)(ptr) ;
1360 }
1361
1362 /* ---------------------------------------------------------------- */
1363
1364 /** @brief Set the printf function
1365 ** @param printf_func pointer to a @c printf implementation.
1366 ** Set @c print_func to NULL to disable printf.
1367 **/
1368
1369 void
vl_set_printf_func(printf_func_t printf_func)1370 vl_set_printf_func (printf_func_t printf_func)
1371 {
1372 vl_get_state()->printf_func = printf_func ? printf_func : do_nothing_printf ;
1373 }
1374
1375 /** @brief Get the printf function
1376 ** @return printf_func pointer to the @c printf implementation.
1377 ** @sa ::vl_set_printf_func.
1378 **/
1379
1380 printf_func_t
vl_get_printf_func(void)1381 vl_get_printf_func (void) {
1382 return vl_get_state()->printf_func ;
1383 }
1384
1385 /* ---------------------------------------------------------------- */
1386 /** @brief Get processor time
1387 ** @return processor time in seconds.
1388 ** @sa ::vl_tic, ::vl_toc
1389 **/
1390
1391 double
vl_get_cpu_time()1392 vl_get_cpu_time ()
1393 {
1394 #ifdef VL_OS_WIN
1395 VlThreadState * threadState = vl_get_thread_specific_state() ;
1396 LARGE_INTEGER mark ;
1397 QueryPerformanceCounter (&mark) ;
1398 return (double)mark.QuadPart / (double)threadState->ticFreq.QuadPart ;
1399 #else
1400 return (double)clock() / (double)CLOCKS_PER_SEC ;
1401 #endif
1402 }
1403
1404 /** @brief Reset processor time reference
1405 ** The function resets VLFeat TIC/TOC time reference. There is one
1406 ** such reference per thread.
1407 ** @sa ::vl_get_cpu_time, ::vl_toc.
1408 **/
1409
1410 void
vl_tic(void)1411 vl_tic (void)
1412 {
1413 VlThreadState * threadState = vl_get_thread_specific_state() ;
1414 #ifdef VL_OS_WIN
1415 QueryPerformanceCounter (&threadState->ticMark) ;
1416 #else
1417 threadState->ticMark = clock() ;
1418 #endif
1419 }
1420
1421 /** @brief Get elapsed time since tic
1422 ** @return elapsed time in seconds.
1423 **
1424 ** The function
1425 ** returns the processor time elapsed since ::vl_tic was called last.
1426 **
1427 ** @remark In multi-threaded applications, there is an independent
1428 ** timer for each execution thread.
1429 **
1430 ** @remark On UNIX, this function uses the @c clock() system call.
1431 ** On Windows, it uses the @c QueryPerformanceCounter() system call,
1432 ** which is more accurate than @c clock() on this platform.
1433 **/
1434
1435 double
vl_toc(void)1436 vl_toc (void)
1437 {
1438 VlThreadState * threadState = vl_get_thread_specific_state() ;
1439 #ifdef VL_OS_WIN
1440 LARGE_INTEGER tocMark ;
1441 QueryPerformanceCounter(&tocMark) ;
1442 return (double) (tocMark.QuadPart - threadState->ticMark.QuadPart) /
1443 threadState->ticFreq.QuadPart ;
1444 #else
1445 return (double) (clock() - threadState->ticMark) / CLOCKS_PER_SEC ;
1446 #endif
1447 }
1448
1449 /* ---------------------------------------------------------------- */
1450 /** @brief Get the default random number generator.
1451 ** @return random number generator.
1452 **
1453 ** The function returns a pointer to the default
1454 ** random number generator.
1455 ** There is one such generator per thread.
1456 **/
1457
1458 VL_EXPORT VlRand *
vl_get_rand(void)1459 vl_get_rand (void)
1460 {
1461 return &vl_get_thread_specific_state()->rand ;
1462 }
1463
1464 /* ---------------------------------------------------------------- */
1465 /* Library construction and destruction routines */
1466 /* --------------------------------------------------------------- */
1467
1468 /** @internal@brief Construct a new thread state object
1469 ** @return new state structure.
1470 **/
1471
1472 static VlThreadState *
vl_thread_specific_state_new(void)1473 vl_thread_specific_state_new (void)
1474 {
1475 VlThreadState * self ;
1476 #if defined(DEBUG)
1477 printf("VLFeat DEBUG: thread constructor begins.\n") ;
1478 #endif
1479 self = malloc(sizeof(VlThreadState)) ;
1480 self->lastError = 0 ;
1481 self->lastErrorMessage[0] = 0 ;
1482 #if defined(VL_OS_WIN)
1483 QueryPerformanceFrequency (&self->ticFreq) ;
1484 self->ticMark.QuadPart = 0 ;
1485 #else
1486 self->ticMark = 0 ;
1487 #endif
1488 vl_rand_init (&self->rand) ;
1489
1490 return self ;
1491 }
1492
1493 /** @internal@brief Delete a thread state structure
1494 ** @param self thread state object.
1495 **/
1496
1497 static void
vl_thread_specific_state_delete(VlThreadState * self)1498 vl_thread_specific_state_delete (VlThreadState * self)
1499 {
1500 #if defined(DEBUG)
1501 printf("VLFeat DEBUG: thread destructor begins.\n") ;
1502 #endif
1503 free (self) ;
1504 }
1505
1506 /* ---------------------------------------------------------------- */
1507 /* Library constructor and destructor */
1508 /* ---------------------------------------------------------------- */
1509
1510 /** @internal @brief Initialize VLFeat state */
1511 void
vl_constructor(void)1512 vl_constructor (void)
1513 {
1514 VlState * state ;
1515 #if defined(DEBUG)
1516 printf("VLFeat DEBUG: constructor begins.\n") ;
1517 #endif
1518
1519 state = vl_get_state() ;
1520
1521 #if ! defined(VL_DISABLE_THREADS)
1522 #if defined(DEBUG)
1523 printf("VLFeat DEBUG: constructing thread specific state.\n") ;
1524 #endif
1525 #if defined(VL_THREADS_POSIX)
1526 {
1527 typedef void (*destructorType)(void * );
1528 pthread_key_create (&state->threadKey,
1529 (destructorType)
1530 vl_thread_specific_state_delete) ;
1531 pthread_mutex_init (&state->mutex, NULL) ;
1532 pthread_cond_init (&state->mutexCondition, NULL) ;
1533 }
1534 #elif defined(VL_THREADS_WIN)
1535 InitializeCriticalSection (&state->mutex) ;
1536 state->tlsIndex = TlsAlloc () ;
1537 #endif
1538 #else
1539
1540 /* threading support disabled */
1541 #if defined(DEBUG)
1542 printf("VLFeat DEBUG: constructing the generic thread state instance (threading support disabled).\n") ;
1543 #endif
1544 vl_get_state()->threadState = vl_thread_specific_state_new() ;
1545 #endif
1546
1547 state->malloc_func = malloc ;
1548 state->realloc_func = realloc ;
1549 state->calloc_func = calloc ;
1550 state->free_func = free ;
1551 state->printf_func = printf ;
1552
1553 /* on x86 platforms read the CPUID register */
1554 #if defined(VL_ARCH_IX86) || defined(VL_ARCH_X64) || defined(VL_ARCH_IA64)
1555 _vl_x86cpu_info_init (&state->cpuInfo) ;
1556 #endif
1557
1558 /* get the number of CPUs */
1559 #if defined(VL_OS_WIN)
1560 {
1561 SYSTEM_INFO info;
1562 GetSystemInfo (&info) ;
1563 state->numCPUs = info.dwNumberOfProcessors ;
1564 }
1565 #elif defined(_SC_NPROCESSORS_ONLN)
1566 state->numCPUs = sysconf(_SC_NPROCESSORS_ONLN) ;
1567 #else
1568 state->numCPUs = 1 ;
1569 #endif
1570 state->simdEnabled = VL_TRUE ;
1571
1572 /* get the number of (OpenMP) threads used by the library */
1573 #if defined(_OPENMP)
1574 state->numThreads = omp_get_max_threads() ;
1575 #else
1576 state->numThreads = 1 ;
1577 #endif
1578
1579 #if defined(DEBUG)
1580 printf("VLFeat DEBUG: constructor ends.\n") ;
1581 #endif
1582 }
1583
1584 /** @internal @brief Destruct VLFeat */
1585 void
vl_destructor()1586 vl_destructor ()
1587 {
1588 VlState * state ;
1589 #if defined(DEBUG)
1590 printf("VLFeat DEBUG: destructor begins.\n") ;
1591 #endif
1592
1593 state = vl_get_state() ;
1594
1595 #if ! defined(VL_DISABLE_THREADS)
1596 #if defined(DEBUG)
1597 printf("VLFeat DEBUG: destroying a thread specific state instance.\n") ;
1598 #endif
1599 #if defined(VL_THREADS_POSIX)
1600 {
1601 /* Delete the thread state of this thread as the
1602 destructor is not called by pthread_key_delete or after
1603 the key is deleted. When the library
1604 is unloaded, this thread should also be the last one
1605 using the library, so this is fine.
1606 */
1607 VlThreadState * threadState =
1608 pthread_getspecific(state->threadKey) ;
1609 if (threadState) {
1610 vl_thread_specific_state_delete (threadState) ;
1611 pthread_setspecific(state->threadKey, NULL) ;
1612 }
1613 }
1614 pthread_cond_destroy (&state->mutexCondition) ;
1615 pthread_mutex_destroy (&state->mutex) ;
1616 pthread_key_delete (state->threadKey) ;
1617 #elif defined(VL_THREADS_WIN)
1618 {
1619 /* Delete the thread state of this thread as the
1620 destructor is not called by pthread_key_delete or after
1621 the key is deleted. When the library
1622 is unloaded, this thread should also be the last one
1623 using the library, so this is fine.
1624 */
1625 VlThreadState * threadState =
1626 TlsGetValue(state->tlsIndex) ;
1627 if (threadState) {
1628 vl_thread_specific_state_delete (threadState) ;
1629 TlsSetValue(state->tlsIndex, NULL) ;
1630 }
1631 }
1632 TlsFree (state->tlsIndex) ;
1633 DeleteCriticalSection (&state->mutex) ;
1634 #endif
1635 #else
1636 #if defined(DEBUG)
1637 printf("VLFeat DEBUG: destroying the generic thread state instance (threading support disabled).\n") ;
1638 #endif
1639 vl_thread_specific_state_delete(vl_get_state()->threadState) ;
1640 #endif
1641
1642 #if defined(DEBUG)
1643 printf("VLFeat DEBUG: destructor ends.\n") ;
1644 #endif
1645 }
1646
1647 /* ---------------------------------------------------------------- */
1648 /* Cross-platform call to constructor/destructor */
1649 /* ---------------------------------------------------------------- */
1650
1651 #ifdef __cplusplus
1652 #define INITIALIZER(f) \
1653 static void f(void); \
1654 struct f##_t_ { f##_t_(void) { f(); } }; static f##_t_ f##_; \
1655 static void f(void)
1656 #elif defined(_MSC_VER)
1657 #pragma section(".CRT$XCU",read)
1658 #define INITIALIZER2_(f,p) \
1659 static void f(void); \
1660 __declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \
1661 __pragma(comment(linker,"/include:" p #f "_")) \
1662 static void f(void)
1663 #ifdef _WIN64
1664 #define INITIALIZER(f) INITIALIZER2_(f,"")
1665 #else
1666 #define INITIALIZER(f) INITIALIZER2_(f,"_")
1667 #endif
1668 #else
1669 #define INITIALIZER(f) \
1670 static void f(void) __attribute__((constructor)); \
1671 static void f(void)
1672 #endif
1673
INITIALIZER(vl_initialize)1674 INITIALIZER(vl_initialize)
1675 {
1676 vl_constructor();
1677 atexit(vl_destructor);
1678 }
1679
1680