A Simple Parallel Loop in COMIC

Following code is a simple OpenMP program. In this example, we will convert the OpenMP code to Cell BE code using COMIC library.

#include <stdio.h>
#include <omp.h>
#define SIZE 24

int a[ SIZE ];

int main() {
		int i;

#pragma omp parallel for
		for( i=0; i < SIZE; i++ )
				a[i] = i;

		for( i=0; i < SIZE; i++ )
				printf( "%d\n", a[ i ] );

		return 0;
}

1. Common code

We need the declaration of shared variables.

/* example_common.h */

#define SIZE 24

COMIC_shared (
		 int a[ SIZE ];
);

2. SPE code

/* example_spe.c */

#include <comic_spe.h>
#include "example_common.h"

COMIC_shared_extern();
COMIC_shared_region();

void spe_main_case0() {
		int i, tid, chunk, low, high;

		tid = COMIC_get_spe_thread_id();
		chunk = SIZE / COMIC_MAX_NUM_SPE_THREADS;
		low = tid * chunk;
		high = (tid == ( COMIC_MAX_NUM_SPE_THREADS-1)) ? SIZE : (low + chunk);

		for( i = low; i < high; i++ )
				COMIC_spe_write_int( &COMIC_access(a[i]), i );

		/* implicit barrier */
		COMIC_spe_barrier( 0 );
}

COMIC_spe_main (
		case 0:
				spe_main_case0();
				break;
)

The SPE code is compiled with spuxlc.

$ spuxlc -O3 -I../comic-0.8.0 -c ../comic-0.8.0/comic_spe.c
$ spuxlc -I../comic-0.8.0 -c ../comic-0.8.0/comic_spe_toe.s
$ spuxlc -O5 -I../comic-0.8.0 -c example_spe.c
$ spuxlc -O5 -o example_spe comic_spe.o comic_spe_toe.o example_spe.o
$ ppu32-embedspu COMIC_spe_handle example_spe example_spe_csf.o

3. PPE code

/* example_ppe.c */
#include <stdio.h>
#include <comic_ppe.h>
#include "example_common.h"

COMIC_shared_region();

int main() {
		int i;

		/* initialization of COMIC library */
		COMIC_ppe_init();

		/* parallel execution */
		COMIC_ppe_run( 0 );

		for( i=0; i < SIZE; i++ )
				printf( "%d\n", COMIC_access( a[ i ] ) );

		/* termination of SPE threads */
		COMIC_ppe_exit();

		return 0;
}

PPE code is compiled with ppuxlc.

$ ppuxlc -O5 -I../comic-0.8.0 -I/opt/cell/sdk/src/include/ppu -c ../comic-0.8.0/comic_ppe.c
$ ppuxlc -O5 -I../comic-0.8.0 -c example_ppe.c
$ ppuxlc -O5 -o example_comic -lspe2 -lpthread comic_ppe.o example_ppe.o example_spe_csf.o

Run the executable. Then, you will see the same result with the OpenMP's.

$ ./example_comic

You can download above examples: example.tar.gz