1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
// Start of ispc_util.h.
// This header file implements various operations that are useful only when
// generating ISPC code. This includes wrappers for parts of Futhark's C runtime.
// Expose gang size
export uniform int64_t get_gang_size() {
return programCount;
}
// Generate missing overloads for extract on pointers
#define make_extract(ty) \
static inline uniform ty * uniform extract(uniform ty * varying ptr, uniform int idx) { \
int64 c = (int64)ptr; \
uniform int64 r = extract(c, idx); \
return (uniform ty * uniform)r; \
}
make_extract(int8)
make_extract(int16)
make_extract(int32)
make_extract(int64)
make_extract(uint8)
make_extract(uint16)
make_extract(uint32)
make_extract(uint64)
make_extract(float16)
make_extract(float)
make_extract(double)
/* make_extract(int8* uniform) */
/* make_extract(int16* uniform) */
/* make_extract(int32* uniform) */
/* make_extract(int64* uniform) */
/* make_extract(uint8* uniform) */
/* make_extract(uint16* uniform) */
/* make_extract(uint32* uniform) */
/* make_extract(uint64* uniform) */
/* make_extract(float16* uniform) */
/* make_extract(float* uniform) */
/* make_extract(double* uniform) */
make_extract(struct futhark_context)
make_extract(struct memblock)
// Handling of atomics
// Atomic CAS acts differently in GCC and ISPC, so we emulate it.
#define make_atomic_compare_exchange_wrapper(ty) \
static inline uniform bool atomic_compare_exchange_wrapper(uniform ty * uniform mem, \
uniform ty * uniform old, \
const uniform ty val){ \
uniform ty actual = atomic_compare_exchange_global(mem, *old, val); \
if (actual == *old){ \
return 1; \
} \
*old = actual; \
return 0; \
} \
static inline varying bool atomic_compare_exchange_wrapper(uniform ty * varying mem, \
varying ty * uniform old, \
const varying ty val){ \
varying ty actual = atomic_compare_exchange_global(mem, *old, val); \
bool res = 0; \
if(actual == *old){ \
res = 1; \
} else { \
*old = actual; \
} \
return res; \
} \
static inline varying bool atomic_compare_exchange_wrapper(varying ty * uniform mem, \
varying ty * uniform old, \
const varying ty val){ \
uniform ty * uniform base_mem = (uniform ty * uniform)mem; \
uniform ty * uniform base_old = (uniform ty * uniform)old; \
bool res = 0; \
foreach_active (i) { \
uniform ty * uniform curr_mem = base_mem + i; \
uniform ty * uniform curr_old = base_old + i; \
uniform ty curr_val = extract(val, i); \
uniform bool curr = atomic_compare_exchange_wrapper( \
curr_mem, curr_old, curr_val); \
res = insert(res, i, curr); \
} \
return res; \
} \
static inline uniform bool atomic_compare_exchange_wrapper(uniform ty * uniform mem, \
uniform ty * uniform old, \
const varying ty val){ \
uniform ty v = 0; \
foreach_active (i) v = extract(val, i); \
return atomic_compare_exchange_wrapper(mem, old, v); \
}
make_atomic_compare_exchange_wrapper(int32)
make_atomic_compare_exchange_wrapper(int64)
make_atomic_compare_exchange_wrapper(uint32)
make_atomic_compare_exchange_wrapper(uint64)
make_atomic_compare_exchange_wrapper(float)
make_atomic_compare_exchange_wrapper(double)
// This code generates missing overloads for atomic operations on uniform
// pointers to varying values.
#define make_single_atomic(name, ty) \
static inline ty atomic_##name##_global(varying ty * uniform mem, ty val) { \
uniform ty * uniform base_mem = (uniform ty * uniform)mem; \
ty res = 0; \
foreach_active (i) { \
uniform ty * uniform curr_mem = base_mem + i; \
uniform ty curr_val = extract(val, i); \
uniform ty curr = atomic_##name##_global(curr_mem, curr_val); \
res = insert(res, i, curr); \
} \
return res; \
}
#define make_all_atomic(name) \
make_single_atomic(name, int32) \
make_single_atomic(name, int64) \
make_single_atomic(name, uint32) \
make_single_atomic(name, uint64)
make_all_atomic(add)
make_all_atomic(subtract)
make_all_atomic(and)
make_all_atomic(or)
make_all_atomic(xor)
make_all_atomic(swap)
// This is a hack to prevent literals (which have unbound variability)
// from causing us to pick the wrong overload for atomic operations.
static inline varying int32 make_varying(uniform int32 x) { return x; }
static inline varying int32 make_varying(varying int32 x) { return x; }
static inline varying int64 make_varying(uniform int64 x) { return x; }
static inline varying int64 make_varying(varying int64 x) { return x; }
static inline varying uint32 make_varying(uniform uint32 x) { return x; }
static inline varying uint32 make_varying(varying uint32 x) { return x; }
static inline varying uint64 make_varying(uniform uint64 x) { return x; }
static inline varying uint64 make_varying(varying uint64 x) { return x; }
// Redirect atomic operations to the relevant ISPC overloads.
#define __atomic_fetch_add(x,y,z) atomic_add_global(x,make_varying(y))
#define __atomic_fetch_sub(x,y,z) atomic_sub_global(x,make_varying(y))
#define __atomic_fetch_and(x,y,z) atomic_and_global(x,make_varying(y))
#define __atomic_fetch_or(x,y,z) atomic_or_global(x,make_varying(y))
#define __atomic_fetch_xor(x,y,z) atomic_xor_global(x,make_varying(y))
#define __atomic_exchange_n(x,y,z) atomic_swap_global(x,make_varying(y))
#define __atomic_compare_exchange_n(x,y,z,h,j,k) atomic_compare_exchange_wrapper(x,y,z)
// Memory allocation handling
struct memblock {
int32_t * references;
uint8_t * mem;
int64_t size;
const int8_t * desc;
};
static inline void free(void* ptr) {
delete ptr;
}
static inline void free(void* uniform ptr) {
delete ptr;
}
extern "C" unmasked uniform unsigned char * uniform realloc(uniform unsigned char * uniform ptr, uniform int64_t new_size);
extern "C" unmasked uniform char * uniform lexical_realloc_error(uniform struct futhark_context * uniform ctx, uniform int64_t new_size);
static inline uniform int lexical_realloc(uniform struct futhark_context * uniform ctx,
unsigned char uniform * uniform * uniform ptr,
int64_t uniform * uniform old_size,
uniform int64_t new_size) {
uniform unsigned char * uniform memptr = realloc(*ptr, new_size);
if (memptr == NULL) {
lexical_realloc_error(ctx, new_size);
return FUTHARK_OUT_OF_MEMORY;
} else {
*ptr = memptr;
*old_size = new_size;
return FUTHARK_SUCCESS;
}
}
static inline uniform int lexical_realloc(uniform struct futhark_context *ctx,
unsigned char uniform * uniform * uniform ptr,
int64_t uniform * uniform old_size,
varying int64_t new_size) {
return lexical_realloc(ctx, ptr, old_size, reduce_max(new_size));
}
static inline uniform int lexical_realloc(uniform struct futhark_context * uniform ctx,
unsigned char uniform * varying * uniform ptr,
int64_t uniform * varying old_size,
varying int64_t new_size) {
uniform int err = FUTHARK_SUCCESS;
foreach_active(i){
uniform unsigned char * uniform memptr = realloc(extract(*ptr,i), extract(new_size,i));
if (memptr == NULL) {
lexical_realloc_error(ctx, extract(new_size,i));
err = FUTHARK_OUT_OF_MEMORY;
} else {
*ptr = (uniform unsigned char * varying)insert((int64_t)*ptr, i, (uniform int64_t) memptr);
*old_size = new_size;
}
}
return err;
}
static inline uniform int lexical_realloc(uniform struct futhark_context * uniform ctx,
unsigned char uniform * varying * uniform ptr,
int64_t varying * uniform old_size,
varying int64_t new_size) {
uniform int err = FUTHARK_SUCCESS;
foreach_active(i){
uniform unsigned char * uniform memptr = realloc(extract(*ptr,i), extract(new_size,i));
if (memptr == NULL) {
lexical_realloc_error(ctx, extract(new_size,i));
err = FUTHARK_OUT_OF_MEMORY;
} else {
*ptr = (uniform unsigned char * varying)insert((int64_t)*ptr, i, (uniform int64_t) memptr);
*old_size = new_size;
}
}
return err;
}
static inline uniform int lexical_realloc(uniform struct futhark_context * uniform ctx,
unsigned char uniform * varying * uniform ptr,
size_t varying * uniform old_size,
varying int64_t new_size) {
return lexical_realloc(ctx, ptr, (varying int64_t * uniform)old_size, new_size);
}
static inline uniform int lexical_realloc(uniform struct futhark_context * uniform ctx,
unsigned char varying * uniform * uniform ptr,
size_t varying * uniform old_size,
uniform int64_t new_size) {
uniform int err = FUTHARK_SUCCESS;
uniform unsigned char * uniform memptr = realloc((uniform unsigned char * uniform )*ptr,
new_size*programCount);
if (memptr == NULL) {
lexical_realloc_error(ctx, new_size);
err = FUTHARK_OUT_OF_MEMORY;
} else {
*ptr = (varying unsigned char * uniform)memptr;
*old_size = new_size;
}
return err;
}
static inline uniform int lexical_realloc(uniform struct futhark_context * uniform ctx,
unsigned char varying * uniform * uniform ptr,
size_t varying * uniform old_size,
varying int64_t new_size) {
return lexical_realloc(ctx, ptr, old_size, reduce_max(new_size));
}
extern "C" unmasked uniform int memblock_unref(uniform struct futhark_context * uniform ctx,
uniform struct memblock * uniform lhs,
uniform const char * uniform lhs_desc);
static uniform int memblock_unref(uniform struct futhark_context * varying ctx,
uniform struct memblock * varying lhs,
uniform const char * uniform lhs_desc)
{
uniform int err = 0;
foreach_active(i) {
err |= memblock_unref(extract(ctx,i), extract(lhs,i), lhs_desc);
}
return err;
}
static uniform int memblock_unref(uniform struct futhark_context * uniform ctx,
varying struct memblock * uniform lhs,
uniform const char * uniform lhs_desc)
{
uniform int err = 0;
varying struct memblock _lhs = *lhs;
uniform struct memblock aos[programCount];
aos[programIndex] = _lhs;
foreach_active(i){
err |= memblock_unref(ctx,
&aos[i],
lhs_desc);
}
*lhs = aos[programIndex];
return err;
}
extern "C" unmasked uniform int memblock_alloc(uniform struct futhark_context * uniform ctx,
uniform struct memblock * uniform block,
uniform int64_t size,
uniform const char * uniform block_desc);
static uniform int memblock_alloc(uniform struct futhark_context * varying ctx,
uniform struct memblock * varying block,
varying int64_t size,
uniform const char * uniform block_desc) {
uniform int err = 0;
foreach_active(i){
err |= memblock_alloc(extract(ctx,i), extract(block,i), extract(size, i), block_desc);
}
return err;
}
static uniform int memblock_alloc(uniform struct futhark_context * uniform ctx,
varying struct memblock * uniform block,
uniform int64_t size,
uniform const char * uniform block_desc) {
uniform int err = 0;
varying struct memblock _block = *block;
uniform struct memblock aos[programCount];
aos[programIndex] = _block;
foreach_active(i){
err |= memblock_alloc(ctx, &aos[i], size, block_desc);
}
*block = aos[programIndex];
return err;
}
static uniform int memblock_alloc(uniform struct futhark_context * uniform ctx,
varying struct memblock * uniform block,
varying int64_t size,
uniform const char * uniform block_desc) {
uniform int err = 0;
varying struct memblock _block = *block;
uniform struct memblock aos[programCount];
aos[programIndex] = _block;
foreach_active(i){
err |= memblock_alloc(ctx, &aos[i], extract(size, i), block_desc);
}
*block = aos[programIndex];
return err;
}
extern "C" unmasked uniform int memblock_set(uniform struct futhark_context * uniform ctx,
uniform struct memblock * uniform lhs,
uniform struct memblock * uniform rhs,
uniform const char * uniform lhs_desc);
static uniform int memblock_set (uniform struct futhark_context * uniform ctx,
varying struct memblock * uniform lhs,
varying struct memblock * uniform rhs,
uniform const char * uniform lhs_desc) {
uniform int err = 0;
varying struct memblock _lhs = *lhs;
varying struct memblock _rhs = *rhs;
uniform struct memblock aos1[programCount];
aos1[programIndex] = _lhs;
uniform struct memblock aos2[programCount];
aos2[programIndex] = _rhs;
foreach_active(i) {
err |= memblock_set(ctx,
&aos1[i],
&aos2[i],
lhs_desc);
}
*lhs = aos1[programIndex];
*rhs = aos2[programIndex];
return err;
}
static uniform int memblock_set (uniform struct futhark_context * uniform ctx,
varying struct memblock * uniform lhs,
uniform struct memblock * uniform rhs,
uniform const char * uniform lhs_desc) {
uniform int err = 0;
varying struct memblock _lhs = *lhs;
uniform struct memblock aos1[programCount];
aos1[programIndex] = _lhs;
foreach_active(i) {
err |= memblock_set(ctx,
&aos1[i],
rhs,
lhs_desc);
}
*lhs = aos1[programIndex];
return err;
}
// End of ispc_util.h.
|