Skip to content

Commit ba4ab46

Browse files
committed
[dl-cifar] Optimise initImage using static cache
The initImage function is called hundreds of times, repeating the same computations on the host each time. The redundant computation introduces large host overhead in the measured computation times and skews the comparison between offload programming models. Speed up the initImage function by using a static cache in order to minimise the host overheads in the benchmarked computation.
1 parent 0bd76bb commit ba4ab46

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

dl-cifar/common/image_processing.h

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,32 @@ namespace dl_cifar::common {
102102
static void initImage(float* image, int imageSize) {
103103
Tracer::func_begin("ImageProcessor::initImage");
104104

105-
unsigned seed = 123456789;
106-
for (int index = 0; index < imageSize; index++) {
105+
struct ImageCache {
106+
size_t size{0};
107+
float* image{nullptr};
108+
~ImageCache() {
109+
if (image==nullptr) {return;}
110+
delete[] image;
111+
image = nullptr;
112+
}
113+
};
114+
static ImageCache cache{};
115+
static unsigned seed = 123456789;
116+
117+
// grow the cache allocation to image size
118+
if (imageSize > cache.size) {
119+
float* newCacheImage = new float[imageSize];
120+
std::memcpy(newCacheImage, cache.image, cache.size*sizeof(float));
121+
delete[] cache.image;
122+
cache.image = newCacheImage;
123+
}
124+
125+
// fill image with cached data and compute the remaining part
126+
std::memcpy(image, cache.image, std::min(cache.size,static_cast<size_t>(imageSize))*sizeof(float));
127+
for (; cache.size < imageSize; ++cache.size) {
107128
seed = (1103515245 * seed + 12345) & 0xffffffff;
108-
image[index] = float(seed) * 2.3283064e-10; // 2^-32
129+
cache.image[cache.size-1] = float(seed) * 2.3283064e-10; // 2^-32
130+
image[cache.size-1] = cache.image[cache.size-1];
109131
}
110132
Tracer::func_end("ImageProcessor::initImage");
111133

0 commit comments

Comments
 (0)