Point Cloud Library (PCL)  1.9.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
cutil.h
1 /*
2  * Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
3  *
4  * Please refer to the NVIDIA end user license agreement (EULA) associated
5  * with this source code for terms and conditions that govern your use of
6  * this software. Any use, reproduction, disclosure, or distribution of
7  * this software and related documentation outside the terms of the EULA
8  * is strictly prohibited.
9  *
10  */
11 
12  /*
13 * Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
14 *
15 * Please refer to the NVIDIA end user license agreement (EULA) associated
16 * with this source code for terms and conditions that govern your use of
17 * this software. Any use, reproduction, disclosure, or distribution of
18 * this software and related documentation outside the terms of the EULA
19 * is strictly prohibited.
20 *
21 */
22 
23 
24 /* CUda UTility Library */
25 
26 #ifndef _CUTIL_H_
27 #define _CUTIL_H_
28 
29 #ifdef _WIN32
30 # pragma warning( disable : 4996 ) // disable deprecated warning
31 #endif
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40  // helper typedefs for building DLL
41 #ifdef _WIN32
42 # ifdef BUILD_DLL
43 # define DLL_MAPPING __declspec(dllexport)
44 # else
45 # define DLL_MAPPING __declspec(dllimport)
46 # endif
47 #else
48 # define DLL_MAPPING
49 #endif
50 
51 #ifdef _WIN32
52  #define CUTIL_API __stdcall
53 #else
54  #define CUTIL_API
55 #endif
56 
57  ////////////////////////////////////////////////////////////////////////////
58  //! CUT bool type
59  ////////////////////////////////////////////////////////////////////////////
60  enum CUTBoolean
61  {
62  CUTFalse = 0,
63  CUTTrue = 1
64  };
65 
66  ////////////////////////////////////////////////////////////////////////////
67  //! Deallocate memory allocated within Cutil
68  //! @param pointer to memory
69  ////////////////////////////////////////////////////////////////////////////
70  DLL_MAPPING
71  void CUTIL_API
72  cutFree( void* ptr);
73 
74  ////////////////////////////////////////////////////////////////////////////
75  //! Helper for bank conflict checking (should only be used with the
76  //! CUT_BANK_CHECKER macro)
77  //! @param tidx thread id in x dimension of block
78  //! @param tidy thread id in y dimension of block
79  //! @param tidz thread id in z dimension of block
80  //! @param bdimx block size in x dimension
81  //! @param bdimy block size in y dimension
82  //! @param bdimz block size in z dimension
83  //! @param file name of the source file where the access takes place
84  //! @param line line in the source file where the access takes place
85  //! @param aname name of the array which is accessed
86  //! @param index index into the array
87  ////////////////////////////////////////////////////////////////////////////
88  DLL_MAPPING
89  void CUTIL_API
90  cutCheckBankAccess( unsigned int tidx, unsigned int tidy, unsigned int tidz,
91  unsigned int bdimx, unsigned int bdimy,
92  unsigned int bdimz, const char* file, const int line,
93  const char* aname, const int index);
94 
95  ////////////////////////////////////////////////////////////////////////////
96  //! Find the path for a filename
97  //! @return the path if succeeded, otherwise 0
98  //! @param filename name of the file
99  //! @param executablePath optional absolute path of the executable
100  ////////////////////////////////////////////////////////////////////////////
101  DLL_MAPPING
102  char* CUTIL_API
103  cutFindFilePath(const char* filename, const char* executablePath);
104 
105  ////////////////////////////////////////////////////////////////////////////
106  //! Read file \filename containing single precision floating point data
107  //! @return CUTTrue if reading the file succeeded, otherwise false
108  //! @param filename name of the source file
109  //! @param data uninitialized pointer, returned initialized and pointing to
110  //! the data read
111  //! @param len number of data elements in data, -1 on error
112  //! @note If a NULL pointer is passed to this function and it is
113  //! initialized within Cutil then cutFree() has to be used to
114  //! deallocate the memory
115  ////////////////////////////////////////////////////////////////////////////
116  DLL_MAPPING
117  CUTBoolean CUTIL_API
118  cutReadFilef( const char* filename, float** data, unsigned int* len,
119  bool verbose = false);
120 
121  ////////////////////////////////////////////////////////////////////////////
122  //! Read file \filename containing double precision floating point data
123  //! @return CUTTrue if reading the file succeeded, otherwise false
124  //! @param filename name of the source file
125  //! @param data uninitialized pointer, returned initialized and pointing to
126  //! the data read
127  //! @param len number of data elements in data, -1 on error
128  //! @note If a NULL pointer is passed to this function and it is
129  //! initialized within Cutil then cutFree() has to be used to
130  //! deallocate the memory
131  ////////////////////////////////////////////////////////////////////////////
132  DLL_MAPPING
133  CUTBoolean CUTIL_API
134  cutReadFiled( const char* filename, double** data, unsigned int* len,
135  bool verbose = false);
136 
137  ////////////////////////////////////////////////////////////////////////////
138  //! Read file \filename containing integer data
139  //! @return CUTTrue if reading the file succeeded, otherwise false
140  //! @param filename name of the source file
141  //! @param data uninitialized pointer, returned initialized and pointing to
142  //! the data read
143  //! @param len number of data elements in data, -1 on error
144  //! @note If a NULL pointer is passed to this function and it is
145  //! initialized within Cutil then cutFree() has to be used to
146  //! deallocate the memory
147  ////////////////////////////////////////////////////////////////////////////
148  DLL_MAPPING
149  CUTBoolean CUTIL_API
150  cutReadFilei( const char* filename, int** data, unsigned int* len, bool verbose = false);
151 
152  ////////////////////////////////////////////////////////////////////////////
153  //! Read file \filename containing unsigned integer data
154  //! @return CUTTrue if reading the file succeeded, otherwise false
155  //! @param filename name of the source file
156  //! @param data uninitialized pointer, returned initialized and pointing to
157  //! the data read
158  //! @param len number of data elements in data, -1 on error
159  //! @note If a NULL pointer is passed to this function and it is
160  //! initialized within Cutil then cutFree() has to be used to
161  //! deallocate the memory
162  ////////////////////////////////////////////////////////////////////////////
163  DLL_MAPPING
164  CUTBoolean CUTIL_API
165  cutReadFileui( const char* filename, unsigned int** data,
166  unsigned int* len, bool verbose = false);
167 
168  ////////////////////////////////////////////////////////////////////////////
169  //! Read file \filename containing char / byte data
170  //! @return CUTTrue if reading the file succeeded, otherwise false
171  //! @param filename name of the source file
172  //! @param data uninitialized pointer, returned initialized and pointing to
173  //! the data read
174  //! @param len number of data elements in data, -1 on error
175  //! @note If a NULL pointer is passed to this function and it is
176  //! initialized within Cutil then cutFree() has to be used to
177  //! deallocate the memory
178  ////////////////////////////////////////////////////////////////////////////
179  DLL_MAPPING
180  CUTBoolean CUTIL_API
181  cutReadFileb( const char* filename, char** data, unsigned int* len,
182  bool verbose = false);
183 
184  ////////////////////////////////////////////////////////////////////////////
185  //! Read file \filename containing unsigned char / byte data
186  //! @return CUTTrue if reading the file succeeded, otherwise false
187  //! @param filename name of the source file
188  //! @param data uninitialized pointer, returned initialized and pointing to
189  //! the data read
190  //! @param len number of data elements in data, -1 on error
191  //! @note If a NULL pointer is passed to this function and it is
192  //! initialized within Cutil then cutFree() has to be used to
193  //! deallocate the memory
194  ////////////////////////////////////////////////////////////////////////////
195  DLL_MAPPING
196  CUTBoolean CUTIL_API
197  cutReadFileub( const char* filename, unsigned char** data,
198  unsigned int* len, bool verbose = false);
199 
200  ////////////////////////////////////////////////////////////////////////////
201  //! Write a data file \filename containing single precision floating point
202  //! data
203  //! @return CUTTrue if writing the file succeeded, otherwise false
204  //! @param filename name of the file to write
205  //! @param data pointer to data to write
206  //! @param len number of data elements in data, -1 on error
207  //! @param epsilon epsilon for comparison
208  ////////////////////////////////////////////////////////////////////////////
209  DLL_MAPPING
210  CUTBoolean CUTIL_API
211  cutWriteFilef( const char* filename, const float* data, unsigned int len,
212  const float epsilon, bool verbose = false);
213 
214  ////////////////////////////////////////////////////////////////////////////
215  //! Write a data file \filename containing double precision floating point
216  //! data
217  //! @return CUTTrue if writing the file succeeded, otherwise false
218  //! @param filename name of the file to write
219  //! @param data pointer to data to write
220  //! @param len number of data elements in data, -1 on error
221  //! @param epsilon epsilon for comparison
222  ////////////////////////////////////////////////////////////////////////////
223  DLL_MAPPING
224  CUTBoolean CUTIL_API
225  cutWriteFiled( const char* filename, const float* data, unsigned int len,
226  const double epsilon, bool verbose = false);
227 
228  ////////////////////////////////////////////////////////////////////////////
229  //! Write a data file \filename containing integer data
230  //! @return CUTTrue if writing the file succeeded, otherwise false
231  //! @param filename name of the file to write
232  //! @param data pointer to data to write
233  //! @param len number of data elements in data, -1 on error
234  ////////////////////////////////////////////////////////////////////////////
235  DLL_MAPPING
236  CUTBoolean CUTIL_API
237  cutWriteFilei( const char* filename, const int* data, unsigned int len,
238  bool verbose = false);
239 
240  ////////////////////////////////////////////////////////////////////////////
241  //! Write a data file \filename containing unsigned integer data
242  //! @return CUTTrue if writing the file succeeded, otherwise false
243  //! @param filename name of the file to write
244  //! @param data pointer to data to write
245  //! @param len number of data elements in data, -1 on error
246  ////////////////////////////////////////////////////////////////////////////
247  DLL_MAPPING
248  CUTBoolean CUTIL_API
249  cutWriteFileui( const char* filename,const unsigned int* data,
250  unsigned int len, bool verbose = false);
251 
252  ////////////////////////////////////////////////////////////////////////////
253  //! Write a data file \filename containing char / byte data
254  //! @return CUTTrue if writing the file succeeded, otherwise false
255  //! @param filename name of the file to write
256  //! @param data pointer to data to write
257  //! @param len number of data elements in data, -1 on error
258  ////////////////////////////////////////////////////////////////////////////
259  DLL_MAPPING
260  CUTBoolean CUTIL_API
261  cutWriteFileb( const char* filename, const char* data, unsigned int len,
262  bool verbose = false);
263 
264  ////////////////////////////////////////////////////////////////////////////
265  //! Write a data file \filename containing unsigned char / byte data
266  //! @return CUTTrue if writing the file succeeded, otherwise false
267  //! @param filename name of the file to write
268  //! @param data pointer to data to write
269  //! @param len number of data elements in data, -1 on error
270  ////////////////////////////////////////////////////////////////////////////
271  DLL_MAPPING
272  CUTBoolean CUTIL_API
273  cutWriteFileub( const char* filename,const unsigned char* data,
274  unsigned int len, bool verbose = false);
275 
276  ////////////////////////////////////////////////////////////////////////////
277  //! Load PGM image file (with unsigned char as data element type)
278  //! @return CUTTrue if reading the file succeeded, otherwise false
279  //! @param file name of the image file
280  //! @param data handle to the data read
281  //! @param w width of the image
282  //! @param h height of the image
283  //! @note If a NULL pointer is passed to this function and it is
284  //! initialized within Cutil then cutFree() has to be used to
285  //! deallocate the memory
286  ////////////////////////////////////////////////////////////////////////////
287  DLL_MAPPING
288  CUTBoolean CUTIL_API
289  cutLoadPGMub( const char* file, unsigned char** data,
290  unsigned int *w,unsigned int *h);
291 
292  ////////////////////////////////////////////////////////////////////////////
293  //! Load PPM image file (with unsigned char as data element type)
294  //! @return CUTTrue if reading the file succeeded, otherwise false
295  //! @param file name of the image file
296  //! @param data handle to the data read
297  //! @param w width of the image
298  //! @param h height of the image
299  ////////////////////////////////////////////////////////////////////////////
300  DLL_MAPPING
301  CUTBoolean CUTIL_API
302  cutLoadPPMub( const char* file, unsigned char** data,
303  unsigned int *w,unsigned int *h);
304 
305  ////////////////////////////////////////////////////////////////////////////
306  //! Load PPM image file (with unsigned char as data element type), padding
307  //! 4th component
308  //! @return CUTTrue if reading the file succeeded, otherwise false
309  //! @param file name of the image file
310  //! @param data handle to the data read
311  //! @param w width of the image
312  //! @param h height of the image
313  ////////////////////////////////////////////////////////////////////////////
314  DLL_MAPPING
315  CUTBoolean CUTIL_API
316  cutLoadPPM4ub( const char* file, unsigned char** data,
317  unsigned int *w,unsigned int *h);
318 
319  ////////////////////////////////////////////////////////////////////////////
320  //! Load PGM image file (with unsigned int as data element type)
321  //! @return CUTTrue if reading the file succeeded, otherwise false
322  //! @param file name of the image file
323  //! @param data handle to the data read
324  //! @param w width of the image
325  //! @param h height of the image
326  //! @note If a NULL pointer is passed to this function and it is
327  //! initialized within Cutil then cutFree() has to be used to
328  //! deallocate the memory
329  ////////////////////////////////////////////////////////////////////////////
330  DLL_MAPPING
331  CUTBoolean CUTIL_API
332  cutLoadPGMi( const char* file, unsigned int** data,
333  unsigned int* w, unsigned int* h);
334 
335  ////////////////////////////////////////////////////////////////////////////
336  //! Load PGM image file (with unsigned short as data element type)
337  //! @return CUTTrue if reading the file succeeded, otherwise false
338  //! @param file name of the image file
339  //! @param data handle to the data read
340  //! @param w width of the image
341  //! @param h height of the image
342  //! @note If a NULL pointer is passed to this function and it is
343  //! initialized within Cutil then cutFree() has to be used to
344  //! deallocate the memory
345  ////////////////////////////////////////////////////////////////////////////
346  DLL_MAPPING
347  CUTBoolean CUTIL_API
348  cutLoadPGMs( const char* file, unsigned short** data,
349  unsigned int* w, unsigned int* h);
350 
351  ////////////////////////////////////////////////////////////////////////////
352  //! Load PGM image file (with float as data element type)
353  //! @param file name of the image file
354  //! @param data handle to the data read
355  //! @param w width of the image
356  //! @param h height of the image
357  //! @note If a NULL pointer is passed to this function and it is
358  //! initialized within Cutil then cutFree() has to be used to
359  //! deallocate the memory
360  ////////////////////////////////////////////////////////////////////////////
361  DLL_MAPPING
362  CUTBoolean CUTIL_API
363  cutLoadPGMf( const char* file, float** data,
364  unsigned int* w, unsigned int* h);
365 
366  ////////////////////////////////////////////////////////////////////////////
367  //! Save PGM image file (with unsigned char as data element type)
368  //! @param file name of the image file
369  //! @param data handle to the data read
370  //! @param w width of the image
371  //! @param h height of the image
372  ////////////////////////////////////////////////////////////////////////////
373  DLL_MAPPING
374  CUTBoolean CUTIL_API
375  cutSavePGMub( const char* file, unsigned char* data,
376  unsigned int w, unsigned int h);
377 
378  ////////////////////////////////////////////////////////////////////////////
379  //! Save PPM image file (with unsigned char as data element type)
380  //! @param file name of the image file
381  //! @param data handle to the data read
382  //! @param w width of the image
383  //! @param h height of the image
384  ////////////////////////////////////////////////////////////////////////////
385  DLL_MAPPING
386  CUTBoolean CUTIL_API
387  cutSavePPMub( const char* file, unsigned char *data,
388  unsigned int w, unsigned int h);
389 
390  ////////////////////////////////////////////////////////////////////////////
391  //! Save PPM image file (with unsigned char as data element type, padded to
392  //! 4 bytes)
393  //! @param file name of the image file
394  //! @param data handle to the data read
395  //! @param w width of the image
396  //! @param h height of the image
397  ////////////////////////////////////////////////////////////////////////////
398  DLL_MAPPING
399  CUTBoolean CUTIL_API
400  cutSavePPM4ub( const char* file, unsigned char *data,
401  unsigned int w, unsigned int h);
402 
403  ////////////////////////////////////////////////////////////////////////////
404  //! Save PGM image file (with unsigned int as data element type)
405  //! @param file name of the image file
406  //! @param data handle to the data read
407  //! @param w width of the image
408  //! @param h height of the image
409  ////////////////////////////////////////////////////////////////////////////
410  DLL_MAPPING
411  CUTBoolean CUTIL_API
412  cutSavePGMi( const char* file, unsigned int* data,
413  unsigned int w, unsigned int h);
414 
415  ////////////////////////////////////////////////////////////////////////////
416  //! Save PGM image file (with unsigned short as data element type)
417  //! @param file name of the image file
418  //! @param data handle to the data read
419  //! @param w width of the image
420  //! @param h height of the image
421  ////////////////////////////////////////////////////////////////////////////
422  DLL_MAPPING
423  CUTBoolean CUTIL_API
424  cutSavePGMs( const char* file, unsigned short* data,
425  unsigned int w, unsigned int h);
426 
427  ////////////////////////////////////////////////////////////////////////////
428  //! Save PGM image file (with float as data element type)
429  //! @param file name of the image file
430  //! @param data handle to the data read
431  //! @param w width of the image
432  //! @param h height of the image
433  ////////////////////////////////////////////////////////////////////////////
434  DLL_MAPPING
435  CUTBoolean CUTIL_API
436  cutSavePGMf( const char* file, float* data,
437  unsigned int w, unsigned int h);
438 
439  ////////////////////////////////////////////////////////////////////////////
440  // Command line arguments: General notes
441  // * All command line arguments begin with '--' followed by the token;
442  // token and value are separated by '='; example --samples=50
443  // * Arrays have the form --model=[one.obj,two.obj,three.obj]
444  // (without whitespaces)
445  ////////////////////////////////////////////////////////////////////////////
446 
447  ////////////////////////////////////////////////////////////////////////////
448  //! Check if command line argument \a flag-name is given
449  //! @return CUTTrue if command line argument \a flag_name has been given,
450  //! otherwise 0
451  //! @param argc argc as passed to main()
452  //! @param argv argv as passed to main()
453  //! @param flag_name name of command line flag
454  ////////////////////////////////////////////////////////////////////////////
455  DLL_MAPPING
456  CUTBoolean CUTIL_API
457  cutCheckCmdLineFlag( const int argc, const char** argv,
458  const char* flag_name);
459 
460  ////////////////////////////////////////////////////////////////////////////
461  //! Get the value of a command line argument of type int
462  //! @return CUTTrue if command line argument \a arg_name has been given and
463  //! is of the requested type, otherwise CUTFalse
464  //! @param argc argc as passed to main()
465  //! @param argv argv as passed to main()
466  //! @param arg_name name of the command line argument
467  //! @param val value of the command line argument
468  ////////////////////////////////////////////////////////////////////////////
469  DLL_MAPPING
470  CUTBoolean CUTIL_API
471  cutGetCmdLineArgumenti( const int argc, const char** argv,
472  const char* arg_name, int* val);
473 
474  ////////////////////////////////////////////////////////////////////////////
475  //! Get the value of a command line argument of type float
476  //! @return CUTTrue if command line argument \a arg_name has been given and
477  //! is of the requested type, otherwise CUTFalse
478  //! @param argc argc as passed to main()
479  //! @param argv argv as passed to main()
480  //! @param arg_name name of the command line argument
481  //! @param val value of the command line argument
482  ////////////////////////////////////////////////////////////////////////////
483  DLL_MAPPING
484  CUTBoolean CUTIL_API
485  cutGetCmdLineArgumentf( const int argc, const char** argv,
486  const char* arg_name, float* val);
487 
488  ////////////////////////////////////////////////////////////////////////////
489  //! Get the value of a command line argument of type string
490  //! @return CUTTrue if command line argument \a arg_name has been given and
491  //! is of the requested type, otherwise CUTFalse
492  //! @param argc argc as passed to main()
493  //! @param argv argv as passed to main()
494  //! @param arg_name name of the command line argument
495  //! @param val value of the command line argument
496  ////////////////////////////////////////////////////////////////////////////
497  DLL_MAPPING
498  CUTBoolean CUTIL_API
499  cutGetCmdLineArgumentstr( const int argc, const char** argv,
500  const char* arg_name, char** val);
501 
502  ////////////////////////////////////////////////////////////////////////////
503  //! Get the value of a command line argument list those element are strings
504  //! @return CUTTrue if command line argument \a arg_name has been given and
505  //! is of the requested type, otherwise CUTFalse
506  //! @param argc argc as passed to main()
507  //! @param argv argv as passed to main()
508  //! @param arg_name name of the command line argument
509  //! @param val command line argument list
510  //! @param len length of the list / number of elements
511  ////////////////////////////////////////////////////////////////////////////
512  DLL_MAPPING
513  CUTBoolean CUTIL_API
514  cutGetCmdLineArgumentListstr( const int argc, const char** argv,
515  const char* arg_name, char** val,
516  unsigned int* len);
517 
518  ////////////////////////////////////////////////////////////////////////////
519  //! Extended assert
520  //! @return CUTTrue if the condition \a val holds, otherwise CUTFalse
521  //! @param val condition to test
522  //! @param file __FILE__ macro
523  //! @param line __LINE__ macro
524  //! @note This function should be used via the CONDITION(val) macro
525  ////////////////////////////////////////////////////////////////////////////
526  DLL_MAPPING
527  CUTBoolean CUTIL_API
528  cutCheckCondition( int val, const char* file, const int line);
529 
530  ////////////////////////////////////////////////////////////////////////////
531  //! Compare two float arrays
532  //! @return CUTTrue if \a reference and \a data are identical,
533  //! otherwise CUTFalse
534  //! @param reference handle to the reference data / gold image
535  //! @param data handle to the computed data
536  //! @param len number of elements in reference and data
537  ////////////////////////////////////////////////////////////////////////////
538  DLL_MAPPING
539  CUTBoolean CUTIL_API
540  cutComparef( const float* reference, const float* data,
541  const unsigned int len);
542 
543  ////////////////////////////////////////////////////////////////////////////
544  //! Compare two integer arrays
545  //! @return CUTTrue if \a reference and \a data are identical,
546  //! otherwise CUTFalse
547  //! @param reference handle to the reference data / gold image
548  //! @param data handle to the computed data
549  //! @param len number of elements in reference and data
550  ////////////////////////////////////////////////////////////////////////////
551  DLL_MAPPING
552  CUTBoolean CUTIL_API
553  cutComparei( const int* reference, const int* data,
554  const unsigned int len );
555 
556  ////////////////////////////////////////////////////////////////////////////////
557  //! Compare two unsigned integer arrays, with epsilon and threshold
558  //! @return CUTTrue if \a reference and \a data are identical,
559  //! otherwise CUTFalse
560  //! @param reference handle to the reference data / gold image
561  //! @param data handle to the computed data
562  //! @param len number of elements in reference and data
563  //! @param threshold tolerance % # of comparison errors (0.15f = 15%)
564  ////////////////////////////////////////////////////////////////////////////////
565  DLL_MAPPING
566  CUTBoolean CUTIL_API
567  cutCompareuit( const unsigned int* reference, const unsigned int* data,
568  const unsigned int len, const float epsilon, const float threshold );
569 
570  ////////////////////////////////////////////////////////////////////////////
571  //! Compare two unsigned char arrays
572  //! @return CUTTrue if \a reference and \a data are identical,
573  //! otherwise CUTFalse
574  //! @param reference handle to the reference data / gold image
575  //! @param data handle to the computed data
576  //! @param len number of elements in reference and data
577  ////////////////////////////////////////////////////////////////////////////
578  DLL_MAPPING
579  CUTBoolean CUTIL_API
580  cutCompareub( const unsigned char* reference, const unsigned char* data,
581  const unsigned int len );
582 
583  ////////////////////////////////////////////////////////////////////////////////
584  //! Compare two integers with a tolernance for # of byte errors
585  //! @return CUTTrue if \a reference and \a data are identical,
586  //! otherwise CUTFalse
587  //! @param reference handle to the reference data / gold image
588  //! @param data handle to the computed data
589  //! @param len number of elements in reference and data
590  //! @param epsilon epsilon to use for the comparison
591  //! @param threshold tolerance % # of comparison errors (0.15f = 15%)
592  ////////////////////////////////////////////////////////////////////////////////
593  DLL_MAPPING
594  CUTBoolean CUTIL_API
595  cutCompareubt( const unsigned char* reference, const unsigned char* data,
596  const unsigned int len, const float epsilon, const float threshold );
597 
598  ////////////////////////////////////////////////////////////////////////////////
599  //! Compare two integer arrays witha n epsilon tolerance for equality
600  //! @return CUTTrue if \a reference and \a data are identical,
601  //! otherwise CUTFalse
602  //! @param reference handle to the reference data / gold image
603  //! @param data handle to the computed data
604  //! @param len number of elements in reference and data
605  //! @param epsilon epsilon to use for the comparison
606  ////////////////////////////////////////////////////////////////////////////////
607  DLL_MAPPING
608  CUTBoolean CUTIL_API
609  cutCompareube( const unsigned char* reference, const unsigned char* data,
610  const unsigned int len, const float epsilon );
611 
612  ////////////////////////////////////////////////////////////////////////////
613  //! Compare two float arrays with an epsilon tolerance for equality
614  //! @return CUTTrue if \a reference and \a data are identical,
615  //! otherwise CUTFalse
616  //! @param reference handle to the reference data / gold image
617  //! @param data handle to the computed data
618  //! @param len number of elements in reference and data
619  //! @param epsilon epsilon to use for the comparison
620  ////////////////////////////////////////////////////////////////////////////
621  DLL_MAPPING
622  CUTBoolean CUTIL_API
623  cutComparefe( const float* reference, const float* data,
624  const unsigned int len, const float epsilon );
625 
626  ////////////////////////////////////////////////////////////////////////////////
627  //! Compare two float arrays with an epsilon tolerance for equality and a
628  //! threshold for # pixel errors
629  //! @return CUTTrue if \a reference and \a data are identical,
630  //! otherwise CUTFalse
631  //! @param reference handle to the reference data / gold image
632  //! @param data handle to the computed data
633  //! @param len number of elements in reference and data
634  //! @param epsilon epsilon to use for the comparison
635  ////////////////////////////////////////////////////////////////////////////////
636  DLL_MAPPING
637  CUTBoolean CUTIL_API
638  cutComparefet( const float* reference, const float* data,
639  const unsigned int len, const float epsilon, const float threshold );
640 
641  ////////////////////////////////////////////////////////////////////////////
642  //! Compare two float arrays using L2-norm with an epsilon tolerance for
643  //! equality
644  //! @return CUTTrue if \a reference and \a data are identical,
645  //! otherwise CUTFalse
646  //! @param reference handle to the reference data / gold image
647  //! @param data handle to the computed data
648  //! @param len number of elements in reference and data
649  //! @param epsilon epsilon to use for the comparison
650  ////////////////////////////////////////////////////////////////////////////
651  DLL_MAPPING
652  CUTBoolean CUTIL_API
653  cutCompareL2fe( const float* reference, const float* data,
654  const unsigned int len, const float epsilon );
655 
656  ////////////////////////////////////////////////////////////////////////////////
657  //! Compare two PPM image files with an epsilon tolerance for equality
658  //! @return CUTTrue if \a reference and \a data are identical,
659  //! otherwise CUTFalse
660  //! @param src_file filename for the image to be compared
661  //! @param data filename for the reference data / gold image
662  //! @param epsilon epsilon to use for the comparison
663  //! @param threshold threshold of pixels that can still mismatch to pass (i.e. 0.15f = 15% must pass)
664  //! $param verboseErrors output details of image mismatch to std::err
665  ////////////////////////////////////////////////////////////////////////////////
666  DLL_MAPPING
667  CUTBoolean CUTIL_API
668  cutComparePPM( const char *src_file, const char *ref_file, const float epsilon, const float threshold, bool verboseErrors = false );
669 
670 
671  ////////////////////////////////////////////////////////////////////////////
672  //! Timer functionality
673 
674  ////////////////////////////////////////////////////////////////////////////
675  //! Create a new timer
676  //! @return CUTTrue if a time has been created, otherwise false
677  //! @param name of the new timer, 0 if the creation failed
678  ////////////////////////////////////////////////////////////////////////////
679  DLL_MAPPING
680  CUTBoolean CUTIL_API
681  cutCreateTimer( unsigned int* name);
682 
683  ////////////////////////////////////////////////////////////////////////////
684  //! Delete a timer
685  //! @return CUTTrue if a time has been deleted, otherwise false
686  //! @param name of the timer to delete
687  ////////////////////////////////////////////////////////////////////////////
688  DLL_MAPPING
689  CUTBoolean CUTIL_API
690  cutDeleteTimer( unsigned int name);
691 
692  ////////////////////////////////////////////////////////////////////////////
693  //! Start the time with name \a name
694  //! @param name name of the timer to start
695  ////////////////////////////////////////////////////////////////////////////
696  DLL_MAPPING
697  CUTBoolean CUTIL_API
698  cutStartTimer( const unsigned int name);
699 
700  ////////////////////////////////////////////////////////////////////////////
701  //! Stop the time with name \a name. Does not reset.
702  //! @param name name of the timer to stop
703  ////////////////////////////////////////////////////////////////////////////
704  DLL_MAPPING
705  CUTBoolean CUTIL_API
706  cutStopTimer( const unsigned int name);
707 
708  ////////////////////////////////////////////////////////////////////////////
709  //! Resets the timer's counter.
710  //! @param name name of the timer to reset.
711  ////////////////////////////////////////////////////////////////////////////
712  DLL_MAPPING
713  CUTBoolean CUTIL_API
714  cutResetTimer( const unsigned int name);
715 
716  ////////////////////////////////////////////////////////////////////////////
717  //! Returns total execution time in milliseconds for the timer over all
718  //! runs since the last reset or timer creation.
719  //! @param name name of the timer to return the time of
720  ////////////////////////////////////////////////////////////////////////////
721  DLL_MAPPING
722  float CUTIL_API
723  cutGetTimerValue( const unsigned int name);
724 
725  ////////////////////////////////////////////////////////////////////////////
726  //! Return the average time in milliseconds for timer execution as the
727  //! total time for the timer dividied by the number of completed (stopped)
728  //! runs the timer has made.
729  //! Excludes the current running time if the timer is currently running.
730  //! @param name name of the timer to return the time of
731  ////////////////////////////////////////////////////////////////////////////
732  DLL_MAPPING
733  float CUTIL_API
734  cutGetAverageTimerValue( const unsigned int name);
735 
736  ////////////////////////////////////////////////////////////////////////////
737  //! Macros
738 
739 #if CUDART_VERSION >= 4000
740 #define CUT_DEVICE_SYNCHRONIZE( ) cudaDeviceSynchronize();
741 #else
742 #define CUT_DEVICE_SYNCHRONIZE( ) cudaThreadSynchronize();
743 #endif
744 
745 #if CUDART_VERSION >= 4000
746 #define CUT_DEVICE_RESET( ) cudaDeviceReset();
747 #else
748 #define CUT_DEVICE_RESET( ) cudaThreadExit();
749 #endif
750 
751 // This is for the CUTIL bank checker
752 #ifdef _DEBUG
753  #if __DEVICE_EMULATION__
754  // Interface for bank conflict checker
755  #define CUT_BANK_CHECKER( array, index) \
756  (cutCheckBankAccess( threadIdx.x, threadIdx.y, threadIdx.z, blockDim.x, \
757  blockDim.y, blockDim.z, \
758  __FILE__, __LINE__, #array, index ), \
759  array[index])
760  #else
761  #define CUT_BANK_CHECKER( array, index) array[index]
762  #endif
763 #else
764  #define CUT_BANK_CHECKER( array, index) array[index]
765 #endif
766 
767 # define CU_SAFE_CALL_NO_SYNC( call ) { \
768  CUresult err = call; \
769  if( CUDA_SUCCESS != err) { \
770  fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \
771  err, __FILE__, __LINE__ ); \
772  exit(EXIT_FAILURE); \
773  } }
774 
775 # define CU_SAFE_CALL( call ) CU_SAFE_CALL_NO_SYNC(call);
776 
777 # define CU_SAFE_CTX_SYNC( ) { \
778  CUresult err = cuCtxSynchronize(); \
779  if( CUDA_SUCCESS != err) { \
780  fprintf(stderr, "Cuda driver error %x in file '%s' in line %i.\n", \
781  err, __FILE__, __LINE__ ); \
782  exit(EXIT_FAILURE); \
783  } }
784 
785 # define CUDA_SAFE_CALL_NO_SYNC( call) { \
786  cudaError err = call; \
787  if( cudaSuccess != err) { \
788  fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
789  __FILE__, __LINE__, cudaGetErrorString( err) ); \
790  exit(EXIT_FAILURE); \
791  } }
792 
793 # define CUDA_SAFE_CALL( call) CUDA_SAFE_CALL_NO_SYNC(call); \
794 
795 # define CUDA_SAFE_THREAD_SYNC( ) { \
796  cudaError err = CUT_DEVICE_SYNCHRONIZE(); \
797  if ( cudaSuccess != err) { \
798  fprintf(stderr, "Cuda error in file '%s' in line %i : %s.\n", \
799  __FILE__, __LINE__, cudaGetErrorString( err) ); \
800  } }
801 
802 # define CUFFT_SAFE_CALL( call) { \
803  cufftResult err = call; \
804  if( CUFFT_SUCCESS != err) { \
805  fprintf(stderr, "CUFFT error in file '%s' in line %i.\n", \
806  __FILE__, __LINE__); \
807  exit(EXIT_FAILURE); \
808  } }
809 
810 # define CUT_SAFE_CALL( call) \
811  if( CUTTrue != call) { \
812  fprintf(stderr, "Cut error in file '%s' in line %i.\n", \
813  __FILE__, __LINE__); \
814  exit(EXIT_FAILURE); \
815  }
816 
817  //! Check for CUDA error
818 #ifdef _DEBUG
819 # define CUT_CHECK_ERROR(errorMessage) { \
820  cudaError_t err = cudaGetLastError(); \
821  if( cudaSuccess != err) { \
822  fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
823  errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
824  exit(EXIT_FAILURE); \
825  } \
826  err = CUT_DEVICE_SYNCHRONIZE(); \
827  if( cudaSuccess != err) { \
828  fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
829  errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
830  exit(EXIT_FAILURE); \
831  } \
832  }
833 #else
834 # define CUT_CHECK_ERROR(errorMessage) { \
835  cudaError_t err = cudaGetLastError(); \
836  if( cudaSuccess != err) { \
837  fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
838  errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
839  exit(EXIT_FAILURE); \
840  } \
841  }
842 #endif
843 
844  //! Check for malloc error
845 # define CUT_SAFE_MALLOC( mallocCall ) { \
846  if( !(mallocCall)) { \
847  fprintf(stderr, "Host malloc failure in file '%s' in line %i\n", \
848  __FILE__, __LINE__); \
849  exit(EXIT_FAILURE); \
850  } } while(0);
851 
852  //! Check if condition is true (flexible assert)
853 # define CUT_CONDITION( val) \
854  if( CUTFalse == cutCheckCondition( val, __FILE__, __LINE__)) { \
855  exit(EXIT_FAILURE); \
856  }
857 
858 #if __DEVICE_EMULATION__
859 
860 # define CUT_DEVICE_INIT(ARGC, ARGV)
861 
862 #else
863 
864 # define CUT_DEVICE_INIT(ARGC, ARGV) { \
865  int deviceCount; \
866  CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceCount(&deviceCount)); \
867  if (deviceCount == 0) { \
868  fprintf(stderr, "cutil error: no devices supporting CUDA.\n"); \
869  exit(EXIT_FAILURE); \
870  } \
871  int dev = 0; \
872  cutGetCmdLineArgumenti(ARGC, (const char **) ARGV, "device", &dev); \
873  if (dev < 0) dev = 0; \
874  if (dev > deviceCount-1) dev = deviceCount - 1; \
875  cudaDeviceProp deviceProp; \
876  CUDA_SAFE_CALL_NO_SYNC(cudaGetDeviceProperties(&deviceProp, dev)); \
877  if (deviceProp.major < 1) { \
878  fprintf(stderr, "cutil error: device does not support CUDA.\n"); \
879  exit(EXIT_FAILURE); \
880  } \
881  if (cutCheckCmdLineFlag(ARGC, (const char **) ARGV, "quiet") == CUTFalse) \
882  fprintf(stderr, "Using device %d: %s\n", dev, deviceProp.name); \
883  CUDA_SAFE_CALL(cudaSetDevice(dev)); \
884 }
885 
886 
887  //! Check for CUDA context lost
888 # define CUDA_CHECK_CTX_LOST(errorMessage) { \
889  cudaError_t err = cudaGetLastError(); \
890  if( cudaSuccess != err) { \
891  fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
892  errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
893  exit(EXIT_FAILURE); \
894  } \
895  err = CUT_DEVICE_SYNCHRONIZE(); \
896  if( cudaSuccess != err) { \
897  fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
898  errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
899  exit(EXIT_FAILURE); \
900  } }
901 
902 //! Check for CUDA context lost
903 # define CU_CHECK_CTX_LOST(errorMessage) { \
904  cudaError_t err = cudaGetLastError(); \
905  if( CUDA_ERROR_INVALID_CONTEXT != err) { \
906  fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
907  errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
908  exit(EXIT_FAILURE); \
909  } \
910  err = CUT_DEVICE_SYNCHRONIZE(); \
911  if( cudaSuccess != err) { \
912  fprintf(stderr, "Cuda error: %s in file '%s' in line %i : %s.\n", \
913  errorMessage, __FILE__, __LINE__, cudaGetErrorString( err) );\
914  exit(EXIT_FAILURE); \
915  } }
916 
917 
918 #endif
919 
920 # define CUT_DEVICE_INIT_DRV(cuDevice, ARGC, ARGV) { \
921  cuDevice = 0; \
922  int deviceCount = 0; \
923  CUresult err = cuInit(0); \
924  if (CUDA_SUCCESS == err) \
925  CU_SAFE_CALL_NO_SYNC(cuDeviceGetCount(&deviceCount)); \
926  if (deviceCount == 0) { \
927  fprintf(stderr, "cutil error: no devices supporting CUDA\n"); \
928  exit(EXIT_FAILURE); \
929  } \
930  int dev = 0; \
931  cutGetCmdLineArgumenti(ARGC, (const char **) ARGV, "device", &dev); \
932  if (dev < 0) dev = 0; \
933  if (dev > deviceCount-1) dev = deviceCount - 1; \
934  CU_SAFE_CALL_NO_SYNC(cuDeviceGet(&cuDevice, dev)); \
935  char name[100]; \
936  cuDeviceGetName(name, 100, cuDevice); \
937  if (cutCheckCmdLineFlag(ARGC, (const char **) ARGV, "quiet") == CUTFalse) \
938  fprintf(stderr, "Using device %d: %s\n", dev, name); \
939 }
940 
941 #define CUT_EXIT(argc, argv) \
942  if (!cutCheckCmdLineFlag(argc, (const char**)argv, "noprompt")) { \
943  printf("\nPress ENTER to exit...\n"); \
944  fflush( stdout); \
945  fflush( stderr); \
946  getchar(); \
947  } \
948  exit(EXIT_SUCCESS);
949 
950 
951 #ifdef __cplusplus
952 }
953 #endif // #ifdef _DEBUG (else branch)
954 
955 #endif // #ifndef _CUTIL_H_