From fd421e32780cad46782c16cd4e15947f295a08c7 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Fri, 27 Mar 2020 22:47:57 +0100 Subject: Initial, untested version of controller and worker Worker has been tested to a marginal extent, but the controller is litereally untested. --- examples/mandel/mandel.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/mandel/mandel.c (limited to 'examples/mandel/mandel.c') diff --git a/examples/mandel/mandel.c b/examples/mandel/mandel.c new file mode 100644 index 0000000..f7517ba --- /dev/null +++ b/examples/mandel/mandel.c @@ -0,0 +1,57 @@ +#include +#include +#include + +struct input { + double ltx, lty; + double rbx, rby; + size_t width, height; + size_t maxiter; +}; + +int worker_init(int version) { + fprintf(stderr, "mandel: init(%d)\n", version); + return version == 1 ? 0 : 1; +} + +int worker_run_job(size_t inputsize, const void *input_, size_t *outputsize, void **outputp) { + if (inputsize != sizeof(struct input)) { + fprintf(stderr, "mandel: Input has invalid size %zu (expected %zu)\n", inputsize, sizeof(struct input)); + return -1; + } + + fprintf(stderr, "mandel: run_job()\n"); + + const struct input *const input = input_; + + *outputsize = 4 * input->width * input->height; + uint32_t *const output = *outputp = malloc(*outputsize); + +#pragma omp parallel for + for (size_t yi = 0; yi < input->height; yi++) { + for (size_t xi = 0; xi < input->width; xi++) { + const double y = (input->rby - input->lty) / (input->height - 1) * yi; + const double x = (input->rbx - input->ltx) / (input->width - 1) * xi; + + double a = x, b = y, a2 = a * a, b2 = b * b; + size_t n; + for (n = 0; n < input->maxiter && a2 + b2 < 4; n++) { + b = 2 * a * b + y; + a = a2 - b2 + x; + a2 = a * a; b2 = b * b; + } + + output[input->width * yi + xi] = n; + } + } + + fprintf(stderr, "mandel: run_job() finished\n"); + + return 0; +} + +void worker_free_outdata(size_t size, void *output) { + (void)size; + fprintf(stderr, "mandel: free_outdata()\n"); + free(output); +} -- cgit v1.2.3-54-g00ecf