Files
ispc/examples/noise/noise.cpp
2013-12-10 15:02:58 +04:00

132 lines
4.3 KiB
C++

/*
Copyright (c) 2010-2011, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#define NOMINMAX
#pragma warning (disable: 4244)
#pragma warning (disable: 4305)
#endif
#include <stdio.h>
#include <algorithm>
#include "../timing.h"
#include "noise_ispc.h"
#include <string.h>
using namespace ispc;
extern void noise_serial(float x0, float y0, float x1, float y1,
int width, int height, float output[]);
/* Write a PPM image file with the image */
static void
writePPM(float *buf, int width, int height, const char *fn) {
FILE *fp = fopen(fn, "wb");
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", width, height);
fprintf(fp, "255\n");
for (int i = 0; i < width*height; ++i) {
float v = buf[i] * 255.f;
if (v < 0) v = 0;
if (v > 255) v = 255;
for (int j = 0; j < 3; ++j)
fputc((char)v, fp);
}
fclose(fp);
}
int main(int argc, char *argv[]) {
static unsigned int test_iterations[] = {3, 1};
unsigned int width = 768;
unsigned int height = 768;
float x0 = -10;
float x1 = 10;
float y0 = -10;
float y1 = 10;
if (argc > 1) {
if (strncmp(argv[1], "--scale=", 8) == 0) {
float scale = atof(argv[1] + 8);
width *= scale;
height *= scale;
}
}
if ((argc == 3) || (argc == 4)) {
for (int i = 0; i < 2; i++) {
test_iterations[i] = atoi(argv[argc - 2 + i]);
}
}
float *buf = new float[width*height];
//
// Compute the image using the ispc implementation; report the minimum
// time of three runs.
//
double minISPC = 1e30;
for (int i = 0; i < test_iterations[0]; ++i) {
reset_and_start_timer();
noise_ispc(x0, y0, x1, y1, width, height, buf);
double dt = get_elapsed_mcycles();
printf("@time of ISPC run:\t\t\t[%.3f] million cycles\n", dt);
minISPC = std::min(minISPC, dt);
}
printf("[noise ispc]:\t\t\t[%.3f] million cycles\n", minISPC);
writePPM(buf, width, height, "noise-ispc.ppm");
// Clear out the buffer
for (unsigned int i = 0; i < width * height; ++i)
buf[i] = 0;
//
// And run the serial implementation 3 times, again reporting the
// minimum time.
//
double minSerial = 1e30;
for (int i = 0; i < test_iterations[1]; ++i) {
reset_and_start_timer();
noise_serial(x0, y0, x1, y1, width, height, buf);
double dt = get_elapsed_mcycles();
printf("@time of serial run:\t\t\t[%.3f] million cycles\n", dt);
minSerial = std::min(minSerial, dt);
}
printf("[noise serial]:\t\t\t[%.3f] million cycles\n", minSerial);
writePPM(buf, width, height, "noise-serial.ppm");
printf("\t\t\t\t(%.2fx speedup from ISPC)\n", minSerial/minISPC);
return 0;
}