1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#!/bin/bash
set -e
WORKDIR=$AUTOPKGTEST_ARTIFACTS
cd "$WORKDIR"
# Create the cpp file.
cat <<EOF > mandelbrot.cpp
#include "mandelbrot_ispc.h"
#include <iostream>
int main() {
unsigned int width = 768, height = 512;
unsigned int midWidth = width/2;
unsigned int midHeight = height/2;
float x0 = -2., x1 = 1.;
float y0 = -1., y1 = 1.;
int maxIterations = 256;
int *buf = new int[width*height];
ispc::mandelbrot_ispc(x0, y0, x1, y1, width, height, maxIterations, buf);
std::cout << buf[(midWidth-1)*(midHeight-1)] << "\t|";
std::cout << buf[(midWidth)*(midHeight-1)] << "\t|";
std::cout << buf[(midWidth+1)*(midHeight-1)] << "\t|" << std::endl;
std::cout << buf[(midWidth-1)*(midHeight)] << "\t|";
std::cout << buf[(midWidth)*(midHeight)] << "\t|";
std::cout << buf[(midWidth+1)*(midHeight)] << "\t|" << std::endl;
std::cout << buf[(midWidth-1)*(midHeight+1)] << "\t|";
std::cout << buf[(midWidth)*(midHeight+1)] << "\t|";
std::cout << buf[(midWidth+1)*(midHeight+1)] << "\t|" << std::endl;
return 0;
}
EOF
# Create the ispc file.
cat <<EOF > mandelbrot.ispc
static inline int mandel(float c_re, float c_im, int count) {
float z_re = c_re, z_im = c_im;
int i;
for (i = 0; i < count; ++i) {
if (z_re * z_re + z_im * z_im > 4.)
break;
float new_re = z_re*z_re - z_im*z_im;
float new_im = 2.f * z_re * z_im;
z_re = c_re + new_re;
z_im = c_im + new_im;
}
return i;
}
export void mandelbrot_ispc(uniform float x0, uniform float y0,
uniform float x1, uniform float y1,
uniform int width, uniform int height,
uniform int maxIterations,
uniform int output[])
{
float dx = (x1 - x0) / width;
float dy = (y1 - y0) / height;
for (uniform int j = 0; j < height; j++) {
foreach (i = 0 ... width) {
float x = x0 + i * dx;
float y = y0 + j * dy;
int index = j * width + i;
output[index] = mandel(x, y, maxIterations);
}
}
}
EOF
echo "detecting target architecture..."
ARCH=$(uname -m)
echo $ARCH
if [[ $ARCH == x86_64* ]]; then
echo "X64 Architecture"
ISPC_ARCH=x86-64
elif [[ $ARCH == i*86 ]]; then
echo "X32 Architecture"
ISPC_ARCH=x86
elif [[ $ARCH == arm* ]]; then
echo "ARM Architecture"
ISPC_ARCH=arm
elif [[ $ARCH == "aarch64" ]]; then
echo "Aarch64 Architecture"
ISPC_ARCH=aarch64
else
echo "Unsupported Architecture"
exit 1;
fi
echo "building..."
mkdir -p objs
ispc -O2 mandelbrot.ispc -o objs/mandelbrot_ispc.o -h objs/mandelbrot_ispc.h --arch=$ISPC_ARCH
g++ mandelbrot.cpp -Iobjs/ -O3 -Wall -c -o objs/mandelbrot.o
g++ -Iobjs/ -O3 -Wall -o mandelbrot objs/mandelbrot.o objs/mandelbrot_ispc.o -lm
echo "running..."
test -x mandelbrot
./mandelbrot
|