Re: ENH: allow scipy.sparse matrices to preserve `np.memmap` input data
Julien Jerphanion <[email protected]>
| Newsgroups | gmane.comp.python.scientific.devel |
|---|---|
| Message-ID | <YfrLroeKuw8i4Nu8nLxb_bz41gJ5JLFlhi_YRlDmbvh_0MPuIXXfkDuM0YlKmWj0cKahRpBhl5-iamkyMz17uLAptaCK_LHjMzkKu6zh6qo=@jjerphan.xyz> |
Hi Aaron, Thank you for reporting this use-case! The current implementations of SciPy's sparse matrices cast arrays at creation, but also after some operations as you mentioned. Other issues and RFC reported similar usability problems and potential solutions (with thread-offs not to break other use-cases). I think your request is in line with Scientific Python's Sparse Array Summit which aims at identifying and addressing needs for the whole ecosystem regarding Sparse data-structures: https://discuss.scientific-python.org/t/sparse-array-summit/365 I also think participants to those discussions would benefit from your inputs. What do other participants think? Best, Julien. ------- Original Message ------- On Sunday, March 26th, 2023 at 1:40 PM, Aaron Z. <[email protected]> wrote: > Hi all - this is my first post here. > > I've been using csr and coo matrices for large graph algorithms. I've found that scipy will beat frameworks like pregel on almost any task with large memory machines >200GB+ for storing large adjacency matrices. To run in environments with memory constraints, using numpy.memmap arrays for COO: (row, col, data) or CSR: (indices, indptr, data) has helped to reduce the memory footprint (obviously at the cost of some speed). > > > However, getting `csr_matrix` and `coo_matrix` to accept `np.memmap` format involved quite a bit of hacky monkey patching, because currently the scipy constructors will convert everything to a base ndarray, effectively copying to memory from disk. > > Would there be a good reason not to support this in a future version natively? The changes required would be just some usage of the `subok=True` kwarg for `np.array` which would preserve the `np.memmap` class and using `np.asanyarray` instead of `np.asarray` in other places. > > For example, in the `csr_matrix.__init__`: https://github.com/scipy/scipy/blob/979102e73a8b9612ac3c97c438e40de26e46c9b7/scipy/sparse/_compressed.py#L69-L72 > `python self.indices = np.array(indices, copy=copy, dtype=idx_dtype, subok=True) self.indptr = np.array(indptr, copy=copy, dtype=idx_dtype, subok=True) self.data = np.array(data, copy=copy, dtype=dtype, subok=True)` > keeps input data on disk. > > When the user then performs an operation that creates a new matrix, for example matrix multiplication, it becomes more difficult to keep things to memmaps, because the user has to specify a filepath to keep the new matrix at. But I think a great first step would be to not cast the input data - this still leads to a decrease in memory usage, and the user can choose to memmap the output manually themselves. > > I'd be happy to raise a PR for this if this has some support - looking forward to hearing thoughts! > _______________________________________________ > SciPy-Dev mailing list -- [email protected] > To unsubscribe send an email to [email protected] > https://mail.python.org/mailman3/lists/scipy-dev.python.org/ > Member address: [email protected] _______________________________________________ SciPy-Dev mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3/lists/scipy-dev.python.org/ Member address: [email protected]
publickey - [email protected] - 0xDEDBF4EA.asc
(application/pgp-keys, 3 KB)
-----BEGIN PGP PUBLIC KEY BLOCK----- xsFNBFhax+kBEADWCcFOgTPAO8LnpohB8lh+qOaKyNmC8K5EunMwt35J9mxd XNLy3BbNli5fFU2xl9AqFXj3GeeBvENdQn2jwbDSfzMGpRAZ8KD8nYNgZFsP tckakrpcmS69BF/I/6sdA9o9FP//MoZCEx+yQ7xaA9LRdQA9caFgtmpuqdYJ PPI/IWJLKVjyMjrr+KwGFHfo68sWlkW7dq4dN5sFud1oT0ZnzVEjo+xKV+x/ e7JwpMI5bTz4KXe3ChUJfBZ4ArQHRIiPSqWh980Pr20RGXrCcVJkbChBE0XI KIHefBIHH88zg8LmiL0R3bfEtUCPAJQBSHzcUcPG8mdCOvkYx1fNxddWyp1t H8BFhpbE/foMkXLZq6YtwV4cYPISiJkV7WKKCtwiOqtMcIlI6uwrM0SkGQap 8ZQ6K2Q757+IkPLtgcvCCrv/f1iECcJtMKqO3oG1zSSjjqd1TtfKN+LlcQ+m r5/KiO2iMgVQipOrVfVgEU6CD7Gdn09mY4cnpy8g+WdYuhTcuLF0RlUKM2YW kCCyYgjZQ9hq0qYWzmJ7Il0zHts9nRBoaT/QNRS/qRQChNteGjUjwnRy7oOD MHLoIKfjKQa8sDMHfPgllIgb5o45zkZ2FHorZebfj4rgHPK8Zhu+pZ2/3KMp Jagh1KTAdV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ejzdPEZhUBfsVYsbcbV2Fp0nCTw2/dAheQlCIwmjcZG4fJLoJzP2 nlNptimf14lh7Mas+p7yYqzhpBNCw0lZ41RKwrePTDITe/50IBIgOp9XtTE0 WFaaDg== =/cO6 -----END PGP PUBLIC KEY BLOCK-----
signature.asc
(application/pgp-signature, 855 B)
-----BEGIN PGP SIGNATURE----- Version: ProtonMail wsFzBAEBCAAnBQJkIK8SCRBWtpCpfC41uBYhBN7b9Or9ieiZmPAnSVa2kKl8 LjW4AACYHQ/9GnT5qj6DJjT0S0Cb6qyChMx27/ZbTTZ2/sH1o7jmDMonCZV4 me3WiyCQZaNqWSreEkefnMiZkR+G1b3nNhnnp5jERh8vjAWo1Een1oztZ5h1 +3OyqUY3gWkECLkk8DZSgNkxSGrtO5VmHdVTp4x9LrNEzBUW1sRUNKAOJVgQ qpBNxW3iPIGwegCva14fUGZyMAVY+PpF1/EbK1CrHpn4OUP7uGLDBkiwPuyI kcWcS8dNPqOcnsCPKjTWY8/0vc+FrZAvGZAouq6L+bZgWL4Rb4y3bwGEya7u 1AvNcSUV0mAEcFxetD2YbtiEiH/1lVOphqNOPm7IMXT5GP0Qa4o22KClRK1u VAkrCFer5CllobkGs34dxoukNM3PCqJ+u+9QMyXycbljQSTkHwT8uxU2SAl1 Ci8RRaii//FO99iQA268AJV7A/FY1RR8L3T5+XGb6vwUGnOWuoSSoVPTwLT3 hJZsjuiMuT7ixVm7Za9WKQlEzLqC1SMTUDon7I9UBplCYK5g+BWQLuA7b30L cXJiFeoq2ODo68e+vLDP7k/hcoTzEVK6SiIPp8lA9IVze6M4FjXm/i+/tIU7 x+LgeIDp8RVmBLCvGKDI6BQWJbehh+nIQiJmWKq46Ojh2tmNP61exd99NAyx 5YV3+QNzHBD/PyZBaHGgAh/3UhQC5uDN/Qs= =hKfE -----END PGP SIGNATURE-----