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 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
|
// Start of gpu.h
// Generic functions that use our tiny GPU abstraction layer. The
// entire context must be defined before this header is included. In
// particular we expect the following functions to be available:
static int gpu_free_actual(struct futhark_context *ctx, gpu_mem mem);
static int gpu_alloc_actual(struct futhark_context *ctx, size_t size, gpu_mem *mem_out);
int gpu_launch_kernel(struct futhark_context* ctx,
gpu_kernel kernel,
const char *name, const char *provenance,
const int32_t grid[3],
const int32_t block[3],
unsigned int shared_mem_bytes,
int num_args,
void* args[num_args],
size_t args_sizes[num_args]);
int gpu_memcpy(struct futhark_context* ctx, const char* provenance,
gpu_mem dst, int64_t dst_offset,
gpu_mem src, int64_t src_offset,
int64_t nbytes);
int gpu_scalar_from_device(struct futhark_context* ctx, const char* provenance,
void *dst,
gpu_mem src, size_t offset, size_t size);
int gpu_scalar_to_device(struct futhark_context* ctx, const char* provenance,
gpu_mem dst, size_t offset, size_t size,
void *src);
void gpu_create_kernel(struct futhark_context *ctx,
gpu_kernel* kernel,
const char* name);
static void gpu_init_log(struct futhark_context *ctx) {
if (ctx->cfg->logging) {
fprintf(ctx->log, "Default block size: %ld\n", (long)ctx->cfg->gpu.default_block_size);
fprintf(ctx->log, "Default grid size: %ld\n", (long)ctx->cfg->gpu.default_grid_size);
fprintf(ctx->log, "Default tile size: %ld\n", (long)ctx->cfg->gpu.default_tile_size);
fprintf(ctx->log, "Default register tile size: %ld\n", (long)ctx->cfg->gpu.default_reg_tile_size);
fprintf(ctx->log, "Default cache: %ld\n", (long)ctx->cfg->gpu.default_cache);
fprintf(ctx->log, "Default registers: %ld\n", (long)ctx->cfg->gpu.default_registers);
fprintf(ctx->log, "Default threshold: %ld\n", (long)ctx->cfg->gpu.default_threshold);
fprintf(ctx->log, "Max thread block size: %ld\n", (long)ctx->max_thread_block_size);
fprintf(ctx->log, "Max grid size: %ld\n", (long)ctx->max_grid_size);
fprintf(ctx->log, "Max tile size: %ld\n", (long)ctx->max_tile_size);
fprintf(ctx->log, "Max threshold: %ld\n", (long)ctx->max_threshold);
fprintf(ctx->log, "Max shared memory: %ld\n", (long)ctx->max_shared_memory);
fprintf(ctx->log, "Max registers: %ld\n", (long)ctx->max_registers);
fprintf(ctx->log, "Max cache: %ld\n", (long)ctx->max_cache);
fprintf(ctx->log, "Lockstep width: %ld\n", (long)ctx->lockstep_width);
}
}
// Generic GPU command line options.
void futhark_context_config_set_default_thread_block_size(struct futhark_context_config *cfg, int size) {
cfg->gpu.default_block_size = size;
cfg->gpu.default_block_size_changed = 1;
}
void futhark_context_config_set_default_group_size(struct futhark_context_config *cfg, int size) {
futhark_context_config_set_default_thread_block_size(cfg, size);
}
void futhark_context_config_set_default_grid_size(struct futhark_context_config *cfg, int num) {
cfg->gpu.default_grid_size = num;
cfg->gpu.default_grid_size_changed = 1;
}
void futhark_context_config_set_default_num_groups(struct futhark_context_config *cfg, int num) {
futhark_context_config_set_default_grid_size(cfg, num);
}
void futhark_context_config_set_default_tile_size(struct futhark_context_config *cfg, int size) {
cfg->gpu.default_tile_size = size;
cfg->gpu.default_tile_size_changed = 1;
}
void futhark_context_config_set_default_reg_tile_size(struct futhark_context_config *cfg, int size) {
cfg->gpu.default_reg_tile_size = size;
}
void futhark_context_config_set_default_cache(struct futhark_context_config *cfg, int size) {
cfg->gpu.default_cache = size;
}
void futhark_context_config_set_default_registers(struct futhark_context_config *cfg, int size) {
cfg->gpu.default_registers = size;
}
void futhark_context_config_set_default_threshold(struct futhark_context_config *cfg, int size) {
cfg->gpu.default_threshold = size;
}
int futhark_context_config_set_tuning_param(struct futhark_context_config *cfg,
const char *param_name,
size_t new_value) {
for (int i = 0; i < cfg->num_tuning_params; i++) {
if (strcmp(param_name, cfg->tuning_param_names[i]) == 0) {
cfg->tuning_params[i] = new_value;
return 0;
}
}
if (strcmp(param_name, "default_thread_block_size") == 0 ||
strcmp(param_name, "default_group_size") == 0) {
cfg->gpu.default_block_size = new_value;
return 0;
}
if (strcmp(param_name, "default_grid_size") == 0 ||
strcmp(param_name, "default_num_groups") == 0) {
cfg->gpu.default_grid_size = new_value;
return 0;
}
if (strcmp(param_name, "default_threshold") == 0) {
cfg->gpu.default_threshold = new_value;
return 0;
}
if (strcmp(param_name, "default_tile_size") == 0) {
cfg->gpu.default_tile_size = new_value;
return 0;
}
if (strcmp(param_name, "default_reg_tile_size") == 0) {
cfg->gpu.default_reg_tile_size = new_value;
return 0;
}
if (strcmp(param_name, "default_cache") == 0) {
cfg->gpu.default_cache = new_value;
return 0;
}
if (strcmp(param_name, "default_shared_memory") == 0) {
cfg->gpu.default_shared_memory = new_value;
return 0;
}
return 1;
}
// End of GPU command line optiopns.
// Max number of thead blocks we allow along the second or third
// dimension for transpositions.
#define MAX_TR_THREAD_BLOCKS 65535
struct builtin_kernels {
// We have a lot of ways to transpose arrays.
gpu_kernel map_transpose_1b;
gpu_kernel map_transpose_1b_low_height;
gpu_kernel map_transpose_1b_low_width;
gpu_kernel map_transpose_1b_small;
gpu_kernel map_transpose_1b_large;
gpu_kernel map_transpose_2b;
gpu_kernel map_transpose_2b_low_height;
gpu_kernel map_transpose_2b_low_width;
gpu_kernel map_transpose_2b_small;
gpu_kernel map_transpose_2b_large;
gpu_kernel map_transpose_4b;
gpu_kernel map_transpose_4b_low_height;
gpu_kernel map_transpose_4b_low_width;
gpu_kernel map_transpose_4b_small;
gpu_kernel map_transpose_4b_large;
gpu_kernel map_transpose_8b;
gpu_kernel map_transpose_8b_low_height;
gpu_kernel map_transpose_8b_low_width;
gpu_kernel map_transpose_8b_small;
gpu_kernel map_transpose_8b_large;
// And a few ways of copying.
gpu_kernel lmad_copy_1b;
gpu_kernel lmad_copy_2b;
gpu_kernel lmad_copy_4b;
gpu_kernel lmad_copy_8b;
};
struct builtin_kernels* init_builtin_kernels(struct futhark_context* ctx) {
struct builtin_kernels *kernels = malloc(sizeof(struct builtin_kernels));
gpu_create_kernel(ctx, &kernels->map_transpose_1b, "map_transpose_1b");
gpu_create_kernel(ctx, &kernels->map_transpose_1b_large, "map_transpose_1b_large");
gpu_create_kernel(ctx, &kernels->map_transpose_1b_low_height, "map_transpose_1b_low_height");
gpu_create_kernel(ctx, &kernels->map_transpose_1b_low_width, "map_transpose_1b_low_width");
gpu_create_kernel(ctx, &kernels->map_transpose_1b_small, "map_transpose_1b_small");
gpu_create_kernel(ctx, &kernels->map_transpose_2b, "map_transpose_2b");
gpu_create_kernel(ctx, &kernels->map_transpose_2b_large, "map_transpose_2b_large");
gpu_create_kernel(ctx, &kernels->map_transpose_2b_low_height, "map_transpose_2b_low_height");
gpu_create_kernel(ctx, &kernels->map_transpose_2b_low_width, "map_transpose_2b_low_width");
gpu_create_kernel(ctx, &kernels->map_transpose_2b_small, "map_transpose_2b_small");
gpu_create_kernel(ctx, &kernels->map_transpose_4b, "map_transpose_4b");
gpu_create_kernel(ctx, &kernels->map_transpose_4b_large, "map_transpose_4b_large");
gpu_create_kernel(ctx, &kernels->map_transpose_4b_low_height, "map_transpose_4b_low_height");
gpu_create_kernel(ctx, &kernels->map_transpose_4b_low_width, "map_transpose_4b_low_width");
gpu_create_kernel(ctx, &kernels->map_transpose_4b_small, "map_transpose_4b_small");
gpu_create_kernel(ctx, &kernels->map_transpose_8b, "map_transpose_8b");
gpu_create_kernel(ctx, &kernels->map_transpose_8b_large, "map_transpose_8b_large");
gpu_create_kernel(ctx, &kernels->map_transpose_8b_low_height, "map_transpose_8b_low_height");
gpu_create_kernel(ctx, &kernels->map_transpose_8b_low_width, "map_transpose_8b_low_width");
gpu_create_kernel(ctx, &kernels->map_transpose_8b_small, "map_transpose_8b_small");
gpu_create_kernel(ctx, &kernels->lmad_copy_1b, "lmad_copy_1b");
gpu_create_kernel(ctx, &kernels->lmad_copy_2b, "lmad_copy_2b");
gpu_create_kernel(ctx, &kernels->lmad_copy_4b, "lmad_copy_4b");
gpu_create_kernel(ctx, &kernels->lmad_copy_8b, "lmad_copy_8b");
return kernels;
}
void free_builtin_kernels(struct futhark_context* ctx, struct builtin_kernels* kernels) {
gpu_free_kernel(ctx, kernels->map_transpose_1b);
gpu_free_kernel(ctx, kernels->map_transpose_1b_large);
gpu_free_kernel(ctx, kernels->map_transpose_1b_low_height);
gpu_free_kernel(ctx, kernels->map_transpose_1b_low_width);
gpu_free_kernel(ctx, kernels->map_transpose_1b_small);
gpu_free_kernel(ctx, kernels->map_transpose_2b);
gpu_free_kernel(ctx, kernels->map_transpose_2b_large);
gpu_free_kernel(ctx, kernels->map_transpose_2b_low_height);
gpu_free_kernel(ctx, kernels->map_transpose_2b_low_width);
gpu_free_kernel(ctx, kernels->map_transpose_2b_small);
gpu_free_kernel(ctx, kernels->map_transpose_4b);
gpu_free_kernel(ctx, kernels->map_transpose_4b_large);
gpu_free_kernel(ctx, kernels->map_transpose_4b_low_height);
gpu_free_kernel(ctx, kernels->map_transpose_4b_low_width);
gpu_free_kernel(ctx, kernels->map_transpose_4b_small);
gpu_free_kernel(ctx, kernels->map_transpose_8b);
gpu_free_kernel(ctx, kernels->map_transpose_8b_large);
gpu_free_kernel(ctx, kernels->map_transpose_8b_low_height);
gpu_free_kernel(ctx, kernels->map_transpose_8b_low_width);
gpu_free_kernel(ctx, kernels->map_transpose_8b_small);
gpu_free_kernel(ctx, kernels->lmad_copy_1b);
gpu_free_kernel(ctx, kernels->lmad_copy_2b);
gpu_free_kernel(ctx, kernels->lmad_copy_4b);
gpu_free_kernel(ctx, kernels->lmad_copy_8b);
free(kernels);
}
static int gpu_alloc(struct futhark_context *ctx, FILE *log,
size_t min_size, const char *tag,
gpu_mem *mem_out, size_t *size_out) {
if (min_size < sizeof(int)) {
min_size = sizeof(int);
}
// Round up the allocation to be at least divisible by 4, because that is
// assumed by the code generator.
min_size = (min_size+3) & ~3;
gpu_mem* memptr;
if (free_list_find(&ctx->gpu_free_list, min_size, tag, size_out, (fl_mem*)&memptr) == 0) {
// Successfully found a free block. Is it big enough?
if (*size_out >= min_size) {
if (ctx->cfg->debugging) {
fprintf(log, "No need to allocate: Found a block in the free list.\n");
}
*mem_out = *memptr;
free(memptr);
return FUTHARK_SUCCESS;
} else {
if (ctx->cfg->debugging) {
fprintf(log, "Found a free block, but it was too small.\n");
}
int error = gpu_free_actual(ctx, *memptr);
free(memptr);
if (error != FUTHARK_SUCCESS) {
return error;
}
}
}
*size_out = min_size;
// We have to allocate a new block from the driver. If the
// allocation does not succeed, then we might be in an out-of-memory
// situation. We now start freeing things from the free list until
// we think we have freed enough that the allocation will succeed.
// Since we don't know how far the allocation is from fitting, we
// have to check after every deallocation. This might be pretty
// expensive. Let's hope that this case is hit rarely.
if (ctx->cfg->debugging) {
fprintf(log, "Actually allocating the desired block.\n");
}
int error = gpu_alloc_actual(ctx, min_size, mem_out);
while (error == FUTHARK_OUT_OF_MEMORY) {
if (ctx->cfg->debugging) {
fprintf(log, "Out of GPU memory: releasing entry from the free list...\n");
}
gpu_mem* memptr;
if (free_list_first(&ctx->gpu_free_list, (fl_mem*)&memptr) == 0) {
gpu_mem mem = *memptr;
free(memptr);
error = gpu_free_actual(ctx, mem);
if (error != FUTHARK_SUCCESS) {
return error;
}
} else {
break;
}
error = gpu_alloc_actual(ctx, min_size, mem_out);
}
return error;
}
static int gpu_free(struct futhark_context *ctx,
gpu_mem mem, size_t size, const char *tag) {
gpu_mem* memptr = malloc(sizeof(gpu_mem));
*memptr = mem;
free_list_insert(&ctx->gpu_free_list, size, (fl_mem)memptr, tag);
return FUTHARK_SUCCESS;
}
static int gpu_free_all(struct futhark_context *ctx) {
free_list_pack(&ctx->gpu_free_list);
gpu_mem* memptr;
while (free_list_first(&ctx->gpu_free_list, (fl_mem*)&memptr) == 0) {
gpu_mem mem = *memptr;
free(memptr);
int error = gpu_free_actual(ctx, mem);
if (error != FUTHARK_SUCCESS) {
return error;
}
}
return FUTHARK_SUCCESS;
}
static int gpu_map_transpose(struct futhark_context* ctx,
const char* provenance,
gpu_kernel kernel_default,
gpu_kernel kernel_low_height,
gpu_kernel kernel_low_width,
gpu_kernel kernel_small,
gpu_kernel kernel_large,
const char *name, size_t elem_size,
gpu_mem dst, int64_t dst_offset,
gpu_mem src, int64_t src_offset,
int64_t k, int64_t n, int64_t m) {
int64_t mulx = TR_BLOCK_DIM / n;
int64_t muly = TR_BLOCK_DIM / m;
int32_t mulx32 = mulx;
int32_t muly32 = muly;
int32_t k32 = k;
int32_t n32 = n;
int32_t m32 = m;
gpu_kernel kernel = kernel_default;
int32_t grid[3];
int32_t block[3];
void* args[11];
size_t args_sizes[11] = {
sizeof(gpu_mem), sizeof(int64_t),
sizeof(gpu_mem), sizeof(int64_t),
sizeof(int32_t),
sizeof(int32_t),
sizeof(int32_t),
sizeof(int32_t),
sizeof(int32_t)
};
args[0] = &dst;
args[1] = &dst_offset;
args[2] = &src;
args[3] = &src_offset;
args[7] = &mulx;
args[8] = &muly;
if (dst_offset + k * n * m <= 2147483647L &&
src_offset + k * n * m <= 2147483647L) {
if (m <= TR_BLOCK_DIM/2 && n <= TR_BLOCK_DIM/2) {
if (ctx->logging) { fprintf(ctx->log, "Using small kernel\n"); }
kernel = kernel_small;
grid[0] = ((k * n * m) + (TR_BLOCK_DIM*TR_BLOCK_DIM) - 1) / (TR_BLOCK_DIM*TR_BLOCK_DIM);
grid[1] = 1;
grid[2] = 1;
block[0] = TR_BLOCK_DIM*TR_BLOCK_DIM;
block[1] = 1;
block[2] = 1;
} else if (m <= TR_BLOCK_DIM/2 && TR_BLOCK_DIM < n) {
if (ctx->logging) { fprintf(ctx->log, "Using low-width kernel\n"); }
kernel = kernel_low_width;
int64_t x_elems = m;
int64_t y_elems = (n + muly - 1) / muly;
grid[0] = (x_elems + TR_BLOCK_DIM - 1) / TR_BLOCK_DIM;
grid[1] = (y_elems + TR_BLOCK_DIM - 1) / TR_BLOCK_DIM;
grid[2] = k;
block[0] = TR_BLOCK_DIM;
block[1] = TR_BLOCK_DIM;
block[2] = 1;
} else if (n <= TR_BLOCK_DIM/2 && TR_BLOCK_DIM < m) {
if (ctx->logging) { fprintf(ctx->log, "Using low-height kernel\n"); }
kernel = kernel_low_height;
int64_t x_elems = (m + mulx - 1) / mulx;
int64_t y_elems = n;
grid[0] = (x_elems + TR_BLOCK_DIM - 1) / TR_BLOCK_DIM;
grid[1] = (y_elems + TR_BLOCK_DIM - 1) / TR_BLOCK_DIM;
grid[2] = k;
block[0] = TR_BLOCK_DIM;
block[1] = TR_BLOCK_DIM;
block[2] = 1;
} else {
if (ctx->logging) { fprintf(ctx->log, "Using default kernel\n"); }
kernel = kernel_default;
grid[0] = (m+TR_TILE_DIM-1)/TR_TILE_DIM;
grid[1] = (n+TR_TILE_DIM-1)/TR_TILE_DIM;
grid[2] = k;
block[0] = TR_TILE_DIM;
block[1] = TR_TILE_DIM/TR_ELEMS_PER_THREAD;
block[2] = 1;
}
args[4] = &k32;
args[5] = &m32;
args[6] = &n32;
args[7] = &mulx32;
args[8] = &muly32;
} else {
if (ctx->logging) { fprintf(ctx->log, "Using large kernel\n"); }
kernel = kernel_large;
grid[0] = (m+TR_TILE_DIM-1)/TR_TILE_DIM;
grid[1] = (n+TR_TILE_DIM-1)/TR_TILE_DIM;
grid[2] = k;
block[0] = TR_TILE_DIM;
block[1] = TR_TILE_DIM/TR_ELEMS_PER_THREAD;
block[2] = 1;
args[4] = &k;
args[5] = &m;
args[6] = &n;
args[7] = &mulx;
args[8] = &muly;
args_sizes[4] = sizeof(int64_t);
args_sizes[5] = sizeof(int64_t);
args_sizes[6] = sizeof(int64_t);
args_sizes[7] = sizeof(int64_t);
args_sizes[8] = sizeof(int64_t);
}
// Cap the number of thead blocks we launch and figure out how many
// repeats we need alongside each dimension.
int32_t repeat_1 = grid[1] / MAX_TR_THREAD_BLOCKS;
int32_t repeat_2 = grid[2] / MAX_TR_THREAD_BLOCKS;
grid[1] = repeat_1 > 0 ? MAX_TR_THREAD_BLOCKS : grid[1];
grid[2] = repeat_2 > 0 ? MAX_TR_THREAD_BLOCKS : grid[2];
args[9] = &repeat_1;
args[10] = &repeat_2;
args_sizes[9] = sizeof(repeat_1);
args_sizes[10] = sizeof(repeat_2);
if (ctx->logging) {
fprintf(ctx->log, "\n");
}
return gpu_launch_kernel(ctx, kernel, name, provenance, grid, block,
TR_TILE_DIM*(TR_TILE_DIM+1)*elem_size,
sizeof(args)/sizeof(args[0]), args, args_sizes);
}
#define GEN_MAP_TRANSPOSE_GPU2GPU(NAME, ELEM_TYPE) \
static int map_transpose_gpu2gpu_##NAME \
(struct futhark_context* ctx, const char* provenance, \
gpu_mem dst, int64_t dst_offset, \
gpu_mem src, int64_t src_offset, \
int64_t k, int64_t m, int64_t n) \
{ \
return \
gpu_map_transpose \
(ctx, \
provenance, \
ctx->kernels->map_transpose_##NAME, \
ctx->kernels->map_transpose_##NAME##_low_height, \
ctx->kernels->map_transpose_##NAME##_low_width, \
ctx->kernels->map_transpose_##NAME##_small, \
ctx->kernels->map_transpose_##NAME##_large, \
"map_transpose_" #NAME, sizeof(ELEM_TYPE), \
dst, dst_offset, src, src_offset, \
k, n, m); \
}
static int gpu_lmad_copy(struct futhark_context* ctx, const char* provenance,
gpu_kernel kernel, int r,
gpu_mem dst, int64_t dst_offset, int64_t dst_strides[r],
gpu_mem src, int64_t src_offset, int64_t src_strides[r],
int64_t shape[r]) {
if (r > 8) {
set_error(ctx, strdup("Futhark runtime limitation:\nCannot copy array of greater than rank 8.\n"));
return 1;
}
int64_t n = 1;
for (int i = 0; i < r; i++) { n *= shape[i]; }
void* args[6+(8*3)];
size_t args_sizes[6+(8*3)];
args[0] = &dst;
args_sizes[0] = sizeof(gpu_mem);
args[1] = &dst_offset;
args_sizes[1] = sizeof(dst_offset);
args[2] = &src;
args_sizes[2] = sizeof(gpu_mem);
args[3] = &src_offset;
args_sizes[3] = sizeof(src_offset);
args[4] = &n;
args_sizes[4] = sizeof(n);
args[5] = &r;
args_sizes[5] = sizeof(r);
int64_t zero = 0;
for (int i = 0; i < 8; i++) {
args_sizes[6+i*3] = sizeof(int64_t);
args_sizes[6+i*3+1] = sizeof(int64_t);
args_sizes[6+i*3+2] = sizeof(int64_t);
if (i < r) {
args[6+i*3] = &shape[i];
args[6+i*3+1] = &dst_strides[i];
args[6+i*3+2] = &src_strides[i];
} else {
args[6+i*3] = &zero;
args[6+i*3+1] = &zero;
args[6+i*3+2] = &zero;
}
}
const size_t w = 256; // XXX: hardcoded thread block size.
return gpu_launch_kernel(ctx, kernel, "copy_lmad_dev_to_dev", provenance,
(const int32_t[3]) {(n+w-1)/w,1,1},
(const int32_t[3]) {w,1,1},
0, 6+(8*3), args, args_sizes);
}
#define GEN_LMAD_COPY_ELEMENTS_GPU2GPU(NAME, ELEM_TYPE) \
static int lmad_copy_elements_gpu2gpu_##NAME \
(struct futhark_context* ctx, const char* provenance, \
int r, \
gpu_mem dst, int64_t dst_offset, int64_t dst_strides[r], \
gpu_mem src, int64_t src_offset, int64_t src_strides[r], \
int64_t shape[r]) { \
return gpu_lmad_copy(ctx, provenance, \
ctx->kernels->lmad_copy_##NAME, \
r, \
dst, dst_offset, dst_strides, \
src, src_offset, src_strides, \
shape); \
} \
#define GEN_LMAD_COPY_GPU2GPU(NAME, ELEM_TYPE) \
static int lmad_copy_gpu2gpu_##NAME \
(struct futhark_context* ctx, const char* provenance, \
int r, \
gpu_mem dst, int64_t dst_offset, int64_t dst_strides[r], \
gpu_mem src, int64_t src_offset, int64_t src_strides[r], \
int64_t shape[r]) { \
log_copy(ctx, "GPU to GPU", provenance, r, dst_offset, dst_strides, \
src_offset, src_strides, shape); \
int64_t size = 1; \
for (int i = 0; i < r; i++) { size *= shape[i]; } \
if (size == 0) { return FUTHARK_SUCCESS; } \
int64_t k, n, m; \
if (lmad_map_tr(&k, &n, &m, \
r, dst_strides, src_strides, shape)) { \
log_transpose(ctx, k, n, m); \
return map_transpose_gpu2gpu_##NAME \
(ctx, provenance, dst, dst_offset, src, src_offset, k, n, m); \
} else if (lmad_memcpyable(r, dst_strides, src_strides, shape)) { \
if (ctx->logging) {fprintf(ctx->log, "## Flat copy\n\n");} \
return gpu_memcpy(ctx, \
provenance, \
dst, dst_offset*sizeof(ELEM_TYPE), \
src, src_offset*sizeof(ELEM_TYPE), \
size * sizeof(ELEM_TYPE)); \
} else { \
if (ctx->logging) {fprintf(ctx->log, "## General copy\n\n");} \
return lmad_copy_elements_gpu2gpu_##NAME \
(ctx, provenance, \
r, \
dst, dst_offset, dst_strides, \
src, src_offset, src_strides, \
shape); \
} \
}
static int
lmad_copy_elements_host2gpu(struct futhark_context *ctx, const char *provenance,
size_t elem_size,
int r,
gpu_mem dst, int64_t dst_offset, int64_t dst_strides[r],
unsigned char* src, int64_t src_offset, int64_t src_strides[r],
int64_t shape[r]) {
(void)ctx; (void)provenance; (void)elem_size; (void)r;
(void)dst; (void)dst_offset; (void)dst_strides;
(void)src; (void)src_offset; (void)src_strides;
(void)shape;
set_error(ctx, strdup("Futhark runtime limitation:\nCannot copy unstructured array from host to GPU.\n"));
return 1;
}
static int
lmad_copy_elements_gpu2host (struct futhark_context *ctx, const char *provenance,
size_t elem_size,
int r,
unsigned char* dst, int64_t dst_offset, int64_t dst_strides[r],
gpu_mem src, int64_t src_offset, int64_t src_strides[r],
int64_t shape[r]) {
(void)ctx; (void)provenance; (void)elem_size; (void)r;
(void)dst; (void)dst_offset; (void)dst_strides;
(void)src; (void)src_offset; (void)src_strides;
(void)shape;
set_error(ctx, strdup("Futhark runtime limitation:\nCannot copy unstructured array from GPU to host.\n"));
return 1;
}
static int lmad_copy_host2gpu(struct futhark_context* ctx, const char* provenance,
size_t elem_size, bool sync,
int r,
gpu_mem dst, int64_t dst_offset, int64_t dst_strides[r],
unsigned char* src, int64_t src_offset, int64_t src_strides[r],
int64_t shape[r]) {
log_copy(ctx, "Host to GPU", provenance, r, dst_offset, dst_strides,
src_offset, src_strides, shape);
int64_t size = elem_size;
for (int i = 0; i < r; i++) { size *= shape[i]; }
if (size == 0) { return FUTHARK_SUCCESS; }
int64_t k, n, m;
if (lmad_memcpyable(r, dst_strides, src_strides, shape)) {
if (ctx->logging) {fprintf(ctx->log, "## Flat copy\n\n");}
return memcpy_host2gpu(ctx, provenance,
sync,
dst, dst_offset*elem_size,
src, src_offset*elem_size,
size);
} else {
if (ctx->logging) {fprintf(ctx->log, "## General copy\n\n");}
int error;
error = lmad_copy_elements_host2gpu
(ctx, provenance,
elem_size, r,
dst, dst_offset, dst_strides,
src, src_offset, src_strides,
shape);
if (error == 0 && sync) {
error = futhark_context_sync(ctx);
}
return error;
}
}
static int lmad_copy_gpu2host(struct futhark_context* ctx, const char* provenance,
size_t elem_size, bool sync,
int r,
unsigned char* dst, int64_t dst_offset, int64_t dst_strides[r],
gpu_mem src, int64_t src_offset, int64_t src_strides[r],
int64_t shape[r]) {
log_copy(ctx, "Host to GPU", provenance, r, dst_offset, dst_strides,
src_offset, src_strides, shape);
int64_t size = elem_size;
for (int i = 0; i < r; i++) { size *= shape[i]; }
if (size == 0) { return FUTHARK_SUCCESS; }
int64_t k, n, m;
if (lmad_memcpyable(r, dst_strides, src_strides, shape)) {
if (ctx->logging) {fprintf(ctx->log, "## Flat copy\n\n");}
return memcpy_gpu2host(ctx, provenance,
sync,
dst, dst_offset*elem_size,
src, src_offset*elem_size,
size);
} else {
if (ctx->logging) {fprintf(ctx->log, "## General copy\n\n");}
int error;
error = lmad_copy_elements_gpu2host
(ctx, provenance, elem_size, r,
dst, dst_offset, dst_strides,
src, src_offset, src_strides,
shape);
if (error == 0 && sync) {
error = futhark_context_sync(ctx);
}
return error;
}
}
GEN_MAP_TRANSPOSE_GPU2GPU(1b, uint8_t)
GEN_MAP_TRANSPOSE_GPU2GPU(2b, uint16_t)
GEN_MAP_TRANSPOSE_GPU2GPU(4b, uint32_t)
GEN_MAP_TRANSPOSE_GPU2GPU(8b, uint64_t)
GEN_LMAD_COPY_ELEMENTS_GPU2GPU(1b, uint8_t)
GEN_LMAD_COPY_ELEMENTS_GPU2GPU(2b, uint16_t)
GEN_LMAD_COPY_ELEMENTS_GPU2GPU(4b, uint32_t)
GEN_LMAD_COPY_ELEMENTS_GPU2GPU(8b, uint64_t)
GEN_LMAD_COPY_GPU2GPU(1b, uint8_t)
GEN_LMAD_COPY_GPU2GPU(2b, uint16_t)
GEN_LMAD_COPY_GPU2GPU(4b, uint32_t)
GEN_LMAD_COPY_GPU2GPU(8b, uint64_t)
// End of gpu.h
|