diff --git a/notebooks/omp/hello_world.ipynb b/notebooks/omp/hello_world.ipynb new file mode 100644 index 00000000..f90f20dd --- /dev/null +++ b/notebooks/omp/hello_world.ipynb @@ -0,0 +1,84 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "73cbab37-71dd-477d-981b-f2ec28c01bd6", + "metadata": {}, + "outputs": [], + "source": [ + "#include \n", + "#include \n", + "#include " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef1cd58a-672c-4a6f-843a-6c88fc4911f3", + "metadata": {}, + "outputs": [], + "source": [ + "Cpp::LoadLibrary(\"libomp\");\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c2b754ad-9553-4a42-b990-f990a9a269ed", + "metadata": {}, + "outputs": [], + "source": [ + "int main() {\n", + " int max_threads = omp_get_max_threads();\n", + "\n", + " printf(\"max threads: %d\\n\", max_threads);\n", + " omp_set_num_threads(max_threads);\n", + "\n", + "#pragma omp parallel\n", + " {\n", + " int id = omp_get_thread_num();\n", + " printf(\"Hello World from thread = %d with %d threads\\n\", id, omp_get_num_threads());\n", + " }\n", + "\n", + " printf(\"all done, with hopefully %d threads\\n\", max_threads);\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a37a13d4-fc82-496e-8f42-9e718a8c2aa0", + "metadata": {}, + "outputs": [], + "source": [ + "main();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "af54945c-2412-4aee-bbfc-a2f6fc72d23f", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "C++14", + "language": "C++14", + "name": "xcpp14" + }, + "language_info": { + "codemirror_mode": "text/x-c++src", + "file_extension": ".cpp", + "mimetype": "text/x-c++src", + "name": "c++", + "version": "14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/omp/linked_list.ipynb b/notebooks/omp/linked_list.ipynb new file mode 100644 index 00000000..106cecd6 --- /dev/null +++ b/notebooks/omp/linked_list.ipynb @@ -0,0 +1,224 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "156447d2-9279-45a0-890b-4e519d2c796b", + "metadata": {}, + "outputs": [], + "source": [ + "#include \n", + "#include \n", + "#include \n", + "#include " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c96fdeb0-817d-48c0-af8e-20a52947d60b", + "metadata": {}, + "outputs": [], + "source": [ + "#ifndef N\n", + "#define N 5\n", + "#endif\n", + "#ifndef FS\n", + "#define FS 38\n", + "#endif" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8da842e1-db02-49e0-929d-4e67cbc08172", + "metadata": {}, + "outputs": [], + "source": [ + "Cpp::LoadLibrary(\"libomp\");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "22f97c49-78d1-496e-ac7c-978aed95331a", + "metadata": {}, + "outputs": [], + "source": [ + "struct node {\n", + " int data;\n", + " int fibdata;\n", + " struct node *next;\n", + "};" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b16b1e8a-8831-4b8d-9d57-09deeaaa88ee", + "metadata": {}, + "outputs": [], + "source": [ + "struct node *init_list(struct node *p);\n", + "void processwork(struct node *p);\n", + "int fib(int n);" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0ef8af6c-1d6f-4c68-84bc-3dd1d8092b06", + "metadata": {}, + "outputs": [], + "source": [ + "int fib(int n) {\n", + " int x, y;\n", + " if (n < 2) {\n", + " return (n);\n", + " } else {\n", + " x = fib(n - 1);\n", + " y = fib(n - 2);\n", + " return (x + y);\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1fa0307d-fdc9-4503-95cb-1c6448791354", + "metadata": {}, + "outputs": [], + "source": [ + "void processwork(struct node *p) {\n", + " int n, temp;\n", + " n = p->data;\n", + " temp = fib(n);\n", + "\n", + " p->fibdata = temp;\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "03acb599-9329-49ff-8aff-c0902adb6c3c", + "metadata": {}, + "outputs": [], + "source": [ + "struct node *init_list(struct node *p) {\n", + " int i;\n", + " struct node *head = NULL;\n", + " struct node *temp = NULL;\n", + "\n", + " head = (struct node*) malloc(sizeof(struct node));\n", + " p = head;\n", + " p->data = FS;\n", + " p->fibdata = 0;\n", + " for (i = 0; i < N; i++) {\n", + " temp = (struct node*) malloc(sizeof(struct node));\n", + " p->next = temp;\n", + " p = temp;\n", + " p->data = FS + i + 1;\n", + " p->fibdata = i + 1;\n", + " }\n", + "\n", + " p->next = NULL;\n", + " return head;\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f2dfb41b-e55f-43c0-b7f6-546a1697acb1", + "metadata": {}, + "outputs": [], + "source": [ + "int main() {\n", + " double start, end;\n", + " struct node *p = NULL;\n", + " struct node *temp = NULL;\n", + " struct node *head = NULL;\n", + "\n", + " printf(\"Process linked list\\n\");\n", + " printf(\" Each linked list node will be processed by function 'processwork()'\\n\");\n", + " printf(\" Each ll node will compute %d fibonacci numbers beginning with %d\\n\", N, FS);\n", + "\n", + " omp_set_num_threads(omp_get_max_threads());\n", + "\n", + " p = init_list(p);\n", + " head = p;\n", + "\n", + " start = omp_get_wtime();\n", + "\n", + "#pragma omp parallel\n", + " {\n", + "#pragma omp master\n", + " printf(\"Threads: %d\\n\", omp_get_num_threads());\n", + "\n", + "#pragma omp single\n", + " {\n", + " p = head;\n", + " while (p) {\n", + "#pragma omp task firstprivate(p) // first private is required\n", + " {\n", + " processwork(p);\n", + " }\n", + " p = p->next;\n", + " }\n", + " }\n", + " }\n", + "\n", + " end = omp_get_wtime();\n", + " p = head;\n", + " while (p != NULL) {\n", + " printf(\"%d : %d\\n\", p->data, p->fibdata);\n", + " temp = p->next;\n", + " free(p);\n", + " p = temp;\n", + " }\n", + "\n", + " free(p);\n", + " printf(\"Compute Time: %f seconds\\n\", end - start);\n", + "\n", + " return 0;\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "353e5dfd-fcae-43e6-97e3-ec98070811a1", + "metadata": {}, + "outputs": [], + "source": [ + "main();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a24f21c1-2ddc-4e46-b9ee-7cc98fc2821a", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "C++14", + "language": "C++14", + "name": "xcpp14" + }, + "language_info": { + "codemirror_mode": "text/x-c++src", + "file_extension": ".cpp", + "mimetype": "text/x-c++src", + "name": "c++", + "version": "14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/omp/mandel.ipynb b/notebooks/omp/mandel.ipynb new file mode 100644 index 00000000..7c499cfa --- /dev/null +++ b/notebooks/omp/mandel.ipynb @@ -0,0 +1,132 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "5059dbdd-821d-498a-8716-eb0fcf8a8f5f", + "metadata": {}, + "outputs": [], + "source": [ + "#include \n", + "#include \n", + "#include \n", + "#include " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8b66f96a-14ef-4f23-8024-bcfc42b31e4e", + "metadata": {}, + "outputs": [], + "source": [ + "#define NPOINTS 1000\n", + "#define MAXITER 1000" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d89dd57c-fe19-4233-a33a-df9b24fae98a", + "metadata": {}, + "outputs": [], + "source": [ + "int numoutside = 0;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5c35c479-2f79-46b7-bc66-24be6b1694e0", + "metadata": {}, + "outputs": [], + "source": [ + "void testpoint(double creal, double cimag) {\n", + " // iterate z=z*z+c, until |z| > 2 when point is known to be outside set\n", + " // If loop count reaches MAXITER, point is considered to be inside the set\n", + "\n", + " double zreal, zimag, temp;\n", + " int iter;\n", + " zreal = creal;\n", + " zimag = cimag;\n", + "\n", + " for (iter = 0; iter < MAXITER; iter++) {\n", + " temp = (zreal * zreal) - (zimag * zimag) + creal;\n", + " zimag = zreal * zimag * 2 + cimag;\n", + " zreal = temp;\n", + " if ((zreal * zreal + zimag * zimag) > 4.0) {\n", + " numoutside++;\n", + " break;\n", + " }\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea116fef-7d05-4e29-97a1-55c85c7241d8", + "metadata": {}, + "outputs": [], + "source": [ + "int main() {\n", + " int i, j;\n", + " double area, error, eps = 1.0e-5;\n", + " double cimag, creal;\n", + " // Loop over grid of points in the complex plane which contains the Mandelbrot set,\n", + " // testing each point to see whether it is inside or outside the set.\n", + "\n", + "#pragma omp parallel for private(eps)\n", + " for (i = 0; i < NPOINTS; i++) {\n", + " for (j = 0; j < NPOINTS; j++) {\n", + " creal = -2.0 + 2.5 * (double) (i) / (double) (NPOINTS) + eps;\n", + " cimag = 1.125 * (double) (j) / (double) (NPOINTS) + eps;\n", + " testpoint(creal, cimag);\n", + " }\n", + " }\n", + "\n", + " // Calculate area of set and error estimate and output the results\n", + " area = 2.0 * 2.5 * 1.125 * (double) (NPOINTS * NPOINTS - numoutside) / (double) (NPOINTS * NPOINTS);\n", + " error = area / (double) NPOINTS;\n", + "\n", + " printf(\"Area of Mandlebrot set = %12.8f +/- %12.8f\\n\", area, error);\n", + " printf(\"Correct answer should be around 1.510659\\n\");\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "39cf129c-8106-4e67-a2f1-1a7fff17cd38", + "metadata": {}, + "outputs": [], + "source": [ + "main();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7bcf082a-e0a5-4260-8729-cbfab515cc6a", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "C++14", + "language": "C++14", + "name": "xcpp14" + }, + "language_info": { + "codemirror_mode": "text/x-c++src", + "file_extension": ".cpp", + "mimetype": "text/x-c++src", + "name": "c++", + "version": "14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/notebooks/omp/pi_integral.ipynb b/notebooks/omp/pi_integral.ipynb new file mode 100644 index 00000000..50b9b88d --- /dev/null +++ b/notebooks/omp/pi_integral.ipynb @@ -0,0 +1,109 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "d8967ce2-994c-441e-b796-4099c6c6853c", + "metadata": {}, + "outputs": [], + "source": [ + "#include \n", + "#include \n", + "#include " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "93b89565-44fe-4729-980b-d4f897161b0b", + "metadata": {}, + "outputs": [], + "source": [ + "Cpp::LoadLibrary(\"libomp\");" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9078ac79-ca50-4fef-b785-37f35fec3cab", + "metadata": {}, + "outputs": [], + "source": [ + "static long num_steps = 100000000;\n", + "double step;" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f3c10995-6f29-4d71-9e61-1993ca9d1cc9", + "metadata": {}, + "outputs": [], + "source": [ + "int main() {\n", + " int i, j, num_threads_allocated;\n", + " double x, pi, sum = 0.0;\n", + " double start_time, run_time;\n", + "\n", + " step = 1.0 / (double)num_steps;\n", + " printf(\"Num threads available: %d\\n\", omp_get_max_threads());\n", + " for (i = 1; i <= 4; i++) {\n", + " sum = 0.0;\n", + " omp_set_num_threads(i);\n", + " start_time = omp_get_wtime();\n", + "#pragma omp parallel\n", + " {\n", + " num_threads_allocated = omp_get_num_threads();\n", + "#pragma omp single\n", + " printf(\"Num threads allocated for this run: %d\\n\", num_threads_allocated);\n", + "\n", + "#pragma omp for reduction(+ : sum)\n", + " for (j = 1; j <= num_steps; j++) {\n", + " x = (j - 0.5) * step;\n", + " sum = sum + 4.0 / (1.0 + x * x);\n", + " }\n", + " }\n", + "\n", + " pi = step * sum;\n", + " run_time = omp_get_wtime() - start_time;\n", + " printf(\"pi is %f in %f seconds using %d threads\\n\\n\", pi, run_time, num_threads_allocated);\n", + " }\n", + "}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0f84442a-d947-4860-bd3c-aeeea963b419", + "metadata": {}, + "outputs": [], + "source": [ + "main();" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7066da1-729b-4230-980f-9be9430c19d6", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "C++14", + "language": "C++14", + "name": "xcpp14" + }, + "language_info": { + "codemirror_mode": "text/x-c++src", + "file_extension": ".cpp", + "mimetype": "text/x-c++src", + "name": "c++", + "version": "14" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}