Dip  0.92.4
sym_macros.h
Go to the documentation of this file.
1 /*===========================================================================*/
2 /* */
3 /* This file is part of the SYMPHONY MILP Solver Framework. */
4 /* */
5 /* SYMPHONY was jointly developed by Ted Ralphs (ted@lehigh.edu) and */
6 /* Laci Ladanyi (ladanyi@us.ibm.com). */
7 /* */
8 /* (c) Copyright 2000-2019 Ted Ralphs. All Rights Reserved. */
9 /* */
10 /* This software is licensed under the Eclipse Public License. Please see */
11 /* accompanying file for terms. */
12 /* */
13 /*===========================================================================*/
14 
15 #ifndef _BB_MACROS_H
16 #define _BB_MACROS_H
17 
18 /*-------------------------- Random number generator ------------------------*/
19 
20 #if defined(_MSC_VER) || defined (__MNO_CYGWIN)/* Different function call in
21  Windows */
22 #define SRANDOM(seed) srand(seed)
23 #define RANDOM() rand()
24 #else
25 #define SRANDOM(seed) srandom(seed)
26 #define RANDOM() random()
27 #endif
28 
29 /*---------------------------- Allocation macros ----------------------------*/
30 
31 #ifdef REMALLOC
32 #undef REMALLOC
33 #endif
34 #define REMALLOC(ptr, ptrtype, oldsize, newsize, block_size) \
35 { \
36  if (!ptr || (oldsize < newsize)){ \
37  FREE(ptr); \
38  oldsize = newsize + (int)(block_size); \
39  ptr = (ptrtype *) malloc((size_t)(oldsize) * sizeof(ptrtype)); \
40  } \
41 }
42 
43 #ifdef REALLOC
44 #undef REALLOC
45 #endif
46 #define REALLOC(ptr, ptrtype, oldsize, newsize, block_size) \
47 { \
48  if (!ptr || (oldsize < newsize)){ \
49  oldsize = newsize + (int)(block_size); \
50  ptr = (ptrtype *) realloc((char *)ptr, (size_t) \
51  (oldsize * sizeof(ptrtype))); \
52  } \
53 }
54 
55 /*---------------------------- PVM macros -----------------------------------*/
56 
57 #define READ_INT_DESC(desc) \
58 { \
59  receive_int_array(&(desc).size, 1); \
60  if ((desc).size > 0){ \
61  REMALLOC((desc).list, int, (desc).maxsize, (desc).size, BB_BUNCH); \
62  receive_int_array((desc).list, (desc).size); \
63  } \
64 }
65 
66 #define READ_CHAR_ARRAY_WITH_SIZE(cptr, cnum, maxcnum) \
67 { \
68  receive_int_array(&cnum, 1); \
69  if (cnum > 0){ \
70  REMALLOC(cptr, char, maxcnum, cnum, BB_BUNCH); \
71  receive_char_array(cptr, cnum); \
72  } \
73 }
74 
75 #define READ_STR_LIST(snum, ssize, cptr, sptr) \
76 { \
77  if (snum > 0){ \
78  sptr = (char **) malloc(snum * sizeof(char *)); \
79  cptr = (char *) malloc(ssize * snum * CSIZE); \
80  receive_char_array(cptr, snum * ssize); \
81  for (i = 0; i < snum; i++) \
82  sptr[i] = cptr + i * ssize; \
83  } \
84 }
85 
86 
87 /*--------------------- Parameter reading macros ----------------------------*/
88 
89 #define READPAR_ERROR(x) \
90 { \
91  (void) fprintf(stderr, "\nio: error reading parameter %s\n\n", x); \
92  exit(1); \
93 }
94 
95 #define READ_INT_PAR(par) \
96 if (sscanf(value, "%i", &(par)) != 1){ \
97  (void) fprintf(stderr, "\nio: error reading parameter %s\n\n", key); \
98  exit(1); \
99 }
100 
101 #define READ_STR_PAR(par) \
102 if (sscanf(value, "%s", par) != 1){ \
103  (void) fprintf(stderr, "\nio: error reading parameter %s\n\n", key); \
104  exit(1); \
105 }
106 
107 #define READ_DBL_PAR(par) \
108 if (sscanf(value, "%lf", &(par)) != 1){ \
109  (void) fprintf(stderr, "\nio: error reading parameter %s\n\n", key); \
110  exit(1); \
111 }
112 
113 #define READ_STRINT_PAR(par, str_array, array_size, value) \
114 { \
115  for (i = array_size-1; i >= 0; i--){ \
116  if (! strcmp(str_array[i].str, value)){ \
117  par |= str_array[i].code; \
118  break; \
119  } \
120  } \
121  if (i < 0){ \
122  (void) fprintf(stderr, "\nio: error reading parameter %s\n\n", key); \
123  exit(1); \
124  } \
125 }
126 
127 /*------------------------ Copying macros -----------------------------------*/
128 
129 #define COPY_DBL_ARRAY_DESC(newad, oldad) \
130 if (newad.size > 0){ \
131  newad.stat = (int *) malloc(newad.size*ISIZE); \
132  memcpy((char *)newad.stat, (char *)oldad.stat, oldad.size*ISIZE); \
133  if (newad.type == WRT_PARENT){ \
134  newad.list = (int *) malloc(oldad.size*ISIZE); \
135  memcpy((char *)newad.list, (char *)oldad.list, oldad.size*ISIZE); \
136  } \
137 }
138 
139 #define COPY_STAT(newad, oldad) \
140 if (newad.size > 0){ \
141  newad.stat = (int *) malloc(newad.size*ISIZE); \
142  memcpy((char *)newad.stat, (char *)oldad.stat, oldad.size*ISIZE); \
143 }
144 
145 #define COPY_ARRAY_DESC(newad, oldad) \
146 newad = oldad; \
147 if (newad.size > 0){ \
148  newad.list = (int *) malloc(oldad.size*ISIZE); \
149  memcpy((char *)newad.list, (char *)oldad.list, oldad.size*ISIZE); \
150 }
151 
152 /*--------------- Macro for calling user functions --------------------------*/
153 
154 #define CALL_USER_FUNCTION(f) \
155 switch (f){ \
156  case USER_ERROR: \
157  printf("\n\n*********User error detected -- aborting***********\n\n"); \
158  return(ERROR__USER); \
159  default: \
160  break; \
161 }
162 
163 /*------------- Macro for calling wrapper functions -------------------------*/
164 
165 #define CALL_WRAPPER_FUNCTION(f) \
166 if ((termcode = f) < 0) \
167  return(termcode);
168 
169 /*---------------------- Standard macros ------------------------------------*/
170 
171 #ifdef PRINT
172 #undef PRINT
173 #endif
174 #define PRINT(a, b, c) \
175  if ((a) > (b)) printf c
176 
177 #ifdef FREE
178 #undef FREE
179 #endif
180 #define FREE(p) if (p) {(void)free((char *)(p)); p = NULL;}
181 
182 #ifndef MIN
183 #undef MIN
184 #define MIN(a,b) ((a) < (b) ? (a) : (b))
185 #endif
186 
187 #ifdef MAX
188 #undef MAX
189 #endif
190 #define MAX(a,b) ((a) > (b) ? (a) : (b))
191 
192 
193 #ifdef isset
194 # undef isset
195 # undef isclr
196 #endif
197 #ifdef ISSET_WITH_NBBY
198 # ifndef NBBY
199 # define NBBY 8
200 # endif
201 # define isset(a, i) (((a)[(i)/NBBY] >> ((i) % NBBY)) & 1)
202 #else
203 # define LOG_OF_BITS_PER_BYTE 3
204 # define BITS_PER_BYTE_LESS_ONE 7
205 # define isset(a, i) (((a)[(i) >> LOG_OF_BITS_PER_BYTE] >> \
206  ((i) & BITS_PER_BYTE_LESS_ONE)) & 1)
207 #endif
208 #define isclr(a, i) (! isset(a, i))
209 
210 #ifdef setbit
211 # undef setbit
212 #endif
213 #ifdef ISSET_WITH_NBBY
214 # ifndef NBBY
215 # define NBBY 8
216 # endif
217 # define setbit(a, i) ((a)[(i)/NBBY] |= (1 << ((i) % NBBY)))
218 #else
219 # ifndef LOG_OF_BITS_PER_BYTE
220 # define LOG_OF_BITS_PER_BYTE 3
221 # define BITS_PER_BYTE_LESS_ONE 7
222 # endif
223 # define setbit(a, i) ((a)[(i) >> LOG_OF_BITS_PER_BYTE] |= \
224  (1 << ((i) & BITS_PER_BYTE_LESS_ONE)))
225 #endif
226 
227 #ifndef _MSC_VER
228 #if _OPENMP >= 201107
229 #define OPENMP_ATOMIC_WRITE _Pragma("omp atomic write")
230 #else
231 #define OPENMP_ATOMIC_WRITE _Pragma("omp critical (atomic_write)")
232 #endif
233 #else
234 #if _OPENMP >= 201107
235 #define OPENMP_ATOMIC_WRITE __pragma("omp atomic write")
236 #else
237 #define OPENMP_ATOMIC_WRITE __pragma("omp critical (atomic_write)")
238 #endif
239 #endif
240 
241 #ifndef _MSC_VER
242 #if _OPENMP >= 201107
243 #define OPENMP_ATOMIC_UPDATE _Pragma("omp atomic update")
244 #else
245 #define OPENMP_ATOMIC_UPDATE _Pragma("omp atomic")
246 #endif
247 #else
248 #if _OPENMP >= 201107
249 #define OPENMP_ATOMIC_UPDATE __pragma("omp atomic update")
250 #else
251 #define OPENMP_ATOMIC_UPDATE __pragma("omp atomic")
252 #endif
253 #endif
254 
255 #endif