yingjerkao / uni10

Official Repo for Uni10
28 stars 9 forks source link

uni10.Matrix into Numpy: very slow! #27

Open rezah opened 7 years ago

rezah commented 7 years ago

I need to transfer matrices in Uni10 into numpy ones. But, it takes so much time if you do it with a simple for loop. Please see attached: it compares matrix multiplication with access elements: egM2.py.zip

Any suggestion?

macthecadillac commented 7 years ago

The slowness is not so much about uni10 itself but the way you were processing the data in python. I see that you are using nested for loops in python--python loops are slooooow (true for most if not all scripting languages). When writing python scripts for numerical calculations, use vectorized functions in lieu of loops whenever possible.

Now about the data transferring part. I know there are ways you can pass a C/C++ array to numpy simply by passing a pointer (that way no copying is needed). I read through the documentation of pyUni but I couldn't find a way to access the memory location of the matrix. Perhaps the author could elucidate the situation a little.

How to convert C array pointer to numpy array

rezah commented 7 years ago

You are fully right. I'm looking for to hearing from the authors, to find an efficient way.

yingjerkao commented 7 years ago

We experimented with sharing the view of the uni10 matrix for numpy array using https://docs.scipy.org/doc/numpy/reference/swig.interface-file.html

We were successful at exposing uni10 matrix as a numpy array, but failed to take numpy array as input, so we didn't merge that into pyUni10. I can push half-done version to a new branch if that helps your calculation.

rezah commented 7 years ago

Thank you: there is no need to push it into a new branch.

I just mention my problem for further updates of uni10. Solving a linear equation using least-square algorithm: Ax=b; x? https://docs.scipy.org/doc/numpy-1.10.4/reference/generated/numpy.linalg.lstsq.html It usually happens in iPEPS simulation.