Added Mandelbrot tests
authorbsegovia <devnull@localhost>
Tue, 11 Oct 2011 02:02:35 +0000 (02:02 +0000)
committerKeith Packard <keithp@keithp.com>
Fri, 10 Aug 2012 23:14:55 +0000 (16:14 -0700)
kernels/mandelbrot.cl [new file with mode: 0644]
src/CMakeLists.txt

diff --git a/kernels/mandelbrot.cl b/kernels/mandelbrot.cl
new file mode 100644 (file)
index 0000000..473441e
--- /dev/null
@@ -0,0 +1,59 @@
+// Used to index into the 1D array, so that we can use
+// it effectively as a 2D array
+int index(int x, int y, int width) {
+  return 4*width*y + x*4;
+}
+
+// Turn the raw x coordinates [0, 1] into a scaled x coordinate
+// [0, 1] -> [-2, 1.25]
+float mapX(float x) {
+  return x*3.25 - 2;
+}
+
+// Same purpose as mapX
+// [0, 1] -> [-1.25, 1.25]
+float mapY(float y) {
+  return y*2.5 - 1.25;
+}
+
+__kernel void render(__global char *out) {
+  int x_dim = get_global_id(0);
+  int y_dim = get_global_id(1);
+  int width = get_global_size(0);
+  int height = get_global_size(1);
+  int idx = index(x_dim, y_dim, width);
+
+  float x_origin = mapX((float) x_dim / width);
+  float y_origin = mapY((float) y_dim / height);
+
+  // The Escape time algorithm, it follows the pseduocode from Wikipedia
+  // _very_ closely
+  float x = 0.0;
+  float y = 0.0;
+
+  int iteration = 0;
+
+  // This can be changed, to be more or less precise
+  int max_iteration = 256;
+  while(x*x + y*y <= 4 && iteration < max_iteration) {
+    float xtemp = x*x - y*y + x_origin;
+    y = 2*x*y + y_origin;
+    x = xtemp;
+    iteration++;
+  }
+
+  if(iteration == max_iteration) {
+    // This coordinate did not escape, so it is in the Mandelbrot set
+    out[idx] = 0;
+    out[idx + 1] = 0;
+    out[idx + 2] = 0;
+    out[idx + 3] = 255;
+  } else {
+    // This coordinate did escape, so color based on quickly it escaped
+    out[idx] = iteration;
+    out[idx + 1] = iteration;
+    out[idx + 2] = iteration;
+    out[idx + 3] = 255;
+  }
+
+}
index a22b2f3..c86be02 100644 (file)
@@ -49,7 +49,7 @@ ADD_EXECUTABLE(test_private_memory tests/test_private_memory.c)
 ADD_EXECUTABLE(test_constant_memory tests/test_constant_memory.c)
 ADD_EXECUTABLE(test_memory_leak tests/test_memory_leak.c)
 ADD_EXECUTABLE(test_perf_report tests/test_perf_report.c)
-#ADD_EXECUTABLE(test_trigo tests/test_trigo.c)
+ADD_EXECUTABLE(mandelbrot tests/mandelbrot.c)
 ADD_EXECUTABLE(mersenneTwister tests/mersenneTwister.c)
 ADD_EXECUTABLE(blackscholes tests/blackscholes.c)
 ADD_EXECUTABLE(matmul tests/matmul.c)
@@ -72,7 +72,7 @@ TARGET_LINK_LIBRARIES(test_constant_memory cl_test m)
 TARGET_LINK_LIBRARIES(test_memory_leak cl_test m)
 TARGET_LINK_LIBRARIES(test_write_only cl_test m)
 TARGET_LINK_LIBRARIES(test_perf_report cl_test m)
-#TARGET_LINK_LIBRARIES(test_trigo cl_test m)
+TARGET_LINK_LIBRARIES(mandelbrot cl_test m)
 TARGET_LINK_LIBRARIES(mersenneTwister cl_test m)
 TARGET_LINK_LIBRARIES(blackscholes cl_test m)
 TARGET_LINK_LIBRARIES(matmul cl_test m)