[Beignet] [PATCH 2/2 v2] Add unit performance suite

Yi Sun yi.sun at intel.com
Mon Mar 31 20:15:56 PDT 2014


Unit performance is named uperformance.
The first uperformance is enqueue_copy_buf.

Signed-off-by: Yi Sun <yi.sun at intel.com>

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 23fdd63..0b85379 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -146,6 +146,7 @@ ADD_SUBDIRECTORY(include)
 ADD_SUBDIRECTORY(backend)
 ADD_SUBDIRECTORY(src)
 ADD_SUBDIRECTORY(utests/utest)
+ADD_SUBDIRECTORY(utests/uperformance)
 
 SET(CPACK_PACKAGE_VERSION_MAJOR "${LIBCL_DRIVER_VERSION_MAJOR}")
 SET(CPACK_PACKAGE_VERSION_MINOR "${LIBCL_DRIVER_VERSION_MINOR}")
diff --git a/utests/uperformance/CMakeLists.txt b/utests/uperformance/CMakeLists.txt
new file mode 100644
index 0000000..956e74f
--- /dev/null
+++ b/utests/uperformance/CMakeLists.txt
@@ -0,0 +1,21 @@
+INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}
+                    ${CMAKE_CURRENT_SOURCE_DIR}/../utest
+                    ${CMAKE_CURRENT_SOURCE_DIR}/../include)
+
+
+link_directories (${LLVM_LIBRARY_DIR})
+set (uperformance_sources
+  ../utest/utest_error.c
+  ../utest/utest_assert.cpp
+  ../utest/utest.cpp
+  ../utest/utest_file_map.cpp
+  ../utest/utest_helper.cpp
+  enqueue_copy_buf.cpp)
+
+ADD_LIBRARY(uperformances SHARED ${ADDMATHFUNC} ${uperformance_sources})
+
+#TARGET_LINK_LIBRARIES(uperformances cl m ${OPENGL_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
+TARGET_LINK_LIBRARIES(uperformances cl m)
+
+ADD_EXECUTABLE(uperformance_run uperformance_run.cpp)
+TARGET_LINK_LIBRARIES(uperformance_run uperformances)
diff --git a/utests/uperformance/enqueue_copy_buf.cpp b/utests/uperformance/enqueue_copy_buf.cpp
new file mode 100644
index 0000000..cd8aa6e
--- /dev/null
+++ b/utests/uperformance/enqueue_copy_buf.cpp
@@ -0,0 +1,69 @@
+#include "utest_helper.hpp"
+#include <sys/time.h>
+
+void test_copy_buf(size_t sz, size_t src_off, size_t dst_off, size_t cb)
+{
+  unsigned int i;
+  cl_char* buf0;
+
+  OCL_CREATE_BUFFER(buf[0], 0, sz * sizeof(char), NULL);
+  OCL_CREATE_BUFFER(buf[1], 0, sz * sizeof(char), NULL);
+
+  buf0 = (cl_char *)clEnqueueMapBuffer(queue, buf[0], CL_TRUE, CL_MAP_WRITE, 0, sizeof(char), 0, NULL, NULL, NULL);
+
+  for (i=0; i < sz; i++) {
+    buf0[i]=(rand() & 0xFF);
+  }
+
+  clEnqueueUnmapMemObject(queue, buf[0], buf0, 0, NULL, NULL);
+
+  if (src_off + cb > sz || dst_off + cb > sz) {
+  /* Expect Error. */
+    OCL_ASSERT(clEnqueueCopyBuffer(queue, buf[0], buf[1],
+                 src_off, dst_off, cb*sizeof(char), 0, NULL, NULL));
+    return;
+  }
+
+  OCL_ASSERT(CL_SUCCESS == clEnqueueCopyBuffer(queue, buf[0], buf[1],
+    src_off, dst_off, cb*sizeof(char), 0, NULL, NULL));
+}
+
+int tim_subtract(struct timeval *y, struct timeval *x, struct timeval *result){
+  if ( x->tv_sec > y->tv_sec )
+    return   -1;
+
+  if ((x->tv_sec == y->tv_sec) && (x->tv_usec > y->tv_usec))
+    return   -1;
+
+  if ( result != NULL){
+    result->tv_sec = ( y->tv_sec - x->tv_sec );
+    result->tv_usec = ( y->tv_usec - x->tv_usec );
+
+    if (result->tv_usec < 0){
+      result->tv_sec --;
+      result->tv_usec += 1000000;
+    }
+  }
+
+  int msec = 1000.0*(y->tv_sec - x->tv_sec) + (y->tv_usec - x->tv_usec)/1000.0;
+  return msec;
+}
+
+
+int enqueue_copy_buf(void)
+{
+  size_t i;
+  const size_t sz = 127 *1023 * 1023;
+  struct timeval start,stop;
+
+  gettimeofday(&start,0);
+
+  for (i=0; i<10; i++) {
+    test_copy_buf(sz, 0, 0, sz);
+  }
+
+  gettimeofday(&stop,0);
+  return tim_subtract(&stop, &start, 0);
+}
+
+MAKE_UPERFORMANCE_FROM_FUNCTION(enqueue_copy_buf);
diff --git a/utests/uperformance/uperformance_run.cpp b/utests/uperformance/uperformance_run.cpp
new file mode 100644
index 0000000..b29ccc3
--- /dev/null
+++ b/utests/uperformance/uperformance_run.cpp
@@ -0,0 +1,117 @@
+/*
+ * Copyright © 2012 Intel Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Benjamin Segovia <benjamin.segovia at intel.com>
+ */
+
+/**
+ * \file utest_run.cpp
+ * \author Benjamin Segovia <benjamin.segovia at intel.com>
+ *
+ * Just run the unit tests. The user can possibly provides the subset of it
+ */
+#include "utest_helper.hpp"
+#include "utest_exception.hpp"
+#include <iostream>
+#include <getopt.h>
+
+static const char *shortopts = "c:lanh";
+struct option longopts[] = {
+{"casename", required_argument, NULL, 'c'},
+{"list", no_argument, NULL, 'l'},
+{"all", no_argument, NULL, 'a'},
+{"allnoissue", no_argument, NULL, 'n'},
+{"help", no_argument, NULL, 'h'},
+{0, 0, 0, 0},
+};
+
+void usage()
+{
+    std::cout << "\
+Usage:\n\
+  ./utest_run <option>\n\
+\n\
+  option:\n\
+    -c <casename>: run sub-case named 'casename'\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\
+    -h           : display this usage\n\
+\
+    "<< std::endl;
+}
+
+int main(int argc, char *argv[])
+{
+
+  int c = 0;
+  cl_ocl_init();
+
+  c = getopt_long (argc, argv, shortopts, longopts, NULL);
+
+  if (argc == 1)
+    c = 'n';
+  if (argc == 2 && c < 1 ){
+    c = 'c';
+    optarg = argv[1];
+  }
+
+  do {
+    switch (c)
+    {
+      case 'c':
+        try {
+          UTest::run(optarg);
+        }
+        catch (Exception e){
+          std::cout << "  " << e.what() << "    [SUCCESS]" << std::endl;
+        }
+
+        break;
+
+      case 'l':
+        UTest::listAllCases();
+        break;
+
+      case 'a':
+        try {
+          UTest::runAll();
+        }
+        catch (Exception e){
+          std::cout << "  " << e.what() << "    [SUCCESS]" << std::endl;
+        }
+
+        break;
+
+      case 'n':
+        try {
+          UTest::runAllNoIssue();
+        }
+        catch (Exception e){
+          std::cout << "  " << e.what() << "    [SUCCESS]" << std::endl;
+        }
+
+        break;
+
+      case 'h':
+      default:
+        usage();
+        exit(1);
+    }
+  } while ((c = getopt_long (argc, argv, shortopts, longopts, NULL)) != -1);
+
+  cl_ocl_destroy();
+}
diff --git a/utests/utest/utest.hpp b/utests/utest/utest.hpp
index 01d4a8c..dc47c71 100644
--- a/utests/utest/utest.hpp
+++ b/utests/utest/utest.hpp
@@ -78,6 +78,11 @@ struct UTest
   static void __ANON__##FN##__(void) { UTEST_EXPECT_SUCCESS(FN()); } \
   static const UTest __##FN##__(__ANON__##FN##__, #FN, true);
 
+/*! Turn a function into a unit performance test */
+#define MAKE_UPERFORMANCE_FROM_FUNCTION(FN) \
+  static void __ANON__##FN##__(void) { UPERFORMANCE(FN()); } \
+  static const UTest __##FN##__(__ANON__##FN##__, #FN);
+
 
 /*! No assert is expected */
 #define UTEST_EXPECT_SUCCESS(EXPR) \
@@ -103,5 +108,17 @@ struct UTest
     } \
   } while (0)
 
-#endif /* __UTEST_UTEST_HPP__ */
+#define UPERFORMANCE(EXPR) \
+ do { \
+    int ret = 0; \
+    try { \
+      ret = EXPR; \
+      printf("  %s  [SUCCESS] [Result: %d]\n", #EXPR, ret);\
+    } \
+    catch (Exception e) { \
+      std::cout << "  " << #EXPR << "    [FAILED]" << std::endl; \
+      std::cout << "    " << e.what() << std::endl; \
+    } \
+  } while (0)
 
+#endif /* __UTEST_UTEST_HPP__ */
-- 
1.8.5.3



More information about the Beignet mailing list