Giter VIP home page Giter VIP logo

Comments (4)

shencebebetterme avatar shencebebetterme commented on June 25, 2024

I'm interested in this because I have found an expedient to bypass the arnoldi issue in #360.

For those who need more matrix operations, you can compile ITensor together with other linear algebra packages such as armadillo, and use the matrix functions therein. Below is an example of constructing an armadillo matrix from a rank-2 ITensor using the above v

    int di = 3;
    int dj = 3;
    auto i = Index(di, "i");
    auto j = Index(dj, "j");
    auto T = ITensor(i, j);
    T.randomize();

    auto extractReal = [](Dense<Real> const& d)
    {
    return d.store;
    };

    auto v = applyFunc(extractReal,T.store());

    arma::mat m(&v[0], di, dj, false);

from itensor.

mtfishman avatar mtfishman commented on June 25, 2024

ITensor storage makes use of an std::vector with a custom allocator that allows for allocating uninitialized memory (since std::vector always initializes memory as zeros, which we found to be a pretty significant cost throughout ITensor). You can see that here, for example. You can see the definition here.

Perhaps you can use:

vector_no_init<Real> vec = v;

though I'm not sure if that will "play nice" with your application. Otherwise, you should be able to copy the data over to an std::vector using one of the many std::vector constructors or by copying the data over element by element, if you really need an std::vector.

from itensor.

shencebebetterme avatar shencebebetterme commented on June 25, 2024

Hi Matthew! Thanks a lot!
I've read through the source codes of dense.h and vector_no_init.h and I can understand the implementation now.

from itensor.

shencebebetterme avatar shencebebetterme commented on June 25, 2024

After looking into the ITData class I've found a conceptually simpler and generally much faster (in debug mode) method to extract dense data to a std::vector. This is done in merely two lines.

vector_no_init<Real>& dvec = (*((ITWrap<Dense<Real>>*) & (*T.store()))).d.store;
	
std::vector<Real> vec(dvec.begin(), dvec.end());

The first line may look like a mess, but it's actually not very difficult to understand.

T.store() is a shared pointer to ITData, dereference it and take address again gives a bare pointer to ITData. The bare pointer is then converted to a pointer to the template class ITWrap which is derived from ITData. For a Real dense ITensor this template parameter is the class Dense<Real>. Dereference this pointer again gives an ITWrap class, this is the type of the part inside the outermost bracket, so (...) has type ITWrap.

The actual ITensor data is stored in a Dense<Real>, the d member of ITWrap. Finally (...).d.store is a std::vector with a custom allocator, this is exactly the data vector that we want.

A standard vector can then be initialized by setting the correct iterators. This method is, in debug mode and on my laptop, around 70 times faster than the above applyFunc method, however, there's no performance difference in release mode.

And there're more advantages since the data vector dvec is now completely exposed to us, for example it's easier to retrieve other information or even manipulate the data with STL tools, and not limited to ITensor::visit and ITensor::apply.

from itensor.

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.