内存分析#

峰值内存使用量是GPU编程中的一个常见问题,因为GPU内存通常比可用的CPU内存小。为了轻松识别内存热点,cuDF提供了一个内存分析器。它带有一定的开销,因此避免在性能敏感的代码中使用它。

启用内存分析#

首先,通过调用rmm.statistics.enable_statistics()在RMM中启用内存分析。这将向当前的RMM内存资源添加一个统计资源适配器,使cuDF能够访问内存分析信息。更多详情请参阅RMM文档

其次,通过将memory_profiling选项设置为True来启用cuDF中的内存分析。使用cudf.set_option()或在启动Python解释器之前设置环境变量CUDF_MEMORY_PROFILING=1

要获取性能分析的结果,请使用 cudf.utils.performance_tracking.print_memory_report() 或通过使用以下方式访问原始性能分析数据:cudf.utils.performance_tracking.get_memory_records()

示例#

在下面,我们启用性能分析,做一些工作,然后打印性能分析结果:

>>> import cudf
>>> from cudf.utils.performance_tracking import print_memory_report
>>> from rmm.statistics import enable_statistics
>>> enable_statistics()
>>> cudf.set_option("memory_profiling", True)
>>> cudf.DataFrame({"a": [1, 2, 3]})  # Some work
   a
0  1
1  2
2  3
>>> print_memory_report()  # Pretty print the result of the profiling
Memory Profiling
================

Legends:
ncalls       - number of times the function or code block was called
memory_peak  - peak memory allocated in function or code block (in bytes)
memory_total - total memory allocated in function or code block (in bytes)

Ordered by: memory_peak

ncalls memory_peak memory_total filename:lineno(function)
     1          32           32 cudf/core/dataframe.py:690(DataFrame.__init__)
     2           0            0 cudf/core/index.py:214(RangeIndex.__init__)
     6           0            0 cudf/core/index.py:424(RangeIndex.__len__)