• [安装经验] 树莓派源码安装gcc 7.3.0
    前情回顾:张小白树莓派4B踩坑记(一)https://bbs.huaweicloud.com/blogs/330786张小白树莓派4B踩坑记(二)https://bbs.huaweicloud.com/blogs/330956ARM CPU的MindSpore版本已经装上了,今天我们来玩些什么呢?张小白第一次源码安装gcc 7.3.0失败,这让张小白非常郁闷。今天就在新的系统中看看能不能打破这个僵局。gcc7.3.0的安装按照 https://bbs.huaweicloud.com/blogs/198349 ,应该是  gmp5.0.1+mpc1.1.0+mpfr3.1.0+gcc7.3.0 分别源码安装。wget ftp://ftp.gnu.org/gnu/gmp/gmp-5.0.1.tar.bz2wget http://www.multiprecision.org/downloads/mpc-1.1.0.tar.gzwget http://ftp.gnu.org/gnu/mpfr/mpfr-3.1.0.tar.xzbzip2 -d gmp-5.0.1.tar.bz2tar xvf gmp-5.0.1.tarcd gmp-5.0.1./configure --prefix=/usr/local/gmp-5.0.1抱歉,我不认识这个系统。。加个参数试一下:./configure --prefix=/usr/local/gmp-5.0.1 --build=arm-linux...抱歉。。。张小白再查了下资料,似乎需要依靠automake提供的config.guess才行、下载automake源码:wget https://ftp.gnu.org/gnu/automake/automake-1.16.tar.gz解压后,需要修改automake的Makefile,按下面的箭头所示增加 --no-discard-stderr参数然后再configuremake -j8sudo make install将automake-1.16/lib下的config.guess文件覆盖gmp-5.0.1下的config.guess文件:回到gmp-5.0.1的目录,再重新 ./configure --prefix=/usr/local/gmp-5.0.1make -j8sudo make install -j8同样解压 mpfr进行./configure --prefix=/usr/local/mpfr-3.1.0 --with-gmp=/usr/local/gmp-5.0.1会发现同样的问题,当然也可以采用同样的解决方案:重新./configure --prefix=/usr/local/mpfr-3.1.0 --with-gmp=/usr/local/gmp-5.0.1make -j8sudo make install -j8对于mpc如法炮制:./configure --prefix=/usr/local/mpc-1.1.0 --with-gmp=/usr/local/gmp-5.0.1 --with-mpfr=/usr/local/mpfr-3.1.0make -j8sudo make install -j8修改/etc/ld.so.confsudo ldconfig然后解压gcc的包,开始编译gcc:./configure --with-gmp=/usr/local/gmp-5.0.1 --with-mpc=/usr/local/mpc-1.1.0 --with-mpfr=/usr/local/mpfr-3.1.0 --disable-multilib啊,什么问题:再来一遍看看:加个参数再试一下:./configure --with-gmp=/usr/local/gmp-5.0.1 --with-mpc=/usr/local/mpc-1.1.0 --with-mpfr=/usr/local/mpfr-3.1.0 --disable-multilib --disable-libsanitizermake -j8终于编译成功。sudo make install -j8查看下gcc:/usr/local/bin/gcc是自己源码编译的7.3.0,/usr/bin/gcc是系统自带的。做个备份,删掉/usr/bin下的软链接:这样gcc 7.3.0就OK了。这也算是解决了第一章遇到的装不起来的难题。撒花!(未完待续)
  • [讨论交流] 毕昇编译器加入欧拉开源操作系统软件仓库,欢迎开发者使用~
    毕昇编译器正式加入欧拉开源操作系统软件仓库,在 openEuler 系操作系统下均可通过 yum 安装,目前仅支持 Arm 架构。根据最新 Benchmark 测试结果,相同软硬件条件下,毕昇编译器 2.1.0 比 GCC 9.3.0 性能提升 **24.3%**,使用毕昇编译器可以进一步提升应用性能。 ## 毕昇编译器最新版本 毕昇编译器于 2021 年 12 月 30 日发布 2.1.0 版本,当前版本增强循环优化、结构体重组优化、Block reorder 优化特性,提升 SPEC CPU 2017 多个子项及 HPC workload 的性能;新增支持 pow 初始化立即数拟合、数学函数控制等精度控制选项,进一步增强精度调优选项;支持多线程并行编程技术和 Input/output enhancements (Fortran 2003)/ 异步 IO 特性,满足鲲鹏场景对 Fortran 语言生态的需求。 ![](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202201/30/142419om33imhmifwfxxjr.png) 图1 毕昇编译器概念图 ## 更高的性能评分 测试环境: - 操作系统:openEuler 20.03 - CPU:Kunpeng 920 毕昇编译器开发团队基于毕昇编译器 2.1.0 版本进行性能评测,SPEC CPU® 2017 测试报告显示毕昇编译器 2.1.0 综合得分达到了 399 分,GCC 9.3.0 综合得分为 321 分。在相同软硬件环境下,毕昇编译器的性能比 GCC 高 **24.3%**。 ![](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202201/30/142441obbipsywi04ocmka.png) 图2 毕昇编译器 2.1.0 性能测试报告 ![](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202201/30/142453bppjmvlt9ymp4pt1.png) 图3 GCC 9.3.0 性能测试报告 ## 如何使用 在 openEuler 系操作系统中,可以使用 yum 安装毕昇编译器;其他 Linux 操作系统可以通过软件包方式安装。安装教程如下: 注意:如果系统中有其他版本的 LLVM 编译器,请在安装和卸载毕昇编译器之后立即运行 `hash -r` 命令清空当前窗口的 hash 表,防止 clang 命令被 hash 捕获,出现毕昇编译器或开源 LLVM 编译器无法使用的问题。 ### Yum 源安装 **步骤一:** 在 /etc/yum.repos.d / 目录下增加配置文件 bisheng-comliper.repo,运行如下命令: ``` bash cat \> /etc/yum.repos.d/bisheng-compiler.repo \ EOF \[bisheng-comliper\] name=bisheng-compiler baseurl=https://repo.oepkgs.net/bisheng/aarch64/ enabled=1 gpgcheck=0 priority=100 ``` **步骤二:** 使用 yum 命令进行安装: ``` bash yum update yum install bisheng-compiler ``` ### 软件包安装 **步骤一:** 进入[毕昇编译器产品页](https://www.hikunpeng.com/developer/devkit/compiler/bisheng)下载毕昇编译器: **步骤二:** 设置安装目录 以 /opt/compiler 目录举例,若您的安装目录不同,请以实际目录为准。 1. 创建毕昇编译器安装目录 ``` bash mkdir -p /opt/compiler ``` 2. 将毕昇编译器压缩包拷贝到安装目录下: ``` bash cp bisheng-compiler-2.1.0-aarch64-linux.tar.gz /opt/compiler ``` **步骤三:** 压缩包解压 ``` bash cd /opt/compiler tar -zxvf bisheng-compiler-2.1.0-aarch64-linux.tar.gz ``` 解压完成后在当前目录下出现名为 bisheng-compiler-2.1.0-aarch64-linux 的目录。 **步骤四:** 配置毕昇编译器的环境变量 ``` bash export PATH=/opt/compiler/bisheng-compiler-2.1.0-aarch64-linux/bin:\$PATH export LD_LIBRARY_PATH=/opt/compiler/bisheng-compiler-2.1.0-aarch64-linux/lib:\$LD_LIBRARY_PATH ``` **步骤五:**验证安装是否成功 ``` bash clang -v ``` 返回如下信息即表示安装成功: ``` bash Huawei Bisheng Compiler clang version 12.0.0 (clang-7a3fa32529f2 flang-774a1ed6a7c1)\ Target: aarch64-unknown-linux-gnu\ Thread model: posix ``` 毕昇编译器使用方式和 LLVM 基本一致,新增优化选项及特性使用请参考[毕昇编译器产品页](https://www.hikunpeng.com/developer/devkit/compiler/bisheng)的文档。 ## 交流群 如果在使用毕昇编译器的过程中遇到问题,可以进入 openEuler Compiler SIG 交流讨论。感兴趣的同学可以添加编译器小助手,回复 Compiler 入 openEuler Compiler SIG 微信群。 ![](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202201/30/142514w09xrltbafwswkwx.jpg) 也可以进入[毕昇编译器产品页](https://www.hikunpeng.com/developer/devkit/compiler/bisheng)查找相关资源,包括软件包下载、产品文档、在线课程、沙箱实验、毕昇论坛等。 ------ 关注 **毕昇编译** 获取编译技术更多信息 ![](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202201/30/1425258ednwxsljznys7lh.jpg)
  • [技术干货] 编译gcc 7.3.0报fatal error: sys/ustat.h: No such file错误解决方法
    【问题描述】编译gcc 7.3.0源码执行make && make isstall过程报sanitizer_platform_limits_posix.cc:157:10: fatal error: sys/ustat.h: No such file or directory错误,详细报错如下:【解决方法】修改gcc 7.3.0源码解压路径下的libsanitizer/sanitizer-common/sanitizer-platform-limits-posix.cc文件vim libsanitizer/sanitizer-common/sanitizer-platform-limits-posix.cc注释第157和250行保存修改后,再次执行make && make install即可。
  • [行业资讯] GCC 12添加了对Mold链接器的支持
    GCC 12编译器添加了对Mold链接器支持,Mold在上周发布了1.0版本,据发布声明显示,Mold 1.0 是首个稳定且可用于生产的高速链接器版本,可以替代大部分程序的默认GNU链接器。据GCC 12相关信息显示,使用 Mold 连接器与Gold、BDF和LLD连接器获得了同步的支持。
  • [GCC for op...] GCC for openEuler -mcmodel选项详解
    # GCC for openEuler -mcmodel选项详解 ## 导语 GCC for openEuler是基于开源GCC开发的编译器工具链(包含编译器,汇编器,链接器),在openEuler社区开源发布,并通过鲲鹏社区免费提供二进制包,支持包含ARM、x86在内的多种处理器架构。 本文将向大家详细介绍-mcmodel选项的作用以及GCC for openEuler 在-mcmodel选项上做的新功能支持[1]。 ## 背景 编译的过程中,编译器是不知道要操作的数据在哪里的,计算数据地址的工作是在链接阶段实现的。也就是说,编译器需要先把拿取数据的汇编指令定下来,在编译结束之后,链接器进行重定位计算时再填上指令的操作数是多少。那就有一个问题,编译器如何选择一个合适的指令拿取数据? **PC相对寻址** 在考虑这个问题前,我们先了解一下PC相对地址。PC(Program Count)特指PC寄存器(以下都用PC表示),是计算机处理器内部的一个专用寄存器,用来表示下一个将被执行的指令的地址。在我们的程序中,如果某一条指令访问的符号(变量,函数等)是基于当前指令的相对地址,我们就称这是**PC相对寻址**。 例如跳转指令`b, <label>`中的 `<lable>` 就是**偏移量**,并不是绝对地址。在链接阶段,链接器会根据符号出现的位置计算出正确的偏移,编码到指令中去。程序执行时到这条指令时,就会把`<lable>`所代表的偏移量与当前PC值相加,得到要跳转的指令地址。 类似的还有ADR,ADRP指令。ADR/ADRP分别是获取某个符号的地址/页面地址,并把它们存放到指定的寄存器中。它们的格式是这样的:`ADR Rd, <label>`, `ADRP Rd, <label>`, 这里的`<label>`都是代表当前指令位置的偏移量。 `<label>`受到指令位域的限制,所以偏移量都是有限制的。比如说`b, <label>` 可以基于当前PC有±128MB的寻址范围,`ADR Rd, <label>`有±1MB的寻址范围,`ADRP Rd, <label>`有±4GB的寻址范围。 链接器在链接时会把各个目标文件进行合理布局,使得跳转、函数调用、变量访问等操作都不会离当前指令特别远。通常情况下,使用前面提到的指令能满足绝大多数的情景,但是如果指令要访问的符号超出了4GB(32位)的范围,这时用默认的符号取值方式就会出错。 ## 案例 比如说在AArch64后端如果待链接符号的距离超过4GB(如图1),编译的时候又决定使用ADRP指令(32bit寻址范围,如图2)。此时如果使用这种方式去链接,则会报relocation truncated to fit这样的错(如图3)。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115223rn6mznkcmbscp5ww.png) 图1:符号相对于.bss段的偏移大于4GB ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115230mhnipbboql5nnae5.png) 图2:指令集中对于ADRP指令的描述,可见其寻址范围只在±4GB ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115236bb3qbrxxx1aw9i5i.png) 图3:CESM代码中出问题的字段描述,`R_AARCH64_ ADR_PREL_PG_HI21`对应ADRP指令进行相对PC寻址 那么怎么解决这个问题?在这种情况下需要指导编译器:符号可能特别远(超过4GB),需要生成恰当的指令来获取符号地址。那么编译器就不会再用传统的ADRP这样的指令,而是采用其他的办法。 对此,GCC提供了`-mcmodel`参数,用于指导编译器应该使用哪种模型来生成指令。 下面是GCC的`-mcmodel`对AArch64架构的的官方说明: > -mcmodel=tiny\ > Generate code for the tiny code model. The program and its statically defined symbols must be within 1MB of each other. Programs can be statically or dynamically linked.\ > -mcmodel=small\ > Generate code for the small code model. The program and its statically defined symbols must be within 4GB of each other. Programs can be statically or dynamically linked. This is the default code model.\ > -mcmodel=large\ > Generate code for the large code model. This makes no assumptions about addresses and sizes of sections. Programs can be statically linked only. The -mcmodel=large option is incompatible with -mabi=ilp32, -fpic and -fPIC. `-mcmodel`选项指导编译器做出这样一种假设:代码里所有符号的位置都在某个位宽范围之内。比如`-mcmodel=small`,就是假设所有符号都在4GB范围内,32bit的位宽就可以找到符号的位置,那我们使用ADRP指令就可以了。 但是假设不成立的时候,比如上面的情况,ADRP指令不再适用,需要用位宽更大的指令。这个时候就需要增大`-mcmodel`的预设,使用`-mcmodel=large`,变更寻址方式为LDR指令。LDR指令只能绝对寻址,但是有更大的寻址范围。如果你的应用可以非地址无关编译的话,那么`-mcmodel=large`理论上可以解决所有的问题。 而例如HPC场景中的CESM应用在符号超过4GB寻址范围的时候,作为一些共享库,仍然需要按照地址无关代码(position-independent code,PIC) 的方式编译(-fPIC / -fpic地址无关功能),可以说地址无关代码是动态共享库必须的。这时LDR指令也不再适用,因为GCC对于AArch64的支持非常有限,它仅支持非地址无关代码。这是AArch64独有的一类问题。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115247hj0okyvi92ys76yk.png) 图4:aarch64 `-mcmodle=large`时的寻址方式 在x86上有可以大位宽操作的`mov`指令,它可以实现4GB以上的地址无关寻址。根据与x86后端对比可以发现,x86后端在`-mcmodel=medium`的时候之所以还可以生成地址无关代码,主要原因是其寻址方式还是相对PC寻址。与`-mcmodel=small`相比唯一的变动是相对寻址的指令由`mov`变成了`movabs`,可以进行更大范围的寻址(64bit)。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/11534776zvvrndb3pxqwsa.png) 图5:x86 ABI 中对于`-mcmodle=medium`、`-mcmodle=large`的描述 反观AArch64后端,其实相对PC寻址的指令和64bit的加法指令都是有的,甚至是64位的相对PC寻址方式在ABI中都是有的(如图6),缺少的是这种重定位方式。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/1153556gwxcjxe5z9bogjo.png) 图6: AArch64 ABI 中关于64位相对PC寻址的描述 ## -mcmodel=medium, -mlarge-data-threshold=n GCC for openEuler根据上述问题的痛点,新开发了`-mcmodel=medium`, `-mlarge-data-threshold=n`两个选项。此选项使能了32bit之外的动态取址操作。在使用`-mcmodel=medium`时,对于符号size大于`aarch64_data_threshold`的符号使用**通过mov序列来获取PC值的offset,再与PC值相加**的方式实现64bit的相对PC寻址,在地址无关选项打开时,可以实现64bit相对PC寻址,获取GOT表入口,并且通过mov序列+LDR方式获取符号。 说明:`aarch64_data_threshold`的默认值为2^16 = 65536,用户可以使用`-mlarge-data-threshold=n`选项指定大符号的阈值为n。 ### 举例 如图7所示,假设`foovar`的符号距离寻址指令的距离大于4GB,`-mcmodel=small`会使用ADRP+ADD指令进行符号拿取,而`foovar`在链接时计算距离的方式是如**使用方法**中的.bss+size方式,在链接时会报`relocation truncated to fit`错误。在此使用图8中的mov序列+PC寻址方式可将寻址范围扩大至64位,解决由于地址溢出导致的报错。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115415ma3rw5yimprdy7xc.png) 图7:smallcode model寻址方式 在这种模式下,通过`adrp`和`add`指令获取`foovar`的地址。`adrp`是PC相对寻址,它会把`foovar`的页地址偏移量与当前PC值相加,并存储到`x0`寄存器,下一条指令`add`把页内地址(低12位)再加到`x0`寄存器上,这样就得到了`foovar`的地址。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/1154194wi8hxhxv25zq8qi.png) 图8:mov序列+PC寻址方式 可以看出这里使用了`movz`, `movk`, `adr`, `sub`, `add`这样一系列的指令最终得到了`foovar`的地址。`movz`和3条`movk`指令的作用是把`foovar`的64位的偏移量分4次,每次转存16位,依次存放到了x0寄存器,`adr x8, .`的作用是获取当前的PC值,`sub`是对PC值做一些修正,然后`add`是把64位偏移量与修正后的PC值相加。这样就得到具有64位PC相对地址的`foovar`地址了。 ### 使用方法 用例: libdemo.cpp ```cpp libdemo.cpp #include <iostream> char arr[10][1*1024*1024*1024]; void set_and_print(){ arr[8][0]='A'; std::cout << arr[8][0] << std::endl; } ``` 上述代码定义了一个二维数组`arr`,第一维有10个元素,每一个元素又是一个总大小为1GB的字符数组。在访问`arr[8][0]`时需要偏移8GB,已经超过了`ADR`, `ADRP`这样的取值范围。 main.cpp ```cpp extern char arr[10][1*1024*1024*1024]; void set_and_print(); int main(){ set_and_print(); return 0; } ``` 主程序会使用共享库中的`set_and_print`函数。 现在我们来编译上面的代码。如下图所示,如果不指定`-mcmodel`参数就会报`relocation truncated to fit`错误;当程序指定`-mcmodel=large`时,又与-fpic冲突,无法生成动态共享库。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115429zarqzuytqgldk7nu.png) 图9:不指定`-mcmodel`,或者指定`-mcmodel=large`进行编译,编译失败 当我们使用GCC for openEuler开发的`-mcmodel=medium`和`-mlarge-data-threshold=1`后,动态共享库被成功创建了,主程序也能正常调用它,并且得到正确的结果。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115437xqpvju6swe765vqm.png) 图10:指定-mcmodel=medium -mlarge-data-threshold=1 进行编译,编译成功 我们对共享库进行反编译查看汇编代码就会看到,取址指令已经是movz, movk这样的序列了。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115459f5g4w3kuytygifs3.png) 编译命令: ```bash c++ libdemo.cpp -fpic -shared -o libdemo.so -mcmodel=medium -mlarge-data-threshold=1 c++ main.cpp libdemo.so -o main ``` 运行主程序: ```bash ./main ``` ## 总结 该选项通过软件模拟的方式,使用多条指令去模拟`movabs`指令,使得在HPC领域一些需要大范围地址无关寻址的应用能够平滑地从其他平台迁移到鲲鹏平台中来。 所以在GCC for openEuler使用过程中,若出现`relocation truncated to fit`错误,可以尝试添加编译选项`-mcmodel=medium -mlarge-data-threshold=1`解决。 时间问题暂时写到此处,后续会继续更新一些GCC for openEuler或者毕昇编译器相关优化选项的介绍,感兴趣的朋友敬请博客留言,也可以点击文末**阅读原文**进入GCC for openEuler网页下载使用GCC for openEuler。 ## 参考 [1] https://bbs.huaweicloud.com/blogs/272527 [2] https://www.hikunpeng.com/developer/devkit/compiler/gcc [3] https://gcc.gnu.org/onlinedocs/gcc/AArch64-Options.html ## 后记 欢迎加入Compiler SIG交流群与大家共同交流学习编译技术相关内容,扫码添加小助手微信邀请你进入Compiler SIG交流群。 ![image.png](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/115534xw07sqvtqm9iloxa.png) ------ 原文转载自[毕昇编译-GCC for openEuler -mcmodel选项详解](https://mp.weixin.qq.com/s/9OXRMg6xCTSdlhx_44BCvA) ![毕昇编译-二维码.jpg](https://bbs-img.huaweicloud.com/data/forums/attachment/forum/202112/25/1156583kgzvnhtfxuzozuy.jpg) 访问GCC for openEuler网页:https://www.hikunpeng.com/developer/devkit/compiler/gcc
  • [迁移系列] 【TD自定义函数】edit_distance_similarity(p1,p2)
    函数名称:edit_distance_similarity(p1,p2)函数使用方法: 1.1      编写函数代码1.2      使用gcc命令编译测试代码生成动态库gcc -shared -fpic -o edit_distance_similarity.so edit_distance_similarity.cpp -I /opt/huawei/Bigdata/mppdb/core/include/postgresql/server/注意:安装gcc版本要求在5.4.0或者5.4.0之上1.3      将edit_distance_similarity.so放到/opt/huawei/Bigdata/mppdb/core/lib/目录下1.4      自定义函数调用C函数登录数据库,创建创建c函数:create or replace function edit_distance_similarity(text,text)returns integeras '/opt/huawei/Bigdata/mppdb/core/lib/edit_distance_similarity.so','edit_distance_similarity'language c strict fenced IMMUTABLE;其中...../ edit_distance_similarity.so 指定了库文件的绝对路径1.5      查看自定义函数select pg_proc.proname,pg_type.typname,pg_proc.pronargs from pg_proc join pg_type on (pg_proc.prorettype = pg_type.oid) where pg_type.typname != 'void'and pronamespace = (select pg_namespace.oid from pg_namespace where nspname = 'public');1.6      调用自定义函数,测试结果如下:建表:create table test_pp (name varchar(100));插入数据:insert into test_pp values ('河北沧州东朔股份有限公司'),('河北沧州东朔集团有限公司'),('沧州东朔房地产开有限公司'),('沧州东朔房地产开发有限公司'),('沧州东朔房地产开发有发公司'),('东朔'),('斯大林'),('毛主席');测试执行语句:select * from (select '河北沧州东朔股份有限公司',name, edit_distance_similarity('河北沧州东朔股份有限公司',name) 相似度 from test_pp t) order by 相似度 desc; 测试结果与oracle执行sys.utl_match.edit_distance_similarity执行结果完全相同edit_distance_similarity.cpp文件:#include "postgres.h" #include "fmgr.h" #include "utils/builtins.h" PG_MODULE_MAGIC; extern "C" Datum edit_distance_similarity(PG_FUNCTION_ARGS); int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } int length(char * t) { int len=0; if(NULL == t) return 0; while(*t != '\0') { len++; t++; } return len; } int ld(char * s, char * t, int sLen, int tLen) { int si; int ti; char ch1; char ch2; int cost; int d[sLen+1][tLen+1]; if(NULL == s || NULL == t) return 0; if(sLen == 0) { return tLen; } if(tLen == 0) { return sLen; } for(si=0; si<=sLen; si++) { d[si][0] = si; } for(ti=0; ti<=tLen; ti++) { d[0][ti] = ti; } for(si=1; si<=sLen; si++) { ch1 = s[si-1]; for(ti=1; ti<=tLen; ti++) { ch2 = t[ti-1]; if(ch1 == ch2) { cost = 0; } else { cost = 1; } d[si][ti] = min(min(d[si-1][ti]+1, d[si][ti-1]+1),d[si-1][ti-1]+cost); } } return d[sLen][tLen]; } Datum edit_distance_similarity(PG_FUNCTION_ARGS) { int sLen=0; int tLen=0; int ldd=0; float tmp=0; int value=0; Datum source = PG_GETARG_DATUM(0); Datum target = PG_GETARG_DATUM(1); char *src=TextDatumGetCString(source); char *tar=TextDatumGetCString(target); sLen = length(src); tLen = length(tar); if(sLen==0 && tLen==0) return 100; ldd = ld(src, tar, sLen, tLen); tmp = 100*(1 - (double) ldd / max(length(src), length(tar))); value = (int)tmp; if(0 < (tmp - value)) value = value + 1; return value; } /* int main (void) { int a = edit_distance_similarity("河北沧州东朔股份有限公司","东朔"); printf("%d\n",a); return 1; } */ edit_distance_similarity1.cpp文件:#include "postgres.h" #include "fmgr.h" #include "utils/builtins.h" PG_MODULE_MAGIC; extern "C" Datum edit_distance_similarity(PG_FUNCTION_ARGS); /* int min(int a, int b) { return a < b ? a : b; } int max(int a, int b) { return a > b ? a : b; } int length(char * t) { int len=0; if(NULL == t) return 0; while(*t != '\0') { len++; t++; } return len; } int ld(char * s, char * t, int sLen, int tLen) { int si; int ti; char ch1; char ch2; int cost; int d[sLen+1][tLen+1]; if(NULL == s || NULL == t) return 0; if(sLen == 0) { return tLen; } if(tLen == 0) { return sLen; } for(si=0; si<=sLen; si++) { d[si][0] = si; } for(ti=0; ti<=tLen; ti++) { d[0][ti] = ti; } for(si=1; si<=sLen; si++) { ch1 = s[si-1]; for(ti=1; ti<=tLen; ti++) { ch2 = t[ti-1]; if(ch1 == ch2) { cost = 0; } else { cost = 1; } d[si][ti] = min(min(d[si-1][ti]+1, d[si][ti-1]+1),d[si-1][ti-1]+cost); } } return d[sLen][tLen]; } */ Datum edit_distance_similarity(PG_FUNCTION_ARGS) {/* int sLen=0; int tLen=0; int ldd=0; float tmp=0; int value=0; Datum source = PG_GETARG_DATUM(0); Datum target = PG_GETARG_DATUM(1); char *src=TextDatumGetCString(source); char *tar=TextDatumGetCString(target); sLen = length(src); tLen = length(tar); if(sLen==0 && tLen==0) return 100; ldd = ld(src, tar, sLen, tLen); tmp = 100*(1 - (double) ldd / max(length(src), length(tar))); value = (int)tmp; if(0 < (tmp - value)) value = value + 1; */ return 1; } /* int main (void) { int a = edit_distance_similarity("河北沧州东朔股份有限公司","东朔"); printf("%d\n",a); return 1; } */
  • [经验案例] Ascend 910(训练)快速入门及demo跑通
    推荐基础开发环境配置软件/硬件说明推荐配置/版本获取方式硬件支持ARM或者X86Atlas800-9000Atlas800-9010/操作系统Centos/UbuntuCentOS 7.6Ubuntu 18.04官网Driver驱动A800-9010-npu-driver_5.1.rc2_linux-x86_64.runA800-9010-npu-firmware_5.1.rc2.runA800-9000A800-9010官网一、安装昇腾驱动1、获取驱动驱动、CANN需要通过官网获取,以下例子,昇腾设备为:Atlas 800-9010,服务器为centos x86_64 7.6版本(CentOS-7-x86_64-DVD-1810.iso)驱动下载:cid:link_2获取的驱动文件:A800-9010-npu-driver_5.1.rc2_linux-x86_64.runA800-9010-npu-firmware_5.1.rc2.run2、环境搭建具体环境搭建,可以参考:cid:link_1(1)安装驱动./A800-9010-npu-driver_5.1.rc2_linux-x86_64.run --full./A800-9010-npu-firmware_5.1.rc2.run --full(2)配置Device网卡IP当用户想使用多卡环境进行分布式训练时, 需要提前配置设备的网卡IP,用于多个device间通信以实现网络模型参数的同步更新。首先在服务器BMC后台界面输入如下指令,确认系统为SMP架构还是AMP架构:ipmcget -d npuworkmode如上图所示,如果确认系统架构为SMP的情况下,可执行如下指令配置各网卡的IP:(以8卡环境为例)hccn_tool -i 0 -ip -s address 192.168.100.101 netmask 255.255.255.0hccn_tool -i 1 -ip -s address 192.168.101.101 netmask 255.255.255.0hccn_tool -i 2 -ip -s address 192.168.102.101 netmask 255.255.255.0hccn_tool -i 3 -ip -s address 192.168.103.101 netmask 255.255.255.0hccn_tool -i 4 -ip -s address 192.168.100.100 netmask 255.255.255.0hccn_tool -i 5 -ip -s address 192.168.101.100 netmask 255.255.255.0hccn_tool -i 6 -ip -s address 192.168.102.100 netmask 255.255.255.0hccn_tool -i 7 -ip -s address 192.168.103.100 netmask 255.255.255.0网卡IP配置要求:AI Server中的第0/4,1/5,2/6,3/7号网卡需处于同一网段,第0/1/2/3号网卡在不同网段,第4/5/6/7号网卡在不同网段对于集群场景,各AI Server对应的位置的device需处于同一网段,例如AI Server1和AI Server2的0号网卡需处于同一网段,AI Server1和AI Server2的1号网卡需处于同一网段。IP地址需要根据实际情况修改(3)安装完成后npu-smi info 显示安装成功二、安装docker如果用户需要使用AI框架(TensorFlow、PyTorch、MindSpore等)进行模型的训练操作,建议其配置docker环境,本样例主要展示在CentOS 7.6系统中安装docker的过程。首先清理系统原有的docker安装包:yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine然后安装docker需要的系统依赖包,并配置docker下载源(建议使用国内阿里源):sudo yum install -y yum-utils yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 接下来用户需要根据自己需求下载安装特定版本的docker,可以通过如下命令查看当前所有版本号:yum list docker-ce --showduplicates | sort -r比如本次用例需要下载18.06.3.ce-3.e17版本,则执行如下命令进行安装:sudo yum install docker-ce-18.06.3.ce-3.el7 docker-ce-cli-18.06.3.ce-3.el7 containerd.io最后确认docker是否安装成功:[root@k8s-master 22.0.2]# docker versionClient: Docker Engine - Community Version: 20.10.14 API version: 1.41 Go version: go1.16.15 Git commit: a224086 Built: Thu Mar 24 01:49:57 2022 OS/Arch: linux/amd64 Context: default Experimental: trueServer: Docker Engine - Community Engine: Version: 20.10.14 API version: 1.41 (minimum version 1.12) Go version: go1.16.15 Git commit: 87a90dc Built: Thu Mar 24 01:48:24 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.5.11 GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8 ascend: Version: 1.0.3 GitCommit: v1.0.3-0-gf46b6ba docker-init: Version: 0.19.0 GitCommit: de40ad0[root@k8s-master 22.0.2]#三、拉取AI框架训练docker通过 昇腾镜像仓库 拉取AI框架训练dockercid:link_7镜像拉取后,通过docker images确认一下[root@k8s-master 22.0.2]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEascendhub.huawei.com/public-ascendhub/mindspore-modelzoo 22.0.RC1-ubuntu18.04 1d2c528a90e3 4 months ago 7.53GBascendhub.huawei.com/public-ascendhub/pytorch-modelzoo 22.0.RC1 37fc4f811546 4 months ago 15GBascendhub.huawei.com/public-ascendhub/tensorflow-modelzoo 22.0.RC1-ubuntu18.04 f01b18f3bd8d 4 months ago 7.27GB[root@k8s-master 22.0.2]#四、基于MindSpore ResNet50训练Demo跑通本样例将展示ResNet50模型训练的全过程,下面将分别从数据准备、模型准备、启动训练任务、精度验证这四个方面来进行阐述。1、数据准备本样例使用的是Cifar10数据集,关于该数据集的详细介绍可参考:http://www.cs.toronto.edu/~kriz/cifar.html,用户可以直接从官网下载数据集到本地环境:wget http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gztar -xvf cifar-10-binary.tar.gz 接下来需要我们手动对cifar-10-binary目录下的文件进行整理,筛选出训练数据集和验证数据集,最终目录组织如下:cifar-10/├── readme.html├── test│ ├── batches.meta.txt│ └── test_batch.bin└── train ├── batches.meta.txt ├── data_batch_1.bin ├── data_batch_2.bin ├── data_batch_3.bin ├── data_batch_4.bin └── data_batch_5.bin本案例中,将数据集放在/data/atlas_dls/cifar-10/目录下2、ResNet50模型准备关于ResNet50模型的训练脚本我们可以直接从Ascend社区ModelZoo获得,建议大家直接下载modelzoo代码仓:MindSporecid:link_8Pytorchcid:link_5TensorFlowcid:link_4然后进入到models-master/official/cv/resnet目录下即可。本案例中,将代码仓放在/home/demo/train/目录下[root@k8s-master train]#[root@k8s-master train]# pwd/home/demo/train[root@k8s-master train]#[root@k8s-master train]# lsmodels-master models-master.zip[root@k8s-master train]#[root@k8s-master train]# cd models-master/official/cv/resnet[root@k8s-master resnet]#[root@k8s-master resnet]# pwd/home/demo/train/models-master/official/cv/resnet[root@k8s-master resnet]#3、训练任务启动MindSpore ModelZoo为所有模型提供了用于一键训练和验证的脚本,用户只需根据使用场景调用相应的脚本即可。在模型训练过程中,用户只需调用train.py脚本,分别传入--net、--dataset、--dataset-path参数的值即可。(1)通过工具hccl_tool自动生成配置表a. 下载hccl_tool工具cid:link_3b. 请确保物理环境已配置device的网卡IP。配置方式请参考对应版本《CANN 软件安装指南》中的“配置device的网卡IP”章节。cid:link_0c. 通过工具hccl_tool自动生成配置表,须在裸机上执行命令。请将生成的配置表拷贝到/data/atlas_dls/目录下。python3 hccl_tools.py --device_num "[0,8)"(2)docker启动在/home/demo/train/models-master/official/cv/resnet/scripts/目录下执行以下命令:bash ./docker_start.sh ascendhub.huawei.com/public-ascendhub/mindspore-modelzoo:22.0.RC1-ubuntu18.04 /data/atlas_dls/ /home/demo/train/# 参数1:docker镜像# 参数2:数据集目录# 参数3:模型脚本目录(3)启动单机八卡cifar10训练bash ./run_distribute_train.sh resnet50 cifar10 /data/atlas_dls/hccl_8p_01234567_127.0.0.1.json /data/atlas_dls/cifar-10/train/# 参数1:模型名称,名称可选值[resnet18|resnet50|resnet101|se-resnet50]。# 参数2:数据集名称,数据集可选值[cifar10|imagenet2012]。# 参数3:环境中多卡信息配置表路径。# 参数4:数据集路径(训练集)。(4)查看log.txt文件内容,如果出现如下信息,说明训练任务正常启动:(5)再启动一个控制台,在裸机上输入npu-smi info,HBM-Usage都不为零,说明八张卡都已经在跑训练服务。[root@k8s-master 22.0.2]# npu-smi info+-------------------------------------------------------------------------------------------+| npu-smi 22.0.2 Version: 22.0.2 |+----------------------+---------------+----------------------------------------------------+| NPU Name | Health | Power(W) Temp(C) Hugepages-Usage(page)|| Chip | Bus-Id | AICore(%) Memory-Usage(MB) HBM-Usage(MB) |+======================+===============+====================================================+| 0 910A | OK | 67.2 40 15 / 15 || 0 | 0000:61:00.0 | 0 758 / 15077 31129 / 32768 |+======================+===============+====================================================+| 1 910A | OK | 64.4 37 15 / 15 || 0 | 0000:DB:00.0 | 0 910 / 15077 31129 / 32768 |+======================+===============+====================================================+| 2 910A | OK | 63.7 37 15 / 15 || 0 | 0000:B2:00.0 | 0 1061 / 15077 31129 / 32768 |+======================+===============+====================================================+| 3 910A | OK | 62.9 42 15 / 15 || 0 | 0000:3E:00.0 | 0 3769 / 15077 31129 / 32768 |+======================+===============+====================================================+| 4 910A | OK | 66.2 41 15 / 15 || 0 | 0000:60:00.0 | 0 758 / 15077 31129 / 32768 |+======================+===============+====================================================+| 5 910A | OK | 62.7 36 15 / 15 || 0 | 0000:DA:00.0 | 0 1517 / 15077 31129 / 32768 |+======================+===============+====================================================+| 6 910A | OK | 67.3 37 15 / 15 || 0 | 0000:B1:00.0 | 0 1213 / 15077 31129 / 32768 |+======================+===============+====================================================+| 7 910A | OK | 68.5 40 15 / 15 || 0 | 0000:3D:00.0 | 0 3468 / 15077 31129 / 32768 |+======================+===============+====================================================+[root@k8s-master 22.0.2]#(6)训练结束后,本地会生成多个ckpt文件4、精度验证精度验证过程也非常简单,只需调用eval.py传入评估需要的参数--net、--dataset、--dataset_path以及--checkpoint_path即可,具体操作如下:(1)cifar10精度测试bash ./run_eval.sh resnet50 cifar10 /data/atlas_dls/cifar-10/test/ /home/demo/train/models-master/official/cv/resnet/scripts/train_parallel0/ckpt_0/resnet-90_195.ckpt# 参数1:模型名称,名称可选值[resnet18|resnet50|resnet101|se-resnet50]。# 参数2:数据集名称,数据集可选值[cifar10|imagenet2012]。# 参数3:数据集路径(验证集)。# 参数4:待验证的ckpt文件路径。(2)查看精度等待精度测试完后,在eva目录下cat log如上图所示,该模型的TOP1精度为93.1%,TOP5精度为99.7%到这里ResNet50模型训练的过程就结束了,用户可以拿着训练好的ckpt文件直接用MindSpore API执行推理,或者先导出成AIR格式的模型,然后通过atc工具转成可用于ACL离线推理的om模型。【注意】ModelZoo使用求助专区:cid:link_6
  • [问题求助] 鲲鹏GCC编译器里面的libhpc目录文件,在毕昇编译器下面对应的位置在哪里呢?谢谢
    【功能模块】SCALAPACK_LIBS=/path/to/SCALAPACK/libscalapack.a MATH_LIBS= -Wl,-rpath=/path/to/KPGCC/gcc-9.3.1-2020.12-aarch64-linux/lib64/libhpc鲲鹏GCC编译器里面的libhpc目录文件,在毕昇编译器下面对应的位置在哪里呢?谢谢/opt/compiler/bisheng-compiler-1.3.3-aarch64-linux/bin/【操作步骤&问题现象】1、2、【截图信息】【日志信息】(可选,上传日志内容或者附件)
  • [安装] gcc7.3.0make报错
    【功能模块】GCC7.5.0-->7.3.0安装【操作步骤&问题现象】wget http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gztar -zxvf gcc-7.3.0.tar.gzcd gcc-7.3.0./configure --prefix=/home/warten/mpc-1.1.0 --with-gmp=/home/warten/gmp-6.1.2 --with-mpfr=/home/warten/mpfr-4.1.0 --disable-multilibmake -j8【截图信息】【日志信息】(可选,上传日志内容或者附件)
  • [技术干货] Ubuntu安装华为云MindSpore的gcc7.3.0版本环境
    一、选择安装命令,我们学校要求是如此,根据实际要求而定二、查看环境要求2.1确定glibc的版本打开终端,输入:ldd --version  输出:ldd (Ubuntu GLIBC 2.27-3ubuntu1.4) 2.27Copyright (C) 2018 自由软件基金会。这是一个自由软件;请见源代码的授权条款。本软件不含任何没有担保;甚至不保证适销性或者适合某些特殊目的。由 Roland McGrath 和 Ulrich Drepper 编写。glibe>2.17自行确认ubuntu的版本,我的是18.042.2安装GCC 7.3.0版本这一块根据张小白教你如何在Ubuntu 18.04上源码安装MindSpore V1.0-云社区-华为云Part Two:源码安装gcc 7.3.0来做终端输入:sudo apt-get updatesudo apt-get install aptitudesudo aptitude install gccsudo apt install gccsudo apt-get install g++检查出gcc版本为7.5.0,而后开始愉快的安装7.3.0旅程  2.2.1 下载安装包安装gcc 7.3.0需要完成以下内容:mpc+gmp+mpfr+gcc根据我的实际需要将下载版本更改成了gmp-6.1.2.tar.bz2,mpfr-4.1.0.tar.xz,mpc-1.1.0.tar.gz​ # 到 http://www.multiprecision.org/mpc/download.html 网站下载 mpc-1.1.0.tar.gz wget http://www.multiprecision.org/downloads/mpc-1.1.0.tar.gz# 到 ftp://ftp.gnu.org/gnu/gmp/gmp-6.1.2.tar.bz2 下载gmp-6.1.2.tar.bz2 wget ftp://ftp.gnu.org/gnu/gmp/gmp-6.1.2.tar.bz2  ​ 下载mpfr-4.1.0.tar.xz:https://www.mpfr.org/mpfr-current/#download 手动移动到与gmp同一级的文件夹下。下载gcc 7.3.0:wget http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz所有的版本请自行更改   2.2.2 解压包    2.2.2.1 解压gmpbzip2 -d gmp-6.1.2.tar.bz2 tar xvf gmp-6.1.2.tar    2.2.2.2 解压mpfrxz -d mpfr-4.1.0.tar.xz tar xvf mpfr-4.1.0.tar    2.2.2.3 解压mpcgzip -d mpc*.gz tar xvf mpc*.tar    2.2.2.4 解压gcc同2.2.2.22.2.3 将包复制到 /usr/local/路径之下sudo cp -r gmp-6.1.2 /usr/local/sudo cp -r mpfr-4.1.0 /usr/local/sudo cp -r mpc-1.1.0 /usr/local/sudo cp -r gcc-7.3.0 /usr/local/查看列表qingzhuan@qingzhuan-System-Product-Name:~$ cd /usr/localqingzhuan@qingzhuan-System-Product-Name:/usr/local$ ls -l 2.2.4 安装包安装gmp:cd gmp-6.1.2./configure --prefix=/usr/local/gmp-6.1.2如果缺少什么东西,按照提示安装就好,再进行一遍configure再往下makemake checksudo make installgmp安装完毕安装mpfr:cd mpfr-4.1.0./configure --prefix=/usr/local/mpfr-4.1.0 --with-gmp=/usr/local/gmp-6.1.2makemake checksudo make install安装mpccd mpc-1.1.0./configure --prefix=/usr/local/mpc-1.1.0 --with-gmp=/usr/local/gmp-6.1.2 --with-mpfr=/usr/local/mpfr-4.1.0makemake checksudo make install 2.2.5 配置库环境在/etc/ld.so.conf添加以下行/usr/local/gmp-6.1.2/lib/usr/local/mpfr-4.1.0/lib/usr/local/mpc-1.1.0/lib这时候发现/ld.so.conf只读,于是更改成可读写就可以了先进入超级管理员模式su更改文件读写属性:chmod 666 /etc/ld.so.conf打开文件添加:/usr/local/gmp-6.1.2/lib/usr/local/mpfr-4.1.0/lib/usr/local/mpc-1.1.0/lib 保存退出,环境配置完成。2.2.6 安装gcccd gcc-7.3.0./configure --with-gmp=/usr/local/gmp-6.1.2 --with-mpc=/usr/local/mpc-1.1.0 --with-mpfr=/usr/local/mpfr-4.1.0 --disable-multilibmakemake checksudo make installmake的时间比较长,要耐心一点~接下来将/usr/bin/gcc 和g++重新指向 /usr/local/bin/gcc和g++提前进入超级用户模式:root@ubuntu:/usr/bin# cp gcc gcc7.5root@ubuntu:/usr/bin# cp g++ g++7.5root@ubuntu:/usr/bin# rm gccroot@ubuntu:/usr/bin# rm g++ root@ubuntu:/usr/bin# ln -s /usr/local/bin/gcc gccroot@ubuntu:/usr/bin# ln -s /usr/local/bin/g++ g++root@ubuntu:/usr/bin# ls -lrt gcclrwxrwxrwx 1 root root 18 Oct  2 18:54 gcc -> /usr/local/bin/gccroot@ubuntu:/usr/bin# ls -lrt g++lrwxrwxrwx 1 root root 18 Oct  2 18:54 g++ -> /usr/local/bin/g++检查一下版本:qingzhuan@qingzhuan-System-Product-Name:~$ gcc --versiongcc (GCC) 7.3.0Copyright © 2017 Free Software Foundation, Inc.本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;包括没有适销性和某一专用目的下的适用性担保。qingzhuan@qingzhuan-System-Product-Name:~$ g++ --versiong++ (GCC) 7.3.0Copyright © 2017 Free Software Foundation, Inc.本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;包括没有适销性和某一专用目的下的适用性担保。gcc 7.3.0安装完成————————————————原文链接:https://blog.csdn.net/jiduqiulianga/article/details/120869940
  • [问题求助] bearpi-hm_nano环境搭建过程中GCC_RISCV超链接点了无法下载。复制超链接也下载失败
    【功能模块】【操作步骤&问题现象】1、2、【截图信息】【日志信息】(可选,上传日志内容或者附件)
  • [问题求助] 使用鲲鹏性能分析工具在数据库场景进行热点函数分析,如何增加或修改GCC编译选项?
    【功能模块】【操作步骤&问题现象】1、2、【截图信息】GCC版本为7.3.0【日志信息】(可选,上传日志内容或者附件)
  • [问题求助] sudo: aarch64-himix100-linux-gcc: command not found
    Hi3559A V100R001C02SPC030 SDK包osdrv编译时提示如下:编译器是有安装的当加上sudo又提示如下:请问这是什么原因?
  • [技术干货] mindspore1.3.0的编译环境之GCC7.3.0的安装
    下载gcc 7.3.0版本安装包,执行以下命令:              wget  http://ftp.gnu.org/gnu/gcc/gcc-7.3.0/gcc-7.3.0.tar.gz执行tar -xzf gcc-7.3.0.tar.gz解压源码包。执行cd gcc-7.3.0,进入到源码包目录。继续下面操作前清空系统内的环境变量:export LIBRARY_PATH=export LD_LIBRARY_PATH= export C_INCLUDE_PATH= export CPLUS_INCLUDE_PATH=运行以下命令,进行安装前的配置。安装依赖环境:./contrib/download_prerequisites配置环境:./configure --enable-bootstrap -enable-threads=posix --enable-checking=release --enable-languages=c,c++ --disable-multilib 编译安装:make -j8 && sudo make install原文链接:https://blog.csdn.net/skytttttt9394/article/details/119571179?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522163063161516780265459602%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=163063161516780265459602&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v29_ecpm-8-119571179.pc_search_result_control_group&utm_term=MindSpore&spm=1018.2226.3001.4187
  • [openEuler] 基于openEuler-20.03-LTS-SP1安装部署Codeviz4.6.2
    操作系统和软件介绍操作系统为openEuler 20.03 (LTS-SP1)   可用如下命令查询:cat /etc/os-release uname -iCodeViz介绍CodeViz是《Understanding The Linux Virtual Memory Manager》的作者 Mel Gorman 写的一款分析C/C++源代码中函数调用关系的open source工具(类似的open source软件有 egypt、ncc)。其基本原理是给 GCC 打个补丁(如果你的gcc版本不符合它的要求还得先下载正确的gcc版本),让它在编译每个源文件时 dump 出其中函数的 call graph,然后用 Perl 脚本收集并整理调用关系,转交给Graphviz绘制图形(Graphviz属于后端,CodeViz属于前端)。Graphviz + CodeViz编译安装1. 安装 GraphViz调用图的生成依赖于 GraphViz,所以首先要安装 GraphViz。可以下载源码包编译、安装。可以直接yum安装:[root@1ocalhost ~]# yum install graphviz2. 安装 CodeViz下载CodeVize源码包: 很多源都失效了,还好下面这个还在 https://github.com/petersenna/codeviz 进入/root目录下载[root@1ocalhost ~]# cd /root/ [root@1ocalhost ~]# wget https://github.com/petersenna/codeviz/archive/refs/heads/master.zip --2021-08-24 00:12:07-- https://github.com/petersenna/codeviz/archive/refs/heads/master.zip Resolving github.com (github.com)... 52.74.223.119 Connecting to github.com (github.com)|52.74.223.119|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://codeload.github.com/petersenna/codeviz/zip/refs/heads/master [following] --2021-08-24 00:12:10-- https://codeload.github.com/petersenna/codeviz/zip/refs/heads/master Resolving codeload.github.com (codeload.github.com)... 20.205.243.165 Connecting to codeload.github.com (codeload.github.com)|20.205.243.165|:443... connected. HTTP request sent, awaiting response... 200 OK Length: unspecified [application/zip] Saving to: ‘master.zip’ master.zip [ <=> ] 2.62M 5.12MB/s in 0.5s 2021-08-24 00:12:11 (5.12 MB/s) - ‘master.zip’ saved [2753095]复制一份并解压[root@1ocalhost ~]# cp master.zip codeviz.zip [root@1ocalhost ~]# unzip codeviz.zip 进入解压后的目录:[root@1ocalhost ~]# cd codeviz-master/CodeViz 使用了一个 patch 版本的 GCC 编译器,而且不同的 CodeViz 版本使用的GCC 版本也不同,可以下载 CodeViz 的源码包后查看 Makefile 文件来确定要使用的 GCC 版本,codeviz-1.0.12 使用 GCC-4.6.2。实际上安装 CodeViz 时安装脚本make会检查当前的GCC版本如果不符合则会自动下载对应的 GCC并打 patch,但由于GCC较大如果网速不好且在虚拟机中的话容易下载失败或系统错误什么的,因此这里我们还是分步安装比较好,先安装gcc再回来安装 CodeViz。1)安装 GCC下载gcc-4.6.2.tar.gz到/root/codeviz-master目录下的compilers里。下载地址:ftp://ftp.gnu.org/pub/gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.gz[root@1ocalhost codeviz-master]# cd compilers/ [root@1ocalhost compilers]# wget ftp://ftp.gnu.org/pub/gnu/gcc/gcc-4.6.2/gcc-4.6.2.tar.gzCodeViz 的安装脚本 compilers/install_gcc-4.6.2.sh 会自动检测 compilers 目录下是否有 gcc 的源码包,若没有则自动下载并打 patch。这里前面已经下载,则剩下的就是解压安装了。install_gcc-3.4.6.sh 会解压缩 gcc打 patch,并将其安装到指定目录。安装: ./install_gcc-4.6.2.sh[root@1ocalhost compilers]# ./install_gcc-4.6.2.sh注意:这里可能安装时有些错误,具体错误及解决方案见文末后面。 GMP, MPFR, MPC这三个库安装好之后,我们修改一下install_gcc-4.6.2.sh文件,在# Configure and compile的地方指定三个库安装路径:--with-gmp=/usr/local/gmp-4.3.2/ --with-mpfr=/usr/local/mpfr-2.4.2/ --with-mpc=/usr/local/mpc-0.8.1/ [root@1ocalhost compilers]# vim install_gcc-4.6.2.sh 再次安装: ./install_gcc-4.6.2.sh 需要耐心等待一段时间。[root@1ocalhost compilers]# ./install_gcc-4.6.2.sh这次报错,是由于缺少32位兼容包。/usr/include/gnu/stubs.h:7:27: fatal error: gnu/stubs-32.h: No such file or directory解决办法:[root@1ocalhost compilers]# wget http://www.rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/glibc-devel-2.17-317.el7.i686.rpm --2021-08-24 17:37:54-- http://www.rpmfind.net/linux/centos/7.9.2009/os/x86_64/Packages/glibc-devel-2.17-317.el7.i686.rpm Resolving www.rpmfind.net (www.rpmfind.net)... 195.220.108.108 Connecting to www.rpmfind.net (www.rpmfind.net)|195.220.108.108|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1128200 (1.1M) [application/x-rpm] Saving to: ‘glibc-devel-2.17-317.el7.i686.rpm’ glibc-devel-2.17-317.el7.i686 100%[=================================================>] 1.08M 694KB/s in 1.6s 2021-08-24 17:37:59 (694 KB/s) - ‘glibc-devel-2.17-317.el7.i686.rpm’ saved [1128200/1128200] [root@1ocalhost compilers]# rpm -i --force --nodeps glibc-devel-2.17-317.el7.i686.rpm warning: glibc-devel-2.17-317.el7.i686.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY/usr/include/gnu/下查看,文件stubs-32.h已经存在安装过程发现还需要做如下修改:/root/codeviz-master/compilers/gcc-graph/gcc-4.6.2/gcc/config/i386/linux-unwind.h文件中的struct siginfo 改成 siginfo_t,struct ucontext 改为 ucontext_t将struct ucontext *uc_ = context->cfa; 改为 struct ucontext_t *uc_ = context->cfa;[root@1ocalhost compilers]# vi /root/codeviz-master/compilers/gcc-graph/gcc-4.6.2/gcc/config/i386/linux-unwind.h 设置环境变量:vi /etc/profile export LD_LIBRARY_PATH=/usr/local/gmp-4.3.2/lib:/usr/local/mpfr-2.4.2/lib:/usr/local/mpc-0.8.1/lib //使其生效即可 source /etc/profile最后一次安装使用如下命令:[root@1ocalhost gcc-4.6.2]# cd /root/codeviz-master/compilers/gcc-graph/gcc-4.6.2 [root@1ocalhost gcc-4.6.2]# ./configure --prefix=/usr/local/gcc-4.6.2 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-4.3.2 --with-mpfr=/usr/local/mpfr-2.4.2 --with-mpc=/usr/local/mpc-0.8.1make终于编译成功了,效果图入下:makemake install gcc4.6.2安装成功如下 /usr/local/gcc-4.6.2/bin/gcc -v Using built-in specs. COLLECT_GCC=/usr/local/gcc-4.6.2/bin/gcc COLLECT_LTO_WRAPPER=/usr/local/gcc-4.6.2/libexec/gcc/x86_64-unknown-linux-gnu/4.6.2/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: ./configure --prefix=/usr/local/gcc-4.6.2 --enable-threads=posix --disable-checking --disable-multilib --enable-languages=c,c++ --with-gmp=/usr/local/gmp-4.3.2 --with-mpfr=/usr/local/mpfr-2.4.2 --with-mpc=/usr/local/mpc-0.8.1 Thread model: posix gcc version 4.6.2 (GCC) 2)安装 Codeviz4.6.2[root@1ocalhost gcc-4.6.2]# cd /root/codeviz-master/ [root@1ocalhost codeviz-master]# ./configure && make install-codevizchecking for dot.../usr/bin/dot checking for make.../usr/bin/make checking for gcc.../usr/bin/gcc checking dot can generate graphs...yes checking perl library path.../usr/local/lib64/perl5 Creating Makefile Creating testsuite/c/Makefile Configured. Running "make" will compile a patched version of gcc suitable for use with codeviz. Running "make install" when it completes will install the patched version of gcc to and the other codeviz scripts under /usr/local. When compiling other programs for use with codeviz, make sure you use the patched version of gcc. For example, to build a project based on C, the following should work; make CC=/bin/gcc Configuration: PREFIX = /usr/local GCC version to patch = 4.6.2 Patched GCC install path = /usr/local/gccgraph mkdir -p /usr/local/bin cp /root/codeviz-master/bin/* /usr/local/bin mkdir -p /usr/local/lib64/perl5 cp -rv lib/CodeViz /usr/local/lib64/perl5 'lib/CodeViz/Collect.pm' -> '/usr/local/lib64/perl5/CodeViz/Collect.pm' 'lib/CodeViz/CollectCNcc.pm' -> '/usr/local/lib64/perl5/CodeViz/CollectCNcc.pm' 'lib/CodeViz/CollectCObjdump.pm' -> '/usr/local/lib64/perl5/CodeViz/CollectCObjdump.pm' 'lib/CodeViz/CollectCXref.pm' -> '/usr/local/lib64/perl5/CodeViz/CollectCXref.pm' 'lib/CodeViz/CollectCppDepn.pm' -> '/usr/local/lib64/perl5/CodeViz/CollectCppDepn.pm' 'lib/CodeViz/CollectPPStack.pm' -> '/usr/local/lib64/perl5/CodeViz/CollectPPStack.pm' 'lib/CodeViz/Format.pm' -> '/usr/local/lib64/perl5/CodeViz/Format.pm' 'lib/CodeViz/Graph.pm' -> '/usr/local/lib64/perl5/CodeViz/Graph.pm' 'lib/CodeViz/IPC.pm' -> '/usr/local/lib64/perl5/CodeViz/IPC.pm' 'lib/CodeViz/Layout.pm' -> '/usr/local/lib64/perl5/CodeViz/Layout.pm' 'lib/CodeViz/Output.pm' -> '/usr/local/lib64/perl5/CodeViz/Output.pm' 'lib/CodeViz/PPCStack.pm' -> '/usr/local/lib64/perl5/CodeViz/PPCStack.pm' 'lib/CodeViz/PPOprofile.pm' -> '/usr/local/lib64/perl5/CodeViz/PPOprofile.pm' 'lib/CodeViz/PPStack.pm' -> '/usr/local/lib64/perl5/CodeViz/PPStack.pm' 'lib/CodeViz/SourceMarkup.pm' -> '/usr/local/lib64/perl5/CodeViz/SourceMarkup.pm' 'lib/CodeViz/VRML.pm' -> '/usr/local/lib64/perl5/CodeViz/VRML.pm'不需要 make ,因为make的作用就是检测是否有gcc若没有则下载源码包,所以这里只要安装 codeviz 即可。具体查看 Makefile 文件。注意:这里为什么不是通常用的make install,因为这里make install的作用是先安装gcc再安装codeviz,而前面已经安装了 gcc,所以这里只需要安装 codeviz ,即make install-codeviz脚本,该脚本也就是将genfull 和 gengraph 复制到/usr/local/bin 目录下。目前为止,CodeViz 安装完成了。安装过程出现的错误及解决方案1. 在运行./install_gcc-4.6.2.sh时出现下面错误:gcc configure: error: Building GCC requires GMP 4.2+, MPFR 2.3.1+ and MPC 0.8.0+从错误中可以看出:GCC编译需要GMP, MPFR, MPC这三个库(有的系统已经安装了就没有这个提示,我的没有安装),gcc 源码包中自带了一个gcc依赖库安装脚本download_prerequisites,位置在gcc源码目录中的 contrib/download_prerequisites,因此只需要进入该目录,直接运行脚本安装即可:. /download_prerequisitescd /root/codeviz-master/compilers/gcc-graph/gcc-4.6.2/contrib ./download_prerequisites[root@1ocalhost contrib]# pwd /root/codeviz-master/compilers/gcc-graph/gcc-4.6.2/contrib [root@1ocalhost contrib]# ./download_prerequisites --2021-08-24 01:03:37-- ftp://gcc.gnu.org/pub/gcc/infrastructure/mpfr-2.4.2.tar.bz2 => ‘mpfr-2.4.2.tar.bz2’ Resolving gcc.gnu.org (gcc.gnu.org)... 8.43.85.97, 2620:52:3:1:0:246e:9693:128c Connecting to gcc.gnu.org (gcc.gnu.org)|8.43.85.97|:21... connected. Logging in as anonymous ... Logged in! ==> SYST ... done. ==> PWD ... done. ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done. ==> SIZE mpfr-2.4.2.tar.bz2 ... 1077886 ==> PASV ... done. ==> RETR mpfr-2.4.2.tar.bz2 ... done. Length: 1077886 (1.0M) (unauthoritative) mpfr-2.4.2.tar.bz2 100%[================================>] 1.03M 20.2KB/s in 84s 2021-08-24 01:05:09 (12.6 KB/s) - ‘mpfr-2.4.2.tar.bz2’ saved [1077886] --2021-08-24 01:05:09-- ftp://gcc.gnu.org/pub/gcc/infrastructure/gmp-4.3.2.tar.bz2 => ‘gmp-4.3.2.tar.bz2’ Resolving gcc.gnu.org (gcc.gnu.org)... 8.43.85.97, 2620:52:3:1:0:246e:9693:128c Connecting to gcc.gnu.org (gcc.gnu.org)|8.43.85.97|:21... connected. Logging in as anonymous ... Logged in! ==> SYST ... done. ==> PWD ... done. ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done. ==> SIZE gmp-4.3.2.tar.bz2 ... 1897483 ==> PASV ... done. ==> RETR gmp-4.3.2.tar.bz2 ... done. Length: 1897483 (1.8M) (unauthoritative) gmp-4.3.2.tar.bz2 100%[================================>] 1.81M 14.0KB/s in 2m 17s 2021-08-24 01:07:32 (13.5 KB/s) - ‘gmp-4.3.2.tar.bz2’ saved [1897483] --2021-08-24 01:07:32-- ftp://gcc.gnu.org/pub/gcc/infrastructure/mpc-0.8.1.tar.gz => ‘mpc-0.8.1.tar.gz’ Resolving gcc.gnu.org (gcc.gnu.org)... 8.43.85.97, 2620:52:3:1:0:246e:9693:128c Connecting to gcc.gnu.org (gcc.gnu.org)|8.43.85.97|:21... connected. Logging in as anonymous ... Logged in! ==> SYST ... done. ==> PWD ... done. ==> TYPE I ... done. ==> CWD (1) /pub/gcc/infrastructure ... done. ==> SIZE mpc-0.8.1.tar.gz ... 544950 ==> PASV ... done. ==> RETR mpc-0.8.1.tar.gz ... done. Length: 544950 (532K) (unauthoritative) mpc-0.8.1.tar.gz 100%[================================>] 532.18K 27.0KB/s in 18s 2021-08-24 01:07:55 (28.8 KB/s) - ‘mpc-0.8.1.tar.gz’ saved [544950] [root@1ocalhost contrib]# PS:该脚本内容如下: 1234567891011121314151617181920212223242526272829303132333435363738#! /bin/sh # Download some prerequisites needed by gcc.# Run this from the top level of the gcc source tree and the gcc# build will do the right thing.## (C) 2010 Free Software Foundation## This program 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.## This program 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 this program. If not, see http://www.gnu.org/licenses/. MPFR=mpfr-2.4.2GMP=gmp-4.3.2MPC=mpc-0.8.1 wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2 || exit 1tar xjf $MPFR.tar.bz2 || exit 1ln -sf $MPFR mpfr || exit 1 wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$GMP.tar.bz2 || exit 1tar xjf $GMP.tar.bz2  || exit 1ln -sf $GMP gmp || exit 1 wget ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPC.tar.gz || exit 1tar xzf $MPC.tar.gz || exit 1ln -sf $MPC mpc || exit 1 rm $MPFR.tar.bz2 $GMP.tar.bz2 $MPC.tar.gz  exit 1可见是通过wget的方式下载安装,因此如果没有安装wget则需要先安装下。大家仔细看下这个脚本,发现非常简单,就是从网上自动下载三个依赖库并解压,然后建立三个改名后的软链接分别指向这三个库,这里建立软链接过程中也可能出错,具体看问题2这里我们自行安装,根据如下顺序分别安装GMP,MPFR和MPC(mpfr依赖gmp,mpc依赖gmp和mpfr)安装gmp: 编译gmp时需要m4文件,这里我们提前安装m4[root@1ocalhost contrib]# yum install m4 Last metadata expiration check: 0:23:04 ago on Tue 24 Aug 2021 10:56:09 AM CST. Dependencies resolved. ================================================================================================== Package Architecture Version Repository Size ================================================================================================== Installing: m4 x86_64 1.4.18-13.oe1 OS 90 k Transaction Summary ================================================================================================== Install 1 Package Total download size: 90 k Installed size: 203 k Is this ok [y/N]: y Downloading Packages: m4-1.4.18-13.oe1.x86_64.rpm 97 kB/s | 90 kB 00:00 -------------------------------------------------------------------------------------------------- Total 97 kB/s | 90 kB 00:00 Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : m4-1.4.18-13.oe1.x86_64 1/1 Verifying : m4-1.4.18-13.oe1.x86_64 1/1 Installed: m4-1.4.18-13.oe1.x86_64 Complete![root@1ocalhost contrib]# cd gmp-4.3.2/ [root@1ocalhost gmp-4.3.2]# ./configure --prefix=/usr/local/gmp-4.3.2make install安装mpfr:[root@1ocalhost gmp-4.3.2]# cd ../mpfr-2.4.2/ [root@1ocalhost mpfr-2.4.2]# pwd /root/codeviz-master/compilers/gcc-graph/gcc-4.6.2/contrib/mpfr-2.4.2 [root@1ocalhost mpfr-2.4.2]# ./configure --prefix=/usr/local/mpfr-2.4.2 --with-gmp=/usr/local/gmp-4.3.2/make install安装mpc: [root@1ocalhost mpfr-2.4.2]# cd ../mpc-0.8.1/ [root@1ocalhost mpc-0.8.1]# pwd /root/codeviz-master/compilers/gcc-graph/gcc-4.6.2/contrib/mpc-0.8.1 [root@1ocalhost mpc-0.8.1]# ./configure --prefix=/usr/local/mpc-0.8.1 --with-gmp=/usr/local/gmp-4.3.2/ --with-mpfr=/usr/local/mpfr-2.4.2/make install2.  解决ln -s 软链接产生Too many levels of symbolic links错误从网上查找了一下原因,原来是建立软连接的时候采用的是相对路径,所以才会产生这样的错误,解决方式是采用绝对路径建立软链接:这样问题就解决了。
总条数:130 到第
上滑加载中