-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_object.c
1345 lines (1131 loc) · 40.5 KB
/
mem_object.c
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
/*
* @file mem_object.c
* @brief Provides basic abstraction for OpenCL memory object
*
* @see mem_object.h
*
* Copyright 2014 by Samsung Electronics, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. */
#include "mem_object.h"
#include "steel_thread.h"
#include <stdlib.h>
#include <string.h>
/*! \cond PRIVATE
* Gather event execution time in microseconds
*/
static cl_double Gather_Time_uS(cl_event* event)
{
cl_double no_time;
cl_ulong start, end;
no_time = -1.0;
OCL_CHECK_EXISTENCE(event, no_time);
clWaitForEvents(1, event);
start = end = 0;
clGetEventProfilingInfo(*event, CL_PROFILING_COMMAND_START,
sizeof(cl_ulong), &start, NULL);
clGetEventProfilingInfo(*event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong),
&end, NULL);
return (cl_double) (end - start) * (cl_double) (1e-03);
}
/*! \endcond */
/**
* \related cl_Mem_Object_t
*
* This function destroys child structures & release allocated memory
*
* @param[in,out] self pointer to structure, in which 'Destroy' function pointer
* is defined to point on this function.
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*
*/
static ret_code Mem_Object_Destroy(scow_Mem_Object *self)
{
cl_int ret = CL_SUCCESS;
OCL_CHECK_EXISTENCE(self, CL_SUCCESS);
self->error->Destroy(self->error);
self->timer->Destroy(self->timer);
/* Unmap object, if mapped. Check for MEM_OBJ_NOT_MAPPED return code, as
* this is no error when destroying memory object, which may be not mapped.
*/
ret = self->Unmap(self, CL_TRUE, NULL, DONT_MEASURE, NULL, NULL);
if (ret != CL_SUCCESS && ret != MEM_OBJ_NOT_MAPPED)
{
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
}
/* Release allocated memory for OpenCL memory object. Check for error code
* CL_INVALID_MEM_OBJECT, as soon as we may go into 'Destroy()' function as
* result of failed memory object creation attempt - that isn't error.
*/
ret = clReleaseMemObject(self->cl_mem_object);
if (ret != CL_SUCCESS && ret != CL_INVALID_MEM_OBJECT)
{
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
}
free(self);
return CL_SUCCESS;
}
/**
* \related cl_Mem_Object_t
*
* This function returns pointer to OpenCL memory object
*
* @param[in,out] self pointer to structure, in which 'Get_Mem_Obj' function pointer
* is defined to point on this function.
*
* @return pointer ot OpenCL memory object of type 'cl_mem', NULL pointer in
* case of error.
*/
static cl_mem* Mem_Object_Get_Mem_Obj(scow_Mem_Object *self)
{
OCL_CHECK_EXISTENCE(self, (cl_mem* )0x0);
return &self->cl_mem_object;
}
/**
* \related cl_Mem_Object_t
*
* 1D buffer can't has width, so, if called, this function returns 0 & set
* error code.
*
* @param[in,out] self pointer to structure, in which 'Get_Height' function pointer
* is defined to point on this function.
*
* @return always 0. Also, it sets error code via
* 'error' structure pointer of structure, defined by pointer 'this'
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static size_t Buffer_Get_Width(scow_Mem_Object *self)
{
OCL_CHECK_EXISTENCE(self, 0);
self->error->Set_Last_Code(self->error,
CALLING_UNDEF_ACCESSOR);
return 0;
}
/**
* \related cl_Mem_Object_t
*
* This function returns width of OpenCL image.
*
* @param[in,out] self pointer to structure, in which 'Get_Width' function pointer
* is defined to point on this function.
*
* @return width of image in case of success. Otherwise it sets error code via
* 'error' structure pointer of structure, defined by pointer 'this'
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static size_t Image_Get_Width(scow_Mem_Object *self)
{
OCL_CHECK_EXISTENCE(self, 0);
return self->width;
}
/**
* \related cl_Mem_Object_t
*
* 1D buffer can't has height, so, if called, this function returns 0 & set
* error code.
*
* @param[in,out] self pointer to structure, in which 'Get_Height' function pointer
* is defined to point on this function.
*
* @return always 0. Also, it sets error code via
* 'error' structure pointer of structure, defined by pointer 'this'
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static size_t Buffer_Get_Height(scow_Mem_Object *self)
{
OCL_CHECK_EXISTENCE(self, 0);
self->error->Set_Last_Code(self->error,
CALLING_UNDEF_ACCESSOR);
return 0;
}
/**
* \related cl_Mem_Object_t
*
* This function returns height of OpenCL image.
*
* @param[in,out] self pointer to structure, in which 'Get_Height' function pointer
* is defined to point on this function.
*
* @return height of image in case of success. Otherwise it sets error code via
* 'error' structure pointer of structure, defined by pointer 'this'
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static size_t Image_Get_Height(scow_Mem_Object *self)
{
OCL_CHECK_EXISTENCE(self, 0);
return self->height;
}
/**
* \related cl_Mem_Object_t
*
* 1D buffer can't has row pitch, so, if called, this function returns 0 & set
* error code.
*
* @param[in,out] self pointer to structure, in which 'Get_Height' function pointer
* is defined to point on this function.
*
* @return always 0. Also, it sets error code via
* 'error' structure pointer of structure, defined by pointer 'this'
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static size_t Buffer_Get_Row_Pitch(scow_Mem_Object *self)
{
OCL_CHECK_EXISTENCE(self, 0);
self->error->Set_Last_Code(self->error, CALLING_UNDEF_ACCESSOR);
return 0;
}
/**
* \related cl_Mem_Object_t
*
* This function returns row pitch of OpenCL image.
*
* @param[in,out] self pointer to structure, in which 'Get_Row_Pitch' function pointer
* is defined to point on this function.
*
* @return row pitch of image in case of success. Otherwise it sets error code via
* 'error' structure pointer of structure, defined by pointer 'this'
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static size_t Image_Get_Row_Pitch(scow_Mem_Object *self)
{
OCL_CHECK_EXISTENCE(self, 0);
return self->row_pitch;
}
/**
* \related cl_Mem_Object_t
*
* This function maps OpenCL memory object into Host-accessible memory & returns
* pointer to mapped memory
*
* @param[in,out] self pointer to structure, in which 'Map' function pointer
* is defined to point on this function.
* @param[in] blocking_map flag of type 'cl_bool' that denotes, should operation
* be blocking or not.
* @param [in] map_flags mapping flags, that denotes how memory object should be
* mapped
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return pointer to Host-accessible region of memory in case of success, NULL
* pointer otherwise. In that case function sets error value, which is available
* through cl_Error_t structure, defined by pointer 'self->error'
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static void* Buffer_Map(
scow_Mem_Object *self,
cl_bool blocking_map,
cl_map_flags map_flags,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret;
cl_event mapping_ready, *p_mapping_ready;
OCL_CHECK_EXISTENCE(self, NULL);
if (blocking_map > CL_TRUE)
{
self->error->Set_Last_Code(self->error, INVALID_BLOCKING_FLAG);
return NULL;
}
(evt_to_generate != NULL) ?
(p_mapping_ready = evt_to_generate) :
(p_mapping_ready = &mapping_ready);
// We can't map the object, that is already mapped
if (self->mapped_to_region != NULL)
{
self->error->Set_Last_Code(self->error, BUFFER_IN_USE);
return VOID_MEM_OBJ_PTR;
}
cl_command_queue q =
(explicit_queue == NULL) ?
(self->parent_thread->q_data_dtoh) : (explicit_queue);
/* Save mapped pointer inside a structure in case if memory object is being
* destroyed without unmapping it at first.
*/
self->mapped_to_region = clEnqueueMapBuffer(q, self->cl_mem_object,
blocking_map, map_flags, 0, self->size, 0,
NULL, p_mapping_ready, &ret);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS,
self->error->Set_Last_Code(self->error, ret), NULL);
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_mapping_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
case DONT_MEASURE:
break;
default:
break;
}
if (p_mapping_ready != evt_to_generate){
clReleaseEvent(*p_mapping_ready);
}
return self->mapped_to_region;
}
/**
* \related cl_Mem_Object_t
*
* This function map OpenCL Image into Host-accessible memory & returns pointer
* to mapped memory region
* @param[in,out] self pointer to structure, in which 'Map' function pointer
* is defined to point on this function.
* @param[in] blocking_map flag of type 'cl_bool' that denotes, should operation
* be blocking or not.
* @param [in] map_flags mapping flags, that denotes how memory object should be
* mapped
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return pointer to Host-accessible region of memory in case of success, NULL
* pointer otherwise. In that case function sets error value, which is available
* through cl_Error_t structure, defined by pointer 'self->error'
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static void* Image_Map(
scow_Mem_Object *self,
cl_bool blocking_map,
cl_map_flags map_flags,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret;
cl_event mapping_ready, *p_mapping_ready;
const size_t origin[3] =
{ 0, 0, 0 }, region[3] =
{ self->width, self->height, 1 };
OCL_CHECK_EXISTENCE(self, NULL);
if (blocking_map > CL_TRUE)
{
self->error->Set_Last_Code(self->error, INVALID_BLOCKING_FLAG);
return NULL;
}
(evt_to_generate != NULL) ?
(p_mapping_ready = evt_to_generate) : (p_mapping_ready =
&mapping_ready);
// We can't map the object, that is already mapped
if (self->mapped_to_region != NULL)
{
self->error->Set_Last_Code(self->error, BUFFER_IN_USE);
return VOID_MEM_OBJ_PTR;
}
cl_command_queue q =
(explicit_queue == NULL) ?
(self->parent_thread->q_data_dtoh) : (explicit_queue);
/* Save mapped pointer inside a structure in case if memory object is being
* destroyed without unmapping it at first.
*/
self->mapped_to_region = clEnqueueMapImage(q, self->cl_mem_object,
blocking_map, map_flags, origin, region, &self->row_pitch, NULL, 0,
NULL, p_mapping_ready, &ret);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS,
self->error->Set_Last_Code(self->error, ret), NULL);
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_mapping_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
case DONT_MEASURE:
break;
default:
break;
}
if (p_mapping_ready != evt_to_generate){
clReleaseEvent(*p_mapping_ready);
}
return self->mapped_to_region;
}
/**
* \related cl_Mem_Object_t
*
* This fucntion unmaps previously mapped memory for OpenCL buffer.
*
* @param[in,out] self pointer to structure, in which 'Unmap' function pointer
* is defined to point on this function.
* @param[out] p_mapped_ptr pointer to pointer, that was returned as the
* result of mapping operation.
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed.
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static ret_code Mem_Object_Unmap(
scow_Mem_Object *self,
cl_bool blocking_map,
void **p_mapped_ptr,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret;
cl_event *p_unmapping_ready;
OCL_CHECK_EXISTENCE(self, INVALID_BUFFER_GIVEN);
OCL_CHECK_EXISTENCE(self->mapped_to_region, MEM_OBJ_NOT_MAPPED);
if (p_mapped_ptr)
{
OCL_CHECK_EXISTENCE(*p_mapped_ptr, INVALID_BUFFER_GIVEN);
/* Check if we are trying to unmap pointer, that was mapped from different
* Memory Object. */
if (self->mapped_to_region != *p_mapped_ptr)
{
OCL_DIE_ON_ERROR(WRONG_PARENT_OBJECT, CL_SUCCESS, NULL,
WRONG_PARENT_OBJECT);
}
}
/* We generate event in any case - because later we may want to wait for
* unmapping completion. */
(evt_to_generate != NULL) ?
(p_unmapping_ready = evt_to_generate) :
(p_unmapping_ready = &self->unmap_evt);
cl_command_queue q =
(explicit_queue == NULL) ?
(self->parent_thread->q_data_htod) : (explicit_queue);
ret = clEnqueueUnmapMemObject(q, self->cl_mem_object,
self->mapped_to_region, 0, NULL, p_unmapping_ready);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
self->mapped_to_region = NULL;
self->row_pitch = 0;
if (p_mapped_ptr != NULL)
{
*p_mapped_ptr = NULL;
}
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_unmapping_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
case DONT_MEASURE:
break;
default:
if (blocking_map)
{
ret = clWaitForEvents(1, p_unmapping_ready);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
}
break;
}
if (p_unmapping_ready != evt_to_generate){
clReleaseEvent(*p_unmapping_ready);
}
return ret;
}
/**
* \related cl_Mem_Object_t
*
* This function read data from Host-accessible memory region, defined by argument
* 'source' & write that data into OpenCL buffer memory object, defined by
* argument 'self'
*
* @param[in,out] self pointer to structure, in which 'Write' function pointer
* is defined to point on this function.
* @param[in] blocking_flag flag, that denotes, should operation be blocking or not.
* @param[in] source pointer to Host-accessible memory region, that
* contain data to be write in OpenCL memory object.
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed.
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static ret_code Buffer_Send_To_Device(
scow_Mem_Object *self,
cl_bool blocking_flag,
void *source,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret = CL_SUCCESS;
cl_event write_ready, *p_write_ready = (cl_event*) 0x0;
OCL_CHECK_EXISTENCE(self, INVALID_BUFFER_GIVEN);
OCL_CHECK_EXISTENCE(source, INVALID_BUFFER_GIVEN);
(evt_to_generate != NULL) ?
(p_write_ready = evt_to_generate) :
(p_write_ready = &write_ready);
cl_command_queue q = (explicit_queue == NULL) ?
(self->parent_thread->q_data_htod) :
(explicit_queue);
ret = clEnqueueWriteBuffer(q, self->cl_mem_object, blocking_flag, 0,
self->size, source, 0, NULL, p_write_ready);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_write_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
case DONT_MEASURE:
break;
default:
break;
}
if (p_write_ready != evt_to_generate){
clReleaseEvent(*p_write_ready);
}
return ret;
}
/**
* \related cl_Mem_Object_t
*
* This function read data from Host-accessible memory region, defined by argument
* 'source' & write that data into OpenCL Image memory object, definded by
* argument'self'
* @param[in,out] self pointer to structure, in which 'Write' function pointer
* is defined to point on this function.
* @param[in] blocking_flag flag, that denotes, should operation be blocking or not.
* @param[in] source pointer to Host-accessible memory region, that
* contain data to be write in OpenCL memory object.
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed.
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static ret_code Image_Send_To_Device(
scow_Mem_Object *self,
cl_bool blocking_flag,
void *source,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret = CL_SUCCESS;
cl_event write_ready, *p_write_ready = (cl_event*) 0x0;
OCL_CHECK_EXISTENCE(self, INVALID_BUFFER_GIVEN);
OCL_CHECK_EXISTENCE(source, INVALID_BUFFER_GIVEN);
const size_t origin[3] =
{ 0, 0, 0 }, region[3] =
{ self->width, self->height, 1 };
(evt_to_generate != NULL) ?
(p_write_ready = evt_to_generate) : (p_write_ready = &write_ready);
cl_command_queue q =
(explicit_queue == NULL) ?
(self->parent_thread->q_data_htod) : (explicit_queue);
ret = clEnqueueWriteImage(q, self->cl_mem_object, blocking_flag, origin,
region, self->row_pitch, 0, source, 0, NULL, p_write_ready);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_write_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
case DONT_MEASURE:
break;
default:
break;
}
if (p_write_ready != evt_to_generate){
clReleaseEvent(*p_write_ready);
}
return ret;
}
/**
* \related cl_Mem_Object_t
*
* This function reads OpenCL buffer memory object & write that data into
* Host-accessible memory region.
*
* @param[in,out] self pointer to structure, in which 'Read' function pointer
* is defined to point on this function.
* @param[in] blocking_flag flag, that denotes, should operation be blocking or not.
* @param[out] destination pointer to Host-accessible memory region, where
* data from OpenCL memory object will be written to
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed.
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static ret_code Buffer_Get_From_Device(
scow_Mem_Object *self,
cl_bool blocking_flag,
void *destination,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret = CL_SUCCESS;
cl_event read_ready, *p_read_ready = (cl_event*) 0x0;
OCL_CHECK_EXISTENCE(self, INVALID_BUFFER_GIVEN);
OCL_CHECK_EXISTENCE(destination, INVALID_BUFFER_GIVEN);
(evt_to_generate != NULL) ?
(p_read_ready = evt_to_generate) : (p_read_ready = &read_ready);
cl_command_queue q =
(explicit_queue == NULL) ?
(self->parent_thread->q_data_dtoh) : (explicit_queue);
ret = clEnqueueReadBuffer(q, self->cl_mem_object, blocking_flag, 0,
self->size, destination, 0, NULL, p_read_ready);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_read_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
case DONT_MEASURE:
break;
default:
break;
}
if (p_read_ready != evt_to_generate){
clReleaseEvent(*p_read_ready);
}
return ret;
}
/**
* \related cl_Mem_Object_t
*
* This function reads OpenCL Image memory object & write that data into
* Host-accessible memory region.
*
* @param[in,out] self pointer to structure, in which 'Read' function pointer
* is defined to point on this function.
* @param[in] blocking_flag flag, that denotes, should operation be blocking or not.
* @param[out] destination pointer to Host-accessible memory region, where
* data from OpenCL memory object will be written to
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed.
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static ret_code Image_Get_From_Device(
scow_Mem_Object *self,
cl_bool blocking_flag,
void *destination,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret = CL_SUCCESS;
cl_event read_ready, *p_read_ready = (cl_event*) 0x0;
OCL_CHECK_EXISTENCE(self, INVALID_BUFFER_GIVEN);
OCL_CHECK_EXISTENCE(destination, INVALID_BUFFER_GIVEN);
const size_t origin[3] =
{ 0, 0, 0 }, region[3] =
{ self->width, self->height, 1 };
(evt_to_generate != NULL) ?
(p_read_ready = evt_to_generate) : (p_read_ready = &read_ready);
cl_command_queue q =
(explicit_queue == NULL) ?
(self->parent_thread->q_data_dtoh) : (explicit_queue);
ret = clEnqueueReadImage(q, self->cl_mem_object, blocking_flag, origin,
region, self->row_pitch, 0, destination, 0, NULL, p_read_ready);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_read_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
case DONT_MEASURE:
break;
default:
break;
}
if (p_read_ready != evt_to_generate){
clReleaseEvent(*p_read_ready);
}
return ret;
}
/**
* \related cl_Mem_Object_t
*
* This function copies content of one OpenCL buffer memory object into another.
*
* @param[in,out] self pointer to structure, in which 'Copy' function pointer
* is defined to point on this function.
* @param[out] dest pointer to another Memory Object structure, where the data
* from 'self' will be copied to.
* @param[in] blocking_flag flag, that denotes, should operation be blocking or not.
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed.
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static ret_code Buffer_Copy(
scow_Mem_Object *self,
scow_Mem_Object *dest,
cl_bool blocking_flag,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret = CL_SUCCESS;
cl_event copy_ready, *p_copy_ready = (cl_event*) 0x0;
OCL_CHECK_EXISTENCE(self, INVALID_BUFFER_GIVEN);
OCL_CHECK_EXISTENCE(dest, INVALID_BUFFER_GIVEN);
// Can't copy distinct memory objects
if (self->obj_mem_type != dest->obj_mem_type)
{
return DISTINCT_MEM_OBJECTS;
}
// Can't copy bigger object into smaller one
if (self->size > dest->size)
{
return INVALID_BUFFER_SIZE;
}
// If src & dest are the same, no need to copy at all, just reset timer.
if (self == dest)
{
self->timer->current_time_device = 0;
return CL_SUCCESS;
}
(evt_to_generate == NULL) ? (p_copy_ready = ©_ready) : (p_copy_ready =
evt_to_generate);
cl_command_queue q =
(explicit_queue == NULL) ?
(self->parent_thread->q_data_dtod) : (explicit_queue);
ret = clEnqueueCopyBuffer(q, self->cl_mem_object, dest->cl_mem_object, 0, 0,
self->size, 0, NULL, p_copy_ready);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_copy_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
default:
break;
}
if (p_copy_ready != evt_to_generate){
clReleaseEvent(*p_copy_ready);
}
return ret;
}
/**
* \related cl_Mem_Object_t
*
* This function copy content of one OpenCL Image memory object into another.
* @param[in,out] self pointer to structure, in which 'Copy' function pointer
* is defined to point on this function.
* @param[out] dest pointer to another Memory Object structure, where the data
* from 'self' will be copied to.
* @param[in] blocking_flag flag, that denotes, should operation be blocking or not.
* @param[in] time_mode enumeration, that denotes how time measurement should be
* performed.
* @param[out] evt_to_generate pointer to OpenCL event that will be generated
* at the end of operation.
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.
*
* @see cl_err_codes.h for detailed error description.
* @see 'cl_Error_t' structure for error handling.
*/
static ret_code Image_Copy(
scow_Mem_Object *self,
scow_Mem_Object *dest,
cl_bool blocking_flag,
TIME_STUDY_MODE time_mode,
cl_event *evt_to_generate,
cl_command_queue explicit_queue)
{
cl_int ret = CL_SUCCESS;
cl_event copy_ready, *p_copy_ready = (cl_event*) 0x0;
const size_t origin[3] =
{ 0, 0, 0 }, region[3] =
{ self->width, self->height, 1 };
if (self->obj_mem_type != dest->obj_mem_type)
{
return DISTINCT_MEM_OBJECTS;
}
// If src & dest are the same, no need to copy at all, just reset timer
if (self == dest)
{
self->timer->current_time_device = 0;
self->timer->total_time_device += self->timer->current_time_device;
return CL_SUCCESS;
}
(evt_to_generate != NULL) ?
(p_copy_ready = evt_to_generate) : (p_copy_ready = ©_ready);
OCL_CHECK_EXISTENCE(self, INVALID_BUFFER_GIVEN);
OCL_CHECK_EXISTENCE(dest, INVALID_BUFFER_GIVEN);
// Can't copy bigger image into smaller one
if ((self->row_pitch > dest->row_pitch) || (self->height > dest->height)
|| (self->width > dest->width))
{
return INVALID_BUFFER_SIZE;
}
cl_command_queue q =
(explicit_queue == NULL) ?
(self->parent_thread->q_data_dtod) : (explicit_queue);
ret = clEnqueueCopyImage(q, self->cl_mem_object, dest->cl_mem_object,
origin, origin, region, 0, NULL, p_copy_ready);
OCL_DIE_ON_ERROR(ret, CL_SUCCESS, NULL, ret);
switch (time_mode)
{
case MEASURE:
self->timer->current_time_device = Gather_Time_uS(p_copy_ready);
self->timer->total_time_device += self->timer->current_time_device;
break;
case DONT_MEASURE:
break;
default:
break;
}
if (p_copy_ready != evt_to_generate){
clReleaseEvent(*p_copy_ready);
}
return ret;
}
/**
* \related cl_Mem_Object_t
*
* This function swaps pointers to OpenCL memory objects. It can be used in
* some cases as quicker alternative to copy.
*
* @param[in,out] self pointer to structure, in which 'Swap' function pointer
* is defined to point on this function.
* @param[in,out] dest pointer to structure with which memory objects will be
* swapped
*
* @return CL_SUCCESS in case of success, error code of type 'ret_code' otherwise.