美文网首页
CPU 和 GPU 计算能力比较代码

CPU 和 GPU 计算能力比较代码

作者: 廿怎么念 | 来源:发表于2020-06-12 10:26 被阅读0次

随便玩玩,体验效果。

参考来源

框架:Tensorflow 2.2.0

显卡:GTX1660Ti

CPU: i7-10750h

结果展示

从上图中我们可知,当计算量较小时,gpu和cpu并无太大差别,甚至gpu要稍微弱一些。当计算量增大时,gpu的优势就凸显出来了,gpu计算耗时增长较小,此处几乎无增长,但cpu耗时急速上升。



import tensorflow as tf

import timeit

import matplotlib.pyplot as plt

'''

以矩阵A[10,n]和矩阵B[n,10]的乘法运算(分别在cpu和gpu上运行)来测试

通过改变n大小,增减计算量

'''

def cpu_gpu_compare(n):

    with tf.device('/cpu:0'):                    ##指定操作用cpu计算

        cpu_a = tf.random.normal([10,n])        ##生成符合高斯分布的随机数矩阵

        cpu_b = tf.random.normal([n,10])

    print(cpu_a.device,cpu_b.device)

    with tf.device('/gpu:0'):

        gpu_a = tf.random.normal([10,n])

        gpu_b = tf.random.normal([n,10])

    print(gpu_a.device,gpu_b.device)

    def cpu_run():

        with tf.device('/cpu:0'):              ##矩阵乘法,此操作采用cpu计算

            c = tf.matmul(cpu_a,cpu_b)

        return c

    def gpu_run():

        with tf.device('/gpu:0'):              ##矩阵乘法,此操作采用gpu计算

            c = tf.matmul(gpu_a,gpu_b)

        return c             

    ##第一次计算需要热身,避免将初始化时间计算在内

    cpu_time = timeit.timeit(cpu_run,number=5)

    gpu_time = timeit.timeit(gpu_run,number=5)

    print('warmup:',cpu_time,gpu_time) #可注释掉,只画图

    ##正式计算5次,取平均值,可修改为其他次数

    cpu_time = timeit.timeit(cpu_run,number=5)

    gpu_time = timeit.timeit(gpu_run,number=5)

    print('run_time:',cpu_time,gpu_time) #可注释掉,只画图

    return cpu_time,gpu_time

# 设置gpu内存占用方式为增长式占用,根据实际模型大小申请显存资源

def gpu_growth():

    gpus = tf.config.experimental.list_physical_devices('GPU')

    if gpus:

        try:

            for gpu in gpus:

                tf.config.experimental.set_memory_growth(gpu, True)

        except RuntimeError as e:

                print(e)

def main():

    gpu_growth()

    n_list1 = range(1,5000,100) #设置计算矩阵维度大小

    n_list2 = range(5001,50000,1000) #设置计算矩阵维度大小

    n_list = list(n_list1)+list(n_list2)

    time_cpu =[]

    time_gpu =[]

    for n in n_list:

        t=cpu_gpu_compare(n)

        time_cpu.append(t[0]) # t[0] cpu_time

        time_gpu.append(t[1]) # t[1] gpu_time

    plt.plot(n_list,time_cpu,color = 'red',label='cpu')

    plt.plot(n_list,time_gpu,color='green',linewidth=1.0,linestyle='--',label='gpu')

    plt.ylabel('耗时',fontproperties = 'SimHei',fontsize = 20)

    plt.xlabel('计算量',fontproperties = 'SimHei',fontsize = 20)

    plt.title('cpu和gpu计算力比较',fontproperties = 'SimHei',fontsize = 30)

    plt.legend(loc='upper right')

    plt.show()

if __name__ =='__main__':

    main()

    print('Done!')

相关文章

  • CPU 和 GPU 计算能力比较代码

    随便玩玩,体验效果。 参考来源 框架:Tensorflow 2.2.0 显卡:GTX1660Ti CPU: i7-...

  • iOS底层day11 - 性能优化

    CPU & GPU 屏幕成像原理: 卡贞 CPU 计算和GPU渲染是具有周期性的,当 CPU 计算和GPU渲染的时...

  • GPU编程--CPU和GPU的设计区别

    本篇结构 前言 概论 CPU简介 GPU简介 并行计算 CPU/GPU对比 适于GPU计算的场景 GPU开发环境 ...

  • iOS图形学(三):屏幕成像原理

    一、GPU和CPU架构 1. GPU由来 无 GPU 的时代,所有任务都在 CPU 中进行计算和处理,但是随着时代...

  • 2018年小白比特币挖矿Q&A

    1.GPU和CPU GPU和CPU均可用来计算。日常应用主要是CPU(中央处理器)进行总的处理,GPU(图形处理器...

  • [code]关于圆角图片问题

    CPU、GPU在渲染图片时是如何工作的? 计算机系统中CPU、GPU协同工作,CPU计算好显示的内容给GPU,GP...

  • GPU 与 CPU 性能比较

    GPU 与 CPU 性能比较 理解 GPU 和 CPU 之间区别的一种简单方式是比较它们如何处理任务。CPU 由专...

  • iOS图像渲染原理

    首先回答一个问题:CPU和GPU都能进行图形渲染,只是GPU 图形渲染的并行计算能力速度更快 屏幕图像显示原理 下...

  • 架构设计读书笔记-性能篇(一)

    概述 计算机的性能,从硬件来说,一个和IO相关(磁盘、内存),一个和计算能力相关(CPU、GPU)。说到软件的性能...

  • Android 优化之硬件加速

    原理 可以简单理解为通过底层软件代码,将 CPU 不擅长的图形计算转换为 GPU 专用指令,由 GPU 完成。 当...

网友评论

      本文标题:CPU 和 GPU 计算能力比较代码

      本文链接:https://www.haomeiwen.com/subject/kfhgtktx.html