GPU驱动中copy_from_user的去除2d性能提升3倍

本文介绍了一种通过调整GPU驱动中的copy_from_user函数提高2D性能的方法。通过对内核和用户程序的修改,成功将2D性能提升了3.3倍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原】GPU驱动中copy_from_user的去除2d性能提升3倍

   最近做GPU的2D分析,我们在进行df_dok的oprofile分析时,发现我们的copy_from_user这个函数所占的比重最大,这个时候就突然想起了一件事情,那就是我们在做驱动的时候,因为我们的内核态不能访问用户态的数据,所以就加了一个copy_from_user,那么这个copy_from_user究竟在我们整个测试的copy_from_user中占了多大的比重呢?我们做了一个简单的测试,从次数和拷贝的大小两个方面来看,我们发现这个加上的copy_from_user占了99%的copy_from_user的时间,于是我们做了一个决定,在我们访存性能不理想的状态下,把这个copy_from_user干掉。

 

目标:把GPU中添加的copy_from_uer函数拿去掉。

 

首先来分析一下这个copy_from_user的由来,我们上文提到,主要原因是我们的内核态不能访问用户态数据(这个和ARM不太一样,ARM的低3G空间内核态都是可以访问的,而我们则不同。)我们的unicore,它的内核在访问用户空间时会挂掉,为什么会挂掉呢,于是我们一帮人开始研究手册,发现了下面几点:

1、内核的高1G,是只有内核能访问的,用户态不能访问,这个和arm一样,没啥好说的。

2、低2G的空间,只能用户态访问,内核不能访问。这个就是我们为什么增加copy_from_user的原因。

3、2G-3G的空间,内核和用户都可以访问。这个就是一个新大陆,最终确定了我们整个方案的思路。

 

所以,我们决定了解决的方案为把我们内核想要访问的这个数据放到2G-3G中去,这样就会减少copy的过程,从而提升整个2D的性能。

 

下面我们接着来看一下,我们需要访问的这个数据是怎么来的,通过源码的分析,我们最终发现这个数据是通过用户态malloc的来的。为什么说是最终呢,因为最初我们以为这个数据是一个局部变量是在栈上的,而事实上它却是堆上,为什么会出这个错误呢,从源头来看,我们看到一个局部的结构体变量,这个结构体有一个结构体指针成员,这个成员所指向的数据是我们所要copy的数据,开始我们没有注意以为结构体得成员就是首地址的偏移,直到后来才发现,这个数据是在堆上。

 

因此,所要copy的数据是malloc得到的数据,它位于堆上。

 

接着,我们就来看怎么把malloc的数据放到2G开始的地方了。看内核,分析了malloc的实现函数,后来发现没用,这个就不具体说了,但是我们发现了两个限制的宏。他们均位于arch/unicore/include/asm/memory.h中,分别为

#define TASK_SIZE               (UL(0xc0000000) - UL(0x41000000))

#define TASK_UNMAPPED_BASE      (UL(0xc000000) / 3)

宏TASD_SIZE是用来限制用户空间的上限的,我们的内核限制了最上面是2G不到的地方,所以这个地方需要把它改了。

宏TASK_UNMAPPED_BASE是用来表示malloc申请较大的空间时的起始地址。我们现在是0x40000000,因此要把它干了。

 

因此,我们需要修改两个内核限制的宏:

#define TASK_SIZE               (UL(0xc0000000) - UL(0x10000000))

#define TASK_UNMAPPED_BASE      (UL(0x90000000))

 

Ok,内核限制解除了,下面就来搞用户程序了,我们首先通过unicore32-linux-ld verbose得到编译器的链接文件,然后发现

  /* Read-only sections, merged into text segment: */

  PROVIDE (__executable_start = 0x02000000); . = 0x02000000 + SIZEOF_HEADERS;

  .interp         : { *(.interp) }

  .note.gnu.build-id : { *(.note.gnu.build-id) }

,所以把这个里面的0x02000000,改成0x82000000,本来只是想把.bss_start那边改掉的,后来发现简单的测试时时可以的,但是加载动态库的时候就挂掉了。所以后来干脆把所有的都放到2G-3G去了。这里还需要注意的是从应用程序来看,不光是要修改动态库,还要修改应用程序的编译,也就是在编译时加上(-T 链接文件目录)这个的选项。

 

因此,需要重新修改文件的链接和加载位置。

 

这样就解决了copy_from_user的问题,我们2d性能提升了3.3倍。数据如下:

 

测试项目

有copy_from_user

无copy_from_user

 

 

 

Anti-aliased Text

3.016 secs (* 9.549 KChars/sec) [ 99.6%]

3.104 secs (* 32.474 KChars/sec) [100.3%]

Anti-aliased Text (blend)

3.008 secs (* 9.574 KChars/sec) [ 99.6%]

3.076 secs (* 32.769 KChars/sec) [100.3%]

Fill Rectangle

3.220 secs (* 18.317 MPixel/sec) [ 99.6%]

3.012 secs (* 69.626 MPixel/sec) [100.0%]

Fill Rectangle (blend)

3.044 secs (* 17.223 MPixel/sec) [100.0%]

3.028 secs (* 60.601 MPixel/sec) [100.3%]

Fill Rectangles [10]

3.580 secs (* 128.143 MPixel/sec) [ 87.7%]

3.652 secs (* 179.452 MPixel/sec) [ 29.8%]

Fill Rectangles [10] (blend)

5.152 secs (* 76.322 MPixel/sec) [ 56.6%]

4.780 secs (* 95.973 MPixel/sec) [ 17.3%]

Fill Triangles

3.352 secs (* 8.798 MPixel/sec) [ 99.7%]

3.092 secs (* 29.673 MPixel/sec) [ 98.7%]

Fill Triangles (blend)

3.044 secs (* 8.611 MPixel/sec) [100.0%]

3.004 secs (* 27.270 MPixel/sec) [ 99.6%]

Draw Rectangle

3.048 secs (* 0.295 KRects/sec) [100.3%]

3.068 secs (* 1.140 KRects/sec) [ 99.6%]

Draw Rectangle (blend)

3.352 secs (* 0.298 KRects/sec) [100.0%]

3.044 secs (* 1.084 KRects/sec) [100.0%]

Draw Lines [10]

3.060 secs (* 2.941 KLines/sec) [ 99.6%]

3.008 secs (* 10.970 KLines/sec) [100.0%]

Draw Lines [10] (blend)

3.200 secs (* 2.812 KLines/sec) [100.0%]

3.096 secs (* 9.689 KLines/sec) [100.0%]

Fill Spans

3.320 secs (* 15.791 MPixel/sec) [ 99.0%]

3.128 secs (* 50.283 MPixel/sec) [ 99.0%]

Fill Spans (blend)

3.060 secs (* 14.991 MPixel/sec) [100.0%]

3.140 secs (* 43.829 MPixel/sec) [ 99.0%]

Blit

3.140 secs (* 18.784 MPixel/sec) [ 99.6%]

3.004 secs (* 69.811 MPixel/sec) [100.3%]

Blit 180

3.156 secs (* 18.688 MPixel/sec) [100.0%]

3.092 secs (* 69.944 MPixel/sec) [100.0%]

Blit colorkeyed

3.288 secs (* 17.938 MPixel/sec) [100.0%]

3.088 secs (* 61.546 MPixel/sec) [100.3%]

Blit destination colorkeyed

3.280 secs (* 17.982 MPixel/sec) [ 99.6%]

3.076 secs (* 61.786 MPixel/sec) [ 99.6%]

Blit with format conversion

3.100 secs (* 19.026 MPixel/sec) [100.0%]

3.048 secs (* 68.804 MPixel/sec) [100.6%]

Blit with colorizing

3.244 secs (* 18.181 MPixel/sec) [ 99.6%]

3.008 secs (* 65.361 MPixel/sec) [100.0%]

Blit from 32bit (blend)

3.344 secs (* 17.638 MPixel/sec) [100.2%]

3.084 secs (* 61.625 MPixel/sec) [100.0%]

Blit from 32bit (blend) with coloring

3.004 secs (* 17.452 MPixel/sec) [100.3%]

3.036 secs (* 58.283 MPixel/sec) [100.3%]

Stretch Blit

3.260 secs (* 21.224 MPixel/sec) [100.0%]

3.000 secs (* 69.950 MPixel/sec) [100.0%]

Stretch Blit colorkeyed

3.108 secs (* 19.584 MPixel/sec) [ 99.6%]

3.000 secs (* 63.572 MPixel/sec) [ 99.6%]

(gprMaxenv) D:\2025Spring\sx\py3_10gprMax\gprMax-v.3.1.7>python -m gprMax user_models/cylinder_Ascan_2D.in -gpu === Electromagnetic modelling software based on the Finite-Difference Time-Domain (FDTD) method === www.gprmax.com __ __ __ _ _ __ _ __| \/ | __ ___ __ / _` | &#39;_ \| &#39;__| |\/| |/ _` \ \/ / | (_| | |_) | | | | | | (_| |> < \__, | .__/|_| |_| |_|\__,_/_/\_\ |___/|_| v3.1.6 (Big Smoke) Copyright (C) 2015-2023: The University of Edinburgh Authors: Craig Warren and Antonis Giannopoulos gprMax is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. gprMax is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with gprMax. If not, see www.gnu.org/licenses. Host: LAPTOP-UVIB8C5I | LENOVO 82JQ | 0 x unknown (8 cores, 16 cores with Hyper-Threading) | 15.9GiB RAM | Windows 10 (64-bit) Traceback (most recent call last): File "D:\2025Spring\sx\py3_10gprMax\gprMax-v.3.1.7\gprMax\utilities.py", line 380, in detect_check_gpus import pycuda.driver as drv File "D:\ProgramData\anaconda3\envs\gprMaxenv\lib\site-packages\pycuda\driver.py", line 66, in <module> from pycuda._driver import * # noqa ImportError: DLL load failed while importing _driver: %1 不是有效的 Win32 应用程序。 During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\ProgramData\anaconda3\envs\gprMaxenv\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "D:\ProgramData\anaconda3\envs\gprMaxenv\lib\runpy.py", line 86, in _run_code exec(code, run_globals) File "D:\2025Spring\sx\py3_10gprMax\gprMax-v.3.1.7\gprMax\__main__.py", line 6, in <module> gprMax.gprMax.main() File "D:\2025Spring\sx\py3_10gprMax\gprMax-v.3.1.7\gprMax\gprMax.py", line 69, in main run_main(args) File "D:\2025Spring\sx\py3_10gprMax\gprMax-v.3.1.7\gprMax\gprMax.py", line 136, in run_main gpus, allgpustext = detect_check_gpus(args.gpu) File "D:\2025Spring\sx\py3_10gprMax\gprMax-v.3.1.7\gprMax\utilities.py", line 382, in detect_check_gpus raise ImportError(&#39;To use gprMax in GPU mode the pycuda package must be installed, and you must have a NVIDIA CUDA-Enabled GPU (https://developer.nvidia.com/cuda-gpus).&#39;) ImportError: To use gprMax in GPU mode the pycuda package must be installed, and you must have a NVIDIA CUDA-Enabled GPU (https://developer.nvidia.com/cuda-gpus).
最新发布
07-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值