Giter Club home page Giter Club logo

Comments (2)

adtzlr avatar adtzlr commented on June 16, 2024
import felupe as fem
import numpy as np
from threading import Thread

mesh = fem.Cube(n=51)
region = fem.RegionHexahedron(mesh)
field = fem.FieldContainer([fem.Field(region, dim=3)])

boundaries, loadcase = fem.dof.uniaxial(field, clamped=True)


class Threaded(fem.constitution.ConstitutiveMaterial):
    def __init__(self, material, axis, nthreads, **kwargs):
        self.material = material
        self.x = self.material.x
        self.axis = axis
        self._axes = [slice(None)] * abs(self.axis + 1)
        self.nthreads = nthreads
        self._P = None
        self._A = None
        for key, value in kwargs.items():
            setattr(self, key, value)

    def _kernel_grad(self, F, chunk):
        self.material.gradient([F, None], out=self._P[..., slice(*chunk), *self._axes])

    def _kernel_hess(self, F, chunk):
        self.material.hessian([F, None], out=self._A[..., slice(*chunk), *self._axes])

    def chunks(self, F):
        defgrads = np.array_split(F, self.nthreads, axis=self.axis)
        chunksizes = np.cumsum([chunk.shape[self.axis] for chunk in defgrads])
        chunks = np.vstack([np.append(0, chunksizes[:-1]), chunksizes]).T
        return defgrads, chunksizes, chunks

    def evaluate(self, target, defgrads, chunks):
        threads = [
            Thread(target=target, args=(F, chunk)) for F, chunk in zip(defgrads, chunks)
        ]
        for t in threads:
            t.start()
        for t in threads:
            t.join()

    def gradient(self, x):
        F, statevars = x
        defgrads, chunks, chunksizes = self.chunks(F)

        if self._P is None:
            self._P = np.zeros_like(F)

        self.evaluate(self._kernel_grad, defgrads, chunks)
        return [self._P, None]

    def hessian(self, x):
        F, statevars = x
        defgrads, chunks, chunksizes = self.chunks(F)

        if self._A is None:
            self._A = np.zeros((*F.shape[:2], *F.shape))

        self.evaluate(self._kernel_hess, defgrads, chunks)
        return [self._A]


umat_threaded = Threaded(fem.NeoHooke(mu=1, bulk=2), axis=-1, nthreads=16)
solid_threaded = fem.SolidBody(umat_threaded, field)

umat = fem.NeoHooke(mu=1, bulk=2)
solid = fem.SolidBody(umat, field)

gives

>>> %timeit -r1 -n1 solid.assemble.matrix(field)
12.6 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

>>> %timeit -r1 -n1 solid_threaded.assemble.matrix(field)
10 s ± 0 ns per loop (mean ± std. dev. of 1 run, 1 loop each)

from felupe.

adtzlr avatar adtzlr commented on June 16, 2024

Nice idea but it seems that the effort is not worth it.

from felupe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.