 | 级别: 中级 魏 琼 (weiqiong@cn.ibm.com), 软件工程师, IBM 中国软件开发中心
2007 年 3 月 26 日 更新 2008 年 6 月 27 日 在普通的 PC 机上就可以模拟出 Cell 多核处理器的环境,进行程序开发和编译运行。本文主要介绍在 x86 机器上搭建 Cell 编程环境及其向量化编程的实现,最后介绍了如何做简单的性能测试。
简介
Cell 处理器是由Sony-Toshiba-IBM共同开发的高效能微处理晶片,具备强大的运算效能。Cell 有一个主 PowerPC 处理核心(PPE),八个专用辅助处理核心(SPE),每个都能够运行自己的小程序。我们可以在 Fedora Core7 上安装 Cell 的 SDK3.0 包来模拟 Cell 的编译和运行环境。在 SDK 环境下编译出来的程序,可以直接copy到真实的 Cell Blade Server上正常运行。
安装 SDK 3.0
首先,需要安装好 Fedora Core7 及配置好其 gnome 界面。查看是否有如下程序包,如果没有,yum install安装:# yum install rsync sed tcl wget
然后,从 IBM 的 developworks 网站http://www-128.ibm.com/developerworks/power/cell/ 下载 SDK 的 iso 和 rpm 包,一起是 4 个文件。其中两个文件是基本库: cell-install-3.0.0-1.0.noarch.rpm 和 CellSDK-Devel-Fedora_3.0.0.1.0.iso 。另外两个文件是扩展的一些库:cell-extras-Fedora-license-3.0.0-2.0.noarch.rpm 和 CellSDK-Extras-Fedora_3.0.0.1.0.iso 。安装步骤如下:
[root@wq_desktop ~]# mkdir -p /root/sdk3.0
[root@wq_desktop ~]# cd /root/sdk3.0
[root@wq_desktop sdk3.0]# rpm -ivh cell-install-3.0.0-1.0.noarch.rpm
warning: cell-install-3.0.0-1.0.noarch.rpm: Header V3 DSA
signature: NOKEY, key ID 9ac02885
Preparing... ########################################### [100%]
1:cell-install ########################################### [100%]
[root@wq_desktop sdk3.0]# cd /opt/cell
[root@wq_desktop cell]# ./cellsdk --gui --iso /root/sdk3.0 install
cellsdk INFO-2023: Trying to install or update cell-product-license
cellsdk INFO-2015: Calling pirut
Loading "installonlyn" plugin
|
接着会出现软件包安装提示界面(图1):
图1:SDK3.0软件包安装
提示“软件安装已完成”后,退出安装界面,在 console 中运行:
# /opt/cell/cellsdk_sync_simulator install
这个安装的时候有些慢,最后出现“Installation of RPMs into Simulator sysroot image is complete.”。缺省只是安装部分必须的 rpm 包,如果还需要安装 xlc 和其他 Cell 编程的扩展高级库,可以自己将那两个 iso 文件 mount –o loop 到某个目录下安装。安装完后的目录结构如下:
| 目录 | 内容 | 备注 |
|---|
| /opt/cell/sdk/src | 例子程序 | | | /opt/cell/sdk/prototype/src | 使用ALF的例子程序 | | | /opt/cell/sdk/docs | pdf文档 | 包含Cell编程手册 | | /opt/ibm/systemsim-cell | mambo有关的东东 | 包括了一个小的Linux操作系统 | | /opt/cell/toolchain | 编译器和函数库 | |
运行 mambo
安装完 SDK 后,如下运行 mambo:
[root@wq_desktop ~]# export PATH=/opt/ibm/systemsim-cell/bin:$PATH
[root@wq_desktop ~]# systemsim -g
GUI Enabled
Licensed Materials - Property of IBM.
(C) Copyright IBM Corporation 2001, 2007
All Rights Reserved.
Using initial run script /opt/ibm/systemsim-cell/bin/../lib/cell/systemsim.tcl
GUI not initialized. Execute tcl command 'gui_init'.
building tree....
clearing existing Openfirmware tree
done building tree.
LOAD : Opening ELF image file: /opt/ibm/systemsim-cell/bin/../images/cell/vmlinux
Elf text start address saved is 0x0000000001000000
Elf_ReadImage: Opening ELF image file: /opt/ibm/systemsim-cell/bin/../images/cell/vmlinux
Elf_ReadImage: alloc-ed 8522472 bytes for
/opt/ibm/systemsim-cell/bin/../images/cell/vmlinux
LOAD : ELF startup: PC=0x0000000001000000, msr=0x1000000000000000
LOAD : gpr[1]=0x000000000FFFFF90, gpr[2]=0x0000000000000000
systemsim %
|
得到 mambo 运行后的界面(图2):
图 2:mambo 运行
点击图2界面上“Mode”,选择“Fast Mode”,然后点击“Go”按钮,得到如下Linux操作系统启动后的界面(图3),因为是启动了一个独立的 Linux 操作系统,所以有些慢:
图 3:Linux操作系统运行
简单编程实现
SDK3.0 中的 SPE 运行管理库完全摒弃了 libspe1.2 而采用了 libspe2。我们创建一个 test 目录来做一下 Cell 的简单编程,该 test 程序实现用 PPU 调度 SPU 计算 4 个整数乘 4 个浮点数的结果:
[root@wq_desktop ~]# mkdir -p test test/ppu test/spu
[root@wq_desktop ~]# cd test
[root@wq_desktop test]# vi Makefile
[root@wq_desktop test]# vi ppu/Makefile
[root@wq_desktop test]# vi spu/Makefile
[root@wq_desktop test]# vi ppu/test_p.c
[root@wq_desktop test]# vi spu/test_spu.c
[root@wq_desktop test]# make
|
3 个 Makefile 文件和两个 .c 文件的内容如下:
清单 1:3 个 Makefile 文件和两个 .c 文件
[root@wq_desktop test]# more Makefile
DIRS = spu ppu
include /opt/cell/sdk/buildutils/make.footer
[root@wq_desktop test]# more ppu/Makefile
PROGRAM_ppu := ../test
IMPORTS = ../spu/libtest_spu.a -lspe2 -lpthread
include /opt/cell/sdk/buildutils/make.footer
[root@wq_desktop test]# more spu/Makefile
PROGRAMS_spu := test_spu
LIBRARY_embed := libtest_spu.a
include /opt/cell/sdk/buildutils/make.footer
[root@wq_desktop test]# more ppu/test_p.c
#include <pthread.h>
#include <libspe2.h>
#define SPU_THREADS 2
extern spe_program_handle_t test_spu;
void *ppu_pthread_function(void *arg)
{
spe_context_ptr_t ctx;
unsigned int entry = SPE_DEFAULT_ENTRY;
ctx = *((spe_context_ptr_t *)arg);
if (spe_context_run(ctx, &entry, 0, NULL, NULL, NULL) < 0) {
perror ("Failed running context");
return NULL;
}
pthread_exit(NULL);
}
int main ( )
{
int i;
spe_context_ptr_t ctxs[SPU_THREADS];
pthread_t threads[SPU_THREADS];
for(i=0; i<SPU_THREADS; i++)
{
if ((ctxs[i] = spe_context_create (0, NULL)) == NULL)
{
perror ("Failed creating context");
return -1;
}
if (spe_program_load (ctxs[i], &test_spu))
{
perror ("Failed loading program");
return -1;
}
if (pthread_create (&threads[i], NULL, &ppu_pthread_function, &ctxs[i]))
{
perror ("Failed creating thread");
return -1;
}
}
for (i=0; i<SPU_THREADS; i++)
{
if (pthread_join (threads[i], NULL))
{
perror("Failed pthread_join");
return -1;
}
}
return (0);
}
[root@wq_desktop test]# more spu/test_spu.c
#include <stdio.h>
int main()
{
int a[4] = {120, 230, 580, 970};
float b[4] = {1.8, 1.98, 0.68, 0.7};
int c[4] = {0};
int i = 0;
for( i = 0; i < 4; i ++ )
{
c[i] = a[i] * b[i];
printf("c[%d] = %d\n", i, c[i]);
}
return 0;
}
|
在 test 目录下生成一个 test 可执行文件,需要将此文件 copy 到 mambo 环境里的 Linux 中才能运行,在 mambo 中的 console 窗口键入如下指令(图4):
图 4:在 mambo 的 Linux 中运行程序
向量化编程实现
SPU 强大的计算性能需要数据的向量化,才能最好的发挥出来,我们接着将刚才的程序向量化:
[root@localhost test]# cp spu/test_spu.c spu/test_spu.c.bak
[root@localhost test]# vi spu/test_spu.c
|
test_spu.c 文件内容如下:
清单 2:test_spu.c 文件
[root@localhost test]# more spu/test_spu.c
#include <stdio.h>
int c[4] __attribute__ ((aligned (128)));
int main()
{
vector signed int a = {120, 230, 580, 970};
vector float b = {1.8, 1.98, 0.68, 0.7};
vector signed int cVec;
vector float tmp;
int i = 0;
tmp = spu_convtf( a, 0 );
tmp = spu_mul( tmp, b );
cVec = spu_convts( tmp, 0 );
*(vector signed int *)c = cVec;
for( i = 0; i < 4; i ++ )
{
printf("c[%d] = %d\n", i, c[i]);
}
return 0;
}
|
简要性能分析
mambo 可以精确地计算出 SPU 上程序运行时 CPU Cycle 数,据此我们可以对自己的程序不断的做性能调优。如果要求 mambo 精确的统计出 SPU 上程序的 cycle 数,点击 mambo 上的“Mode”按钮,然后选择“Cycle Models”,如下界面(图5)配置好 mambo:
图 5:SPU Mode 配置
配置好后,关闭弹出的选项窗口。然后如上图 4 所述在 console 中运行 test 程序,在 mambo 的左栏中即可看见性能数据,如图 6 所示:
图6:查看性能数据
结束语
在多核处理器日益普及的今天,通过对于 Cell 编程的实践,可以很快发现这个异构多核处理器的独特之处。Cell 芯片的应用范围将会越来越广,它在游戏市场、嵌入式系统、并行应用、分布式计算等领域都有一定的优势。这个与众不同的处理器,目前除了被用在著名的 PS3 游戏机,还被用于 IBM 的 Blade Center QS22,目前世界上最快的超级计算机 Roadrunner 使用的也是 Cell 芯片。随着 Cell 相应 SDK 包的完善和改进,Cell 上的编程过程也会变得越来越简单和高效。
参考资料
关于作者  | |  | 魏琼, IBM中国软件开发中心,CETI部门软件工程师,从事cell blade server上的应用库向量化编程工作,喜爱Linux,精通Linux上的C编程。 |
对本文的评价
|  |