{ "cells": [ { "cell_type": "markdown", "id": "4ab4aa83-a3a6-489f-baa5-ddd7e10f0174", "metadata": {}, "source": [ "# Field Transfer Threads Performance" ] }, { "cell_type": "markdown", "id": "fb3dd576-dad3-4bcf-a321-b91cb32e8f51", "metadata": {}, "source": [ "## Import " ] }, { "cell_type": "code", "execution_count": null, "id": "f0882b99-00b7-43b1-934e-4d3134ff64ca", "metadata": {}, "outputs": [], "source": [ "from Muscat.Containers.MeshCreationTools import CreateCube\n", "from Muscat.FE.Fields.FEField import FEField\n", "from Muscat.Containers.ConstantRectilinearMeshTools import CreateConstantRectilinearMesh\n", "from Muscat.LinAlg.Transform import Transform\n", "from Muscat.Helpers.TextFormatHelper import TFormat\n", "from Muscat.Containers.NativeTransfer import NativeTransfer\n", "import numpy as np\n", "import time\n", "import pyvista\n", "pyvista.global_theme._jupyter_backend = 'panel' # remove this line to get interactive 3D plots\n" ] }, { "cell_type": "markdown", "id": "190c645c-27ad-4f62-88de-31ffade3d4ca", "metadata": {}, "source": [ "## Mesh and Target Points Generation" ] }, { "cell_type": "code", "execution_count": null, "id": "81689c00-4196-47b6-a33f-a38b059a3138", "metadata": {}, "outputs": [], "source": [ "N = 50\n", "inputmesh = CreateCube(dimensions=[N, N, N], origin=[-1.0] * 3, spacing=[2 / (N - 1), 2 / (N - 1), 2 / (N - 1)])\n", "inputFEField = FEField(name=\"3DTo3DNative\", mesh=inputmesh)\n", "\n", "N = 60\n", "outmesh = CreateConstantRectilinearMesh(dimensions=[N, N, N], origin=[-1.0] * 3, spacing=[2 / (N - 1), 2 / (N - 1), 2 / (N - 1)])\n", "print(\"Input mesh:\")\n", "print(inputmesh)\n", "print(\"Output mesh:\")\n", "print(outmesh)\n" ] }, { "cell_type": "markdown", "id": "d715aa54-000e-4234-8e12-1d58fd5f1015", "metadata": {}, "source": [ "## Deform output mesh \n", "\n", "Make the problem more realistic" ] }, { "cell_type": "code", "execution_count": null, "id": "97ae3d2e-cca9-4ce0-b4d0-9798310f25af", "metadata": {}, "outputs": [], "source": [ "op = Transform()\n", "op.keepNormalized = False\n", "op.keepOrthogonal = False\n", "op.SetFirst([1.4, 0.56, 0])\n", "op.SetSecond([-1.135, 1.42486, 1.6102])\n", "\n", "outmesh.nodes = np.ascontiguousarray(op.ApplyTransform(outmesh.nodes))\n" ] }, { "cell_type": "markdown", "id": "81036731-5eb5-4b79-a5c0-43556bc9d336", "metadata": {}, "source": [ "## Generate Data" ] }, { "cell_type": "code", "execution_count": null, "id": "8015d35e-a2ee-40bc-bc9c-177e2f91e72c", "metadata": {}, "outputs": [], "source": [ "x = inputmesh.nodes[:, 0]\n", "y = inputmesh.nodes[:, 1]\n", "data = (x - 0.5) ** 2 - y * 0.5 + x * y * 0.25\n" ] }, { "cell_type": "markdown", "id": "933277ed-df3d-43a9-87dc-f753f7b98caf", "metadata": {}, "source": [ "## Start transfer timing..." ] }, { "cell_type": "code", "execution_count": null, "id": "2beb9040-97e8-4d6c-a1ca-d6c8e5844e15", "metadata": {}, "outputs": [], "source": [ "print(f\"Number of elements in the origin mesh = {inputmesh.GetNumberOfElements()}\")\n", "print(f\"Number of points in the target cloud point = {outmesh.GetNumberOfNodes()}\")\n" ] }, { "cell_type": "markdown", "id": "ef7b1b4a-3d78-4ce6-8f4f-27d5c934bc41", "metadata": {}, "source": [ "## Some utilities " ] }, { "cell_type": "code", "execution_count": null, "id": "b942c413-e773-4ea2-93fa-f9c3beb357b7", "metadata": {}, "outputs": [], "source": [ "def PrintRow(datarow):\n", " res = \"|\"\n", " for d in datarow:\n", " if type(d) is str:\n", " res += TFormat.Center(str(d), fill=\" \", width=20) + \"|\"\n", " else:\n", " res += TFormat.Center(\"%.4f\" % d, fill=\" \", width=20) + \"|\"\n", " print(res)\n" ] }, { "cell_type": "markdown", "id": "6c8316cf-a4d9-4dee-9553-f8b5dd1fe2c5", "metadata": {}, "source": [ "## Initialization" ] }, { "cell_type": "code", "execution_count": null, "id": "1a01584f-6a9b-4ae0-8e59-4985309de5be", "metadata": {}, "outputs": [], "source": [ "setFieldTime = time.time()\n", "nt = NativeTransfer()\n", "nt.SetVerbose(False)\n", "searchStrategies = ((True,False,False,False),\n", " (False,True,False,False),\n", " (False,True,True,False),\n", " (False,False,False,True),\n", " (True,True,False,True))\n", "\n", "for ss in searchStrategies:\n", " nt.SetUsePointSearch(ss[0])\n", " nt.SetUseElementSearch(ss[1])\n", " nt.SetUseElementSearchFast(ss[2])\n", " nt.SetUseEdgeSearch(ss[3])\n", " setFieldTime = time.time()\n", " nt.SetSourceFEField(inputFEField)\n", " st = time.time() - setFieldTime\n", " print(f\"Set Up time (SetSourceFEField) for {ss} : {st} [s]\")\n" ] }, { "cell_type": "markdown", "id": "fc6448fa-f530-4657-8db3-d5bfb7dfa404", "metadata": {}, "source": [ "## Threads Scalability" ] }, { "cell_type": "code", "execution_count": null, "id": "e9f117ae-16b0-4d72-b7ae-0a9f848d0218", "metadata": {}, "outputs": [], "source": [ "nt.SetUsePointSearch(True)\n", "nt.SetUseElementSearch(False)\n", "nt.SetUseElementSearchFast(False)\n", "nt.SetUseEdgeSearch(False)\n", "nt.SetSourceFEField(inputFEField)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "dbf50eb8-a919-4bf6-8607-7ebacfc81049", "metadata": {}, "outputs": [], "source": [ "from Muscat.Helpers.CPU import GetNumberOfAvailableCores\n", "print(f\"GetNumberOfAvailableCores: {GetNumberOfAvailableCores()}\")\n", "nbCores = (np.arange(int(np.ceil(np.sqrt(GetNumberOfAvailableCores()))))**2+1)\n", "nbCores[-1] = GetNumberOfAvailableCores()\n" ] }, { "cell_type": "code", "execution_count": null, "id": "c9c30c03-e143-41d0-ad3b-adc9d7932e61", "metadata": {}, "outputs": [], "source": [ "print(f\"Set Up time (SetSourceFEField) {st} [s]\")\n", "print(\"_\"*(8*20+9))\n", "output = [\"method\"]\n", "output.extend(f\"cpp [s] {th} thread \" for th in nbCores)\n", "output.extend(f\"speedup {th}/1 \" for th in nbCores[1:])\n", "PrintRow(output)\n", "for method in [\"Interp/Nearest\", \"Nearest/Nearest\", \"Interp/Clamp\", \"Interp/Extrap\"]:\n", " nt.SetTransferMethod(method)\n", " nt.SetTargetPoints(outmesh.nodes)\n", " times = []\n", " for nbThreads in nbCores:\n", " nt.SetMaxConcurrency(nbThreads)\n", " cppStartTime = time.time()\n", " nt.Compute()\n", " cppStopTime = time.time()\n", " op = nt.GetOperator()\n", " times.append(cppStopTime - cppStartTime)\n", "\n", " output = [method]\n", " output.extend(times)\n", " output.extend([times[0] / (t if t > 0 else 1) for t in times[1:]])\n", "\n", " PrintRow(output)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.10" } }, "nbformat": 4, "nbformat_minor": 5 }