Speed Up Computations with Parallel GPU Computing
Write code that will use the maximum available precision on the specific CUDA or OpenCL device.
code = "
__kernel void julia_kernel(__global float *set, long width, long height, float cx, float cy) {
long xIndex = get_global_id(0);
long yIndex = get_global_id(1);
long ii;
float x = ZOOM_LEVEL*(width/2 - xIndex);
float y = ZOOM_LEVEL*(height/2 - yIndex);
float tmp;
float c;
if (xIndex < width && yIndex < height) {
for (ii = 0; ii < MAX_ITERATIONS && x*x + y*y < BAILOUT; ii++) {
tmp = x*x - y*y + cx;
y = 2*x*y + cy;
x = tmp;
}
c = log(0.1f + sqrt(x*x + y*y));
set[xIndex + yIndex*width] = c;
}
}
";Needs["OpenCLLink`"]
{width, height} = {512, 512};
jset = OpenCLMemoryAllocate["Float", {height, width}];
JuliaCalculate = OpenCLFunctionLoad[code, "julia_kernel", {{"Float", _, "Output"}, _Integer, _Integer, "Float", "Float"}, {16, 16}, "Defines" -> {"MAX_ITERATIONS" -> 10, "ZOOM_LEVEL" -> "0.0050", "BAILOUT" -> "4.0"}];Manipulate[
JuliaCalculate[jset, width, height, c[[1]], c[[2]], {width, height}];
ReliefPlot[OpenCLMemoryGet[jset], DataRange -> {{-2.0, 2.0}, {-2.0, 2.0}}, ImageSize -> 256, ColorFunction -> "SunsetColors"],
{{c, {0, 1}}, {-2, -2}, {2, 2}, Locator}]