Giter VIP home page Giter VIP logo

Comments (5)

Oceania2018 avatar Oceania2018 commented on August 11, 2024

With python gil's own limitations, I don't think it's necessary to call the same numpy object with multiple threads from outside.

https://wiki.python.org/moin/GlobalInterpreterLock

from numpy.net.

henon avatar henon commented on August 11, 2024

@pkingwsd: right now, the GIL locking has not been put inside of each call to np. As stated in the pythonnet documentation you should do all calls to Python inside of the lock like this:

using (Py.GIL())
{
    np.matmul(a, b);
}

Do you think it would be better to have this locking automatically in every API call or should the user put all his processing inside of a lock? I am undecided.

from numpy.net.

pkingwsd avatar pkingwsd commented on August 11, 2024

@henon : pls try it.np.matmul(a, b);// no response.

   var a = np.arange(1000);
        var b = np.arange(1000);
            Task.Run(() => {

                using (Py.GIL())
                {
                   np.matmul(a, b);// no response
                   Console.WriteLine("work");

                }
            });

        Console.ReadLine();

from numpy.net.

henon avatar henon commented on August 11, 2024

Before using background threads you need to call PythonEngine.BeginAllowThreads();
Check out this issue on pythonnet that explains it: pythonnet/pythonnet#109

Console.WriteLine("Efficient matrix multiplication with NumPy:");
// before starting the measurement, let us call numpy once to get the setup checks done. 
var stopwatch = Stopwatch.StartNew();
np.arange(1);

var a1 = np.arange(60000).reshape(300, 200);
var a2 = np.arange(80000).reshape(200, 400);

var result = np.matmul(a1, a2);
stopwatch.Stop();

Console.WriteLine($"execution time with NumPy: {stopwatch.Elapsed.TotalMilliseconds}ms\n");
Console.WriteLine("Result:\n" + result.repr);


Console.WriteLine("executing on bg thread");

var a = np.arange(1000);
var b = np.arange(1000);

// https://github.com/pythonnet/pythonnet/issues/109
PythonEngine.BeginAllowThreads();

Task.Run(() =>
{
    using (Py.GIL())
    {
        np.matmul(a, b);
        Console.WriteLine("matmul on bg thread is done");
    }
}).Wait();
Console.WriteLine("Press key");

Console.ReadKey();

from numpy.net.

pkingwsd avatar pkingwsd commented on August 11, 2024

awesome.

from numpy.net.

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.