Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sycl/source/detail/compression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class ZSTDCompressor {

// Get the singleton instance of the ZSTDCompressor class.
static ZSTDCompressor &GetSingletonInstance() {
static ZSTDCompressor instance;
// Use thread_local to ensure that each thread has its own instance.
// This avoids issues with concurrent access to the ZSTD contexts.
thread_local ZSTDCompressor instance;
return instance;
}

Expand Down
34 changes: 34 additions & 0 deletions sycl/unittests/compression/CompressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//
//===----------------------------------------------------------------------===//

#include "../thread_safety/ThreadUtils.h"
#include <detail/compression.hpp>
#include <sycl/sycl.hpp>

Expand Down Expand Up @@ -79,3 +80,36 @@ TEST(CompressionTest, EmptyInputTest) {
std::string decompressedStr((char *)decompressedData.get(), decompressedSize);
ASSERT_EQ(input, decompressedStr);
}

// Test to check for concurrent compression and decompression.
TEST(CompressionTest, ConcurrentCompressionDecompression) {
std::string data = "Concurrent compression and decompression test!";

constexpr size_t ThreadCount = 20;

Barrier b(ThreadCount);
{
auto testCompressDecompress = [&](size_t threadId) {
b.wait();
size_t compressedDataSize = 0;
auto compressedData = ZSTDCompressor::CompressBlob(
data.c_str(), data.size(), compressedDataSize, 3);

ASSERT_NE(compressedData, nullptr);
ASSERT_GT(compressedDataSize, (size_t)0);

size_t decompressedSize = 0;
auto decompressedData = ZSTDCompressor::DecompressBlob(
compressedData.get(), compressedDataSize, decompressedSize);

ASSERT_NE(decompressedData, nullptr);
ASSERT_GT(decompressedSize, (size_t)0);

std::string decompressedStr((char *)decompressedData.get(),
decompressedSize);
ASSERT_EQ(data, decompressedStr);
};

::ThreadPool MPool(ThreadCount, testCompressDecompress);
}
}
Loading