Skip to content

qttools.datastructures.routines#

[docs] module qttools.datastructures.routines

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# Copyright (c) 2024 ETH Zurich and the authors of the qttools package.

from collections.abc import Callable

from mpi4py.MPI import Intracomm, Request

from qttools import xp
from qttools.comm import comm
from qttools.comm.comm import GPU_AWARE_MPI
from qttools.datastructures.dsdbsparse import DSDBSparse
from qttools.profiling import Profiler
from qttools.utils.gpu_utils import synchronize_device

profiler = Profiler()


@profiler.profile(level="api")
def correct_out_range_index(i: int, k: int, num_blocks: int):
    # find the index of block in the matrix being repeated into open-end
    # based on the difference of row and col, ie diagonal
    diag = k - i
    k_1 = min(max(k, 0), num_blocks - 1)
    i_1 = k_1 - diag  # keep the same diag
    i_2 = min(max(i_1, 0), num_blocks - 1)
    k_2 = i_2 + diag  # keep the same diag
    return (i_2, k_2)


@profiler.profile(level="api")
def bd_matmul(
    a: DSDBSparse,
    b: DSDBSparse | list[DSDBSparse],
    out: DSDBSparse | None,
    b_op: Callable | None = None,
    in_num_diag: int = 3,
    out_num_diag: int = 5,
    spillover_correction: bool = False,
    accumulator_dtype=None,
):
    """Matrix multiplication of two `a @ b` BD DSDBSparse matrices.

    Parameters
    ----------
    a : DSDBSparse
        The first block diagonal matrix.
    b : DSDBSparse
        The second block diagonal matrix.
    out : DSDBSparse
        The output matrix. This matrix must have the same block size as
        `a` and `b`. It will compute up to `out_num_diag` diagonals.
    in_num_diag: int
        The number of diagonals in input matrices
    out_num_diag: int
        The number of diagonals in output matrices
    spillover_correction : bool, optional
        Whether to apply spillover corrections to the output matrix.
        This is necessary when the matrices represent open-ended
        systems. The default is False.
    accumulator_dtype : data type, optional
        The data type of the temporary accumulator matrices. The default is complex128.

    TODO: replace @ by appropriate gemm

    """
    if b_op is None and isinstance(b, list):
        raise ValueError("When b is a list, b_op must be provided")

    if (
        a.distribution_state == "nnz"
        or (not isinstance(b, list) and b.distribution_state == "nnz")
        or (isinstance(b, list) and any([bi.distribution_state == "nnz" for bi in b]))
    ):
        raise ValueError(
            "Matrix multiplication is not supported for matrices in nnz distribution state."
        )
    num_blocks = len(a.block_sizes)

    if accumulator_dtype is None:
        accumulator_dtype = a.dtype

    # Make sure the output matrix is initialized to zero.
    if out is not None:
        out.data = 0
        out_block = False
        # NOTE: Using the stack attribute to force caching of the data view.
        out_ = out.stack[...]
    else:
        out_block = True
        out = {}

    a_ = a.stack[...]
    if isinstance(b, list):
        b_ = [bi.stack[...] for bi in b]
    else:
        b_ = b.stack[...]

    for i in range(num_blocks):
        for j in range(
            max(i - out_num_diag // 2, 0), min(i + out_num_diag // 2 + 1, num_blocks)
        ):
            if out_block:
                partsum = xp.zeros(
                    (a.block_sizes[i], a.block_sizes[j]), dtype=accumulator_dtype
                )
            else:
                partsum = (out_.blocks[i, j]).astype(accumulator_dtype)

            for k in range(i - in_num_diag // 2, i + in_num_diag // 2 + 1):
                if abs(j - k) > in_num_diag // 2:
                    continue
                out_range = (k < 0) or (k >= num_blocks)
                if out_range and (not spillover_correction):
                    continue
                else:
                    if out_range:
                        i_a, k_a = correct_out_range_index(i, k, num_blocks)
                        k_b, j_b = correct_out_range_index(k, j, num_blocks)
                        if isinstance(b, list):
                            sum_b = xp.zeros_like(b_[0].blocks[k_b, j_b])
                            for bi_ in b_:
                                sum_b = b_op(sum_b, bi_.blocks[k_b, j_b])
                            partsum += a_.blocks[i_a, k_a] @ sum_b
                        else:
                            partsum += a_.blocks[i_a, k_a] @ b_.blocks[k_b, j_b]
                    else:
                        if isinstance(b, list):
                            sum_b = xp.zeros_like(b_[0].blocks[k, j])
                            for bi_ in b_:
                                sum_b = b_op(sum_b, bi_.blocks[k, j])
                            partsum += a_.blocks[i, k] @ sum_b
                        else:
                            partsum += a_.blocks[i, k] @ b_.blocks[k, j]

            if out_block:
                out[i, j] = partsum
            else:
                out_.blocks[i, j] = partsum

    if out_block:
        return out


@profiler.profile(level="api")
def bd_sandwich(
    a: DSDBSparse,
    b: DSDBSparse,
    out: DSDBSparse | None,
    in_num_diag: int = 3,
    out_num_diag: int = 7,
    spillover_correction: bool = False,
    accumulator_dtype=None,
    accumulate: bool = False,
):
    """Compute the sandwich product `a @ b @ a` BTD DSDBSparse matrices.

    Parameters
    ----------
    a : DSDBSparse
        The first block tridiagonal matrix.
    b : DSDBSparse
        The second block tridiagonal matrix.
    out : DSDBSparse
        The output matrix. This matrix must have the same block size as
        `a`, and `b`. It will compute up to `out_num_diag` diagonals.
    in_num_diag: int
        The number of diagonals in input matrices
    out_num_diag: int
        The number of diagonals in output matrices
    spillover_correction : bool, optional
        Whether to apply spillover corrections to the output matrix.
        This is necessary when the matrices represent open-ended
        systems. The default is False.
    accumulator_dtype : data type, optional
        The data type of the temporary accumulator matrices. The default is complex128.

    TODO: replace @ by appropriate gemm

    """
    if a.distribution_state == "nnz" or b.distribution_state == "nnz":
        raise ValueError(
            "Matrix multiplication is not supported for matrices in nnz distribution state."
        )
    num_blocks = len(a.block_sizes)

    if accumulator_dtype is None:
        accumulator_dtype = a.dtype

    # Make sure the output matrix is initialized to zero.
    if out is not None:
        if not accumulate:
            out.data = 0
        out_block = False
        # NOTE: Using the stack attribute to force caching of the data view.
        out_ = out.stack[...]
    else:
        out_block = True
        out = {}

    a_ = a.stack[...]
    b_ = b.stack[...]

    for i in range(num_blocks):

        ab_ik = [None] * num_blocks * 2

        for m in range(i - in_num_diag // 2, i + in_num_diag // 2 + 1):

            out_range = (m < 0) or (m >= num_blocks)
            if out_range and (not spillover_correction):
                continue
            else:
                if out_range:
                    a_i, a_m = correct_out_range_index(i, m, num_blocks)
                else:
                    a_i, a_m = i, m

            a_im = a_.blocks[a_i, a_m]

            for k in range(m - in_num_diag // 2, m + in_num_diag // 2 + 1):
                out_range = (k < 0) or (k >= num_blocks) or (m < 0) or (m >= num_blocks)
                if out_range and (not spillover_correction):
                    continue
                else:
                    if out_range:
                        b_m, b_k = correct_out_range_index(m, k, num_blocks)
                    else:
                        b_m, b_k = m, k
                if ab_ik[k] is None:
                    ab_ik[k] = (a_im @ b_.blocks[b_m, b_k]).astype(
                        accumulator_dtype
                    )  # cast data type
                else:
                    ab_ik[k] += (a_im @ b_.blocks[b_m, b_k]).astype(
                        accumulator_dtype
                    )  # cast data type

        if out.symmetry:
            range_j_min = i
        else:
            range_j_min = max(i - out_num_diag // 2, 0)

        for j in range(range_j_min, min(i + out_num_diag // 2 + 1, num_blocks)):

            if out_block:
                partsum = xp.zeros(
                    (a.block_sizes[i], a.block_sizes[j]), dtype=accumulator_dtype
                )
            else:
                partsum = (out_.blocks[i, j]).astype(
                    accumulator_dtype
                )  # cast data type

            for k in range(j - in_num_diag // 2, j + in_num_diag // 2 + 1):
                out_range = (k < 0) or (k >= num_blocks)
                if out_range and (not spillover_correction):
                    continue
                else:
                    if out_range:
                        a_k, a_j = correct_out_range_index(k, j, num_blocks)
                    else:
                        a_k, a_j = k, j
                if ab_ik[k] is None:
                    continue
                partsum += (ab_ik[k] @ a_.blocks[a_k, a_j]).astype(
                    accumulator_dtype
                )  # cast data type

            if out_block:
                out[i, j] = partsum
            else:
                if accumulate:
                    out_.blocks[i, j] += partsum
                else:
                    out_.blocks[i, j] = partsum

    if out_block:
        return out


@profiler.profile(level="api")
def btd_matmul(
    a: DSDBSparse,
    b: DSDBSparse,
    out: DSDBSparse,
    spillover_correction: bool = False,
):
    """Matrix multiplication of two `a @ b` BTD DSDBSparse matrices.

    Parameters
    ----------
    a : DSDBSparse
        The first block tridiagonal matrix.
    b : DSDBSparse
        The second block tridiagonal matrix.
    out : DSDBSparse
        The output matrix. This matrix must have the same block size as
        `a` and `b`. It will compute up to pentadiagonal.
    spillover_correction : bool, optional
        Whether to apply spillover corrections to the output matrix.
        This is necessary when the matrices represent open-ended
        systems. The default is False.

    """
    if a.distribution_state == "nnz" or b.distribution_state == "nnz":
        raise ValueError(
            "Matrix multiplication is not supported for matrices in nnz distribution state."
        )
    num_blocks = len(a.block_sizes)

    # Make sure the output matrix is initialized to zero.
    out.data = 0

    # NOTE: Using the stack attribute to force caching of the data view.
    out_ = out.stack[...]
    a_ = a.stack[...]
    b_ = b.stack[...]

    for i in range(num_blocks):
        for j in range(max(0, i - 2), min(num_blocks, i + 3)):
            out_ij = out.blocks[i, j]
            for k in range(max(0, i - 1), min(num_blocks, i + 2)):
                out_ij += a_.blocks[i, k] @ b_.blocks[k, j]

            out_.blocks[i, j] = out_ij

    if not spillover_correction:
        return

    # Corrections accounting for the fact that the matrices should have
    # open ends.
    out_.blocks[0, 0] += a_.blocks[1, 0] @ b_.blocks[0, 1]
    out_.blocks[-1, -1] += a_.blocks[-2, -1] @ b_.blocks[-1, -2]


@profiler.profile(level="api")
def btd_sandwich(
    a: DSDBSparse,
    b: DSDBSparse,
    out: DSDBSparse,
    spillover_correction: bool = False,
):
    """Compute the sandwich product `a @ b @ a` BTD DSDBSparse matrices.

    Parameters
    ----------
    a : DSDBSparse
        The first block tridiagonal matrix.
    b : DSDBSparse
        The second block tridiagonal matrix.
    out : DSDBSparse
        The output matrix. This matrix must have the same block size as
        `a`, and `b`. It will compute up to heptadiagonal.
    spillover_correction : bool, optional
        Whether to apply spillover corrections to the output matrix.
        This is necessary when the matrices represent open-ended
        systems. The default is False.

    """
    if a.distribution_state == "nnz" or b.distribution_state == "nnz":
        raise ValueError(
            "Matrix multiplication is not supported for matrices in nnz distribution state."
        )
    num_blocks = len(a.block_sizes)

    # Make sure the output matrix is initialized to zero.
    out.data = 0

    # NOTE: Using the stack attribute to force caching of the data view.
    out_ = out.stack[...]
    a_ = a.stack[...]
    b_ = b.stack[...]

    for i in range(num_blocks):
        for j in range(max(0, i - 3), min(num_blocks, i + 4)):
            out_ij = out_.blocks[i, j]
            for k in range(max(0, i - 2), min(num_blocks, i + 3)):
                a_kj = a_.blocks[k, j]
                for m in range(max(0, i - 1), min(num_blocks, i + 2)):
                    out_ij += a_.blocks[i, m] @ b_.blocks[m, k] @ a_kj

            out_.blocks[i, j] = out_ij

    if not spillover_correction:
        return

    # Corrections accounting for the fact that the matrices should have
    # open ends.
    out_.blocks[0, 0] += (
        a_.blocks[1, 0] @ b_.blocks[0, 1] @ a_.blocks[0, 0]
        + a_.blocks[0, 0] @ b_.blocks[1, 0] @ a_.blocks[0, 1]
        + a_.blocks[1, 0] @ b_.blocks[0, 0] @ a_.blocks[0, 1]
    )
    out_.blocks[0, 1] += a_.blocks[1, 0] @ b_.blocks[0, 1] @ a_.blocks[0, 1]
    out_.blocks[1, 0] += a_.blocks[1, 0] @ b_.blocks[1, 0] @ a_.blocks[0, 1]

    out_.blocks[-1, -1] += (
        a_.blocks[-2, -1] @ b_.blocks[-1, -2] @ a_.blocks[-1, -1]
        + a_.blocks[-1, -1] @ b_.blocks[-2, -1] @ a_.blocks[-1, -2]
        + a_.blocks[-2, -1] @ b_.blocks[-1, -1] @ a_.blocks[-1, -2]
    )
    out_.blocks[-1, -2] += a_.blocks[-2, -1] @ b_.blocks[-1, -2] @ a_.blocks[-1, -2]
    out_.blocks[-2, -1] += a_.blocks[-2, -1] @ b_.blocks[-2, -1] @ a_.blocks[-1, -2]


class BlockMatrix(dict):

    def __init__(
        self,
        dsdbsparse: DSDBSparse,
        local_keys: set[tuple[int, int]],
        origin: tuple[int, int],
        mapping=None,
    ):
        self.dsdbsparse = dsdbsparse
        self.local_keys = local_keys
        self.origin = origin
        mapping = mapping or {}
        super(BlockMatrix, self).__init__(mapping)
        self.blocks = self.dsdbsparse.blocks

    def __getitem__(self, key):
        if super(BlockMatrix, self).__contains__(key):
            return super(BlockMatrix, self).__getitem__(key)
        if key in self.local_keys:
            key = (key[0] - self.origin[0], key[1] - self.origin[1])
            return self.blocks[key]
        rank = comm.block.rank if comm.block is not None else 0
        print(f"Something bad happened: {rank=}, {key=}, {self.origin=}")
        # return None
        raise KeyError(key)
        # return xp.zeros((int(self.dbsparse.block_sizes[key[0]]),
        #                  int(self.dbsparse.block_sizes[key[1]])),
        #                 dtype=self.dbsparse.local_data.dtype)

    def __setitem__(self, key, val):
        if key in self.local_keys:
            key = (key[0] - self.origin[0], key[1] - self.origin[1])
            self.blocks[key] = val
        else:
            return super(BlockMatrix, self).__setitem__(key, val)

    def toarray(self):
        size = int(sum(self.dsdbsparse.block_sizes))
        out = xp.zeros((size, size), dtype=self.dsdbsparse.data.dtype)
        for i, (isz, ioff) in enumerate(
            zip(self.dsdbsparse.block_sizes, self.dsdbsparse.block_offsets)
        ):
            for j, (jsz, joff) in enumerate(
                zip(self.dsdbsparse.block_sizes, self.dsdbsparse.block_offsets)
            ):
                try:
                    out[ioff : ioff + isz, joff : joff + jsz] = self[i, j]
                except KeyError:
                    pass
        return out


def arrow_partition_halo_comm(
    a: BlockMatrix,
    b: BlockMatrix,
    a_num_diag: int,
    b_num_diag: int,
    start_block: int,
    end_block: int,
    comm: Intracomm,
):
    """Communicate halo blocks between neighboring ranks assuming arrow partitioning.

    NOTE: The method works ONLY IF the ranks need to communicate ONLY with their immediate neighbors,
    i.e., rank - 1 and rank + 1.

    """

    num_blocks = a.dsdbsparse.num_blocks
    a_ssz = a.dsdbsparse.shape[:-2]
    b_ssz = b.dsdbsparse.shape[:-2]

    bsz = a.dsdbsparse.block_sizes
    dtype = a.dsdbsparse.dtype
    a_off = a_num_diag // 2
    b_off = b_num_diag // 2
    c_off = a_off + b_off
    rank = comm.rank if comm is not None else 0

    synchronize_device()
    comm.Barrier() if comm is not None else None
    # halo_comm_start = time.perf_counter()

    reqs = []
    # Send halo blocks to previous rank
    if start_block > 0:
        for i in range(start_block, min(num_blocks, start_block + c_off)):
            for j in range(
                max(start_block, i - a_off), min(a.dsdbsparse.num_blocks, i + a_off + 1)
            ):
                reqs.append(comm.Isend(a[i, j], dest=rank - 1, tag=0))
        for j in range(start_block, min(num_blocks, start_block + c_off)):
            for i in range(
                max(start_block, j - b_off), min(b.dsdbsparse.num_blocks, j + b_off + 1)
            ):
                reqs.append(comm.Isend(b[i, j], dest=rank - 1, tag=1))
    # Send halo blocks to next rank
    if end_block < a.dsdbsparse.num_blocks:
        for i in range(end_block, min(num_blocks, end_block + a_off)):
            for j in range(max(0, i - a_off), min(end_block, i + a_off + 1)):
                reqs.append(comm.Isend(a[i, j], dest=rank + 1, tag=0))
    if end_block < b.dsdbsparse.num_blocks:
        for j in range(end_block, min(num_blocks, end_block + b_off)):
            for i in range(max(0, j - b_off), min(end_block, j + b_off + 1)):
                reqs.append(comm.Isend(b[i, j], dest=rank + 1, tag=1))
    # Receive halo blocks from next rank
    if end_block < a.dsdbsparse.num_blocks:
        for i in range(end_block, min(num_blocks, end_block + c_off)):
            for j in range(
                max(end_block, i - a_off), min(a.dsdbsparse.num_blocks, i + a_off + 1)
            ):
                a[i, j] = xp.empty((a_ssz) + (bsz[i], bsz[j]), dtype=dtype)
                reqs.append(comm.Irecv(a[i, j], source=rank + 1, tag=0))
    if end_block < b.dsdbsparse.num_blocks:
        for j in range(end_block, min(num_blocks, end_block + c_off)):
            for i in range(
                max(end_block, j - b_off), min(b.dsdbsparse.num_blocks, j + b_off + 1)
            ):
                b[i, j] = xp.empty((b_ssz) + (bsz[i], bsz[j]), dtype=dtype)
                reqs.append(comm.Irecv(b[i, j], source=rank + 1, tag=1))
    # Receive halo blocks from previous rank
    if start_block > 0:
        for i in range(start_block, min(num_blocks, start_block + a_off)):
            for j in range(max(0, i - a_off), min(start_block, i + a_off + 1)):
                a[i, j] = xp.empty((a_ssz) + (bsz[i], bsz[j]), dtype=dtype)
                reqs.append(comm.Irecv(a[i, j], source=rank - 1, tag=0))
        for j in range(start_block, min(num_blocks, start_block + b_off)):
            for i in range(max(0, j - b_off), min(start_block, i + b_off + 1)):
                b[i, j] = xp.empty((b_ssz) + (bsz[i], bsz[j]), dtype=dtype)
                reqs.append(comm.Irecv(b[i, j], source=rank - 1, tag=1))
    Request.Waitall(reqs)

    synchronize_device()
    # halo_comm_end = time.perf_counter()
    # comm.Barrier() if comm is not None else None
    # halo_comm_end_all = time.perf_counter()
    # if comm.rank == 0:
    #     print(f"halo_comm_time: {halo_comm_end - halo_comm_start}", flush=True)
    #     print(f"halo_comm_time_all: {halo_comm_end_all - halo_comm_start}", flush=True)


def arrow_partition_halo_comm_nccl(
    a: BlockMatrix,
    b: BlockMatrix,
    a_num_diag: int,
    b_num_diag: int,
    start_block: int,
    end_block: int,
    comm: Intracomm,
    nccl_comm,
):
    """Communicate halo blocks between neighboring ranks assuming arrow partitioning.

    NOTE: The method works ONLY IF the ranks need to communicate ONLY with their immediate neighbors,
    i.e., rank - 1 and rank + 1.

    """

    num_blocks = a.dsdbsparse.num_blocks
    a_ssz = a.dsdbsparse.shape[:-2]
    b_ssz = b.dsdbsparse.shape[:-2]
    bsz = a.dsdbsparse.block_sizes
    dtype = a.dsdbsparse.dtype
    a_off = a_num_diag // 2
    b_off = b_num_diag // 2
    c_off = a_off + b_off
    rank = comm.rank if comm is not None else 0

    synchronize_device()
    comm.Barrier()
    # halo_comm_start = time.perf_counter()

    # Send halo blocks to previous rank
    def _send_to_previous():
        if start_block > 0:
            for i in range(start_block, min(num_blocks, start_block + c_off)):
                for j in range(
                    max(start_block, i - a_off),
                    min(a.dsdbsparse.num_blocks, i + a_off + 1),
                ):
                    nccl_comm.send(a[i, j], rank - 1)
            for j in range(start_block, min(num_blocks, start_block + c_off)):
                for i in range(
                    max(start_block, j - b_off),
                    min(b.dsdbsparse.num_blocks, j + b_off + 1),
                ):
                    nccl_comm.send(b[i, j], rank - 1)

    # Receive halo blocks from next rank
    def _recv_from_next():
        if end_block < a.dsdbsparse.num_blocks:
            for i in range(end_block, min(num_blocks, end_block + c_off)):
                for j in range(
                    max(end_block, i - a_off),
                    min(a.dsdbsparse.num_blocks, i + a_off + 1),
                ):
                    a[i, j] = xp.empty((a_ssz) + (bsz[i], bsz[j]), dtype=dtype)
                    nccl_comm.recv(a[i, j], rank + 1)
        if end_block < b.dsdbsparse.num_blocks:
            for j in range(end_block, min(num_blocks, end_block + c_off)):
                for i in range(
                    max(end_block, j - b_off),
                    min(b.dsdbsparse.num_blocks, j + b_off + 1),
                ):
                    b[i, j] = xp.empty((b_ssz) + (bsz[i], bsz[j]), dtype=dtype)
                    nccl_comm.recv(b[i, j], rank + 1)

    # Send halo blocks to next rank
    def _send_to_next():
        if end_block < a.dsdbsparse.num_blocks:
            for i in range(end_block, min(num_blocks, end_block + a_off)):
                for j in range(max(0, i - a_off), min(end_block, i + a_off + 1)):
                    nccl_comm.send(a[i, j], rank + 1)
        if end_block < b.dsdbsparse.num_blocks:
            for j in range(end_block, min(num_blocks, end_block + b_off)):
                for i in range(max(0, j - b_off), min(end_block, j + b_off + 1)):
                    nccl_comm.send(b[i, j], rank + 1)

    # Receive halo blocks from previous rank
    def _recv_from_previous():
        if start_block > 0:
            for i in range(start_block, min(num_blocks, start_block + a_off)):
                for j in range(max(0, i - a_off), min(start_block, i + a_off + 1)):
                    a[i, j] = xp.empty((a_ssz) + (bsz[i], bsz[j]), dtype=dtype)
                    nccl_comm.recv(a[i, j], rank - 1)
            for j in range(start_block, min(num_blocks, start_block + b_off)):
                for i in range(max(0, j - b_off), min(start_block, i + b_off + 1)):
                    b[i, j] = xp.empty((b_ssz) + (bsz[i], bsz[j]), dtype=dtype)
                    nccl_comm.recv(b[i, j], rank - 1)

    if rank % 2 == 0:
        _send_to_previous()
        _recv_from_next()
        _send_to_next()
        _recv_from_previous()
    else:
        _recv_from_next()
        _send_to_previous()
        _recv_from_previous()
        _send_to_next()

    synchronize_device()
    # halo_comm_end = time.perf_counter()
    # comm.Barrier()
    # halo_comm_end_all = time.perf_counter()
    # if comm.rank == 0:
    #     print(f"halo_comm_time: {halo_comm_end - halo_comm_start}", flush=True)
    #     print(f"halo_comm_time_all: {halo_comm_end_all - halo_comm_start}", flush=True)


def bd_matmul_distr(
    a: DSDBSparse | BlockMatrix,
    b: DSDBSparse | BlockMatrix,
    out: DSDBSparse | None,
    a_num_diag: int = 3,
    b_num_diag: int = 3,
    out_num_diag: int = 5,
    start_block: int = 0,
    end_block: int = None,
    spillover_correction: bool = False,
    accumulator_dtype=None,
):
    """Matrix multiplication of two `a @ b` BD DSDBSparse matrices.

    Parameters
    ----------
    a : DSDBSparse
        The first block diagonal matrix.
    b : DSDBSparse
        The second block diagonal matrix.
    out : DSDBSparse
        The output matrix. This matrix must have the same block size as
        `a` and `b`. It will compute up to `out_num_diag` diagonals.
    in_num_diag: int
        The number of diagonals in input matrices
    out_num_diag: int
        The number of diagonals in output matrices
    spillover_correction : bool, optional
        Whether to apply spillover corrections to the output matrix.
        This is necessary when the matrices represent open-ended
        systems. The default is False.
    accumulator_dtype : data type, optional
        The data type of the temporary accumulator matrices. The default is complex128.

    TODO: replace @ by appropriate gemm

    """
    # if a.distribution_state == "nnz" or b.distribution_state == "nnz":
    #     raise ValueError(
    #         "Matrix multiplication is not supported for matrices in nnz distribution state."
    #     )

    if isinstance(a, BlockMatrix):
        a_ = a
        num_blocks = len(a.dsdbsparse.block_sizes)
        end_block = end_block or num_blocks
        accumulator_dtype = accumulator_dtype or a.dsdbsparse.dtype
    else:
        num_blocks = len(a.block_sizes)
        end_block = end_block or num_blocks
        accumulator_dtype = accumulator_dtype or a.dtype
        local_keys = set()
        for i in range(start_block, end_block):
            for j in range(start_block, min(num_blocks, i + a_num_diag // 2 + 1)):
                local_keys.add((i, j))
        for j in range(start_block, end_block):
            for i in range(end_block, min(num_blocks, j + a_num_diag // 2 + 1)):
                local_keys.add((i, j))
        a_ = BlockMatrix(a, local_keys, (start_block, start_block))

    if isinstance(b, BlockMatrix):
        b_ = b
    else:
        local_keys = set()
        for i in range(start_block, end_block):
            for j in range(start_block, min(num_blocks, i + b_num_diag // 2 + 1)):
                local_keys.add((i, j))
        for j in range(start_block, end_block):
            for i in range(end_block, min(num_blocks, j + b_num_diag // 2 + 1)):
                local_keys.add((i, j))
        b_ = BlockMatrix(b, local_keys, (start_block, start_block))

    if hasattr(comm.block, "_nccl_comm"):
        # if False:
        arrow_partition_halo_comm_nccl(
            a_,
            b_,
            a_num_diag,
            b_num_diag,
            start_block,
            end_block,
            comm.block._mpi_comm,
            comm.block._nccl_comm,
        )
    elif GPU_AWARE_MPI or comm.block.size == 1 or xp.__name__ == "numpy":
        arrow_partition_halo_comm(
            a_, b_, a_num_diag, b_num_diag, start_block, end_block, comm.block._mpi_comm
        )
    else:
        # TODO: host_mpi implementation or unify one is needed
        raise ValueError(
            "GPU_AWARE_MPI is not enabled. Please enable it to use this function."
        )

    # Make sure the output matrix is initialized to zero.
    if out is not None:
        out.data[:] = 0
        local_keys = set()
        for i in range(start_block, end_block):
            for j in range(start_block, min(num_blocks, i + out_num_diag // 2 + 1)):
                local_keys.add((i, j))
        for j in range(start_block, end_block):
            for i in range(end_block, min(num_blocks, j + out_num_diag // 2 + 1)):
                local_keys.add((i, j))
        out_ = BlockMatrix(out, local_keys, (start_block, start_block))
    else:
        out_ = BlockMatrix(b_.dsdbsparse, set(), (start_block, start_block))

    for sector in (
        (start_block, end_block, start_block, num_blocks),
        (end_block, num_blocks, start_block, end_block),
    ):

        brow_start, brow_end, bcol_start, bcol_end = sector

        for i in range(brow_start, brow_end):
            for j in range(
                max(i - out_num_diag // 2, bcol_start),
                min(i + out_num_diag // 2 + 1, bcol_end),
            ):
                partsum = None

                for k in range(i - a_num_diag // 2, i + a_num_diag // 2 + 1):
                    if abs(j - k) > b_num_diag // 2:
                        continue
                    out_range = (k < 0) or (k >= num_blocks)
                    if out_range and (not spillover_correction):
                        continue
                    else:
                        if out_range:
                            i_a, k_a = correct_out_range_index(i, k, num_blocks)
                            k_b, j_b = correct_out_range_index(k, j, num_blocks)
                        else:
                            i_a, k_a = i, k
                            k_b, j_b = k, j
                        try:
                            if partsum is None:
                                partsum = (a_[i_a, k_a] @ b_[k_b, j_b]).astype(
                                    accumulator_dtype
                                )
                            else:
                                partsum += a_[i_a, k_a] @ b_[k_b, j_b]
                        except Exception as e:
                            rank = comm.block.rank if comm.block is not None else 0
                            print(e)
                            print(
                                f"Something bad happened: {rank=}, {i=}, {j=}, {k=}, {i_a=}, {k_a=}, {k_b=}, {j_b=}"
                            )

                out_[i, j] = partsum

    return out_


def bd_sandwich_distr(
    a: DSDBSparse,
    b: DSDBSparse,
    out: DSDBSparse | None,
    in_num_diag: int = 3,
    out_num_diag: int = 7,
    start_block: int = 0,
    end_block: int = None,
    spillover_correction: bool = False,
    accumulator_dtype=None,
):
    """Matrix multiplication of two `a @ b` BD DSDBSparse matrices.

    Parameters
    ----------
    a : DSDBSparse
        The first block diagonal matrix.
    b : DSDBSparse
        The second block diagonal matrix.
    out : DSDBSparse
        The output matrix. This matrix must have the same block size as
        `a` and `b`. It will compute up to `out_num_diag` diagonals.
    in_num_diag: int
        The number of diagonals in input matrices
    out_num_diag: int
        The number of diagonals in output matrices
    spillover_correction : bool, optional
        Whether to apply spillover corrections to the output matrix.
        This is necessary when the matrices represent open-ended
        systems. The default is False.
    accumulator_dtype : data type, optional
        The data type of the temporary accumulator matrices. The default is complex128.

    TODO: replace @ by appropriate gemm

    """

    num_blocks = len(a.block_sizes)
    end_block = end_block or num_blocks
    accumulator_dtype = accumulator_dtype or a.dtype
    local_keys = set()
    for i in range(start_block, end_block):
        for j in range(
            max(start_block, i - in_num_diag // 2),
            min(num_blocks, i + in_num_diag // 2 + 1),
        ):
            local_keys.add((i, j))
    for j in range(start_block, end_block):
        for i in range(
            max(end_block, j - in_num_diag // 2),
            min(num_blocks, j + in_num_diag // 2 + 1),
        ):
            local_keys.add((i, j))
    a_ = BlockMatrix(a, local_keys, (start_block, start_block))
    b_ = BlockMatrix(b, local_keys, (start_block, start_block))

    tmp_num_diag = 2 * in_num_diag - 1
    tmp = bd_matmul_distr(
        a_,
        b_,
        None,
        in_num_diag,
        in_num_diag,
        tmp_num_diag,
        start_block,
        end_block,
        False,
        accumulator_dtype,
    )
    out_ = bd_matmul_distr(
        tmp,
        a_,
        out,
        tmp_num_diag,
        in_num_diag,
        out_num_diag,
        start_block,
        end_block,
        False,
        accumulator_dtype,
    )

    if spillover_correction:
        if in_num_diag != 3:
            raise NotImplementedError(
                "Spillover correction is only implemented for in_num_diag=3."
            )

        # NOTE: This is only correct for BTD (tridiagonal) matrices with open ends.
        if start_block == 0:
            out_[0, 0] += (
                a_[1, 0] @ b_[0, 1] @ a_[0, 0]
                + a_[0, 0] @ b_[1, 0] @ a_[0, 1]
                + a_[1, 0] @ b_[0, 0] @ a_[0, 1]
            )
            out_[0, 1] += a_[1, 0] @ b_[0, 1] @ a_[0, 1]
            if not out_.dsdbsparse.symmetry:
                out_[1, 0] += a_[1, 0] @ b_[1, 0] @ a_[0, 1]

        if end_block == a.num_blocks:
            m1 = a.num_blocks - 1
            m2 = a.num_blocks - 2
            out_[m1, m1] += (
                a_[m2, m1] @ b_[m1, m2] @ a_[m1, m1]
                + a_[m1, m1] @ b_[m2, m1] @ a_[m1, m2]
                + a_[m2, m1] @ b_[m1, m1] @ a_[m1, m2]
            )
            out_[m1, m2] += a_[m2, m1] @ b_[m1, m2] @ a_[m1, m2]
            if not out_.dsdbsparse.symmetry:
                out_[m2, m1] += a_[m2, m1] @ b_[m2, m1] @ a_[m1, m2]

    return out_