changeset 1552:40a554d45fc6 draft

select task array or task list exec option
author Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
date Sun, 17 Feb 2013 14:51:45 +0900
parents 57317332f6ef
children 21ddbc06dcde
files example/many_task/main.cc example/many_task/sort.cc example/many_task/sort.h
diffstat 3 files changed, 171 insertions(+), 78 deletions(-) [+]
line wrap: on
line diff
--- a/example/many_task/main.cc	Sun Feb 17 12:59:12 2013 +0900
+++ b/example/many_task/main.cc	Sun Feb 17 14:51:45 2013 +0900
@@ -26,7 +26,7 @@
 
 static int length = 1200;
 CPU_TYPE spe_cpu = SPE_ANY;
-int task_array_num=0;
+int use_task_array=0;
 // prototype
 void TMend(TaskManager *);
 
@@ -64,7 +64,8 @@
             sort_task = SortSimple;
         }
         if (strcmp(argv[i], "-ta") == 0 ) {
-            task_array_num = atoi(argv[++i]);
+            use_task_array = 1;
+            
         }
     }
 
@@ -109,10 +110,18 @@
 
     sorter.split_num = get_split_num(sorter.data_length, sorter.cpuNum); // data の分割数
     int half_num = sorter.split_num-1;
-    sorter.fsort = (Task**)manager->allocate(sizeof(Task*)*sorter.split_num);
-    sorter.bsort = (Task**)manager->allocate(sizeof(Task*)*half_num);
-    memset((void*)sorter.bsort,0, sizeof(HTaskPtr)*half_num);
-
+    if (use_task_array==1) {
+        sorter.fsort_task = (Task**)manager->allocate(sizeof(Task*)*sorter.split_num);
+        sorter.bsort_task = (Task**)manager->allocate(sizeof(Task*)*half_num);
+    } else {
+        sorter.fsort = (HTaskPtr*)manager->allocate(sizeof(Task*)*sorter.split_num);
+        sorter.bsort = (HTaskPtr*)manager->allocate(sizeof(Task*)*half_num);
+    }
+    if (use_task_array==1) {
+        memset((void*)sorter.bsort_task,0, sizeof(HTaskPtr)*half_num);
+    } else {
+        memset((void*)sorter.bsort,0, sizeof(HTaskPtr)*half_num);
+    }
     for (int i = 0; i < length; i++) {
         sorter.data[i].index = manager->get_random()%10000;
         sorter.data[i].ptr   = i;
--- a/example/many_task/sort.cc	Sun Feb 17 12:59:12 2013 +0900
+++ b/example/many_task/sort.cc	Sun Feb 17 14:51:45 2013 +0900
@@ -7,8 +7,8 @@
 extern int get_split_num(int len, int num);
 extern int all;  // allocate task at once
 extern CPU_TYPE spe_cpu ;
-extern int task_array_num;
-
+int task_array_num = 3;
+extern int use_task_array;
 /**
  * 一つの block にある data の数が MAX_BLOCK_SIZE 超えないような
  * len の分割数を返す
@@ -57,90 +57,172 @@
     if (--sort_count < 0) {
         return 0;
     }
-
-    HTask **task_array_f = (HTask**)manager->allocate(sizeof(HTask*)*s->split_num);
-    HTask **task_array_b = (HTask**)manager->allocate(sizeof(HTask*)*half_num);
+    if (use_task_array) {
+        HTask **task_array_f = (HTask**)manager->allocate(sizeof(HTask*)*s->split_num);
+        HTask **task_array_b = (HTask**)manager->allocate(sizeof(HTask*)*half_num);
 
-    for (int i = 0; i < s->split_num;i++) {
-        task_array_f[i] = manager->create_task_array(QUICK_SORT, task_array_num,1,1,1);
-        s->fsort[i]=0;
-    }
-    for (int i = 0; i<half_num;i++) {
-        task_array_b[i] = manager->create_task_array(QUICK_SORT, task_array_num,1,1,1);
-        s->bsort[i]=0;
-    }
-    for (int i = 0; i < s->split_num-1; i++) {
-        s->fsort[i] = task_array_f[i]->next_task_array(QUICK_SORT,s->fsort[i]);
-        s->fsort[i]->set_param(0,(memaddr)block_num);
-        s->fsort[i]->set_inData(0,(memaddr)&s->data[i*block_num], sizeof(Data)*block_num);
-        if (i>0 && s->bsort[i-1]) {
-            task_array_f[i]->wait_for(task_array_b[i-1]);
+        for (int i = 0; i < s->split_num;i++) {
+            task_array_f[i] = manager->create_task_array(QUICK_SORT, task_array_num,1,1,1);
+            s->fsort_task[i]=0;
+        }
+        for (int i = 0; i<half_num;i++) {
+            task_array_b[i] = manager->create_task_array(QUICK_SORT, task_array_num,1,1,1);
+            s->bsort_task[i]=0;
         }
-        if (i<s->split_num-2 && s->bsort[i]) {
-            task_array_f[i]->wait_for(task_array_b[i]);
+        for (int i = 0; i < s->split_num-1; i++) {
+            s->fsort_task[i] = task_array_f[i]->next_task_array(QUICK_SORT,s->fsort_task[i]);
+            s->fsort_task[i]->set_param(0,(memaddr)block_num);
+            s->fsort_task[i]->set_inData(0,(memaddr)&s->data[i*block_num], sizeof(Data)*block_num);
+            if (i>0 && s->bsort_task[i-1]) {
+                task_array_f[i]->wait_for(task_array_b[i-1]);
+            }
+            if (i<s->split_num-2 && s->bsort_task[i]) {
+                task_array_f[i]->wait_for(task_array_b[i]);
+            }
         }
-    }
+
+        // 最後の block は端数なので last_block_num を使う
+        {
+
+            int i = s->split_num-1;
 
-    // 最後の block は端数なので last_block_num を使う
-    {
+            s->fsort_task[i] = task_array_f[i]->next_task_array(QUICK_SORT,s->fsort_task[i]);
+            s->fsort_task[i]->set_param(0,(memaddr)last_block_num);
+            s->fsort_task[i]->set_inData(0,(memaddr)&s->data[i*block_num], sizeof(Data)*last_block_num);
+            if (i>0 && s->bsort_task[i-1]) {
+                task_array_f[i]->wait_for(task_array_b[i-1]);
+            }
+        }
 
-        int i = s->split_num-1;
+        if (s->split_num > 1) {
 
-        s->fsort[i] = task_array_f[i]->next_task_array(QUICK_SORT,s->fsort[i]);
-        s->fsort[i]->set_param(0,(memaddr)last_block_num);
-        s->fsort[i]->set_inData(0,(memaddr)&s->data[i*block_num], sizeof(Data)*last_block_num);
-        if (i>0 && s->bsort[i-1]) {
-            task_array_f[i]->wait_for(task_array_b[i-1]);
-        }
-   }
+            for (int i = 0; i < half_num-1; i++) {
+                if (s->bsort_task[i]) s->bsort_task[i]=0;
+                s->bsort_task[i] = task_array_b[i]->next_task_array(QUICK_SORT,s->bsort_task[i]);
+                s->bsort_task[i]->set_inData(0,(memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*block_num);
+                s->bsort_task[i]->set_param(0,(memaddr)block_num);
+            }
+
+            {
+                int i = half_num-1;
 
-    if (s->split_num > 1) {
+                if (s->bsort_task[i]) s->bsort_task[i]=0;
+                s->bsort_task[i] = task_array_b[i]->next_task_array(QUICK_SORT,s->bsort_task[i]);
+                s->bsort_task[i]->set_inData(0,(memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num);
+                s->bsort_task[i]->set_param(0,(memaddr)last_half_block_num);
+            }
 
-        for (int i = 0; i < half_num-1; i++) {
-            if (s->bsort[i]) s->bsort[i]=0;
-            s->bsort[i] = task_array_b[i]->next_task_array(QUICK_SORT,s->bsort[i]);
-            s->bsort[i]->set_inData(0,(memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*block_num);
-            s->bsort[i]->set_param(0,(memaddr)block_num);
+            for (int i = 0; i < half_num; i++) {
+                task_array_b[i]->wait_for(task_array_f[i]);
+                task_array_b[i]->wait_for(task_array_f[i+1]);
+                task_array_b[i]->no_auto_free();
+                task_array_b[i]->spawn_task_array(s->bsort_task[i]->next());
+                task_array_b[i]->set_cpu(spe_cpu);
+                task_array_b[i]->flip();
+                task_array_b[i]->spawn();
+            }
         }
 
-        {
-            int i = half_num-1;
+        HTaskPtr restart = manager->create_task(SortSimple,0,0,0,0);
+        restart->set_param(0,(memaddr)s);
+        if (!all) restart->wait_for(task_array_f[0]);
+        for (int i = 0; i < s->split_num; i++) {
+            task_array_f[i]->spawn_task_array(s->fsort_task[i]->next());
+            task_array_f[i]->set_cpu(spe_cpu);
+            task_array_f[i]->flip();
+            task_array_f[i]->spawn();
+        }
+        if (sort_count == 1) {
+            // last loop wait for all task
+            // we should not need this?
+            for (int i = 0; i < half_num; i++) {
+                restart->wait_for(task_array_b[i]);
+                task_array_b[i]->auto_free();
+            }
+        }
+        restart->spawn();
+    } else {
+        
+        for (int i = 0; i < s->split_num-1; i++) {
+            s->fsort[i] = manager->create_task(QUICK_SORT,
+                                               (memaddr)&s->data[i*block_num], sizeof(Data)*block_num,
+                                               (memaddr)&s->data[i*block_num], sizeof(Data)*block_num);
 
-            if (s->bsort[i]) s->bsort[i]=0;
-            s->bsort[i] = task_array_b[i]->next_task_array(QUICK_SORT,s->bsort[i]);
-            s->bsort[i]->set_inData(0,(memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num);
-            s->bsort[i]->set_param(0,(memaddr)last_half_block_num);
+            s->fsort[i]->flip();
+
+            if (i>0 && s->bsort[i-1]) {
+                s->fsort[i]->wait_for(s->bsort[i-1]);
+            }
+            if (i<s->split_num-2 && s->bsort[i]) {
+                s->fsort[i]->wait_for(s->bsort[i]);
+            }
+            s->fsort[i]->set_cpu(spe_cpu);
+            s->fsort[i]->set_param(0,(memaddr)block_num);
         }
 
-        for (int i = 0; i < half_num; i++) {
-            task_array_b[i]->wait_for(task_array_f[i]);
-            task_array_b[i]->wait_for(task_array_f[i+1]);
-            task_array_b[i]->no_auto_free();
-            task_array_b[i]->spawn_task_array(s->bsort[i]->next());
-            task_array_b[i]->set_cpu(spe_cpu);
-            task_array_b[i]->flip();
-            task_array_b[i]->spawn();
+        // 最後㮠block ã¯ç«¯æ•°ãªã®ã§ last_block_num を使ã†
+        {
+            int i = s->split_num-1;
+
+            s->fsort[i] = manager->create_task(QUICK_SORT,
+                                               (memaddr)&s->data[i*block_num], sizeof(Data)*last_block_num,
+                                               (memaddr)&s->data[i*block_num], sizeof(Data)*last_block_num);
+            s->fsort[i]->flip();
+            if (i>0 && s->bsort[i-1]) {
+                s->fsort[i]->wait_for(s->bsort[i-1]);
+            }
+            s->fsort[i]->set_cpu(spe_cpu);
+            s->fsort[i]->set_param(0,(memaddr)last_block_num);
         }
-    }
+
+        if (s->split_num > 1) {
+
+            for (int i = 0; i < half_num-1; i++) {
+                if (s->bsort[i]) manager->free_htask(s->bsort[i]);
+                s->bsort[i] = manager->create_task(QUICK_SORT,
+                                                   (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*block_num,
+                                                   (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*block_num);
+                s->bsort[i]->flip();
+                s->bsort[i]->set_cpu(spe_cpu);
+                s->bsort[i]->set_param(0,(memaddr)block_num);
+            }
+
+            {
+                int i = half_num-1;
 
-    HTaskPtr restart = manager->create_task(SortSimple,0,0,0,0);
-    restart->set_param(0,(memaddr)s);
-    if (!all) restart->wait_for(task_array_f[0]);
-    for (int i = 0; i < s->split_num; i++) {
-        task_array_f[i]->spawn_task_array(s->fsort[i]->next());
-        task_array_f[i]->set_cpu(spe_cpu);
-        task_array_f[i]->flip();
-        task_array_f[i]->spawn();
+                if (s->bsort[i]) manager->free_htask(s->bsort[i]);
+                s->bsort[i] = manager->create_task(QUICK_SORT,
+                                                   (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num,
+                                                   (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num);
+                s->bsort[i]->flip();
+                s->bsort[i]->set_cpu(spe_cpu);
+                s->bsort[i]->set_param(0,(memaddr)last_half_block_num);
+            }
+
+            for (int i = 0; i < half_num; i++) {
+                s->bsort[i]->wait_for(s->fsort[i]);
+                s->bsort[i]->wait_for(s->fsort[i+1]);
+                s->bsort[i]->no_auto_free();
+                s->bsort[i]->spawn();
+            }
+        }
+
+        HTaskPtr restart = manager->create_task(SortSimple,0,0,0,0);
+        restart->set_param(0,(memaddr)s);
+        if (!all) restart->wait_for(s->fsort[0]);
+        for (int i = 0; i < s->split_num; i++) {
+            s->fsort[i]->spawn();
+        }
+        if (sort_count == 1) {
+            // last loop wait for all task
+            // we should not need this?
+            for (int i = 0; i < half_num; i++) {
+                restart->wait_for(s->bsort[i]);
+                s->bsort[i]->auto_free();
+            }
+        }
+        restart->spawn();
     }
-    if (sort_count == 1) {
-        // last loop wait for all task
-        // we should not need this?
-        for (int i = 0; i < half_num; i++) {
-            restart->wait_for(task_array_b[i]);
-            task_array_b[i]->auto_free();
-        }
-    }
-    restart->spawn();
 
     return 0;
 }
--- a/example/many_task/sort.h	Sun Feb 17 12:59:12 2013 +0900
+++ b/example/many_task/sort.h	Sun Feb 17 14:51:45 2013 +0900
@@ -10,8 +10,10 @@
     int data_length;
     int split_num;
     long cpuNum;
-    Task **fsort;
-    Task **bsort;
+    HTaskPtr *fsort;
+    HTaskPtr *bsort;
+    Task **fsort_task;
+    Task **bsort_task;
     DataPtr data;
 } Sort, *SortPtr;