common.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00047 #ifndef BASE_COMMON_H
00048 #define BASE_COMMON_H
00049 
00050 #ifndef _REENTRANT
00051 #define _REENTRANT
00052 #endif
00053 
00054 #include "fastlib/base/basic_types.h" /*generated by build*/
00055 #include "fastlib/base/compiler.h"
00056 #include "fastlib/base/ansi_colors.h"
00057 //#include "base/basic_types.h" /*generated by build*/
00058 //#include "compiler.h"
00059 //#include "ansi_colors.h"
00060 
00061 #include <stdlib.h>
00062 #include <stdio.h>
00063 #include <string.h>
00064 #include <ctype.h>
00065 #include <math.h>
00066 #include <limits.h>
00067 #include <float.h>
00068 
00069 EXTERN_C_BEGIN
00070 
00072 #define NOP ((void)0)
00073 
00075 #define COMMA ,
00076 
00077 
00078 
00079 /* Types and definitions to assist managment of problem scale. */
00080 
00081 /* Ensure that one and only one problem scale is selected. */
00082 #if defined(SCALE_MASSIVE)
00083 #if defined(SCALE_LARGE) || defined(SCALE_NORMAL)
00084 #error Only one of SCALE_MASSIVE, SCALE_LARGE, or SCALE_NORMAL may be defined.
00085 #endif
00086 #elif defined(SCALE_LARGE) 
00087 #if defined(SCALE_NORMAL)
00088 #error Only one of SCALE_MASSIVE, SCALE_LARGE, or SCALE_NORMAL may be defined.
00089 #endif
00090 #elif !defined(SCALE_NORMAL)
00091 #define SCALE_NORMAL
00092 #endif
00093 
00112 #if defined(SCALE_MASSIVE)
00113 typedef int64 index_t;          /* For larger than RAM data sets. */
00114 #elif defined(SCALE_LARGE)
00115 typedef ssize_t index_t;        /* As large as this machine can handle. */
00116 #elif defined(SCALE_NORMAL)
00117 typedef int index_t;            /* Normal sized data; usually 32-bit. */
00118 #endif
00119 
00129 #if defined(SCALE_MASSIVE)
00130 #define LI L64
00131 #elif defined(SCALE_LARGE)
00132 #define LI "l"                  /* TODO: confirm correct. */
00133 #elif defined(SCALE_NORMAL)
00134 #define LI ""
00135 #endif
00136 
00138 #define KILOBYTE (((size_t)1) << 10)
00139 
00140 #define MEGABYTE (((size_t)1) << 20)
00141 
00142 #define GIGABYTE (((size_t)1) << 30)
00143 /* Add more of these as necessary. */
00144 
00145 
00146 
00147 /* Tools for FASTlib stderr messages, warnings, and errors. */
00148 
00150 extern int segfault_on_abort;
00152 extern int abort_on_nonfatal;
00154 extern int pause_on_nonfatal;
00156 extern int print_notify_locs;
00157 
00159 typedef enum {
00161   FL_MSG_FATAL = 0,
00163   FL_MSG_NONFATAL = 1,
00165   FL_MSG_NOTIFY_STAR = 2,
00167   FL_MSG_NOTIFY = 3
00168 } fl_msg_t;
00169 
00171 extern char fl_msg_marker[];
00173 extern const char *fl_msg_color[];
00174 
00181 COMPILER_NO_RETURN
00182 void fl_abort(void);
00183 
00189 void fl_pause(void);
00190 
00192 void fl_print_msg_header(char marker, const char *color);
00193 
00195 void fl_print_msg_loc(const char *file, const char *func, int line);
00196 
00198 COMPILER_NO_RETURN
00199 COMPILER_PRINTF(4, 5)
00200 void fl_print_fatal_msg(const char *file, const char *func, int line,
00201                         const char* format, ...);
00202 
00204 COMPILER_PRINTF(5, 6)
00205 void fl_print_msg(const char *file, const char *func, int line,
00206                   fl_msg_t msg_type, const char* format, ...);
00207 
00213 #define FATAL(msg_params...) (fl_print_fatal_msg( \
00214     __FILE__, __FUNCTION__, __LINE__, msg_params))
00215 
00222 #define NONFATAL(msg_params...) (fl_print_msg( \
00223     __FILE__, __FUNCTION__, __LINE__, FL_MSG_NONFATAL, msg_params))
00224 
00231 #define NOTIFY_STAR(msg_params...) (fl_print_msg( \
00232     __FILE__, __FUNCTION__, __LINE__, FL_MSG_NOTIFY_STAR, msg_params))
00233 
00240 #define NOTIFY(msg_params...) (fl_print_msg( \
00241     __FILE__, __FUNCTION__, __LINE__, FL_MSG_NOTIFY, msg_params))
00242 
00255 void fl_print_progress(const char *name, int perc);
00256 
00257 
00258 
00268 void hex_to_stream(FILE *stream, const char *src, const char *ok_char);
00269 
00284 char *hex_to_string(char *dest, const char *src, const char *ok_char);
00285 
00297 char *unhex_in_place(char *str);
00298 
00299 
00300 
00301 /* Tools for expressing success or failure of FASTlib functions. */
00302 
00322 typedef enum {
00324   SUCCESS_FAIL = 31,
00326   SUCCESS_WARN = 48,
00328   SUCCESS_PASS = 96
00329 } success_t;
00330 
00336 #define PASSED(x) (likely((x) >= SUCCESS_PASS))
00337 
00343 #define FAILED(x) (unlikely((x) <= SUCCESS_FAIL))
00344 
00351 #define MUST_PASS_MSG(x, msg_params...) \
00352     (likely(x >= SUCCESS_PASS) ? NOP : FATAL(msg_params))
00353 
00360 #define MUST_PASS(x) \
00361     MUST_PASS_MSG(x, "MUST_PASS failed: %s", #x)
00362 
00369 #define MUST_NOT_FAIL_MSG(x, msg_params...) \
00370     (likely(x > SUCCESS_FAIL) ? NOP : FATAL(msg_params))
00371 
00378 #define MUST_NOT_FAIL(x) \
00379     MUST_NOT_FAIL_MSG(x, "MUST_NOT_FAIL failed: %s", #x)
00380 
00382 #define SUCCESS_FROM_C(x) (unlikely((x) < 0) ? SUCCESS_FAIL : SUCCESS_PASS)
00383 
00384 
00385 
00386 EXTERN_C_END
00387 
00388 #endif /* BASE_COMMON_H */
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3