Skip to content
Snippets Groups Projects
Commit 771eb62b authored by ylvse560's avatar ylvse560
Browse files

Lab 3 files

parent 0760146d
Branches
No related tags found
No related merge requests found
################################################################################
# ____ _ ____ _ _ ____ #
# / ___|| | _____| _ \| | | | |___ \ #
# \___ \| |/ / _ \ |_) | | | | __) | #
# ___) | < __/ __/| |_| | / __/ #
# |____/|_|\_\___|_| \___/ |_____| #
# #
# ~ SkePU 2 main Makefile ~ #
################################################################################
# ---------------------------------------------------------------------------- #
# Test program names and target directories.
OUT_DIR = bin
# All SkePU 2 example programs (used for 'make all-[parallel|sequential]' and 'make clean').
PROGS = addone dotproduct average median
PTESTS = $(addprefix $(OUT_DIR)/, $(PROGS))
include Makefile.include
# ---------------------------------------------------------------------------- #
# Make recipies begin here.
# Builds all test programs, parallel implementations, using the precompiler.
all-parallel: $(PTESTS)
# Precompiles and builds a single parallel test program.
$(OUT_DIR)/%: %.cpp
$(DBGR) $(SKEPU) -name $*_precompiled $< -dir $(OUT_DIR) $(SKEPU_FLAGS)
$(DBGR) $(CXX) $@_precompiled.$(FILETYPE) -o $@ $(TARGET_FLAGS)
# Precompiles and builds a single parallel test program.
$(OUT_DIR)/median: median.cpp
$(DBGR) $(SKEPU) -name median_precompiled $< -dir $(OUT_DIR) $(SKEPU_FLAGS)
$(DBGR) $(SKEPU) -name support_precompiled support.cpp -dir $(OUT_DIR) $(SKEPU_FLAGS)
$(DBGR) $(CXX) $@_precompiled.$(FILETYPE) $(OUT_DIR)/support_precompiled.$(FILETYPE) lodepng.cpp -o $@ $(TARGET_FLAGS)
# Precompiles and builds a single parallel test program.
$(OUT_DIR)/average: average.cpp
$(DBGR) $(SKEPU) -name average_precompiled $< -dir $(OUT_DIR) $(SKEPU_FLAGS)
$(DBGR) $(SKEPU) -name support_precompiled support.cpp -dir $(OUT_DIR) $(SKEPU_FLAGS)
$(DBGR) $(CXX) $@_precompiled.$(FILETYPE) $(OUT_DIR)/support_precompiled.$(FILETYPE) lodepng.cpp -o $@ $(TARGET_FLAGS)
# Deletes all temporary files (including all precompiled sources) and binaries.
clean:
-$(RM) $(OUT_DIR)/*_precompiled.cpp $(OUT_DIR)/*_cl_source.inl $(OUT_DIR)/*.cu $(PTESTS)
/**************************
** TDDD56 Lab 3
***************************
** Author:
** August Ernstsson
**************************/
#include <iostream>
#include <skepu>
float addOneFunc(float a)
{
return a+1;
}
int main(int argc, const char* argv[])
{
/* Program parameters */
if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " <input size> <backend>\n";
exit(1);
}
const size_t size = std::stoul(argv[1]);
auto spec = skepu::BackendSpec{argv[2]};
skepu::setGlobalBackendSpec(spec);
/* Skeleton instances */
auto addOneMap = skepu::Map(addOneFunc);
/* SkePU containers */
skepu::Vector<float> input(size), res(size);
// input.randomize(0, 9);
// This is how to measure execution times with SkePU
auto dur = skepu::benchmark::measureExecTime([&]
{
// Code to be measured here
addOneMap(res, input);
});
/* This is how to print the time */
std::cout << "Time: " << (dur.count() / 10E6) << " seconds.\n";
/* Print vector for debugging */
// std::cout << "Input: " << input << "\n";
// std::cout << "Result: " << res << "\n";
return 0;
}
/**************************
** TDDD56 Lab 3
***************************
** Author:
** August Ernstsson
**************************/
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <time.h>
#include <iterator>
#include <skepu>
#include "support.h"
unsigned char average_kernel(skepu::Region2D<unsigned char> m, size_t elemPerPx)
{
float scaling = 1.0 / ((m.oj/elemPerPx*2+1)*(m.oi*2+1));
float res = 0;
for (int y = -m.oi; y <= m.oi; ++y)
for (int x = -m.oj; x <= m.oj; x += elemPerPx)
res += m(y, x);
return res * scaling;
}
unsigned char average_kernel_1d(skepu::Region1D<unsigned char> m, size_t elemPerPx)
{
// your code here
return m(0);
}
unsigned char gaussian_kernel(skepu::Region1D<unsigned char> m, const skepu::Vec<float> stencil, size_t elemPerPx)
{
// your code here
return m(0);
}
int main(int argc, char* argv[])
{
if (argc < 5)
{
std::cout << "Usage: " << argv[0] << " input output radius [backend]\n";
exit(1);
}
LodePNGColorType colorType = LCT_RGB;
std::string inputFileName = argv[1];
std::string outputFileName = argv[2];
const int radius = atoi(argv[3]);
auto spec = skepu::BackendSpec{argv[4]};
skepu::setGlobalBackendSpec(spec);
// Create the full path for writing the image.
std::stringstream ss;
ss << (2 * radius + 1) << "x" << (2 * radius + 1);
std::string outputFile = outputFileName + ss.str();
// Read the padded image into a matrix. Create the output matrix without padding.
// Padded version for 2D MapOverlap, non-padded for 1D MapOverlap
ImageInfo imageInfo;
skepu::Matrix<unsigned char> inputMatrixPad = ReadAndPadPngFileToMatrix(inputFileName, radius, colorType, imageInfo);
skepu::Matrix<unsigned char> inputMatrix = ReadPngFileToMatrix(inputFileName, colorType, imageInfo);
skepu::Matrix<unsigned char> outputMatrix(imageInfo.height, imageInfo.width * imageInfo.elementsPerPixel, 120);
// more containers...?
// Original version
{
auto conv = skepu::MapOverlap(average_kernel);
conv.setOverlap(radius, radius * imageInfo.elementsPerPixel);
auto timeTaken = skepu::benchmark::measureExecTime([&]
{
conv(outputMatrix, inputMatrixPad, imageInfo.elementsPerPixel);
});
WritePngFileMatrix(outputMatrix, outputFile + "-average.png", colorType, imageInfo);
std::cout << "Time for combined: " << (timeTaken.count() / 10E6) << "\n";
}
// Separable version
// use conv.setOverlapMode(skepu::Overlap::[ColWise RowWise]);
// and conv.setOverlap(<integer>)
{
auto conv = skepu::MapOverlap(average_kernel_1d);
auto timeTaken = skepu::benchmark::measureExecTime([&]
{
// your code here
});
// WritePngFileMatrix(outputMatrix, outputFile + "-separable.png", colorType, imageInfo);
std::cout << "Time for separable: " << (timeTaken.count() / 10E6) << "\n";
}
// Separable gaussian
{
skepu::Vector<float> stencil = sampleGaussian(radius);
// skeleton instance, etc here (remember to set backend)
auto timeTaken = skepu::benchmark::measureExecTime([&]
{
// your code here
});
// WritePngFileMatrix(outputMatrix, outputFile + "-gaussian.png", colorType, imageInfo);
std::cout << "Time for gaussian: " << (timeTaken.count() / 10E6) << "\n";
}
return 0;
}
/**************************
** TDDD56 Lab 3
***************************
** Author:
** August Ernstsson
**************************/
#include <iostream>
#include <skepu>
/* SkePU user functions */
/*
float userfunction(...)
{
// your code here
}
// more user functions...
*/
int main(int argc, const char* argv[])
{
if (argc < 2)
{
std::cout << "Usage: " << argv[0] << " <input size> <backend>\n";
exit(1);
}
const size_t size = std::stoul(argv[1]);
auto spec = skepu::BackendSpec{argv[2]};
// spec.setCPUThreads(<integer value>);
skepu::setGlobalBackendSpec(spec);
/* Skeleton instances */
// auto instance = skepu::Map(userfunction);
// ...
/* SkePU containers */
skepu::Vector<float> v1(size, 1.0f), v2(size, 2.0f);
/* Compute and measure time */
float resComb, resSep;
auto timeComb = skepu::benchmark::measureExecTime([&]
{
// your code here
});
auto timeSep = skepu::benchmark::measureExecTime([&]
{
// your code here
});
std::cout << "Time Combined: " << (timeComb.count() / 10E6) << " seconds.\n";
std::cout << "Time Separate: " << ( timeSep.count() / 10E6) << " seconds.\n";
std::cout << "Result Combined: " << resComb << "\n";
std::cout << "Result Separate: " << resSep << "\n";
return 0;
}
This diff is collapsed.
This diff is collapsed.
/**************************
** TDDD56 Lab 3
***************************
** Author:
** August Ernstsson
**************************/
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <time.h>
#include <iterator>
#include <skepu>
#include "support.h"
unsigned char median_kernel(skepu::Region2D<unsigned char> image, size_t elemPerPx)
{
// your code here
return image(0,0);
}
int main(int argc, char* argv[])
{
LodePNGColorType colorType = LCT_RGB;
if (argc < 5)
{
std::cout << "Usage: " << argv[0] << " input output radius [backend]\n";
exit(1);
}
std::string inputFileName = argv[1];
std::string outputFileName = argv[2];
const int radius = atoi(argv[3]);
auto spec = skepu::BackendSpec{argv[4]};
skepu::setGlobalBackendSpec(spec);
// Create the full path for writing the image.
std::stringstream ss;
ss << (2 * radius + 1) << "x" << (2 * radius + 1);
std::string outputFileNamePad = outputFileName + ss.str() + "-median.png";
// Read the padded image into a matrix. Create the output matrix without padding.
ImageInfo imageInfo;
skepu::Matrix<unsigned char> inputMatrix = ReadAndPadPngFileToMatrix(inputFileName, radius, colorType, imageInfo);
std::cout << imageInfo.height << ", " << imageInfo.width << std::endl;
std::cout << inputMatrix.total_cols() << ", " << inputMatrix.total_rows() << std::endl;
skepu::Matrix<unsigned char> outputMatrix(imageInfo.height, imageInfo.width * imageInfo.elementsPerPixel, 120);
std::cout << outputMatrix.total_cols() << ", " << outputMatrix.total_rows() << std::endl;
// Skeleton instance
auto calculateMedian = skepu::MapOverlap(median_kernel);
calculateMedian.setOverlap(radius, radius * imageInfo.elementsPerPixel);
auto timeTaken = skepu::benchmark::measureExecTime([&]
{
calculateMedian(outputMatrix, inputMatrix, imageInfo.elementsPerPixel);
});
WritePngFileMatrix(outputMatrix, outputFileNamePad, colorType, imageInfo);
std::cout << "Time: " << (timeTaken.count() / 10E6) << "\n";
return 0;
}
#include <cmath>
#include "support.h"
// Reads a file from png and retuns it as a skepu::Matrix. Uses a library called LodePNG.
// Also returns information about the image as an out variable in imageInformation.
skepu::Matrix<unsigned char>
ReadPngFileToMatrix(std::string filePath, LodePNGColorType colorType, ImageInfo& imageInformation)
{
std::vector<unsigned char> fileContents, image;
unsigned imageWidth, imageHeight;
unsigned error = lodepng::decode(image, imageWidth, imageHeight, filePath, colorType);
if (error)
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
int elementsPerPixel = (colorType == LCT_GREY) ? 1 : 3;
// Create a matrix which fits the image and the padding needed.
skepu::Matrix<unsigned char> inputMatrix(imageHeight, (imageWidth) * elementsPerPixel);
int nonEdgeStartX = 0;
int nonEdgeEndX = inputMatrix.total_cols();
int nonEdgeStartY = 0;
int nonEdgeEndY = inputMatrix.total_rows();
// Initialize the inner real image values. The image is placed in the middle of the matrix,
// surrounded by the padding.
for (int i = nonEdgeStartY; i < nonEdgeEndY; i++)
for (int j = nonEdgeStartX; j < nonEdgeEndX; j++)
inputMatrix(i, j)= image[(i - nonEdgeStartY) * imageWidth * elementsPerPixel + (j - nonEdgeStartX)];
imageInformation.height = imageHeight;
imageInformation.width = imageWidth;
imageInformation.elementsPerPixel = elementsPerPixel;
return inputMatrix;
}
// Reads a file from png and retuns it as a skepu::Matrix. Uses a library called LodePNG.
// Also returns information about the image as an out variable in imageInformation.
skepu::Matrix<unsigned char>
ReadAndPadPngFileToMatrix(std::string filePath, int kernelRadius, LodePNGColorType colorType, ImageInfo& imageInformation)
{
std::vector<unsigned char> fileContents, image;
unsigned imageWidth, imageHeight;
unsigned error = lodepng::decode(image, imageWidth, imageHeight, filePath, colorType);
if (error)
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
int elementsPerPixel = (colorType == LCT_GREY) ? 1 : 3;
std::cout << "Här: " << imageHeight << ", " << imageWidth << ", " << kernelRadius << std::endl;
// Create a matrix which fits the image and the padding needed.
skepu::Matrix<unsigned char> inputMatrix(imageHeight + 2*kernelRadius, (imageWidth + 2*kernelRadius) * elementsPerPixel);
int nonEdgeStartX = kernelRadius * elementsPerPixel;
int nonEdgeEndX = inputMatrix.total_cols() - kernelRadius * elementsPerPixel;
int nonEdgeStartY = kernelRadius;
int nonEdgeEndY = inputMatrix.total_rows() - kernelRadius;
// Initialize the inner real image values. The image is placed in the middle of the matrix,
// surrounded by the padding.
for (int i = nonEdgeStartY; i < nonEdgeEndY; i++)
for (int j = nonEdgeStartX; j < nonEdgeEndX; j++)
inputMatrix(i, j)= image[(i - nonEdgeStartY) * imageWidth * elementsPerPixel + (j - nonEdgeStartX)];
// Initialize padding. // Init topmost rows
for (int row = 0;row < kernelRadius; row++)
{
for (int col = 0; col < inputMatrix.total_cols(); col++)
{
int minClampEdgeX = nonEdgeStartX + col % elementsPerPixel;
int maxClampEdgeX = nonEdgeEndX - elementsPerPixel + col % elementsPerPixel;
int xIndex = std::min(maxClampEdgeX, std::max(col, minClampEdgeX));
int yIndex = std::min(nonEdgeEndY - 1, std::max(row, nonEdgeStartY));
inputMatrix(row, col) = inputMatrix(yIndex, xIndex);
}
}
// Init middle rows
for (int row = kernelRadius; row < nonEdgeEndY; row++)
{
for (int col = 0; col < nonEdgeStartX; col++)
{
int minClampEdgeX = nonEdgeStartX + col % elementsPerPixel;
int maxClampEdgeX = nonEdgeEndX - elementsPerPixel + col % elementsPerPixel;
inputMatrix(row, col) = inputMatrix(row, minClampEdgeX);
inputMatrix(row, col + nonEdgeEndX) = inputMatrix(row, maxClampEdgeX);
}
}
// Init bottom rows
for (int row = nonEdgeEndY; row < inputMatrix.total_rows(); row++)
{
for (int col = 0; col < inputMatrix.total_cols(); col++)
{
int minClampEdgeX = nonEdgeStartX + col % elementsPerPixel;
int maxClampEdgeX = nonEdgeEndX - elementsPerPixel + col % elementsPerPixel;
int xIndex = std::min(maxClampEdgeX, std::max(col, minClampEdgeX));
int yIndex = std::min(nonEdgeEndY - 1, std::max(row, nonEdgeStartY));
inputMatrix(row, col) = inputMatrix(yIndex, xIndex);
}
}
imageInformation.height = imageHeight;
imageInformation.width = imageWidth;
imageInformation.elementsPerPixel = elementsPerPixel;
return inputMatrix;
}
void WritePngFileMatrix(skepu::Matrix<unsigned char> imageData, std::string filePath, LodePNGColorType colorType, ImageInfo& imageInformation)
{
std::vector<unsigned char> imageDataVector;
for (int i = 0; i < imageData.total_rows(); i++)
for (int j = 0; j < imageData.total_cols() ;j++)
imageDataVector.push_back(imageData(i, j));
unsigned error = lodepng::encode(filePath, &imageDataVector[0], imageInformation.width, imageInformation.height, colorType);
if(error)
std::cout << "decoder error " << error << ": " << lodepng_error_text(error) << std::endl;
}
skepu::Vector<float> sampleGaussian(int radius)
{
skepu::Vector<float> vals(radius * 2 + 1);
float s = radius / 3.0;
for (int i = -radius; i <= radius; ++i)
{
double x = (float)i;
vals[i+radius] = exp(-x*x / (2*s*s)) / sqrt(2*s*s*M_PI);
}
return vals;
}
#include <skepu>
#include "lodepng.h"
// Information about the image. Used to rebuild the image after filtering.
struct ImageInfo
{
int width;
int height;
int elementsPerPixel;
};
skepu::Matrix<unsigned char>
ReadPngFileToMatrix(
std::string filePath,
LodePNGColorType colorType,
ImageInfo& imageInformation
);
// Reads a file from png and retuns it as a skepu::Matrix. Uses a library called LodePNG.
// Also returns information about the image as an out variable in imageInformation.
skepu::Matrix<unsigned char>
ReadAndPadPngFileToMatrix(
std::string filePath,
int kernelRadius,
LodePNGColorType colorType,
ImageInfo& imageInformation
);
void
WritePngFileMatrix(
skepu::Matrix<unsigned char> imageData,
std::string filePath,
LodePNGColorType colorType,
ImageInfo& imageInformation
);
skepu::Vector<float> sampleGaussian(int size);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment