Skip to content

Commit 8ed5566

Browse files
authored
[SYCL] Add get_range accessor method (#1050)
Align with the change in the spec KhronosGroup/SYCL-Docs#31. Image Accessors: Removed get_size() method and added get_range() method. Local Accessors: Added get_range() method. Signed-off-by: Garima Gupta <[email protected]>
1 parent defe007 commit 8ed5566

File tree

5 files changed

+121
-78
lines changed

5 files changed

+121
-78
lines changed

sycl/include/CL/sycl/accessor.hpp

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ class image_accessor
267267
#ifndef __SYCL_DEVICE_ONLY__
268268
: public detail::AccessorBaseHost {
269269
size_t MImageCount;
270-
size_t MImageSize;
271270
image_channel_order MImgChannelOrder;
272271
image_channel_type MImgChannelType;
273272
#else
@@ -277,9 +276,8 @@ class image_accessor
277276
AccessTarget>::type;
278277
OCLImageTy MImageObj;
279278
char MPadding[sizeof(detail::AccessorBaseHost) +
280-
sizeof(size_t /*MImageSize*/) + sizeof(size_t /*MImageCount*/) +
281-
sizeof(image_channel_order) + sizeof(image_channel_type) -
282-
sizeof(OCLImageTy)];
279+
sizeof(size_t /*MImageCount*/) + sizeof(image_channel_order) +
280+
sizeof(image_channel_type) - sizeof(OCLImageTy)];
283281

284282
protected:
285283
void imageAccessorInit(OCLImageTy Image) { MImageObj = Image; }
@@ -342,7 +340,7 @@ class image_accessor
342340

343341
#ifdef __SYCL_DEVICE_ONLY__
344342

345-
sycl::vec<int, Dimensions> getCountInternal() const {
343+
sycl::vec<int, Dimensions> getRangeInternal() const {
346344
return __invoke_ImageQuerySize<sycl::vec<int, Dimensions>, OCLImageTy>(
347345
MImageObj);
348346
}
@@ -356,10 +354,10 @@ class image_accessor
356354

357355
#else
358356

359-
sycl::vec<int, Dimensions> getCountInternal() const {
357+
sycl::vec<int, Dimensions> getRangeInternal() const {
360358
// TODO: Implement for host.
361359
throw runtime_error(
362-
"image::getCountInternal() is not implemented for host");
360+
"image::getRangeInternal() is not implemented for host");
363361
return sycl::vec<int, Dimensions>{1};
364362
}
365363

@@ -397,7 +395,6 @@ class image_accessor
397395
AccessMode, detail::getSyclObjImpl(ImageRef).get(),
398396
Dimensions, ImageElementSize),
399397
MImageCount(ImageRef.get_count()),
400-
MImageSize(MImageCount * ImageElementSize),
401398
MImgChannelOrder(detail::getSyclObjImpl(ImageRef)->getChannelOrder()),
402399
MImgChannelType(detail::getSyclObjImpl(ImageRef)->getChannelType()) {
403400
detail::EventImplPtr Event =
@@ -429,7 +426,6 @@ class image_accessor
429426
AccessMode, detail::getSyclObjImpl(ImageRef).get(),
430427
Dimensions, ImageElementSize),
431428
MImageCount(ImageRef.get_count()),
432-
MImageSize(MImageCount * ImageElementSize),
433429
MImgChannelOrder(detail::getSyclObjImpl(ImageRef)->getChannelOrder()),
434430
MImgChannelType(detail::getSyclObjImpl(ImageRef)->getChannelType()) {
435431
checkDeviceFeatureSupported<info::device::image_support>(
@@ -455,32 +451,39 @@ class image_accessor
455451
// get_count() method : Returns the number of elements of the SYCL image this
456452
// SYCL accessor is accessing.
457453
//
458-
// get_size() method : Returns the size in bytes of the SYCL image this SYCL
459-
// accessor is accessing. Returns ElementSize*get_count().
454+
// get_range() method : Returns a range object which represents the number of
455+
// elements of dataT per dimension that this accessor may access.
456+
// The range object returned must equal to the range of the image this
457+
// accessor is associated with.
460458

461459
#ifdef __SYCL_DEVICE_ONLY__
462-
size_t get_size() const {
463-
int ChannelType = __invoke_ImageQueryFormat<int, OCLImageTy>(MImageObj);
464-
int ChannelOrder = __invoke_ImageQueryOrder<int, OCLImageTy>(MImageObj);
465-
int ElementSize = getSPIRVElementSize(ChannelType, ChannelOrder);
466-
return (ElementSize * get_count());
467-
}
468460

469-
template <int Dims = Dimensions> size_t get_count() const;
461+
size_t get_count() const { return get_range<Dimensions>().size(); }
470462

471-
template <> size_t get_count<1>() const { return getCountInternal(); }
472-
template <> size_t get_count<2>() const {
473-
cl_int2 Count = getCountInternal();
474-
return (Count.x() * Count.y());
475-
};
476-
template <> size_t get_count<3>() const {
477-
cl_int3 Count = getCountInternal();
478-
return (Count.x() * Count.y() * Count.z());
479-
};
463+
template <int Dims = Dimensions, typename = detail::enable_if_t<Dims == 1>>
464+
range<1> get_range() const {
465+
cl_int Range = getRangeInternal();
466+
return range<1>(Range);
467+
}
468+
template <int Dims = Dimensions, typename = detail::enable_if_t<Dims == 2>>
469+
range<2> get_range() const {
470+
cl_int2 Range = getRangeInternal();
471+
return range<2>(Range[0], Range[1]);
472+
}
473+
template <int Dims = Dimensions, typename = detail::enable_if_t<Dims == 3>>
474+
range<3> get_range() const {
475+
cl_int3 Range = getRangeInternal();
476+
return range<3>(Range[0], Range[1], Range[3]);
477+
}
480478

481479
#else
482-
size_t get_size() const { return MImageSize; };
483480
size_t get_count() const { return MImageCount; };
481+
482+
template <int Dims = Dimensions, typename = detail::enable_if_t<(Dims > 0)>>
483+
range<Dims> get_range() const {
484+
return detail::convertToArrayOfN<Dims, 1>(getAccessRange());
485+
}
486+
484487
#endif
485488

486489
// Available only when:
@@ -566,7 +569,7 @@ class __image_array_slice__ {
566569
CoordElemType LastCoord = 0;
567570

568571
if (std::is_same<float, CoordElemType>::value) {
569-
sycl::vec<int, Dimensions + 1> Size = MBaseAcc.getCountInternal();
572+
sycl::vec<int, Dimensions + 1> Size = MBaseAcc.getRangeInternal();
570573
LastCoord =
571574
MIdx / static_cast<float>(Size.template swizzle<Dimensions>());
572575
} else {
@@ -608,27 +611,31 @@ class __image_array_slice__ {
608611
}
609612

610613
#ifdef __SYCL_DEVICE_ONLY__
611-
size_t get_size() const { return MBaseAcc.getElementSize() * get_count(); }
612-
613-
template <int Dims = Dimensions> size_t get_count() const;
614+
size_t get_count() const { return get_range<Dimensions>().size(); }
614615

615-
template <> size_t get_count<1>() const {
616-
cl_int2 Count = MBaseAcc.getCountInternal();
617-
return Count.x();
616+
template <int Dims = Dimensions, typename = detail::enable_if_t<Dims == 1>>
617+
range<1> get_range() const {
618+
cl_int2 Count = MBaseAcc.getRangeInternal();
619+
return range<1>(Count.x());
620+
}
621+
template <int Dims = Dimensions, typename = detail::enable_if_t<Dims == 2>>
622+
range<2> get_range() const {
623+
cl_int3 Count = MBaseAcc.getRangeInternal();
624+
return range<2>(Count.x(), Count.y());
618625
}
619-
template <> size_t get_count<2>() const {
620-
cl_int3 Count = MBaseAcc.getCountInternal();
621-
return (Count.x() * Count.y());
622-
};
623-
#else
624626

625-
size_t get_size() const {
626-
return MBaseAcc.MImageSize / MBaseAcc.getAccessRange()[Dimensions];
627-
};
627+
#else
628628

629629
size_t get_count() const {
630630
return MBaseAcc.MImageCount / MBaseAcc.getAccessRange()[Dimensions];
631-
};
631+
}
632+
633+
template <int Dims = Dimensions,
634+
typename = detail::enable_if_t<(Dims == 1 || Dims == 2)>>
635+
range<Dims> get_range() const {
636+
return detail::convertToArrayOfN<Dims, 1>(MBaseAcc.getAccessRange());
637+
}
638+
632639
#endif
633640

634641
private:
@@ -1099,6 +1106,11 @@ class accessor<DataT, Dimensions, AccessMode, access::target::local,
10991106

11001107
size_t get_count() const { return getSize().size(); }
11011108

1109+
template <int Dims = Dimensions, typename = detail::enable_if_t<(Dims > 0)>>
1110+
range<Dims> get_range() const {
1111+
return detail::convertToArrayOfN<Dims, 1>(getSize());
1112+
}
1113+
11021114
template <int Dims = Dimensions,
11031115
typename = detail::enable_if_t<Dims == 0 && IsAccessAnyWrite>>
11041116
operator RefType() const {

sycl/test/basic_tests/device_event.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ int test_strideN(size_t stride) {
9595
// that are not supposed to happen, but who knows..., c) to see those
9696
// values at the end if something goes wrong during the ASYNC MEM COPY.
9797
out_ptr[item.get_global_id()[0]] = item.get_global_id()[0] + 700;
98-
98+
// Just a check of get_range() API.
99+
local_acc.get_range();
99100
item.barrier();
100101

101102
// Copy from local memory to global memory.

sycl/test/basic_tests/image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int main() {
6969
TestQueue Q{sycl::default_selector()};
7070
Q.submit([&](sycl::handler &CGH) {
7171
auto ImgAcc = Img.get_access<sycl::float4, SYCLRead>(CGH);
72-
CGH.single_task<class EmptyKernel>([=]() { ImgAcc.get_size(); });
72+
CGH.single_task<class EmptyKernel>([=]() { ImgAcc.get_range(); });
7373
});
7474
}
7575

sycl/test/basic_tests/image_array.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int main() {
3232
READ_I = 0,
3333
READ_SAMPLER_F = 0,
3434
READ_SAMPLER_I = 0,
35-
GET_SIZE,
35+
GET_RANGE,
3636
GET_COUNT,
3737
WRITE1,
3838
WRITE2,
@@ -96,11 +96,11 @@ int main() {
9696
ResAcc[READ_SAMPLER_F] |= sycl::any(sycl::isnotequal(Val, ValRef));
9797
}
9898

99-
// Check that the size and count of 1D image in 1D image array == width
99+
// Check that the range and count of 1D image in 1D image array == width
100100
// of 2d image.
101101

102-
ResAcc[GET_SIZE] |= (ImgAcc.get_size() / ImgSize[1]) !=
103-
ImgArrayAcc[CoordI.y()].get_size();
102+
ResAcc[GET_RANGE] |= sycl::range<1>(ImgAcc.get_range()[0]) !=
103+
ImgArrayAcc[CoordI.y()].get_range();
104104

105105
ResAcc[GET_COUNT] |= (ImgAcc.get_count() / ImgSize[1]) !=
106106
ImgArrayAcc[CoordI.y()].get_count();

0 commit comments

Comments
 (0)