{"id":444,"date":"2023-01-27T08:24:27","date_gmt":"2023-01-27T08:24:27","guid":{"rendered":"https:\/\/thunder.snu.ac.kr\/wordpress\/?page_id=444"},"modified":"2023-01-30T07:47:40","modified_gmt":"2023-01-30T07:47:40","slug":"a-simple-parallel-loop-in-comic","status":"publish","type":"page","link":"https:\/\/thunder.snu.ac.kr\/?page_id=444","title":{"rendered":"A Simple Parallel Loop in COMIC"},"content":{"rendered":"\n<p>Following code is a simple OpenMP program. In this example, we will convert the OpenMP code to Cell BE code using COMIC library.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-tertiary-background-color has-background\">#include &lt;stdio.h&gt;\n#include &lt;omp.h&gt;\n#define SIZE 24\n\nint a[ SIZE ];\n\nint main() {\n    int i;\n\n#pragma omp parallel for\n    for( i=0; i &lt; SIZE; i++ )\n        a[i] = i;\n\n    for( i=0; i &lt; SIZE; i++ )\n        printf( \"%d\\n\", a[ i ] );\n\n    return 0;\n}<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">1. Common code<\/h3>\n\n\n\n<p>We need the declaration of shared variables.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-tertiary-background-color has-background\">\/* example_common.h *\/\n\n#define SIZE 24\n\nCOMIC_shared (\n     int a[ SIZE ];\n);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. SPE code<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted has-tertiary-background-color has-background\">\/* example_spe.c *\/\n\n#include &lt;comic_spe.h&gt;\n#include \"example_common.h\"\n\nCOMIC_shared_extern();\nCOMIC_shared_region();\n\nvoid spe_main_case0() {\n    int i, tid, chunk, low, high;\n\n    tid = COMIC_get_spe_thread_id();\n    chunk = SIZE \/ COMIC_MAX_NUM_SPE_THREADS;\n    low = tid * chunk;\n    high = (tid == ( COMIC_MAX_NUM_SPE_THREADS-1)) ? SIZE : (low + chunk);\n\n    for( i = low; i &lt; high; i++ )\n        COMIC_spe_write_int( &amp;COMIC_access(a[i]), i );\n\n    \/* implicit barrier *\/\n    COMIC_spe_barrier( 0 );\n}\n\nCOMIC_spe_main (\n    case 0:\n        spe_main_case0();\n        break;\n)<\/pre>\n\n\n\n<p>The SPE code is compiled with spuxlc.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-tertiary-background-color has-background\">$ spuxlc -O3 -I..\/comic-0.8.0 -c ..\/comic-0.8.0\/comic_spe.c\n$ spuxlc -I..\/comic-0.8.0 -c ..\/comic-0.8.0\/comic_spe_toe.s\n$ spuxlc -O5 -I..\/comic-0.8.0 -c example_spe.c\n$ spuxlc -O5 -o example_spe comic_spe.o comic_spe_toe.o example_spe.o\n$ ppu32-embedspu COMIC_spe_handle example_spe example_spe_csf.o<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. PPE code<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted has-tertiary-background-color has-background\">\/* example_ppe.c *\/\n#include &lt;stdio.h&gt;\n#include &lt;comic_ppe.h&gt;\n#include \"example_common.h\"\n\nCOMIC_shared_region();\n\nint main() {\n    int i;\n\n    \/* initialization of COMIC library *\/\n    COMIC_ppe_init();\n\n    \/* parallel execution *\/\n    COMIC_ppe_run( 0 );\n\n    for( i=0; i &lt; SIZE; i++ )\n        printf( \"%d\\n\", COMIC_access( a[ i ] ) );\n\n    \/* termination of SPE threads *\/\n    COMIC_ppe_exit();\n\n    return 0;\n}<\/pre>\n\n\n\n<p>PPE code is compiled with ppuxlc.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-tertiary-background-color has-background\">$ ppuxlc -O5 -I..\/comic-0.8.0 -I\/opt\/cell\/sdk\/src\/include\/ppu -c ..\/comic-0.8.0\/comic_ppe.c\n$ ppuxlc -O5 -I..\/comic-0.8.0 -c example_ppe.c\n$ ppuxlc -O5 -o example_comic -lspe2 -lpthread comic_ppe.o example_ppe.o example_spe_csf.o<\/pre>\n\n\n\n<p>Run the executable. Then, you will see the same result with the OpenMP&#8217;s.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-tertiary-background-color has-background\">$ .\/example_comic<\/pre>\n\n\n\n<p>You can download above examples:\u00a0<a href=\"http:\/\/thunder.snu.ac.kr\/wordpress\/software\/comic\/files\/example.tar.gz\" data-type=\"URL\" data-id=\"http:\/\/thunder.snu.ac.kr\/wordpress\/software\/comic\/files\/example.tar.gz\">example.tar.gz<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Following code is a simple OpenMP program. In this example, we will convert the OpenMP code to Cell BE code using COMIC library. #include &lt;stdio.h&gt; #include &lt;omp.h&gt; #define SIZE 24 int a[ SIZE ]; int main() { int i; #pragma omp parallel for for( i=0; i &lt; SIZE; i++ ) a[i] = i; for( i=0; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"footnotes":""},"class_list":["post-444","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/thunder.snu.ac.kr\/index.php?rest_route=\/wp\/v2\/pages\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thunder.snu.ac.kr\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/thunder.snu.ac.kr\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/thunder.snu.ac.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thunder.snu.ac.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=444"}],"version-history":[{"count":2,"href":"https:\/\/thunder.snu.ac.kr\/index.php?rest_route=\/wp\/v2\/pages\/444\/revisions"}],"predecessor-version":[{"id":492,"href":"https:\/\/thunder.snu.ac.kr\/index.php?rest_route=\/wp\/v2\/pages\/444\/revisions\/492"}],"wp:attachment":[{"href":"https:\/\/thunder.snu.ac.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}