Created
July 4, 2016 10:00
-
-
Save feliwir/808d30647501d0f8bb3b31a5f97711dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define STB_IMAGE_IMPLEMENTATION | |
#include "stb_image.h" | |
#define STB_IMAGE_WRITE_IMPLEMENTATION | |
#include "stb_image_write.h" | |
#include <chrono> | |
#include <iostream> | |
#include <cilk/cilk.h> | |
#include <thread> | |
//n components per pixel | |
void grayscale_cilk(float* data, int width, int height) | |
{ | |
for (int c = 1;c < 3;++c) | |
{ | |
data[0:width*height: 3] += data[c:width*height:3]; | |
} | |
data[0:width*height*3] /= 3; | |
for (int c = 1 ;c < 3;++c) | |
{ | |
data[c:width*height : 3] = data[0:width*height : 3]; | |
} | |
} | |
//n components per pixel | |
void grayscale_scalar(float* data, int width, int height) | |
{ | |
for (int px = 0;px<height*width;++px) | |
{ | |
float average = 0.0; | |
for (int c = 0;c < 3;++c) | |
average += data[px * 3 + c]; | |
average /= 3; | |
for (int c = 0;c < 3;++c) | |
data[px * 3 + c] = average; | |
} | |
} | |
int main(int argc, char** argv) | |
{ | |
int x, y, n; | |
const char* filename = "test.png"; | |
float *data_scal = stbi_loadf(filename, &x, &y, &n, 0); | |
float* data_cilk = new float[x*y*n]; | |
std::copy(data_scal,data_scal+x*y*n,data_cilk); | |
float* data_cilk_p = new float[x*y*n]; | |
std::copy(data_scal,data_scal+x*y*n,data_cilk_p); | |
float* data_scal_p = new float[x*y*n]; | |
std::copy(data_scal,data_scal+x*y*n,data_scal_p); | |
//BENCHMARKS | |
//CILK | |
std::cout << "CILK:" << std::endl; | |
auto start = std::chrono::high_resolution_clock::now(); | |
grayscale_cilk(data_cilk, x, y); | |
auto end = std::chrono::high_resolution_clock::now(); | |
auto elapsed = end - start; | |
auto time = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count(); | |
std::cout <<"Time in nanoseonds: " << time << std::endl; | |
//SCALAR | |
std::cout << "SCALAR:" << std::endl; | |
start = std::chrono::high_resolution_clock::now(); | |
grayscale_scalar(data_scal, x, y); | |
end = std::chrono::high_resolution_clock::now(); | |
elapsed = end - start; | |
time = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count(); | |
std::cout <<"Time in nanoseonds: " << time << std::endl; | |
//CILK MULTITHREADED | |
std::cout << "CILK MULTITHREADED:" << std::endl; | |
start = std::chrono::high_resolution_clock::now(); | |
cilk_for(int i=0;i<4;++i) | |
grayscale_cilk(data_cilk_p+ i*(y/4)*x*n, x, y/4); | |
end = std::chrono::high_resolution_clock::now(); | |
elapsed = end - start; | |
time = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count(); | |
std::cout <<"Time in nanoseonds: " << time << std::endl; | |
//SCALAR MULTITHREADED | |
std::cout << "SCALAR MULTITHREADED:" << std::endl; | |
start = std::chrono::high_resolution_clock::now(); | |
cilk_for(int i=0;i<4;++i) | |
grayscale_scalar(data_scal_p+ i*(y/4)*x*n, x, y/4); | |
end = std::chrono::high_resolution_clock::now(); | |
elapsed = end - start; | |
time = std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed).count(); | |
std::cout <<"Time in nanoseonds: " << time << std::endl; | |
//IMAGE WRITING | |
std::cout << "Writing images... "<< std::endl; | |
unsigned char* ldr_cilk = stbi__hdr_to_ldr(data_cilk, x, y, n); | |
stbi_write_png("test_cilk.png", x, y, n, ldr_cilk,x*n); | |
unsigned char* ldr_scal = stbi__hdr_to_ldr(data_scal, x, y, n); | |
stbi_write_png("test_scalar.png", x, y, n, ldr_scal, x*n); | |
unsigned char* ldr_cilk_p = stbi__hdr_to_ldr(data_cilk_p, x, y, n); | |
stbi_write_png("test_cilk_p.png", x, y, n, ldr_cilk_p, x*n); | |
unsigned char* ldr_scal_p = stbi__hdr_to_ldr(data_scal_p, x, y, n); | |
stbi_write_png("test_scalar.png", x, y, n, ldr_scal_p, x*n); | |
delete ldr_cilk; | |
delete ldr_scal; | |
delete ldr_cilk_p; | |
delete ldr_scal_p; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment