aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2020-03-27 22:47:57 +0100
committerTom Smeding <tom.smeding@gmail.com>2020-03-27 22:47:57 +0100
commitfd421e32780cad46782c16cd4e15947f295a08c7 (patch)
tree04632f49f7c8860dee4237a0afe8292a949bdc9e /examples
Initial, untested version of controller and worker
Worker has been tested to a marginal extent, but the controller is litereally untested.
Diffstat (limited to 'examples')
-rw-r--r--examples/.gitignore1
-rw-r--r--examples/kaas/Makefile9
-rw-r--r--examples/kaas/kaas.c32
-rw-r--r--examples/mandel/Makefile9
-rw-r--r--examples/mandel/mandel.c57
5 files changed, 108 insertions, 0 deletions
diff --git a/examples/.gitignore b/examples/.gitignore
new file mode 100644
index 0000000..140f8cf
--- /dev/null
+++ b/examples/.gitignore
@@ -0,0 +1 @@
+*.so
diff --git a/examples/kaas/Makefile b/examples/kaas/Makefile
new file mode 100644
index 0000000..e7d5154
--- /dev/null
+++ b/examples/kaas/Makefile
@@ -0,0 +1,9 @@
+.PHONY: all clean
+
+all: kaas.so
+
+clean:
+ rm -f kaas.so
+
+kaas.so: kaas.c
+ gcc -Wall -Wextra -fPIC -shared $< -o $@
diff --git a/examples/kaas/kaas.c b/examples/kaas/kaas.c
new file mode 100644
index 0000000..f823442
--- /dev/null
+++ b/examples/kaas/kaas.c
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+
+__attribute__((constructor))
+void constr() {
+ printf("kaas: constructor!\n");
+}
+
+__attribute__((destructor))
+void destr() {
+ printf("kaas: destructor!\n");
+}
+
+int worker_init(int version) {
+ printf("kaas: initialised with version %d\n", version);
+ return 0;
+}
+
+int worker_run_job(size_t size, void *input_, size_t *outsize, void **outputp) {
+ const char *input = input_;
+ char *output = malloc(size);
+ for (size_t i = 0; i < size; i++) output[i] = toupper(input[i]);
+ *outsize = size;
+ *outputp = output;
+ return 0;
+}
+
+void worker_free_outdata(size_t size, void *data) {
+ (void)size;
+ free(data);
+}
diff --git a/examples/mandel/Makefile b/examples/mandel/Makefile
new file mode 100644
index 0000000..4311ee9
--- /dev/null
+++ b/examples/mandel/Makefile
@@ -0,0 +1,9 @@
+.PHONY: all clean
+
+all: mandel.so
+
+clean:
+ rm -f mandel.so
+
+mandel.so: mandel.c
+ gcc -Wall -Wextra -fPIC -shared -O3 -fopenmp $< -o $@
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 <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+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);
+}