Create a new partition of an NDArray. That is, an object which assigns each row of an NDArray to a specific partition. Parameters ---------- array_size : int The first dimension of the array being partitioned. num_parts : int The number of parts to divide the arr
| 472 | |
| 473 | |
| 474 | class NDArrayPartition(object): |
| 475 | """Create a new partition of an NDArray. That is, an object which assigns |
| 476 | each row of an NDArray to a specific partition. |
| 477 | |
| 478 | Parameters |
| 479 | ---------- |
| 480 | array_size : int |
| 481 | The first dimension of the array being partitioned. |
| 482 | num_parts : int |
| 483 | The number of parts to divide the array into. |
| 484 | mode : String |
| 485 | The type of partition. Currently, the only valid values are |
| 486 | 'remainder' and 'range'. |
| 487 | 'remainder' assigns rows based on remainder when dividing the row id by the |
| 488 | number of parts (e.g., i % num_parts). |
| 489 | 'range' assigns rows based on which part of the range 'part_ranges' |
| 490 | they fall into. |
| 491 | part_ranges : Tensor or dgl.NDArray, Optional |
| 492 | Should only be specified when the mode is 'range'. Should be of the |
| 493 | length `num_parts + 1`, and be the exclusive prefix-sum of the number |
| 494 | of nodes in each partition. That is, for 3 partitions, we could have |
| 495 | the list [0, a, b, 'array_size'], and all rows with index less |
| 496 | than 'a' are assigned to partition 0, all rows with index greater than |
| 497 | or equal to 'a' and less than 'b' are in partition 1, and all rows |
| 498 | with index greater or equal to 'b' are in partition 2. Should have |
| 499 | the same context as the partitioned NDArray (i.e., be on the same GPU). |
| 500 | |
| 501 | Examples |
| 502 | -------- |
| 503 | |
| 504 | A partition of a homgeonous graph `g`, where the vertices are |
| 505 | striped across processes can be generated via: |
| 506 | |
| 507 | >>> from dgl.partition import NDArrayPartition |
| 508 | >>> part = NDArrayPartition(g.num_nodes(), num_parts, mode='remainder' ) |
| 509 | |
| 510 | A range based partition of a homogenous graph `g`'s nodes, where |
| 511 | the nodes are stored in contiguous memory. This converts an existing |
| 512 | range based partitioning (e.g. from a |
| 513 | dgl.distributed.graph_partition_book.RangePartitionBook) |
| 514 | 'max_node_map', to an NDArrayPartition 'part'. |
| 515 | |
| 516 | >>> part_range = [0] |
| 517 | >>> for part in part_book.metadata(): |
| 518 | >>> part_range.append(part_range[-1] + part['num_nodes']) |
| 519 | >>> part = NDArrayPartition(g.num_nodes(), num_parts, mode='range', |
| 520 | ... part_ranges=part_range) |
| 521 | """ |
| 522 | |
| 523 | def __init__( |
| 524 | self, array_size, num_parts, mode="remainder", part_ranges=None |
| 525 | ): |
| 526 | assert num_parts > 0, 'Invalid "num_parts", must be > 0.' |
| 527 | if mode == "remainder": |
| 528 | assert part_ranges is None, ( |
| 529 | "When using remainder-based " |
| 530 | 'partitioning, "part_ranges" should not be specified.' |
| 531 | ) |
no outgoing calls