// Copyright (c) 2012-2017 VideoStitch SAS // Copyright (c) 2018 stitchEm // // Basic input unpacking tests. #include "testing.hpp" #include "util.hpp" #include "../image/kernels/sharedUtils.hpp" #include namespace VideoStitch { namespace Testing { template __global__ void dumpSharedKernel(T* __restrict__ sharedDst, const unsigned sharedWidth, const unsigned sharedHeight, const T* __restrict__ src, unsigned srcWidth, unsigned srcHeight, unsigned srcOffsetX, unsigned srcOffsetY) { Image::loadToSharedMemory(sharedDst, sharedWidth, sharedHeight, src, srcWidth, srcHeight, srcOffsetX, srcOffsetY); __syncthreads(); } template void groundTruthExtend(std::vector& sharedDst, const int sharedWidth, const int sharedHeight, const std::vector& src, int srcWidth, int srcHeight, int srcOffsetX, int srcOffsetY) { const int realSharedWidth = sharedWidth + left + right; const int realSharedHeight = sharedHeight + top + bottom; sharedDst.clear(); // Top rows: for (int sharedY = 0; sharedY < realSharedHeight; ++sharedY) { int srcY = srcOffsetY + sharedY - top; if (srcY < 0) { srcY = 0; } if (srcY >= srcHeight) { srcY = srcHeight - 1; } for (int sharedX = 0; sharedX < realSharedWidth; ++sharedX) { int srcX = srcOffsetX + sharedX - left; if (srcX < 0) { srcX = 0; } if (srcX >= srcWidth) { srcX = srcWidth - 1; } sharedDst.push_back(src[srcWidth * srcY + srcX]); } } } template void groundTruthZero(std::vector& sharedDst, const int sharedWidth, const int sharedHeight, const std::vector& src, int srcWidth, int srcHeight, int srcOffsetX, int srcOffsetY) { const int realSharedWidth = sharedWidth + left + right; const int realSharedHeight = sharedHeight + top + bottom; sharedDst.clear(); // Top rows: for (int sharedY = 0; sharedY < realSharedHeight; ++sharedY) { const int srcY = srcOffsetY + sharedY - top; for (int sharedX = 0; sharedX < realSharedWidth; ++sharedX) { const int srcX = srcOffsetX + sharedX - left; if (srcY < 0 || srcY >= srcHeight || srcX < 0 || srcX >= srcWidth) { sharedDst.push_back(0); } else { sharedDst.push_back(src[srcWidth * srcY + srcX]); } } } } template void runTest(const int width, const int height, const std::vector& input, const int sharedWidth, const int sharedHeight, const int srcOffsetX, const int srcOffsetY, bool extend) { // std::cout << "sharedWidth=" << sharedWidth << " sharedHeight=" << sharedHeight << " srcOffsetX=" << srcOffsetX << " // srcOffsetY=" << srcOffsetY << std::endl; std::vector output; { DeviceBuffer inputBuffer(width, height); inputBuffer.fill(input); const dim3 dimBlock2D(blockWidth, blockHeight, 1); const dim3 dimGrid2D(1, 1, 1); DeviceBuffer outputBuffer(sharedWidth + left + right, sharedHeight + top + bottom); outputBuffer.fill((T)99); if (extend) { dumpSharedKernel><<>>( outputBuffer.ptr(), sharedWidth, sharedHeight, inputBuffer.ptr(), width, height, srcOffsetX, srcOffsetY); } else { dumpSharedKernel><<>>( outputBuffer.ptr(), sharedWidth, sharedHeight, inputBuffer.ptr(), width, height, srcOffsetX, srcOffsetY); } outputBuffer.readback(output); } std::vector groundTruthOutput; if (extend) { groundTruthExtend(groundTruthOutput, sharedWidth, sharedHeight, input, width, height, srcOffsetX, srcOffsetY); } else { groundTruthZero(groundTruthOutput, sharedWidth, sharedHeight, input, width, height, srcOffsetX, srcOffsetY); } /*for (int y = 0; y < sharedHeight + top + bottom; ++y) { for (int x = 0; x < sharedWidth + left + right; ++x) { std::cout << groundTruthOutput[(sharedWidth + left + right) * y + x]<< " "; } std::cout << std::endl; } for (int y = 0; y < sharedHeight + top + bottom; ++y) { for (int x = 0; x < sharedWidth + left + right; ++x) { std::cout << output[(sharedWidth + left + right) * y + x]<< " "; } std::cout << std::endl; }*/ ENSURE_2D_ARRAY_EQ(groundTruthOutput.data(), output.data(), sharedWidth + left + right, sharedHeight + top + bottom); } template void testSharedUtils(const int width, const int height, bool extend) { std::vector input; for (int i = 0; i < width * height; ++i) { input.push_back((T)i); } for (int sharedWidth = 1; sharedWidth < width; ++sharedWidth) { for (int sharedHeight = 1; sharedHeight < height; ++sharedHeight) { for (int srcOffsetX = 0; srcOffsetX < width; ++srcOffsetX) { for (int srcOffsetY = 0; srcOffsetY < height; ++srcOffsetY) { runTest(width, height, input, sharedWidth, sharedHeight, srcOffsetX, srcOffsetY, extend); } } } } } } // namespace Testing } // namespace VideoStitch int main() { cudaSetDevice(0); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, true); VideoStitch::Testing::testSharedUtils(6, 5, false); { const int width = 512; const int height = 512; std::vector input; for (int i = 0; i < width * height; ++i) { input.push_back((float)i); } VideoStitch::Testing::runTest(width, height, input, 16, 16, 110, 11, true); } cudaDeviceReset(); return 0; }