This repository was archived by the owner on Mar 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathacl_kernel.cpp
More file actions
3377 lines (2998 loc) · 125 KB
/
Copy pathacl_kernel.cpp
File metadata and controls
3377 lines (2998 loc) · 125 KB
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2010-2021 Intel Corporation
// SPDX-License-Identifier: BSD-3-Clause
// System headers.
#include <algorithm>
#include <cassert>
#include <iostream>
#include <sstream>
#include <stdio.h>
#include <unordered_map>
#include <vector>
// External library headers.
#include <CL/opencl.h>
#include <acl_hash/acl_hash.h>
#include <acl_threadsupport/acl_threadsupport.h>
#include <pkg_editor/pkg_editor.h>
// Internal headers.
#include <acl.h>
#include <acl_context.h>
#include <acl_device_op.h>
#include <acl_event.h>
#include <acl_globals.h>
#include <acl_hostch.h>
#include <acl_icd_dispatch.h>
#include <acl_kernel.h>
#include <acl_mem.h>
#include <acl_profiler.h>
#include <acl_program.h>
#include <acl_sampler.h>
#include <acl_support.h>
#include <acl_svm.h>
#include <acl_types.h>
#include <acl_usm.h>
#include <acl_util.h>
#include <acl_visibility.h>
#ifdef __GNUC__
#pragma GCC visibility push(protected)
#endif
// Kernels
// ========
//
// Lifecycle of cl_kernel:
// States are:
// "new" - initial state. It's attached to a built program.
// "ready_to_run" - All its arguments ar specified
//
// Data model:
//
// cl_kernel has:
// - name
// - reference to program
// - reference to interface, discovered via the
// program->context->device[]->name
// - arg values
//
// acl_kernel_interface_t has:
// - num_args
// - arg_info[]
// .addr_space: one of LOCAL|GLOBAL|CONSTANT
// .category: PLAIN, MEM_OBJ, SAMPLER
// .size
//////////////////////////////
// Variables and macros
// Prints more logs for debugging purposes.
static int debug_verbosity = 0;
#define ACL_KERNEL_DEBUG_MSG_VERBOSE(verbosity, m, ...) \
do { \
if (debug_verbosity >= verbosity) { \
printf((m), ##__VA_ARGS__); \
fflush(stdout); \
} \
} while (0)
// Local functions
static size_t l_round_up_for_alignment(size_t x);
static int l_init_kernel(cl_kernel kernel, cl_program program,
const acl_accel_def_t *accel_def,
const acl_device_binary_t *dev_bin,
cl_int *errcode_ret);
static cl_int l_load_consistently_built_kernels_in_program(
cl_program program,
std::vector<std::pair<const acl_device_binary_t *, const acl_accel_def_t *>>
&accel_ret);
static int l_kernel_interfaces_match(const acl_accel_def_t &a,
const acl_accel_def_t &b);
static size_t l_local_mem_size(cl_kernel kernel);
static cl_int l_enqueue_kernel_with_type(
cl_command_queue command_queue, cl_kernel kernel, cl_uint work_dim,
const size_t *global_work_offset, const size_t *global_work_size,
const size_t *local_work_size, cl_uint num_events_in_wait_list,
const cl_event *event_wait_list, cl_event *event, cl_command_type type);
static void l_get_arg_offset_and_size(cl_kernel kernel, cl_uint arg_index,
size_t *start_idx_ret, size_t *size_ret);
static cl_int l_copy_and_adjust_arguments_for_device(
cl_kernel kernel, cl_device_id device, char *buf, cl_uint *num_bytes,
acl_mem_migrate_t *memory_migration,
std::vector<aocl_mmd_streaming_kernel_arg_info_t> &streaming_args);
static void l_abort_use_of_wrapper(acl_kernel_invocation_wrapper_t *wrapper);
static void l_complete_kernel_execution(cl_event event);
static cl_bool l_check_mem_type_support_on_kernel_arg(
cl_kernel kernel, cl_uint arg_index,
acl_system_global_mem_type_t expected_type);
unsigned int l_get_kernel_arg_mem_id(const cl_kernel kernel, cl_uint arg_index);
ACL_DEFINE_CL_OBJECT_ALLOC_FUNCTIONS(cl_kernel);
//////////////////////////////
// OpenCL API
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clRetainKernelIntelFPGA(cl_kernel kernel) {
std::scoped_lock lock{acl_mutex_wrapper};
if (!acl_kernel_is_valid(kernel)) {
return CL_INVALID_KERNEL;
}
acl_retain(kernel);
return CL_SUCCESS;
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clRetainKernel(cl_kernel kernel) {
return clRetainKernelIntelFPGA(kernel);
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clReleaseKernelIntelFPGA(cl_kernel kernel) {
std::scoped_lock lock{acl_mutex_wrapper};
if (!acl_kernel_is_valid(kernel)) {
return CL_INVALID_KERNEL;
}
acl_print_debug_msg("Release kernel %p\n", kernel);
if (1 == acl_ref_count(kernel)) {
// kernel had profile data, free the buffer
if (kernel->profile_data) {
acl_free(kernel->profile_data);
kernel->profile_data = 0;
}
if (kernel->printf_device_buffer) {
clReleaseMemObject(kernel->printf_device_buffer);
kernel->printf_device_buffer = 0;
}
if (kernel->printf_device_ptr) {
clSVMFree(kernel->program->context, kernel->printf_device_ptr);
kernel->printf_device_ptr = 0;
}
if (kernel->arg_value) {
acl_delete_arr(kernel->arg_value);
kernel->arg_value = nullptr;
}
acl_untrack_object(kernel);
acl_release(kernel);
acl_program_forget_kernel(kernel->program, kernel);
clReleaseProgram(kernel->program);
} else {
acl_release(kernel);
}
return CL_SUCCESS;
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clReleaseKernel(cl_kernel kernel) {
return clReleaseKernelIntelFPGA(kernel);
}
ACL_EXPORT
CL_API_ENTRY cl_kernel CL_API_CALL clCreateKernelIntelFPGA(
cl_program program, const char *kernel_name, cl_int *errcode_ret) {
cl_int status;
cl_kernel kernel = 0;
std::scoped_lock lock{acl_mutex_wrapper};
// Can't call the callback, because we have no valid context.
if (!acl_program_is_valid(program))
BAIL(CL_INVALID_PROGRAM);
if (!kernel_name)
BAIL_INFO(CL_INVALID_VALUE, program->context, "kernel_name is NULL");
// What device program is associated with this kernel?
// Right now we only support one device per kernel.
const acl_device_binary_t *dev_bin = nullptr;
const auto *accel_def = acl_find_accel_def(program, kernel_name, dev_bin,
&status, program->context, 0);
if (status != CL_SUCCESS)
BAIL(status); // already signaled callback
kernel = acl_program_alloc_kernel(program);
if (kernel == 0) {
BAIL_INFO(CL_OUT_OF_HOST_MEMORY, program->context,
"Could not allocate a program object");
}
l_init_kernel(kernel, program, accel_def, dev_bin, errcode_ret);
if (errcode_ret) {
*errcode_ret = CL_SUCCESS;
}
return kernel;
}
ACL_EXPORT
CL_API_ENTRY cl_kernel CL_API_CALL clCreateKernel(cl_program program,
const char *kernel_name,
cl_int *errcode_ret) {
return clCreateKernelIntelFPGA(program, kernel_name, errcode_ret);
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clCreateKernelsInProgramIntelFPGA(
cl_program program, cl_uint num_kernels, cl_kernel *kernels,
cl_uint *num_kernels_ret) {
std::scoped_lock lock{acl_mutex_wrapper};
if (!acl_program_is_valid(program)) {
return CL_INVALID_PROGRAM;
}
auto context = program->context;
std::vector<std::pair<const acl_device_binary_t *, const acl_accel_def_t *>>
accel_ret;
auto status =
l_load_consistently_built_kernels_in_program(program, accel_ret);
if (status != CL_SUCCESS) {
return status; // already signaled
}
if (accel_ret.size() == 0) {
ERR_RET(CL_INVALID_PROGRAM_EXECUTABLE, context,
"No kernels were built across all devices with the same interface");
}
// Check return buffer spec
if (num_kernels == 0 && kernels) {
ERR_RET(CL_INVALID_VALUE, context,
"num_kernels is zero but kernels array is specified");
}
if (num_kernels > 0 && kernels == 0) {
ERR_RET(CL_INVALID_VALUE, context,
"num_kernels is non-zero but kernels array is not specified");
}
if (kernels) {
// User wants to send the kernels back.
// Result buffer isn't big enough.
if (num_kernels < accel_ret.size()) {
return CL_INVALID_VALUE;
}
// The definitions are in accel_ret. Create the kernels.
status = CL_SUCCESS;
for (cl_uint i = 0; i < accel_ret.size() && status == CL_SUCCESS; ++i) {
cl_kernel kernel = acl_program_alloc_kernel(program);
if (kernel) {
l_init_kernel(kernel, program, accel_ret[i].second, accel_ret[i].first,
&status);
kernels[i] = kernel;
} else {
status = CL_OUT_OF_HOST_MEMORY;
acl_context_callback(context, "Could not allocate a kernel object");
// Unwind the ones we've created
for (cl_uint j = 0; j < i; j++) {
clReleaseKernel(kernels[j]);
}
}
}
}
if (num_kernels_ret)
*num_kernels_ret = static_cast<cl_uint>(accel_ret.size());
return status;
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL
clCreateKernelsInProgram(cl_program program, cl_uint num_kernels,
cl_kernel *kernels, cl_uint *num_kernels_ret) {
return clCreateKernelsInProgramIntelFPGA(program, num_kernels, kernels,
num_kernels_ret);
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clSetKernelArgIntelFPGA(cl_kernel kernel,
cl_uint arg_index,
size_t arg_size,
const void *arg_value) {
const acl_kernel_arg_info_t *arg_info = 0;
cl_context context;
cl_bool is_pipe = CL_FALSE;
cl_bool is_sampler = CL_FALSE;
std::scoped_lock lock{acl_mutex_wrapper};
if (!acl_kernel_is_valid(kernel)) {
return CL_INVALID_KERNEL;
}
context = kernel->program->context;
if (arg_index >= kernel->accel_def->iface.args.size()) {
ERR_RET(CL_INVALID_ARG_INDEX, context, "Argument index is too large");
}
arg_info = &(kernel->accel_def->iface.args[arg_index]);
// Check for valid mem object or sampler
if (arg_info->category == ACL_ARG_MEM_OBJ) {
// In OpenCL 1.2 a pointer to a NULL value is also allowed for arg_values
// representing buffers.
if (arg_value && (*(cl_mem *)arg_value) &&
!acl_mem_is_valid(*(cl_mem *)arg_value))
ERR_RET(CL_INVALID_MEM_OBJECT, context,
"Non-memory object passed in as memory object argument");
} else if (arg_info->category == ACL_ARG_SAMPLER) {
if (arg_value && (arg_size != sizeof(cl_sampler) ||
!acl_sampler_is_valid(*(cl_sampler *)arg_value))) {
ERR_RET(CL_INVALID_SAMPLER, context,
"Non-sampler object passed in as sampler object argument");
}
is_sampler = CL_TRUE;
} else if (arg_size != arg_info->size && arg_value &&
arg_size == sizeof(cl_sampler) &&
acl_sampler_is_valid(*(cl_sampler *)arg_value)) {
is_sampler = CL_TRUE;
}
// Check argument size, and value pointer.
switch (arg_info->addr_space) {
case ACL_ARG_ADDR_LOCAL: /* Size is number of local bytes to allocate */
if (arg_size == 0) {
ERR_RET(CL_INVALID_ARG_SIZE, context,
"Pointer-to-local argument specified zero size");
}
if (arg_value != 0) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"Pointer-to-local argument specified with a non-null value");
}
/* We instantiated a specific mem capacity to handle this pointer.
* Make sure that user didn't ask for more at runtime than they
* specified (and we instantiated) at kernel compile time */
{
unsigned lmem_size_instantiated = arg_info->lmem_size_bytes;
if (arg_size > lmem_size_instantiated) {
ERR_RET(CL_INVALID_ARG_SIZE, context,
"Pointer-to-local argument requested size is larger than "
"maximum specified at compile time");
}
}
break;
case ACL_ARG_ADDR_GLOBAL:
case ACL_ARG_ADDR_CONSTANT:
if (arg_size != sizeof(cl_mem)) {
ERR_RET(CL_INVALID_ARG_SIZE, context,
"Pointer-to-global or Pointer-to-constant argument size is "
"not the size of cl_mem");
}
// Can pass NULL or pointer to NULL in arg_value, or it must be a valid
// memory object.
if (arg_value && (*(cl_mem *)arg_value) &&
!acl_mem_is_valid(*(cl_mem *)arg_value)) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"Pointer-to-global or Pointer-to-constant argument value is "
"not a valid memory object");
}
if (arg_value && (*(cl_mem *)arg_value) &&
arg_info->type_qualifier == ACL_ARG_TYPE_PIPE &&
(*(cl_mem *)arg_value)->mem_object_type == CL_MEM_OBJECT_PIPE) {
is_pipe = CL_TRUE;
}
// If this buffer is an SVM buffer, assume that the user wants the memory to
// be in sync. Treat this the same as an SVM kernel arg and return.
if (arg_value && (*(cl_mem *)arg_value) && (*(cl_mem *)arg_value)->is_svm) {
return clSetKernelArgSVMPointerIntelFPGA(
kernel, arg_index, (*(cl_mem *)arg_value)->host_mem.aligned_ptr);
}
break;
case ACL_ARG_ADDR_NONE:
if (arg_value == NULL) {
ERR_RET(CL_INVALID_ARG_VALUE, context, "Argument value is NULL");
}
if (is_sampler && arg_value != 0 &&
acl_sampler_is_valid_ptr(*((cl_sampler *)arg_value))) {
if (arg_size != sizeof(cl_sampler)) {
ERR_RET(CL_INVALID_ARG_SIZE, context,
"Sampler argument size is not the size of cl_sampler");
}
if (arg_info->size != sizeof(int)) {
ERR_RET(CL_INVALID_ARG_SIZE, context,
"Argument size is the wrong size");
}
} else if (arg_size == sizeof(cl_mem) &&
acl_pipe_is_valid_pointer(*((cl_mem *)arg_value), kernel)) {
is_pipe = CL_TRUE;
} else if (arg_size != arg_info->size) {
ERR_RET(CL_INVALID_ARG_SIZE, context, "Argument size is the wrong size");
}
break;
}
// May be a pipe - but currently the pipe object is empty (no allocated
// memory) and we don't actually read pipe arguments in the kernel so we
// shouldn't spend time setting up the argument properly.
if (is_pipe) {
cl_mem pipe_ptr = *((cl_mem *)arg_value);
assert(pipe_ptr != NULL);
kernel->arg_is_svm[arg_index] = CL_FALSE;
kernel->arg_is_ptr[arg_index] = CL_FALSE;
kernel->arg_defined[arg_index] = 1;
/* If this is a host pipe, create a host channel and bind them together */
if (arg_info->host_accessible && pipe_ptr->host_pipe_info != NULL) {
if (pipe_ptr->host_pipe_info->m_binded_kernel != NULL) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"This pipe has already been bound to a kernel. Cannot "
"rebind to a new kernel");
}
// Check to see if the kernel argument's width matches up with our cl_pipe
if (!context->uses_dynamic_sysdef) {
// In mode 3
// All the device need to have the same host pipe def
for (unsigned int i = 0; i < kernel->program->num_devices; ++i) {
bool found = false;
for (const auto &hostpipe_info :
kernel->program->device[i]
->def.autodiscovery_def.acl_hostpipe_info) {
if (arg_info->pipe_channel_id == hostpipe_info.name) {
// Check direction
if (pipe_ptr->flags & CL_MEM_HOST_READ_ONLY &&
hostpipe_info.is_dev_to_host) {
// Direction match
} else if (pipe_ptr->flags & CL_MEM_HOST_WRITE_ONLY &&
hostpipe_info.is_host_to_dev) {
// Direction match
} else {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"Host accessible pipe direction is not the same "
"of cl_pipe");
}
// Check width
if (pipe_ptr->fields.pipe_objs.pipe_packet_size !=
hostpipe_info.data_width) {
ERR_RET(CL_INVALID_ARG_SIZE, context,
"Host accessible pipe size is not the same size "
"of cl_pipe");
}
// Check max buffer size
if (pipe_ptr->fields.pipe_objs.pipe_max_packets >
hostpipe_info.max_buffer_depth) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"Host accessible pipe max packets size is "
"smaller than cl_pipe requested size");
}
found = true;
}
}
assert(found);
}
} else {
// Not in mode 3
bool found = false;
for (const auto &hostpipe_info :
kernel->dev_bin->get_devdef()
.autodiscovery_def.acl_hostpipe_info) {
if (arg_info->pipe_channel_id == hostpipe_info.name) {
// Check direction
if (pipe_ptr->flags & CL_MEM_HOST_READ_ONLY &&
hostpipe_info.is_dev_to_host) {
// Direction match
} else if (pipe_ptr->flags & CL_MEM_HOST_WRITE_ONLY &&
hostpipe_info.is_host_to_dev) {
// Direction match
} else {
ERR_RET(
CL_INVALID_ARG_VALUE, context,
"Host accessible pipe direction is not the same of cl_pipe");
}
// Check width
if (pipe_ptr->fields.pipe_objs.pipe_packet_size !=
hostpipe_info.data_width) {
ERR_RET(
CL_INVALID_ARG_SIZE, context,
"Host accessible pipe size is not the same size of cl_pipe");
}
// Check max buffer size
if (pipe_ptr->fields.pipe_objs.pipe_max_packets >
hostpipe_info.max_buffer_depth) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"Host accessible pipe max packets size is smaller "
"than cl_pipe requested size");
}
found = true;
}
}
assert(found);
}
// Here we bind the kernel, but we delay hostpipe binding until kernel
// enqueue
pipe_ptr->host_pipe_info->m_binded_kernel = kernel;
pipe_ptr->host_pipe_info->host_pipe_channel_id =
arg_info->pipe_channel_id;
// Always do the binding until kernel enqueue time, because we can only
// figure out which device at enqueue time
pipe_ptr->host_pipe_info->binded = false;
}
return CL_SUCCESS;
}
// Now try saving the value.
// Intel x86 is little-endian, i.e. least significant byte is stored in
// the lowest numbered address.
{
// Determine where to write the value.
size_t start_idx = 0;
size_t iface_arg_size = 0;
l_get_arg_offset_and_size(kernel, arg_index, &start_idx, &iface_arg_size);
// We would write beyond the end of the array!
// Kinda late to inform the user... Maybe it should happen at kernel
// creation time, or at system initialization...
#ifndef REMOVE_VALID_CHECKS
if ((start_idx + iface_arg_size) > kernel->arg_value_size) {
ERR_RET(CL_INVALID_KERNEL, context,
"Argument overflows the space allocated for kernel arguments");
}
#endif
// If the board has both SVM and DGM, make sure kernel argument is DGM
if (arg_info->addr_space == ACL_ARG_ADDR_GLOBAL ||
arg_info->addr_space == ACL_ARG_ADDR_CONSTANT) {
cl_bool context_has_device_with_physical_mem = CL_FALSE;
cl_bool context_has_device_with_svm = CL_FALSE;
for (unsigned idevice = 0; idevice < context->num_devices; ++idevice) {
if (acl_svm_device_supports_physical_memory(
context->device[idevice]->def.physical_device_id)) {
context_has_device_with_physical_mem = CL_TRUE;
break;
}
}
for (unsigned idevice = 0; idevice < context->num_devices; ++idevice) {
if (acl_svm_device_supports_any_svm(
context->device[idevice]->def.physical_device_id)) {
context_has_device_with_svm = CL_TRUE;
break;
}
}
if (context_has_device_with_svm && context_has_device_with_physical_mem &&
kernel->dev_bin->get_devdef()
.autodiscovery_def.num_global_mem_systems > 1 &&
!l_check_mem_type_support_on_kernel_arg(
kernel, arg_index, ACL_GLOBAL_MEM_DEVICE_PRIVATE)) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"cl_mem object was set on kernel argument that doesn't "
"have attribute to access device private memory");
}
}
// If using heterogeneous memory, try to set a default mem_id for the cl_mem
// we are using This is optimization
if (arg_info->addr_space == ACL_ARG_ADDR_GLOBAL) {
// We need to check if this arg is assigned to the correct memory
if (arg_value &&
(*(cl_mem *)arg_value)) { // Can pass in a NULL pointer or pointer to
// NULL, to provide NULL arg
cl_mem mem = *(cl_mem *)arg_value;
if (((mem->flags & CL_MEM_HETEROGENEOUS_INTELFPGA) ||
!arg_info->buffer_location.empty()) &&
mem->allocation_deferred) {
if (!arg_info->buffer_location.empty()) {
for (unsigned gmem_idx = 0;
gmem_idx < kernel->dev_bin->get_devdef()
.autodiscovery_def.num_global_mem_systems;
gmem_idx++) {
// Look for a buffer, if we found this one, use it
assert(!kernel->dev_bin->get_devdef()
.autodiscovery_def.global_mem_defs[gmem_idx]
.name.empty());
if (arg_info->buffer_location ==
kernel->dev_bin->get_devdef()
.autodiscovery_def.global_mem_defs[gmem_idx]
.name) {
// This is actually just a *HINT* since the allocation hasn't
// happened yet !
mem->mem_id = gmem_idx;
break;
}
}
}
}
}
}
if (arg_info->addr_space != ACL_ARG_ADDR_LOCAL) {
if (arg_value == 0) {
// Example: NULL arg for __global or __constant.
// Store a zero value for the pointer.
cl_ulong null_ptr = 0;
safe_memcpy(&(kernel->arg_value[start_idx]), &null_ptr, iface_arg_size,
kernel->arg_value_size - start_idx, iface_arg_size);
kernel->arg_is_svm[arg_index] = CL_FALSE;
kernel->arg_is_ptr[arg_index] = CL_FALSE;
} else if (is_sampler) {
cl_sampler sampler = *(cl_sampler *)arg_value;
assert(sampler != NULL);
int sampler_bitfield = 0;
sampler_bitfield = 0;
switch (sampler->normalized_coords) {
case CL_TRUE:
sampler_bitfield |= CLK_NORMALIZED_COORDS_TRUE;
break;
case CL_FALSE:
sampler_bitfield |= CLK_NORMALIZED_COORDS_FALSE;
break;
// Default is CL_TRUE
default:
sampler_bitfield |= CLK_NORMALIZED_COORDS_TRUE;
break;
}
switch (sampler->addressing_mode) {
case CL_ADDRESS_NONE:
sampler_bitfield |= CLK_ADDRESS_NONE;
break;
case CL_ADDRESS_MIRRORED_REPEAT:
sampler_bitfield |= CLK_ADDRESS_MIRRORED_REPEAT;
break;
case CL_ADDRESS_REPEAT:
sampler_bitfield |= CLK_ADDRESS_REPEAT;
break;
case CL_ADDRESS_CLAMP_TO_EDGE:
sampler_bitfield |= CLK_ADDRESS_CLAMP_TO_EDGE;
break;
case CL_ADDRESS_CLAMP:
sampler_bitfield |= CLK_ADDRESS_CLAMP;
break;
// Default is CL_ADDRESS_CLAMP
default:
sampler_bitfield |= CLK_ADDRESS_CLAMP;
break;
}
switch (sampler->filter_mode) {
case CL_FILTER_NEAREST:
sampler_bitfield |= CLK_FILTER_NEAREST;
break;
case CL_FILTER_LINEAR:
sampler_bitfield |= CLK_FILTER_LINEAR;
break;
// Default is CL_FILTER_NEAREST
default:
sampler_bitfield |= CLK_FILTER_NEAREST;
break;
}
safe_memcpy(&(kernel->arg_value[start_idx]), &sampler_bitfield,
iface_arg_size, kernel->arg_value_size - start_idx,
iface_arg_size);
kernel->arg_is_svm[arg_index] = CL_FALSE;
kernel->arg_is_ptr[arg_index] = CL_FALSE;
} else {
safe_memcpy(&(kernel->arg_value[start_idx]), arg_value, iface_arg_size,
kernel->arg_value_size - start_idx, iface_arg_size);
kernel->arg_is_svm[arg_index] = CL_FALSE;
kernel->arg_is_ptr[arg_index] = CL_FALSE;
}
} else {
// A LOCAL param is an integer saying how many bytes the local
// storage should be.
// The number of bytes is specified by the ***arg_size*** argument.
safe_memcpy(&(kernel->arg_value[start_idx]), &arg_size, iface_arg_size,
kernel->arg_value_size - start_idx, iface_arg_size);
kernel->arg_is_svm[arg_index] = CL_FALSE;
kernel->arg_is_ptr[arg_index] = CL_FALSE;
}
kernel->arg_defined[arg_index] = 1;
}
return CL_SUCCESS;
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clSetKernelArg(cl_kernel kernel,
cl_uint arg_index,
size_t arg_size,
const void *arg_value) {
return clSetKernelArgIntelFPGA(kernel, arg_index, arg_size, arg_value);
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clSetKernelArgSVMPointerIntelFPGA(
cl_kernel kernel, cl_uint arg_index, const void *arg_value) {
cl_context context;
std::scoped_lock lock{acl_mutex_wrapper};
#ifndef REMOVE_VALID_CHECKS
if (!acl_kernel_is_valid(kernel)) {
return CL_INVALID_KERNEL;
}
context = kernel->program->context;
if (arg_index >= kernel->accel_def->iface.args.size()) {
ERR_RET(CL_INVALID_ARG_INDEX, context, "Argument index is too large");
}
if (arg_value == NULL) {
ERR_RET(CL_INVALID_ARG_VALUE, context, "SVM argument is NULL");
}
unsigned expected_alignment =
kernel->accel_def->iface.args[arg_index].alignment;
expected_alignment =
expected_alignment ? expected_alignment : ACL_MEM_ALIGN; // For tests
if ((uintptr_t)arg_value % expected_alignment != 0) {
if (expected_alignment == ACL_MEM_ALIGN) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"SVM argument is not aligned correctly for type. Ensure the "
"kernel argument is targeting the correct buffer location.");
} else {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"SVM argument is not aligned correctly for type.");
}
}
#endif
// Now try saving the value.
{
// Determine where to write the value.
size_t start_idx = 0;
size_t iface_arg_size = 0;
l_get_arg_offset_and_size(kernel, arg_index, &start_idx, &iface_arg_size);
// We would write beyond the end of the array!
// Kinda late to inform the user... Maybe it should happen at kernel
// creation time, or at system initialization...
#ifndef REMOVE_VALID_CHECKS
if ((start_idx + iface_arg_size) > kernel->arg_value_size) {
ERR_RET(CL_INVALID_KERNEL, context,
"Argument overflows the space allocated for kernel arguments");
}
// If the board has both SVM and DGM, make sure kernel argument is SVM
cl_bool context_has_device_with_physical_mem = CL_FALSE;
cl_bool context_has_device_with_svm = CL_FALSE;
for (unsigned idevice = 0; idevice < context->num_devices; ++idevice) {
if (acl_svm_device_supports_physical_memory(
context->device[idevice]->def.physical_device_id)) {
context_has_device_with_physical_mem = CL_TRUE;
break;
}
}
for (unsigned idevice = 0; idevice < context->num_devices; ++idevice) {
if (acl_svm_device_supports_any_svm(
context->device[idevice]->def.physical_device_id)) {
context_has_device_with_svm = CL_TRUE;
break;
}
}
if (context_has_device_with_svm && context_has_device_with_physical_mem &&
kernel->dev_bin->get_devdef().autodiscovery_def.num_global_mem_systems >
1 &&
!l_check_mem_type_support_on_kernel_arg(
kernel, arg_index, ACL_GLOBAL_MEM_SHARED_VIRTUAL)) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"SVM pointer was set on kernel argument that doesn't have "
"attribute to access SVM");
}
#endif
// Track usage of buffers.
// Note: The OpenCL 1.2 spec forbids the kernel object from updating
// reference counts for the arguments. See clSetKernelArg spec.
// We just have to trust the user to not release the memory object too
// early.
safe_memcpy(&(kernel->arg_value[start_idx]), &arg_value, iface_arg_size,
kernel->arg_value_size - start_idx, iface_arg_size);
kernel->arg_is_svm[arg_index] = CL_TRUE;
kernel->arg_is_ptr[arg_index] = CL_TRUE;
kernel->arg_defined[arg_index] = 1;
}
return CL_SUCCESS;
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clSetKernelArgSVMPointer(
cl_kernel kernel, cl_uint arg_index, const void *arg_value) {
return clSetKernelArgSVMPointerIntelFPGA(kernel, arg_index, arg_value);
}
/**
* Set any provided void pointer as kernel arguments
*
* It is assumed that the provided pointer is a valid device address,
* or device global address that kernel can use to point to right address space.
*
* It is the same as `clSetKernelArgMemPointerINTEL` except the validity checks
* are removed. This is because the user provided pointer may not always be usm
* pointer, therefore will not belong to the context (as they are checked in
* clSetKernelArgMemPointerINTEL)
*
* @param kernel the kernel that accept the pointer arg
* @param arg_index which kernel argument accept the value
* @param arg_value the pointer to desired address space
* @return status code, CL_SUCCESS if all operations are successful.
*/
cl_int set_kernel_arg_mem_pointer_without_checks(cl_kernel kernel,
cl_uint arg_index,
void *arg_value) {
std::scoped_lock lock{acl_mutex_wrapper};
if (!acl_kernel_is_valid(kernel)) {
return (CL_INVALID_KERNEL);
}
cl_context context = kernel->program->context;
if (arg_index >= kernel->accel_def->iface.args.size()) {
ERR_RET(CL_INVALID_ARG_INDEX, context, "Argument index is too large");
}
// Determine where to write the value.
size_t start_idx = 0;
size_t iface_arg_size = 0;
l_get_arg_offset_and_size(kernel, arg_index, &start_idx, &iface_arg_size);
safe_memcpy(&(kernel->arg_value[start_idx]), &arg_value, iface_arg_size,
kernel->arg_value_size - start_idx, iface_arg_size);
kernel->arg_is_svm[arg_index] = CL_FALSE;
kernel->arg_is_ptr[arg_index] = CL_TRUE;
kernel->arg_defined[arg_index] = 1;
// double vector size if size < arg_index
while (kernel->ptr_arg_vector.size() <= arg_index) {
kernel->ptr_arg_vector.resize(kernel->ptr_arg_vector.size() * 2);
}
kernel->ptr_arg_vector[arg_index] = arg_value;
return (CL_SUCCESS);
}
ACL_EXPORT
CL_API_ENTRY cl_int CL_API_CALL clSetKernelArgMemPointerINTEL(
cl_kernel kernel, cl_uint arg_index, const void *arg_value) {
std::scoped_lock lock{acl_mutex_wrapper};
if (!acl_kernel_is_valid(kernel)) {
return CL_INVALID_KERNEL;
}
cl_context context = kernel->program->context;
if (arg_index >= kernel->accel_def->iface.args.size()) {
ERR_RET(CL_INVALID_ARG_INDEX, context, "Argument index is too large");
}
// Determine where to write the value.
size_t start_idx = 0;
size_t iface_arg_size = 0;
l_get_arg_offset_and_size(kernel, arg_index, &start_idx, &iface_arg_size);
// We would write beyond the end of the array!
// Kinda late to inform the user... Maybe it should happen at kernel
// creation time, or at system initialization...
#ifndef REMOVE_VALID_CHECKS
if ((start_idx + iface_arg_size) > kernel->arg_value_size) {
ERR_RET(CL_INVALID_KERNEL, context,
"Argument overflows the space allocated for kernel arguments");
}
unsigned expected_alignment =
kernel->accel_def->iface.args[arg_index].alignment;
expected_alignment =
expected_alignment ? expected_alignment : ACL_MEM_ALIGN; // For tests
if ((uintptr_t)arg_value % expected_alignment != 0) {
if (expected_alignment == ACL_MEM_ALIGN) {
ERR_RET(
CL_INVALID_ARG_VALUE, context,
"Pointer argument is not aligned correctly for type. If you are "
"using unified shared memory compile the kernel with the -usm flag.");
} else {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"Pointer argument is not aligned correctly for type.");
}
}
if (!acl_usm_ptr_belongs_to_context(context, arg_value)) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"Pointer argument is not allocated using USM or not "
"allocated in correct context.");
}
// Ensure the USM allocation (arg_value) is compatible with what the kernel
// argument is expecting.
//
// All information we have about the global memory system comes from the
// autodiscovery string now.
//
// We know which interface the kernel argument is connected to.
//
// However, all we know about the USM allocation is which type of allocation
// it is (host, shared, or device). Assuming the board_spec.xml has the
// allocation_type attribute set on the appropriate interfaces we can
// determine which interface this will correspond to.
unsigned kernel_arg_mem_id = l_get_kernel_arg_mem_id(kernel, arg_index);
const auto *kernel_arg_mem =
&kernel->dev_bin->get_devdef()
.autodiscovery_def.global_mem_defs[kernel_arg_mem_id];
acl_usm_allocation_t *usm_alloc =
acl_get_usm_alloc_from_ptr(context, arg_value);
if (usm_alloc == nullptr) {
ERR_RET(CL_INVALID_ARG_VALUE, context,
"Pointer argument is not allocated using USM or not "
"allocated in correct context.");
}
// Try to find the memory interface that corresponds to this allocation.
const acl_system_global_mem_def_t *allocation_mem = nullptr;
for (unsigned gmem_idx = 0;
gmem_idx <
kernel->dev_bin->get_devdef().autodiscovery_def.num_global_mem_systems;
gmem_idx++) {
auto allocation_type = kernel->dev_bin->get_devdef()
.autodiscovery_def.global_mem_defs[gmem_idx]
.allocation_type;
if ((allocation_type & ACL_GLOBAL_MEM_HOST_ALLOCATION &&
usm_alloc->type == CL_MEM_TYPE_HOST_INTEL) ||
(allocation_type & ACL_GLOBAL_MEM_SHARED_ALLOCATION &&
usm_alloc->type == CL_MEM_TYPE_SHARED_INTEL) ||
(allocation_type & ACL_GLOBAL_MEM_DEVICE_ALLOCATION &&
usm_alloc->type == CL_MEM_TYPE_DEVICE_INTEL)) {
allocation_mem = &kernel->dev_bin->get_devdef()
.autodiscovery_def.global_mem_defs[gmem_idx];
break;
}
}
// We will only be able to perform these checks if the "allocation_type" field
// was set for the appropriate interfaces in the board_spec.xml and it is an
// OpenCL compile. Compiler assumes unlabeled pointers are device global mem.
// This assumption is only true for OpenCL compiles.
if (allocation_mem && kernel->accel_def->is_sycl_compile == 0) {
if (kernel_arg_mem->allocation_type ==
ACL_GLOBAL_MEM_UNDEFINED_ALLOCATION) {
// The allocation_type field was not indicated in the board_spec.xml for
// the interface associated with this argument, or it does not contain an
// expected value.
acl_context_callback(
context,
"Warning: Unable to determine expected USM "
"allocation type for this kernel argument. Functional or performance"
" issues may be encountered.");
}
if (usm_alloc->type == CL_MEM_TYPE_DEVICE_INTEL) {
if (!(kernel_arg_mem->allocation_type &