File size: 3,801 Bytes
9ed1ff1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import random
from datetime import datetime

import pandas as pd
from torch.utils.data import Dataset
import torchvision.transforms as T
import torch
import numpy as np

def split_datalist_for_gpu(df, gpu_id, gpu_ids, node_id, node_ids):
    node_index = node_ids.index(node_id)  # Position of the current node in the node list
    gpu_index = gpu_ids.index(gpu_id)  # Position of the current GPU in the GPU list

    # first split the dataframe for different nodes
    total_nodes = len(node_ids)
    rows_per_split = len(df) // total_nodes
    start_index = node_index * rows_per_split
    end_index = start_index + rows_per_split if node_index < total_nodes - 1 else len(df)

    df = df[start_index:end_index]

    # then split the dataframe for different gpus
    total_gpus = len(gpu_ids)
    rows_per_split = len(df) // total_gpus
    start_index = gpu_index * rows_per_split
    end_index = start_index + rows_per_split if gpu_index < total_gpus - 1 else len(df)

    return df[start_index:end_index]

def split_dataframe_for_gpu(df, gpu_id, gpu_ids, node_id, node_ids):
    """
    Splits the dataframe for a specific GPU on a specific node, supporting arbitrary GPU and node identifiers.
    
    Args:
    df (pd.DataFrame): The dataframe to split.
    gpu_id (int): The identifier of the GPU for which the split is intended.
    gpu_ids (list): List of all GPU IDs across all nodes, which can be non-sequential.
    node_id (int): The identifier of the node on which the GPU is located.
    node_ids (list): List of all node IDs, which can be non-sequential.
    
    Returns:
    pd.DataFrame: A subset of the original dataframe intended for the specific GPU on a specific node.
    """
    # Calculate the unique index for this GPU on this node by finding its position in the global list of GPUs
    node_index = node_ids.index(node_id)  # Position of the current node in the node list
    gpu_index = gpu_ids.index(gpu_id)  # Position of the current GPU in the GPU list

    # first split the dataframe for different nodes
    total_nodes = len(node_ids)
    rows_per_split = len(df) // total_nodes
    start_index = node_index * rows_per_split
    end_index = start_index + rows_per_split if node_index < total_nodes - 1 else len(df)

    df = df.iloc[start_index:end_index]

    # then split the dataframe for different gpus
    total_gpus = len(gpu_ids)
    rows_per_split = len(df) // total_gpus
    start_index = gpu_index * rows_per_split
    end_index = start_index + rows_per_split if gpu_index < total_gpus - 1 else len(df)

    return df.iloc[start_index:end_index]


def split_dataframe_for_node(df, node_id, node_ids):
    """
    Splits the dataframe for a specific node, supporting arbitrary node identifiers.
    
    Args:
    df (pd.DataFrame): The dataframe to split.
    node_id (int): The identifier of the node
    node_ids (list): List of all node IDs, which can be non-sequential.
    
    Returns:
    pd.DataFrame: A subset of the original dataframe intended for the specific node.
    """
    # Calculate the unique index for this GPU on this node by finding its position in the global list of GPUs
    node_index = node_ids.index(node_id)  # Position of the current node in the node list
    global_index = node_index # Unique index across all GPUs on all nodes

    # Calculate the total number of splits needed
    total_nodes =  len(node_ids)
    
    # Calculate the number of rows per split
    rows_per_split = len(df) // total_nodes
    
    # Calculate the start and end indices of the rows for this particular split
    start_index = global_index * rows_per_split
    end_index = start_index + rows_per_split if global_index < total_nodes - 1 else len(df)
    
    # Get the subset of the dataframe
    return df.iloc[start_index:end_index]