|
8 | 8 |
|
9 | 9 | #include "../thread_safety/ThreadUtils.h"
|
10 | 10 | #include <detail/compression.hpp>
|
| 11 | +#include <detail/device_binary_image.hpp> |
11 | 12 | #include <sycl/sycl.hpp>
|
12 | 13 |
|
13 | 14 | #include <string>
|
@@ -113,3 +114,61 @@ TEST(CompressionTest, ConcurrentCompressionDecompression) {
|
113 | 114 | ::ThreadPool MPool(ThreadCount, testCompressDecompress);
|
114 | 115 | }
|
115 | 116 | }
|
| 117 | + |
| 118 | +// Test to decompress CompressedRTDeviceImage using multiple threads. |
| 119 | +// The idea behind this test is to ensure that a device image is |
| 120 | +// decompressed only once even if multiple threads try to decompress |
| 121 | +// it at the same time. |
| 122 | +TEST(CompressionTest, ConcurrentDecompressionOfDeviceImage) { |
| 123 | + // Data to compress. |
| 124 | + std::string data = "Hello World! I'm about to get compressed :P"; |
| 125 | + |
| 126 | + // Compress this data. |
| 127 | + size_t compressedSize = 0; |
| 128 | + auto compressedData = ZSTDCompressor::CompressBlob(data.c_str(), data.size(), |
| 129 | + compressedSize, 1); |
| 130 | + |
| 131 | + unsigned char *compressedDataPtr = |
| 132 | + reinterpret_cast<unsigned char *>(compressedData.get()); |
| 133 | + |
| 134 | + const char *EntryName = "Entry"; |
| 135 | + _sycl_offload_entry_struct EntryStruct = { |
| 136 | + /*addr*/ nullptr, const_cast<char *>(EntryName), strlen(EntryName), |
| 137 | + /*flags*/ 0, /*reserved*/ 0}; |
| 138 | + sycl_device_binary_struct BinStruct{/*Version*/ 1, |
| 139 | + /*Kind*/ 4, |
| 140 | + /*Format*/ SYCL_DEVICE_BINARY_TYPE_SPIRV, |
| 141 | + /*DeviceTargetSpec*/ nullptr, |
| 142 | + /*CompileOptions*/ nullptr, |
| 143 | + /*LinkOptions*/ nullptr, |
| 144 | + /*ManifestStart*/ nullptr, |
| 145 | + /*ManifestEnd*/ nullptr, |
| 146 | + /*BinaryStart*/ compressedDataPtr, |
| 147 | + /*BinaryEnd*/ compressedDataPtr + |
| 148 | + compressedSize, |
| 149 | + /*EntriesBegin*/ &EntryStruct, |
| 150 | + /*EntriesEnd*/ &EntryStruct + 1, |
| 151 | + /*PropertySetsBegin*/ nullptr, |
| 152 | + /*PropertySetsEnd*/ nullptr}; |
| 153 | + sycl_device_binary Bin = &BinStruct; |
| 154 | + CompressedRTDeviceBinaryImage Img{Bin}; |
| 155 | + |
| 156 | + // Decompress the image with multiple threads. |
| 157 | + constexpr size_t ThreadCount = 20; |
| 158 | + Barrier b(ThreadCount); |
| 159 | + { |
| 160 | + auto testDecompress = [&](size_t threadId) { |
| 161 | + b.wait(); |
| 162 | + Img.Decompress(); |
| 163 | + |
| 164 | + // Check if decompressed data is same as original data. |
| 165 | + // Img.getRawData will change if there's a race in image decompression |
| 166 | + // and the check will fail. |
| 167 | + for (size_t i = 0; i < Img.getSize(); ++i) { |
| 168 | + ASSERT_EQ(data[i], Img.getRawData().BinaryStart[i]); |
| 169 | + } |
| 170 | + }; |
| 171 | + |
| 172 | + ::ThreadPool MPool(ThreadCount, testDecompress); |
| 173 | + } |
| 174 | +} |
0 commit comments