Marcel-Rodekamp / NSL

Nanosystem Simulation Library (NSL) implements statistical simulations for systems on the nanoscale
Other
2 stars 0 forks source link

Fixing assignment of default constructed Tensors #54

Closed Marcel-Rodekamp closed 2 years ago

Marcel-Rodekamp commented 2 years ago

Objects like NSL::map default construct a NSL::Tensor in order to assign them at a later point (see e.g. NSL::Lattice::SpatialLattice::exp_hopping_matrix_). The way default construction worked before prevented later assignment using the NSL::Tensor::operator= as the shape of the target tensor naturally doesn't match the shape of the source tensor. In this PR a fix for this issue is provided. In order to do this the default construction is now updated to default constructing the underlying torch::Tensor. A default constructed torch::Tensor counts as "not defined", i.e. torch::Tensor::defined returns false. Leveraging this behavior the method operator= can check for defined-ness. If the torch::Tensor is already defined, the behavior remains unchanged if it is undefined, i.e. default constructed, a new memory space is created and the source Tensor is copied in it. The latter is not asynchronous!