comm_runner.py 20.4 KB
Newer Older
root's avatar
root committed
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
import sys
import time
import warnings

import cupy
from cupy import cuda
from cupy.cuda import nccl
from cupy import testing

from cupyx.distributed import init_process_group
from cupyx.distributed._nccl_comm import NCCLBackend
from cupyx.distributed._store import ExceptionAwareProcess
from cupyx.scipy import sparse


nccl_available = nccl.available


N_WORKERS = 2


def _launch_workers(func, args=(), n_workers=N_WORKERS):
    processes = []
    # TODO catch exceptions
    for rank in range(n_workers):
        p = ExceptionAwareProcess(
            target=func,
            args=(rank,) + args)
        p.start()
        processes.append(p)

    for p in processes:
        p.join()


def broadcast(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_broadcast(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        expected = cupy.arange(2 * 3 * 4, dtype=dtype).reshape((2, 3, 4))
        if rank == root:
            in_array = expected
        else:
            in_array = cupy.zeros((2, 3, 4), dtype=dtype)
        comm.broadcast(in_array, root)
        testing.assert_allclose(in_array, expected)

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_broadcast(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_broadcast(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_broadcast, (0, dtype))
        _launch_workers(run_broadcast, (1, dtype))


def reduce(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_reduce(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = cupy.arange(2 * 3 * 4, dtype='f').reshape(2, 3, 4)
        out_array = cupy.zeros((2, 3, 4), dtype='f')
        comm.reduce(in_array, out_array, root)
        if rank == root:
            testing.assert_allclose(out_array, 2 * in_array)

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_reduce(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_reduce(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_reduce, (0, dtype))
        _launch_workers(run_reduce, (1, dtype))


def all_reduce(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_all_reduce(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = cupy.arange(2 * 3 * 4, dtype='f').reshape(2, 3, 4)
        out_array = cupy.zeros((2, 3, 4), dtype='f')

        comm.all_reduce(in_array, out_array)
        testing.assert_allclose(out_array, 2 * in_array)

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_all_reduce(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_all_reduce, (dtype,))


def reduce_scatter(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_reduce_scatter(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = 1 + cupy.arange(
            N_WORKERS * 10, dtype='f').reshape(N_WORKERS, 10)
        out_array = cupy.zeros((10,), dtype='f')

        comm.reduce_scatter(in_array, out_array, 10)
        testing.assert_allclose(out_array, 2 * in_array[rank])

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_reduce_scatter(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_reduce_scatter, (dtype,))


def all_gather(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_all_gather(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = (rank + 1) * cupy.arange(
            N_WORKERS * 10, dtype='f').reshape(N_WORKERS, 10)
        out_array = cupy.zeros((N_WORKERS, 10), dtype='f')
        comm.all_gather(in_array, out_array, 10)
        expected = 1 + cupy.arange(N_WORKERS).reshape(N_WORKERS, 1)
        expected = expected * cupy.broadcast_to(
            cupy.arange(10, dtype='f'), (N_WORKERS, 10))
        testing.assert_allclose(out_array, expected)

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_all_gather(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_all_gather, (dtype,))


def send_and_recv(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_send_and_recv(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = cupy.arange(10, dtype='f')
        out_array = cupy.zeros((10,), dtype='f')
        if rank == 0:
            comm.send(in_array, 1)
        else:
            comm.recv(out_array, 0)
            testing.assert_allclose(out_array, in_array)

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_send_and_recv(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_send_and_recv, (dtype,))


def send_recv(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_send_recv(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = cupy.arange(10, dtype='f')
        for i in range(N_WORKERS):
            out_array = cupy.zeros((10,), dtype='f')
            comm.send_recv(in_array, out_array, i)
            testing.assert_allclose(out_array, in_array)

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_send_recv(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_send_recv, (dtype,))


def scatter(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_scatter(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = 1 + cupy.arange(
            N_WORKERS * 10, dtype='f').reshape(N_WORKERS, 10)
        out_array = cupy.zeros((10,), dtype='f')

        comm.scatter(in_array, out_array, root)
        if rank > 0:
            testing.assert_allclose(out_array, in_array[rank])

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_scatter(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_scatter(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_scatter, (0, dtype))
        _launch_workers(run_scatter, (1, dtype))


def gather(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_gather(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = (rank + 1) * cupy.arange(10, dtype='f')
        out_array = cupy.zeros((N_WORKERS, 10), dtype='f')
        comm.gather(in_array, out_array, root)
        if rank == root:
            expected = 1 + cupy.arange(N_WORKERS).reshape(N_WORKERS, 1)
            expected = expected * cupy.broadcast_to(
                cupy.arange(10, dtype='f'), (N_WORKERS, 10))
            testing.assert_allclose(out_array, expected)

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_gather(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_gather(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_gather, (0, dtype))
        _launch_workers(run_gather, (1, dtype))


def all_to_all(dtype, use_mpi=False):
    if dtype in 'hH':
        return  # nccl does not support int16

    def run_all_to_all(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = cupy.arange(
            N_WORKERS * 10, dtype='f').reshape(N_WORKERS, 10)
        out_array = cupy.zeros((N_WORKERS, 10), dtype='f')
        comm.all_to_all(in_array, out_array)
        expected = (10 * rank) + cupy.broadcast_to(
            cupy.arange(10, dtype='f'), (N_WORKERS, 10))
        testing.assert_allclose(out_array, expected)

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_all_to_all(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_all_to_all, (dtype,))


def barrier(use_mpi=False):
    def run_barrier(rank, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        comm.barrier()
        before = time.time()
        if rank == 0:
            time.sleep(2)
        comm.barrier()
        after = time.time()
        assert int(after - before) == 2

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_barrier(MPI.COMM_WORLD.Get_rank(), True)
    else:
        _launch_workers(run_barrier)


def init(use_mpi=False):
    def run_init(rank, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = init_process_group(N_WORKERS, rank, use_mpi=use_mpi)
        # Do a simple call to verify we got a valid comm
        in_array = cupy.zeros(1)
        if rank == 0:
            in_array = in_array + 1
        comm.broadcast(in_array, 0)
        testing.assert_allclose(in_array, cupy.ones(1))

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_init(MPI.COMM_WORLD.Get_rank(), dtype, True)
        run_init(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_init)


def _make_sparse(dtype):
    data = cupy.array([1, 3, 2, 5, 1, 1], dtype)
    indices = cupy.array([0, 3, 1, 3, 0, 2], 'i')
    indptr = cupy.array([0, 2, 3, 4, 6], 'i')
    return sparse.csr_matrix((data, indices, indptr), shape=(4, 4))


def _make_sparse_empty(dtype):
    data = cupy.array([0], dtype)
    indices = cupy.array([0], 'i')
    indptr = cupy.array([0], 'i')
    return sparse.csr_matrix((data, indices, indptr), shape=(0, 0))


def sparse_send_and_recv(dtype, use_mpi=False):
    def run_send_and_recv(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = _make_sparse(dtype)
        out_array = _make_sparse_empty(dtype)
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        if rank == 0:
            comm.send(in_array, 1)
        else:
            comm.recv(out_array, 0)
            testing.assert_allclose(out_array.todense(), in_array.todense())

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_send_and_recv(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_send_and_recv, (dtype,))


def sparse_send_recv(dtype, use_mpi=False):
    def run_send_recv(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = _make_sparse(dtype)
        out_array = _make_sparse_empty(dtype)
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        if rank == 0:
            comm.send_recv(in_array, out_array, 1)
        else:
            comm.send_recv(in_array, out_array, 0)
            testing.assert_allclose(out_array.todense(), in_array.todense())

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_send_recv(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_send_recv, (dtype,))


def sparse_broadcast(dtype, use_mpi=False):

    def run_broadcast(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        expected = _make_sparse(dtype)
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        if rank == root:
            in_array = expected
        else:
            in_array = _make_sparse_empty(dtype)
        comm.broadcast(in_array, root)
        testing.assert_allclose(in_array.todense(), expected.todense())

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_broadcast(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_broadcast(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_broadcast, (0, dtype,))
        _launch_workers(run_broadcast, (1, dtype,))


def sparse_reduce(dtype, use_mpi=False):

    def run_reduce(rank, root, dtype, op, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = _make_sparse(dtype)
        out_array = _make_sparse_empty(dtype)
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        comm.reduce(in_array, out_array, root, op)
        if rank == root:
            if op == 'sum':
                testing.assert_allclose(
                    out_array.todense(), 2 * in_array.todense())
            else:
                testing.assert_allclose(
                    out_array.todense(),
                    cupy.matmul(in_array.todense(), in_array.todense()))

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_reduce(MPI.COMM_WORLD.Get_rank(), 0, dtype, 'sum', True)
        run_reduce(MPI.COMM_WORLD.Get_rank(), 1, dtype, 'prod', True)
    else:
        _launch_workers(run_reduce, (0, dtype, 'sum'))
        _launch_workers(run_reduce, (1, dtype, 'prod'))


def sparse_all_reduce(dtype, use_mpi=False):

    def run_all_reduce(rank, dtype, op, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = _make_sparse(dtype)
        out_array = _make_sparse_empty(dtype)
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        comm.all_reduce(in_array, out_array, op)
        if op == 'sum':
            testing.assert_allclose(
                out_array.todense(), 2 * in_array.todense())
        else:
            testing.assert_allclose(
                out_array.todense(),
                cupy.matmul(in_array.todense(), in_array.todense()))

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_all_reduce(MPI.COMM_WORLD.Get_rank(), dtype, 'sum', True)
        run_all_reduce(MPI.COMM_WORLD.Get_rank(), dtype, 'prod', True)
    else:
        _launch_workers(run_all_reduce, (dtype, 'sum'))
        _launch_workers(run_all_reduce, (dtype, 'prod'))


def sparse_scatter(dtype, use_mpi=False):

    def run_scatter(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_arrays = [_make_sparse(dtype), 2*_make_sparse(dtype)]
        out_array = _make_sparse_empty(dtype)
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        comm.scatter(in_arrays, out_array, root)
        testing.assert_allclose(
            out_array.todense(), in_arrays[rank].todense())

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_scatter(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_scatter(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_scatter, (0, dtype))
        _launch_workers(run_scatter, (1, dtype))


def sparse_gather(dtype, use_mpi=False):

    def run_gather(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = (rank + 1) * _make_sparse(dtype)
        out_arrays = []
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        comm.gather(in_array, out_arrays, root)
        if rank == root:
            expected = [_make_sparse(dtype), 2 * _make_sparse(dtype)]
            testing.assert_allclose(
                out_arrays[0].todense(), expected[0].todense())
            testing.assert_allclose(
                out_arrays[1].todense(), expected[1].todense())

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_gather(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_gather(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_gather, (0, dtype))
        _launch_workers(run_gather, (1, dtype))


def sparse_all_gather(dtype, use_mpi=False):

    def run_all_gather(rank, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_array = (rank + 1) * _make_sparse(dtype)
        out_arrays = []
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        comm.all_gather(in_array, out_arrays, 0)
        expected = [_make_sparse(dtype), 2 * _make_sparse(dtype)]
        testing.assert_allclose(
            out_arrays[0].todense(), expected[0].todense())
        testing.assert_allclose(
            out_arrays[1].todense(), expected[1].todense())

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_all_gather(MPI.COMM_WORLD.Get_rank(), dtype, True)
        run_all_gather(MPI.COMM_WORLD.Get_rank(), dtype, True)
    else:
        _launch_workers(run_all_gather, (dtype,))
        _launch_workers(run_all_gather, (dtype,))


def sparse_all_to_all(dtype, use_mpi=False):

    def run_all_to_all(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_arrays = [_make_sparse(dtype), 2 * _make_sparse(dtype)]
        out_array = []
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        comm.all_to_all(in_arrays, out_array)
        testing.assert_allclose(
            out_array[0].todense(), (rank + 1) * in_arrays[0].todense())
        testing.assert_allclose(
            out_array[1].todense(), (rank + 1) * in_arrays[0].todense())

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_all_to_all(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_all_to_all(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_all_to_all, (0, dtype))
        _launch_workers(run_all_to_all, (1, dtype))


def sparse_reduce_scatter(dtype, use_mpi=False):

    def run_reduce_scatter(rank, root, dtype, use_mpi=False):
        dev = cuda.Device(rank)
        dev.use()
        comm = NCCLBackend(N_WORKERS, rank, use_mpi=use_mpi)
        in_arrays = [(rank + 1) * _make_sparse(dtype),
                     (rank + 2) * _make_sparse(dtype)]
        out_array = _make_sparse_empty(dtype)
        warnings.filterwarnings(
            'ignore', '.*transferring sparse.*', UserWarning)
        comm.reduce_scatter(in_arrays, out_array, 2)
        target = ((rank + 1) * _make_sparse(dtype)
                  + (rank + 2) * _make_sparse(dtype))
        testing.assert_allclose(
            out_array.todense(), target.todense())

    if use_mpi:
        from mpi4py import MPI
        # This process was run with mpiexec
        run_reduce_scatter(MPI.COMM_WORLD.Get_rank(), 0, dtype, True)
        run_reduce_scatter(MPI.COMM_WORLD.Get_rank(), 1, dtype, True)
    else:
        _launch_workers(run_reduce_scatter, (0, dtype))
        _launch_workers(run_reduce_scatter, (1, dtype))


if __name__ == '__main__':
    # Run the templatized test
    func = globals()[sys.argv[1]]
    # dtype is the char representation
    use_mpi = True if sys.argv[2] == "mpi" else False
    dtype = sys.argv[3] if len(sys.argv) == 4 else None
    if dtype is not None:
        func(dtype, use_mpi)
    else:
        func(use_mpi)