为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Rapids.ai 在 GPU 中做矩阵点积

如何使用 Rapids.ai 在 GPU 中做矩阵点积

FFIVE 2021-10-26 18:35:29
我正在使用CUDF,它是 Nvidia 的Rapids ML 套件的一部分。使用这个套件我将如何做点积?df = cudf.DataFrame([('a', list(range(20))),('b', list(reversed(range(20)))),('c', list(range(20)))])例如,我将如何使用相同的 cudf 对象在上述 Dataframe 上执行点积?
查看完整描述

1 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

cuDF Dataframe 提供了一种apply_rows方法,它能够将方法编译到内核中并在 GPU 上执行。此功能于去年 1 月实施。


import cudf

import numpy


rows = 20000000


df = cudf.DataFrame([

    ('a_in', list(range(rows))),

    ('b_in', list(reversed(range(rows)))),

    ('c_in', list(range(rows)))

])


def kernel(a_in, b_in, c_in, dot):

     for i, (a, b, c) in enumerate(zip(a_in, b_in, c_in)):

         dot[i] = a * b * c


df = df.apply_rows(

    kernel,

    incols=['a_in', 'b_in', 'c_in'],

    outcols=dict(dot=numpy.float64),

    kwargs=dict()

)


[x for x in df['dot']]

普鲁斯


[0.0,

 18.0,

 68.0,

 144.0,

 240.0,

 350.0,

 468.0,

 588.0,

 704.0,

 810.0,

 900.0,

 968.0,

 1008.0,

 1014.0,

 980.0,

 900.0,

 768.0,

 578.0,

 324.0,

 0.0]

至于计算点积...


import cudf

import numpy

import pandas


rows = 20000000


values_a = [float(x) for x in list(range(rows))]

values_b = [float(x) for x in list(reversed(range(rows)))]

values_c = [float(x) for x in list(range(rows))]


def create_cudf_dataframe():

    return cudf.DataFrame([

        ('a_in', values_a),

        ('b_in', values_b),

        ('c_in', values_c)

    ])


def create_pandas_dataframe():

    return pandas.DataFrame(

        data = {

            'a_in': values_a,

            'b_in': values_b,

            'c_in': values_c

        }

    )



def test_cudf(df = None):


    print('\ncomputing dot product using cudf')


    def kernel(a_in, b_in, c_in, dot):

         for i, (a, b, c) in enumerate(zip(a_in, b_in, c_in)):

             dot[i] = a * b * c


    if df is None:

        print(' - creating dataframe using cudf')

        df = create_cudf_dataframe()


    df = df.apply_rows(

        kernel,

        incols=['a_in', 'b_in', 'c_in'],

        outcols=dict(dot=numpy.float64),

        kwargs=dict(),

        cache_key='dot_product_3'

    )


    dp = df['dot'].sum()


    print(dp);



def test_pandas(df = None):

    print('\ncomputing dot product using pandas')

    if df is None:

        print(' - creating dataframe using pandas')

        df = create_pandas_dataframe()


    a = df['a_in']

    b = df['b_in']

    c = df['c_in']


    dp = a.mul(b).mul(c).sum()


    print(dp)


cudf_df = create_cudf_dataframe()

pandas_df = create_pandas_dataframe()


%time test_cudf()

%time test_cudf(cudf_df)

%time test_pandas()

%time test_pandas(pandas_df)

在 ubuntu@18.04 上的 jupyter 上的性能结果在 i7 6700-k 上运行,具有 32GB 内存和 GTX 1080 ti。


computing dot product using cudf

 - creating dataframe using cudf

1.333333066666688e+28

CPU times: user 1.78 s, sys: 273 ms, total: 2.06 s

Wall time: 2.05 s


computing dot product using cudf

1.333333066666689e+28

CPU times: user 19.4 ms, sys: 24 ms, total: 43.4 ms

Wall time: 43.1 ms


computing dot product using pandas

 - creating dataframe using pandas

1.3333330666666836e+28

CPU times: user 7.81 s, sys: 781 ms, total: 8.59 s

Wall time: 8.57 s


computing dot product using pandas

1.3333330666666836e+28

CPU times: user 125 ms, sys: 120 ms, total: 245 ms

Wall time: 245 ms


查看完整回答
反对 回复 2021-10-26
  • 1 回答
  • 0 关注
  • 184 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信