Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up general transpose #9

Open
nstair opened this issue Jul 12, 2023 · 0 comments
Open

Speed up general transpose #9

nstair opened this issue Jul 12, 2023 · 0 comments
Assignees

Comments

@nstair
Copy link
Owner

nstair commented Jul 12, 2023

Speed/clean up the general_transpose function (in tensor.cc). Currently the last for loop in

Tensor Tensor::general_transpose(const std::vector<size_t>& axes) const 
{
    if (axes.size() != ndim()) {
        throw std::invalid_argument("Invalid axes permutation");
    }

    std::vector<size_t> transposed_shape(ndim());
    for (size_t i = 0; i < ndim(); ++i) {
        transposed_shape[i] = shape_[axes[i]];
    }

    // std::shared_ptr<Tensor> transposed_tensor(new Tensor(transposed_shape));
    Tensor transposed_tensor(transposed_shape);

    std::complex<double>* transposed_data = transposed_tensor.data().data();
    // const std::complex<double>* original_data = data_.data();

    // This works but probably can be made more efficient.
    // Fix if it turns out to be a bottleneck
    for (size_t i = 0; i < size_; i++){
        std::vector<size_t> tidx_trans = vidx_to_tidx(i);
        size_t t_vidx = transposed_tensor.tidx_to_trans_vidx(tidx_trans, axes);
        transposed_data[t_vidx] = data_[i];
    }

    return transposed_tensor;  
}

is maybe doing too much work, if you have time and see a way to optimize then give it a try. Try to transpose a large tensor (30x30x30x30) or someting and see if it is faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants