【问题标题】:Change Pandas code into CUDF for GPU utilization将 Pandas 代码更改为 CUDF 以提高 GPU 利用率
【发布时间】:2021-05-09 17:30:34
【问题描述】:

我通过混合正负对来制作成对的图像。这个过程计算量很大,需要大量的 RAM 和处理器。为了加快速度,我想使用 GPU 并将熊猫代码更改为 CUDF。现在,CUDF的文档非常有限,我想把下面的代码改成CUDF。

positives = pd.DataFrame()
for value in tqdm(identities.values(), desc="Positives"):
    positives = positives.append(pd.DataFrame(itertools.combinations(value, 2), columns=["file_x", "file_y"]),
                                 ignore_index=True)
positives["decision"] = "Yes"
print(positives)
samples_list = list(identities.values())
negatives = pd.DataFrame()
######################====================Functions=============##############

def compute_cross_samples(x):
    return pd.DataFrame(itertools.product(*x), columns=["file_x", "file_y"])

####################################
if __name__ == "__main__":
    if Path("positives_negatives.csv").exists():
        df = pd.read_csv("positives_negatives.csv")
    else:
        with ProcessPoolExecutor() as pool:
            # take cpu_count combinations from identities.values
            for combos in tqdm(more_itertools.ichunked(itertools.combinations(identities.values(), 2), cpu_count())):
                # for each combination iterator that comes out, calculate the cross
                for cross_samples in pool.map(compute_cross_samples, combos):
                    # for each product iterator "cross_samples", iterate over its values and append them to negatives
                    negatives = negatives.append(cross_samples)

        negatives["decision"] = "No"

negatives = negatives.sample(positives.shape[0])
df = pd.concat([positives, negatives]).reset_index(drop=True)
df.to_csv("positives_negatives.csv", index=False)`

【问题讨论】:

  • 多处理池不适用于 CUDA。 cudf 数组有一个从 pandas 转换的方法。
  • 没问题,你可以删除多处理代码我只想在 GPU 上运行代码。多处理需要 9 天然后给出错误。在过去的两个月里,我面临着这个问题。需要帮助
  • 您的确切问题是什么?
  • 我必须构建一个非常大的列表并且创建列表的时间很多。我必须通过 GPU 利用率来最小化它。
  • 如果您创建一个最小的、完整的、可重现的示例,社区可能会更好地帮助您。 stackoverflow.com/help/minimal-reproducible-example

标签: python python-3.x pandas numpy cudf


【解决方案1】:

对于您的代码,您需要考虑两件事:

  1. 由于 API 相似,首先要导入 cudf。然后,在您使用 pd(您的 pandas 导入变量名称)的地方,将其替换为 cudf。虽然这是一个开始,please check out this guide 将帮助您了解过渡的基础知识。编码方面,从cudf and dask cuDF tutorial notebooks 开始,尤其是this one

  2. 顺便说一句,除了删除您的 CPU 处理代码之外,您还想重构您的函数以不需要 for loops。 cuDF 和其他 RAPIDS 库在后台为 GPU 并行化代码做了很多工作。添加 for 循环会使过程串行化并减慢您的速度。

  3. 最后,请在此处阅读我们的官方文档文档,这应该有助于您的 CPU -> GPU 重构:https://docs.rapids.ai/api/cudf/stable/api.html

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 2021-02-25
  • 2018-07-04
  • 2020-11-27
  • 2017-06-25
  • 2010-10-27
相关资源
最近更新 更多