parmetis_preprocess.py 15.8 KB
Newer Older
1
2
3
import argparse
import logging
import os
4
import platform
5
6
from pathlib import Path

7
8
import array_readwriter

9
10
import constants

11
12
13
import numpy as np
import pyarrow
import pyarrow.csv as csv
14
15
16
17
18
19
20
from utils import (
    generate_read_list,
    generate_roundrobin_read_list,
    get_idranges,
    get_node_types,
    read_json,
)
21
22
23
24
25
26
27


def get_proc_info():
    """Helper function to get the rank from the
    environment when `mpirun` is used to run this python program.

    Please note that for mpi(openmpi) installation the rank is retrieved from the
28
29
    environment using OMPI_COMM_WORLD_RANK. For mpich it is
    retrieved from the environment using PMI_RANK.
30

31
32
33
34
35
36
    Returns:
    --------
    integer :
        Rank of the current process.
    """
    env_variables = dict(os.environ)
37
38
39
    # mpich
    if "PMI_RANK" in env_variables:
        return int(env_variables["PMI_RANK"])
40
    # openmpi
41
42
43
44
    elif "OMPI_COMM_WORLD_RANK" in env_variables:
        return int(env_variables["OMPI_COMM_WORLD_RANK"])
    else:
        return 0
45

46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def get_world_size():
    """Helper function to get the world size from the
    environment when `mpirun` is used to run this python program.

    Returns:
    --------
    integer :
        Numer of processes created by the executor that created this process.
    """
    env_variables = dict(os.environ)
    # mpich
    if "PMI_SIZE" in env_variables:
        return int(env_variables["PMI_SIZE"])
    # openmpi
    elif "OMPI_COMM_WORLD_SIZE" in env_variables:
        return int(env_variables["OMPI_COMM_WORLD_SIZE"])
    else:
        return 1


67
def gen_edge_files(rank, schema_map, params):
68
69
70
71
72
73
74
75
76
77
78
79
80
    """Function to create edges files to be consumed by ParMETIS
    for partitioning purposes.

    This function creates the edge files and each of these will have the
    following format (meaning each line of these file is of the following format)
    <global_src_id> <global_dst_id>

    Here ``global`` prefix means that globally unique identifier assigned each node
    in the input graph. In this context globally unique means unique across all the
    nodes in the input graph.

    Parameters:
    -----------
81
82
    rank : int
        rank of the current process
83
84
85
86
87
    schema_map : json dictionary
        Dictionary created by reading the metadata.json file for the input dataset.
    output : string
        Location of storing the node-weights and edge files for ParMETIS.
    """
88
    _, ntype_gnid_offset = get_idranges(
89
        schema_map[constants.STR_NODE_TYPE],
90
91
92
93
94
95
        dict(
            zip(
                schema_map[constants.STR_NODE_TYPE],
                schema_map[constants.STR_NUM_NODES_PER_TYPE],
            )
        ),
96
97
98
99
100
    )

    # Regenerate edge files here.
    edge_data = schema_map[constants.STR_EDGES]

101
    outdir = Path(params.output_dir)
102
    os.makedirs(outdir, exist_ok=True)
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

    def process_and_write_back(data_df, idx):
        data_f0 = data_df[:, 0]
        data_f1 = data_df[:, 1]

        global_src_id = data_f0 + ntype_gnid_offset[src_ntype_name][0, 0]
        global_dst_id = data_f1 + ntype_gnid_offset[dst_ntype_name][0, 0]
        cols = [global_src_id, global_dst_id]
        col_names = ["global_src_id", "global_dst_id"]

        out_file_name = Path(edge_data_files[idx]).stem.split(".")[0]
        out_file = os.path.join(
            outdir, etype_name, f"edges_{out_file_name}.csv"
        )
        os.makedirs(os.path.dirname(out_file), exist_ok=True)

        options = csv.WriteOptions(include_header=False, delimiter=" ")
        csv.write_csv(
            pyarrow.Table.from_arrays(cols, names=col_names),
            out_file,
            options,
        )
        return out_file

127
128
    edge_files = []
    for etype_name, etype_info in edge_data.items():
129
        edge_data_files = etype_info[constants.STR_DATA]
130
131
132
133
134
135
136

        # ``edgetype`` strings are in canonical format, src_node_type:edge_type:dst_node_type
        tokens = etype_name.split(":")
        assert len(tokens) == 3

        src_ntype_name = tokens[0]

137
        dst_ntype_name = tokens[2]
138

139
140
141
142
        rank_assignments = generate_roundrobin_read_list(
            len(edge_data_files), params.num_parts
        )
        for file_idx in rank_assignments[rank]:
143
            reader_fmt_meta = {
144
                "name": etype_info[constants.STR_FORMAT][constants.STR_NAME],
145
            }
146
147
148
149
            if reader_fmt_meta["name"] == constants.STR_CSV:
                reader_fmt_meta["delimiter"] = etype_info[constants.STR_FORMAT][
                    constants.STR_FORMAT_DELIMITER
                ]
150
            data_df = array_readwriter.get_array_parser(**reader_fmt_meta).read(
151
                os.path.join(params.input_dir, edge_data_files[file_idx])
152
            )
153
            out_file = process_and_write_back(data_df, file_idx)
154
            edge_files.append(out_file)
155
156
157
158

    return edge_files


159
def gen_node_weights_files(schema_map, params):
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
    """Function to create node weight files for ParMETIS along with the edge files.

    This function generates node-data files, which will be read by the ParMETIS
    executable for partitioning purposes. Each line in these files will be of the
    following format:
        <node_type_id> <node_weight_list> <type_wise_node_id>
    node_type_id -  is id assigned to the node-type to which a given particular
        node belongs to
    weight_list - this is a one-hot vector in which the number in the location of
        the current nodes' node-type will be set to `1` and other will be `0`
    type_node_id - this is the id assigned to the node (in the context of the current
        nodes` node-type). Meaning this id is unique across all the nodes which belong to
        the current nodes` node-type.

    Parameters:
    -----------
    schema_map : json dictionary
        Dictionary created by reading the metadata.json file for the input dataset.
    output : string
        Location of storing the node-weights and edge files for ParMETIS.

    Returns:
    --------
    list :
        List of filenames for nodes of the input graph.
    list :
        List o ffilenames for edges of the input graph.
    """
    rank = get_proc_info()
    ntypes_ntypeid_map, ntypes, ntid_ntype_map = get_node_types(schema_map)
    type_nid_dict, ntype_gnid_offset = get_idranges(
        schema_map[constants.STR_NODE_TYPE],
192
193
194
195
196
197
        dict(
            zip(
                schema_map[constants.STR_NODE_TYPE],
                schema_map[constants.STR_NUM_NODES_PER_TYPE],
            )
        ),
198
199
200
    )

    node_files = []
201
    outdir = Path(params.output_dir)
202
203
204
    os.makedirs(outdir, exist_ok=True)

    for ntype_id, ntype_name in ntid_ntype_map.items():
205
206
207
208
209
210

        # This ntype does not have any train/test/val masks...
        # Each rank will generate equal no. of rows for this node type.
        total_count = schema_map[constants.STR_NUM_NODES_PER_TYPE][ntype_id]
        per_rank_range = np.ones((params.num_parts,), dtype=np.int64) * (
            total_count // params.num_parts
211
        )
212
213
214
215
216
217
218
219
        for i in range(total_count % params.num_parts):
            per_rank_range[i] += 1

        tid_start = np.cumsum([0] + list(per_rank_range[:-1]))
        tid_end = np.cumsum(list(per_rank_range))
        local_tid_start = tid_start[rank]
        local_tid_end = tid_end[rank]
        sz = local_tid_end - local_tid_start
220
221
222

        cols = []
        col_names = []
223
224

        # ntype-id
225
226
227
228
229
        cols.append(
            pyarrow.array(np.ones(sz, dtype=np.int64) * np.int64(ntype_id))
        )
        col_names.append("ntype")

230
        # one-hot vector for ntype-id here.
231
232
233
234
235
236
237
238
239
240
        for i in range(len(ntypes)):
            if i == ntype_id:
                cols.append(pyarrow.array(np.ones(sz, dtype=np.int64)))
            else:
                cols.append(pyarrow.array(np.zeros(sz, dtype=np.int64)))
            col_names.append("w{}".format(i))

        # `type_nid` should be the very last column in the node weights files.
        cols.append(
            pyarrow.array(
241
                np.arange(local_tid_start, local_tid_end, dtype=np.int64)
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
            )
        )
        col_names.append("type_nid")

        out_file = os.path.join(
            outdir, "node_weights_{}_{}.txt".format(ntype_name, rank)
        )
        options = csv.WriteOptions(include_header=False, delimiter=" ")
        options.delimiter = " "

        csv.write_csv(
            pyarrow.Table.from_arrays(cols, names=col_names), out_file, options
        )
        node_files.append(
            (
257
258
                ntype_gnid_offset[ntype_name][0, 0] + local_tid_start,
                ntype_gnid_offset[ntype_name][0, 0] + local_tid_end,
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
                out_file,
            )
        )

    return node_files


def gen_parmetis_input_args(params, schema_map):
    """Function to create two input arguments which will be passed to the parmetis.
    first argument is a text file which has a list of node-weights files,
    namely parmetis-nfiles.txt, and second argument is a text file which has a
    list of edge files, namely parmetis_efiles.txt.
    ParMETIS uses these two files to read/load the graph and partition the graph
    With regards to the file format, parmetis_nfiles.txt uses the following format
    for each line in that file:
        <filename> <global_node_id_start> <global_node_id_end>(exclusive)
    While parmetis_efiles.txt just has <filename> in each line.

    Parameters:
    -----------
    params : argparser instance
        Instance of ArgParser class, which has all the input arguments passed to
        run this program.
    schema_map : json dictionary
        Dictionary object created after reading the graph metadata.json file.
    """

286
    # TODO: This makes the assumption that all node files have the same number of chunks
287
288
289
    ntypes_ntypeid_map, ntypes, ntid_ntype_map = get_node_types(schema_map)
    type_nid_dict, ntype_gnid_offset = get_idranges(
        schema_map[constants.STR_NODE_TYPE],
290
291
292
293
294
295
        dict(
            zip(
                schema_map[constants.STR_NODE_TYPE],
                schema_map[constants.STR_NUM_NODES_PER_TYPE],
            )
        ),
296
297
    )

298
    # Check if <graph-name>_stats.txt exists, if not create one using metadata.
299
    # Here stats file will be created in the current directory.
300
301
302
303
    # No. of constraints, third column in the stats file is computed as follows:
    #   num_constraints = no. of node types + train_mask + test_mask + val_mask
    #   Here, (train/test/val) masks will be set to 1 if these masks exist for
    #   all the node types in the graph, otherwise these flags will be set to 0
304
305
306
    assert (
        constants.STR_GRAPH_NAME in schema_map
    ), "Graph name is not present in the json file"
307
    graph_name = schema_map[constants.STR_GRAPH_NAME]
308
309
310
    if not os.path.isfile(
        os.path.join(params.input_dir, f"{graph_name}_stats.txt")
    ):
311
312
        num_nodes = np.sum(schema_map[constants.STR_NUM_NODES_PER_TYPE])
        num_edges = np.sum(schema_map[constants.STR_NUM_EDGES_PER_TYPE])
313
314
        num_ntypes = len(schema_map[constants.STR_NODE_TYPE])

315
        num_constraints = num_ntypes
316

317
318
319
        with open(
            os.path.join(params.input_dir, f"{graph_name}_stats.txt"), "w"
        ) as sf:
320
            sf.write(f"{num_nodes} {num_edges} {num_constraints}")
321

322
323
324
325
326
    node_files = []
    outdir = Path(params.output_dir)
    os.makedirs(outdir, exist_ok=True)
    for ntype_id, ntype_name in ntid_ntype_map.items():
        global_nid_offset = ntype_gnid_offset[ntype_name][0, 0]
327
328
329
330
331
332
333
334
335
336
337
        total_count = schema_map[constants.STR_NUM_NODES_PER_TYPE][ntype_id]
        per_rank_range = np.ones((params.num_parts,), dtype=np.int64) * (
            total_count // params.num_parts
        )
        for i in range(total_count % params.num_parts):
            per_rank_range[i] += 1
        tid_start = np.cumsum([0] + list(per_rank_range[:-1]))
        tid_end = np.cumsum(per_rank_range)
        logging.info(f" tid-start = {tid_start}, tid-end = {tid_end}")
        logging.info(f" per_rank_range - {per_rank_range}")

338
339
340
        for part_idx in range(params.num_parts):
            local_tid_start = tid_start[part_idx]
            local_tid_end = tid_end[part_idx]
341
            out_file = os.path.join(
342
                outdir, "node_weights_{}_{}.txt".format(ntype_name, part_idx)
343
344
345
346
            )
            node_files.append(
                (
                    out_file,
347
348
                    global_nid_offset + local_tid_start,
                    global_nid_offset + local_tid_end,
349
350
351
                )
            )

352
353
354
355
356
357
358
359
    with open(
        os.path.join(params.output_dir, "parmetis_nfiles.txt"), "w"
    ) as parmetis_nf:
        for node_file in node_files:
            # format: filename global_node_id_start global_node_id_end(exclusive)
            parmetis_nf.write(
                "{} {} {}\n".format(node_file[0], node_file[1], node_file[2])
            )
360
361

    # Regenerate edge files here.
362
    # NOTE: The file names need to match the ones generated by gen_edge_files function
363
364
365
    edge_data = schema_map[constants.STR_EDGES]
    edge_files = []
    for etype_name, etype_info in edge_data.items():
366
367
        edge_data_files = etype_info[constants.STR_DATA]
        for edge_file_path in edge_data_files:
368
369
370
371
            out_file_name = Path(edge_file_path).stem.split(".")[0]
            out_file = os.path.join(
                outdir, etype_name, "edges_{}.csv".format(out_file_name)
            )
372
373
374
375
            edge_files.append(out_file)

    with open(
        os.path.join(params.output_dir, "parmetis_efiles.txt"), "w"
376
377
378
    ) as parmetis_efile:
        for edge_file in edge_files:
            parmetis_efile.write("{}\n".format(edge_file))
379
380
381
382
383
384
385
386
387
388


def run_preprocess_data(params):
    """Main function which will help create graph files for ParMETIS processing

    Parameters:
    -----------
    params : argparser object
        An instance of argparser class which stores command line arguments.
    """
389
    logging.info("Starting to generate ParMETIS files...")
390
    rank = get_proc_info()
391
392
393

    assert os.path.isdir(
        params.input_dir
394
    ), f"Please check `input_dir` argument: {params.input_dit}."
395
396

    schema_map = read_json(os.path.join(params.input_dir, params.schema_file))
397
    gen_node_weights_files(schema_map, params)
398
    logging.info("Done with node weights....")
399

400
    gen_edge_files(rank, schema_map, params)
401
    logging.info("Done with edge weights...")
402
403
404

    if rank == 0:
        gen_parmetis_input_args(params, schema_map)
405
    logging.info("Done generating files for ParMETIS run ..")
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424


if __name__ == "__main__":
    """Main function used to generate temporary files needed for ParMETIS execution.
    This function generates node-weight files and edges files which are consumed by ParMETIS.

    Example usage:
    --------------
    mpirun -np 4 python3 parmetis_preprocess.py --schema <file> --output <target-output-dir>
    """
    parser = argparse.ArgumentParser(
        description="Generate ParMETIS files for input dataset"
    )
    parser.add_argument(
        "--schema_file",
        required=True,
        type=str,
        help="The schema of the input graph",
    )
425
426
    parser.add_argument(
        "--input_dir",
427
        required=True,
428
        type=str,
429
        help="This directory will be used as the relative directory to locate files, if absolute paths are not used",
430
    )
431
432
433
434
435
436
    parser.add_argument(
        "--output_dir",
        required=True,
        type=str,
        help="The output directory for the node weights files and auxiliary files for ParMETIS.",
    )
437
438
439
440
441
442
    parser.add_argument(
        "--num_parts",
        required=True,
        type=int,
        help="Total no. of output graph partitions.",
    )
443
444
445
446
447
448
449
450
    parser.add_argument(
        "--log_level",
        required=False,
        type=str,
        help="Log level to use for execution.",
        default="INFO",
        choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
    )
451
452
    params = parser.parse_args()

453
454
    # Configure logging.
    logging.basicConfig(
455
        level=getattr(logging, params.log_level, None),
456
457
458
459
        format=f"[{platform.node()} \
        %(levelname)s %(asctime)s PID:%(process)d] %(message)s",
    )

460
461
    # Invoke the function to generate files for parmetis
    run_preprocess_data(params)