add utests option: -j which specifies the 'number' of jobs (pthread)

Submitted by Meng, Mengmeng on Sept. 22, 2015, 12:42 p.m.

Details

Message ID D6491F03E58BBA469517B8539027FFB701F96AE0@shsmsx102.ccr.corp.intel.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Meng, Mengmeng Sept. 22, 2015, 12:42 p.m.
Ping for review.

-----Original Message-----
From: Meng, Mengmeng 
Sent: Monday, September 07, 2015 5:05 AM
To: beignet@lists.freedesktop.org
Cc: Meng, Mengmeng
Subject: [PATCH] add utests option: -j which specifies the 'number' of jobs (pthread)

1. the ptread number should be [1 - utestList->size] 2. out-of-order execution for all utests lists
---
 utests/utest.cpp     | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 utests/utest.hpp     |  4 ++++
 utests/utest_run.cpp | 14 ++++++++++++-
 3 files changed, 75 insertions(+), 1 deletion(-)

   option:\n\
     -c <casename>: run sub-case named 'casename'\n\
+    -j <number>  : specifies the 'number' of jobs (thread)\n\
     -l           : list all the available case name\n\
     -a           : run all test cases\n\
     -n           : run all test cases without known issue (default option)\n\
@@ -85,6 +87,16 @@ int main(int argc, char *argv[])
 
         break;
 
+      case 'j':
+        try {
+          UTest::runMultiThread(optarg);
+        }
+        catch (Exception e){
+          std::cout << "  " << e.what() << "    [SUCCESS]" << std::endl;
+        }
+
+        break;
+
       case 'l':
         UTest::listAllCases();
         break;
--
1.9.1

Patch hide | download patch | download mbox

diff --git a/utests/utest.cpp b/utests/utest.cpp index 0a03d8b..2c83a5d 100644
--- a/utests/utest.cpp
+++ b/utests/utest.cpp
@@ -31,6 +31,12 @@ 
 #include <cstring>
 #include <stdlib.h>
 #include <csignal>
+#include <algorithm>
+#include <random>
+#include <chrono>
+#include <iterator>
+#include <semaphore.h>
+#include <unistd.h>
 
 struct signalMap
 {
@@ -39,7 +45,9 @@  struct signalMap
 };
 
 using namespace std;
+sem_t tag;
 vector<UTest> *UTest::utestList = NULL;
+vector<int> v;
 // Initialize and declare statistics struct  RStatistics UTest::retStatistics;
 
@@ -105,6 +113,30 @@  void catch_signal(void){
       perror("Could not set signal handler");
   }
 }
+void *multithread(void * arg)
+{
+  int SerialNumber;
+//  int PhtreadNumber = *(int *)arg;
+
+  while(! v.empty()){
+    sem_wait(&tag);
+
+    SerialNumber = v.back();
+    v.pop_back();
+
+    const  UTest &utest = (*UTest::utestList)[SerialNumber];
+//    printf("thread%d  %d, utests.name is %s\n",PhtreadNumber, SerialNumber,utest.name);
+    UTest::do_run(utest);
+    cl_kernel_destroy(true);
+    cl_buffer_destroy();
+
+    sem_post(&tag);
+    sleep(1);
+  }
+
+  return 0;
+}
+
 
 UTest::UTest(Function fn, const char *name, bool isBenchMark, bool haveIssue, bool needDestroyProgram)
        : fn(fn), name(name), isBenchMark(isBenchMark), haveIssue(haveIssue), needDestroyProgram(needDestroyProgram) { @@ -148,6 +180,32 @@ void UTest::run(const char *name) {
   }
 }
 
+void UTest::runMultiThread(const char *number) {
+  if (number == NULL) return;
+  if (utestList == NULL) return;
+
+  unsigned long i, num;
+  sem_init(&tag, 0, 1);
+
+  num = atoi(number);
+
+  if(num < 1 || num > utestList->size()){
+    printf("the 'number' of jobs to run simultaneously is [1 - %lu]",utestList->size());
+    return;
+  }
+
+  for(i = 0; i < utestList->size(); ++i) v.push_back (i);  unsigned 
+ seed = chrono::system_clock::now ().time_since_epoch ().count ();  
+ shuffle (v.begin (), v.end (), std::default_random_engine (seed));
+
+  pthread_t pthread_arry[num];
+
+  for(i=0; i<num;i++) pthread_create(&pthread_arry[i], NULL, 
+ multithread, (void *)&i);  for(i=0; i<num;i++) 
+ pthread_join(pthread_arry[i], NULL);
+
+  sem_destroy(&tag);
+}
+
 void UTest::runAll(void) {
   if (utestList == NULL) return;
 
diff --git a/utests/utest.hpp b/utests/utest.hpp index 7ae8b87..cda7545 100644
--- a/utests/utest.hpp
+++ b/utests/utest.hpp
@@ -54,6 +54,8 @@  struct UTest
   Function fn;
   /*! Name of the test */
   const char *name;
+  /*! numbers of the jobs */
+  const char *number;
   /*! whether it is a bench mark. */
   bool isBenchMark;
   /*! Indicate whether current test cases has issue to be fixes */ @@ -64,6 +66,8 @@ struct UTest
   static std::vector<UTest> *utestList;
   /*! Run the test with the given name */
   static void run(const char *name);
+  /*! Run the test with the given name */  static void 
+ runMultiThread(const char *number);
   /*! Run all the tests without known issue*/
   static void runAllNoIssue(void);
   /*! Run all the benchmark. */
diff --git a/utests/utest_run.cpp b/utests/utest_run.cpp index 3cc1b6c..8cc15b1 100644
--- a/utests/utest_run.cpp
+++ b/utests/utest_run.cpp
@@ -28,9 +28,10 @@ 
 #include <iostream>
 #include <getopt.h>
 
-static const char *shortopts = "c:lanh";
+static const char *shortopts = "c:j:lanh";
 struct option longopts[] = {
 {"casename", required_argument, NULL, 'c'},
+{"jobs", required_argument, NULL, 'j'},
 {"list", no_argument, NULL, 'l'},
 {"all", no_argument, NULL, 'a'},
 {"allnoissue", no_argument, NULL, 'n'}, @@ -46,6 +47,7 @@ Usage:\n\  \n\

Comments

Although use the pthread, but because of semaphore, the UTest::do_run(utest) still run one by one, right?

> -----Original Message-----

> From: Beignet [mailto:beignet-bounces@lists.freedesktop.org] On Behalf Of

> Meng, Mengmeng

> Sent: Tuesday, September 22, 2015 20:43

> To: beignet@lists.freedesktop.org

> Subject: Re: [Beignet] [PATCH] add utests option: -j which specifies the

> 'number' of jobs (pthread)

> 

> Ping for review.

> 

> -----Original Message-----

> From: Meng, Mengmeng

> Sent: Monday, September 07, 2015 5:05 AM

> To: beignet@lists.freedesktop.org

> Cc: Meng, Mengmeng

> Subject: [PATCH] add utests option: -j which specifies the 'number' of jobs

> (pthread)

> 

> 1. the ptread number should be [1 - utestList->size] 2. out-of-order

> execution for all utests lists

> ---

>  utests/utest.cpp     | 58

> ++++++++++++++++++++++++++++++++++++++++++++++++++++

>  utests/utest.hpp     |  4 ++++

>  utests/utest_run.cpp | 14 ++++++++++++-

>  3 files changed, 75 insertions(+), 1 deletion(-)

> 

> diff --git a/utests/utest.cpp b/utests/utest.cpp index 0a03d8b..2c83a5d

> 100644

> --- a/utests/utest.cpp

> +++ b/utests/utest.cpp

> @@ -31,6 +31,12 @@

>  #include <cstring>

>  #include <stdlib.h>

>  #include <csignal>

> +#include <algorithm>

> +#include <random>

> +#include <chrono>

> +#include <iterator>

> +#include <semaphore.h>

> +#include <unistd.h>

> 

>  struct signalMap

>  {

> @@ -39,7 +45,9 @@ struct signalMap

>  };

> 

>  using namespace std;

> +sem_t tag;

>  vector<UTest> *UTest::utestList = NULL;

> +vector<int> v;

>  // Initialize and declare statistics struct  RStatistics UTest::retStatistics;

> 

> @@ -105,6 +113,30 @@ void catch_signal(void){

>        perror("Could not set signal handler");

>    }

>  }

> +void *multithread(void * arg)

> +{

> +  int SerialNumber;

> +//  int PhtreadNumber = *(int *)arg;

> +

> +  while(! v.empty()){

> +    sem_wait(&tag);

> +

> +    SerialNumber = v.back();

> +    v.pop_back();

> +

> +    const  UTest &utest = (*UTest::utestList)[SerialNumber];

> +//    printf("thread%d  %d, utests.name is %s\n",PhtreadNumber,

> SerialNumber,utest.name);

> +    UTest::do_run(utest);

> +    cl_kernel_destroy(true);

> +    cl_buffer_destroy();

> +

> +    sem_post(&tag);

> +    sleep(1);

> +  }

> +

> +  return 0;

> +}

> +

> 

>  UTest::UTest(Function fn, const char *name, bool isBenchMark, bool

> haveIssue, bool needDestroyProgram)

>         : fn(fn), name(name), isBenchMark(isBenchMark), haveIssue(haveIssue),

> needDestroyProgram(needDestroyProgram) { @@ -148,6 +180,32 @@ void

> UTest::run(const char *name) {

>    }

>  }

> 

> +void UTest::runMultiThread(const char *number) {

> +  if (number == NULL) return;

> +  if (utestList == NULL) return;

> +

> +  unsigned long i, num;

> +  sem_init(&tag, 0, 1);

> +

> +  num = atoi(number);

> +

> +  if(num < 1 || num > utestList->size()){

> +    printf("the 'number' of jobs to run simultaneously is [1 - %lu]",utestList-

> >size());

> +    return;

> +  }

> +

> +  for(i = 0; i < utestList->size(); ++i) v.push_back (i);  unsigned

> + seed = chrono::system_clock::now ().time_since_epoch ().count ();

> + shuffle (v.begin (), v.end (), std::default_random_engine (seed));

> +

> +  pthread_t pthread_arry[num];

> +

> +  for(i=0; i<num;i++) pthread_create(&pthread_arry[i], NULL,

> + multithread, (void *)&i);  for(i=0; i<num;i++)

> + pthread_join(pthread_arry[i], NULL);

> +

> +  sem_destroy(&tag);

> +}

> +

>  void UTest::runAll(void) {

>    if (utestList == NULL) return;

> 

> diff --git a/utests/utest.hpp b/utests/utest.hpp index 7ae8b87..cda7545

> 100644

> --- a/utests/utest.hpp

> +++ b/utests/utest.hpp

> @@ -54,6 +54,8 @@ struct UTest

>    Function fn;

>    /*! Name of the test */

>    const char *name;

> +  /*! numbers of the jobs */

> +  const char *number;

>    /*! whether it is a bench mark. */

>    bool isBenchMark;

>    /*! Indicate whether current test cases has issue to be fixes */ @@ -64,6

> +66,8 @@ struct UTest

>    static std::vector<UTest> *utestList;

>    /*! Run the test with the given name */

>    static void run(const char *name);

> +  /*! Run the test with the given name */  static void

> + runMultiThread(const char *number);

>    /*! Run all the tests without known issue*/

>    static void runAllNoIssue(void);

>    /*! Run all the benchmark. */

> diff --git a/utests/utest_run.cpp b/utests/utest_run.cpp index

> 3cc1b6c..8cc15b1 100644

> --- a/utests/utest_run.cpp

> +++ b/utests/utest_run.cpp

> @@ -28,9 +28,10 @@

>  #include <iostream>

>  #include <getopt.h>

> 

> -static const char *shortopts = "c:lanh";

> +static const char *shortopts = "c:j:lanh";

>  struct option longopts[] = {

>  {"casename", required_argument, NULL, 'c'},

> +{"jobs", required_argument, NULL, 'j'},

>  {"list", no_argument, NULL, 'l'},

>  {"all", no_argument, NULL, 'a'},

>  {"allnoissue", no_argument, NULL, 'n'}, @@ -46,6 +47,7 @@ Usage:\n\  \n\

>    option:\n\

>      -c <casename>: run sub-case named 'casename'\n\

> +    -j <number>  : specifies the 'number' of jobs (thread)\n\

>      -l           : list all the available case name\n\

>      -a           : run all test cases\n\

>      -n           : run all test cases without known issue (default option)\n\

> @@ -85,6 +87,16 @@ int main(int argc, char *argv[])

> 

>          break;

> 

> +      case 'j':

> +        try {

> +          UTest::runMultiThread(optarg);

> +        }

> +        catch (Exception e){

> +          std::cout << "  " << e.what() << "    [SUCCESS]" << std::endl;

> +        }

> +

> +        break;

> +

>        case 'l':

>          UTest::listAllCases();

>          break;

> --

> 1.9.1

> 

> _______________________________________________

> Beignet mailing list

> Beignet@lists.freedesktop.org

> http://lists.freedesktop.org/mailman/listinfo/beignet