ansys / pymapdl

Pythonic interface to MAPDL
https://mapdl.docs.pyansys.com
MIT License
424 stars 120 forks source link

Add single worker functionality to LocalMapdlPool #2653

Closed CesarRodriguezPereira closed 8 months ago

CesarRodriguezPereira commented 8 months ago

Description of the feature

Right now, LocalMapdlPool allows making a pool of worker instances of 2 or more workers. This makes sense on paper, as it really isn't a pool if it's a single worker. However, this limitation seems arbitrary, aside from the concept of a pool having multiple workers, while it has it's downsides, as pools offer tools that can't be easily acessed with a single MAPDL instance. The pool.map function is really powerful, and allowing a single worker would be more efficient when the user is RAM limited, as it lets the map function run sequentially, but retaining features such as the ability to restart crashed instances of mapdl.

These benefits can be implemented by the end user on their own code, by restarting apdl each time and just running their batch runs with a loop rather than a map function, but the limitation seems arbitrary and self imposed. The functionality of the pool is already implemented.

My own example: I have set up a parametric sweep of a few thousand models. On my main computer, with more RAM, I can run a pool of 4 workers before I get RAM limited and notice a slight slowdown due to the use of swap on an SSD. The computer that I can spare for this sweep is more limited: it's faster to run the models sequentially than even with 2 workers. However, some of the parameter combinations I'm running might be a bit unstable, and thus, having the restart functionality implemented is important for my sweep. Thus, I'm running it with 2 workers, as the over-use of RAM is slight enough that the slowdown isn't that noticeable, but I'd be saving time if I ran it with just the 1 worker.

Steps for implementing the feature

Change LocalMapdlPool n_instances check from n_instances >= 2 to n_instances >= 1

Useful links and references

No response

germa89 commented 8 months ago

mmmhh..

I see the value, because of the respawning ... Ok.. then..

But I think this should be ported to the standard MAPDL instance. But for the moment, I will allow only one instance.

germa89 commented 8 months ago

Remember to use the main branch of the repo (clone + pip install .) if you want to use this feature before release.

germa89 commented 8 months ago

I'm not closing this issue until I have studied properly how to implement this in the Mapdl class.

germa89 commented 8 months ago

I don't know how to proceed about this....

Because relaunching MAPDL without notifying the user seems a bit too much... specially because the current model get lost... but you might still want to reuse the files ...

I feel the way to go is just leave it as it is. You can use pool or use error handling (improved in #2671 ).

So with #2671 you could do something like:

done=False

while not done:
    try:
        my_mapdl_code(mapdl)
        done = True

    except MapdlException:
        mapdl = launch_mapdl()
germa89 commented 8 months ago

Digging more into pool.py, I think the right approach will be to port that spawning features to mapdl, and make pool just reuse them. This might be a good idea. But it might take some time. So I lack of manpower to do this.

So I'm closing this issue for the moment, but if I receive enough demand, feedback and/or support, I will reconsider to reopen it and implement it.

germa89 commented 8 months ago

Pinging @koubaa