之前已经用ps3搭建了并行程序开发的实验平台,现在开始实践。程序功能很简单,生成6个线程(ps3中只有6个spe可以使用,当然线程数目不限6个),每个线程输出hello world以及线程id。程序如下:
hello_ppu.c
#include <stdio.h>
#include <libspe.h>
extern spe_program_handle_t hello_spu_handle;
int main()
{
speid_t id[6];
int i;
for (i=0;i<6;i++)
{
id[i] = spe_create_thread(0,&hello_spu_handle,NULL,NULL,-1,0);//建立6个线程
}
for (i=0;i<6;i++)
{
spe_wait(id[i],NULL,0);//等待各个线程结束
}
i = spe_count_physical_spes(); //获取可以使用的spe数目
printf("number of spe available: %d/n",i);
return 0;
}
hello_spu.c
#include <stdio.h>
int main (unsigned long long speid, unsigned long long argp,unsigned long long envp)
{
printf("0x%x: Hello World!/n",(unsigned int) speid);
return 0;
}
编译方法与结果
[luckydog@localhost cell_code]$ spu-gcc hello_spu.c -o hello_spu
[luckydog@localhost cell_code]$ embedspu hello_spu_handle hello_spu hello_spu_csf.o
[luckydog@localhost cell_code]$ gcc hello_ppu.c hello_spu_csf.o -lspe -o hello
[luckydog@localhost cell_code]$ ./hello
0x1001a038: Hello World!
0x1001a3d8: Hello World!
0x1001a778: Hello World!
0x1001a208: Hello World!
0x1001a5a8: Hello World!
0x1001a948: Hello World!
number of spe available: 6
由于spu和ppu使用的指令集不同,需要分别编译。
第一步,编译spu程序。第二步将spu程序嵌入到一个elf文件中,并以hello_spu_handle名字发布,以让链接程序能将spu程序和ppe程序一同链接成可执行文件hello。
输出结果的number of spe available: 6 是目前能使用的spe数目。
hello_ppu.c
#include <stdio.h>
#include <libspe.h>
extern spe_program_handle_t hello_spu_handle;
int main()
{
speid_t id[6];
int i;
for (i=0;i<6;i++)
{
id[i] = spe_create_thread(0,&hello_spu_handle,NULL,NULL,-1,0);//建立6个线程
}
for (i=0;i<6;i++)
{
spe_wait(id[i],NULL,0);//等待各个线程结束
}
i = spe_count_physical_spes(); //获取可以使用的spe数目
printf("number of spe available: %d/n",i);
return 0;
}
hello_spu.c
#include <stdio.h>
int main (unsigned long long speid, unsigned long long argp,unsigned long long envp)
{
printf("0x%x: Hello World!/n",(unsigned int) speid);
return 0;
}
编译方法与结果
[luckydog@localhost cell_code]$ spu-gcc hello_spu.c -o hello_spu
[luckydog@localhost cell_code]$ embedspu hello_spu_handle hello_spu hello_spu_csf.o
[luckydog@localhost cell_code]$ gcc hello_ppu.c hello_spu_csf.o -lspe -o hello
[luckydog@localhost cell_code]$ ./hello
0x1001a038: Hello World!
0x1001a3d8: Hello World!
0x1001a778: Hello World!
0x1001a208: Hello World!
0x1001a5a8: Hello World!
0x1001a948: Hello World!
number of spe available: 6
由于spu和ppu使用的指令集不同,需要分别编译。
第一步,编译spu程序。第二步将spu程序嵌入到一个elf文件中,并以hello_spu_handle名字发布,以让链接程序能将spu程序和ppe程序一同链接成可执行文件hello。
输出结果的number of spe available: 6 是目前能使用的spe数目。