You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by cj...@apache.org on 2018/03/14 17:21:30 UTC

[incubator-mxnet] 04/10: cython timing tests

This is an automated email from the ASF dual-hosted git repository.

cjolivier01 pushed a commit to branch cython
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git

commit d78b76d41577e28ec67bf688a4d047a557828a0b
Author: Olivier <co...@amazon.com>
AuthorDate: Thu Mar 8 15:18:40 2018 -0800

    cython timing tests
---
 CMakeLists.txt                       |  2 +-
 cmake/Modules/FindCython.cmake       | 20 ++++++++++++++-----
 cmake/UseCython.cmake                | 18 +++++++++--------
 python/mxnet/cython/mxcython.pyx     | 12 ++++++++++--
 tests/python/unittest/test_cython.py | 38 ++++++++++++++++++++++++++++++------
 5 files changed, 68 insertions(+), 22 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 04f0795..a3eeb93 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -645,7 +645,7 @@ endif()
 # BEGIN Cython Build
 #
 include(cmake/CythonUtil.cmake)
-mxnet_external_build_cython(2 target_2)
+#mxnet_external_build_cython(2 target_2)
 mxnet_external_build_cython(3 target_3)
 
 if(INSTALL_CYTHON_INPLACE)
diff --git a/cmake/Modules/FindCython.cmake b/cmake/Modules/FindCython.cmake
index 132de50..d728f31 100644
--- a/cmake/Modules/FindCython.cmake
+++ b/cmake/Modules/FindCython.cmake
@@ -42,15 +42,25 @@
 # Use the Cython executable that lives next to the Python executable
 # if it is a local installation.
 
-if(PACKAGE_FIND_VERSION_MAJOR EQUAL 3)
-  set(CYTHON_EXE_NAMES cython3 cython.bat cython)
+message(STATUS "Searching for cython for version: ${PYTHON_VERSION_MAJOR}")
+
+unset(CYTHON_EXE_NAMES CACHE)
+if(PYTHON_VERSION_MAJOR EQUAL 3)
+  message(STATUS "-----_ PYTHON 3")
+  if(UNIX)
+    set(CYTHON_EXE_NAMES cython3 cython)
+  else()
+    set(CYTHON_EXE_NAMES cython3 cython.bat cython)
+  endif()
   message(STATUS " Looking for Cython version 3")
 else()
   set(CYTHON_EXE_NAMES cython.bat cython cython3)
 endif()
 
+unset(CYTHON_EXECUTABLE CACHE)
+
 if(PYTHONINTERP_FOUND)
-  get_filename_component( _python_path ${PYTHON_EXECUTABLE} PATH )
+  get_filename_component(_python_path ${PYTHON_EXECUTABLE} PATH)
   find_program(CYTHON_EXECUTABLE
     NAMES ${CYTHON_EXE_NAMES}
     HINTS ${_python_path}
@@ -59,11 +69,11 @@ else()
   find_program(CYTHON_EXECUTABLE NAMES ${CYTHON_EXE_NAMES})
 endif()
 
-include( FindPackageHandleStandardArgs )
+include(FindPackageHandleStandardArgs)
 find_package_handle_standard_args(Cython DEFAULT_MSG CYTHON_EXECUTABLE)
 
 if(CYTHON_FOUND)
   message(STATUS "Found Cython (executable: ${CYTHON_EXECUTABLE})")
-  mark_as_advanced( CYTHON_EXECUTABLE )
+  mark_as_advanced(CYTHON_EXECUTABLE)
 endif()
 
diff --git a/cmake/UseCython.cmake b/cmake/UseCython.cmake
index 26969dd..f442d72 100644
--- a/cmake/UseCython.cmake
+++ b/cmake/UseCython.cmake
@@ -103,6 +103,13 @@ if(PYTHONINTERP_FOUND)
   if(PYTHONLIBS_FOUND)
     set(PYTHON_DEBUG_LIBRARY ${PYTHON_LIBRARY})
     set(PYTHON_DEBUG_LIBRARIES ${PYTHON_DEBUG_LIBRARIES})
+
+    message(STATUS "PYTHONLIBS_VERSION_STRING: ${PYTHONLIBS_VERSION_STRING}")
+    string(REPLACE "." ";" PYTHON_VERSION_LIST ${PYTHONLIBS_VERSION_STRING})
+    list(GET PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
+    list(GET PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
+    list(GET PYTHON_VERSION_LIST 1 PYTHON_VERSION_PATCH)
+
     find_package(Cython ${python_libs_version} REQUIRED)
     if(CYTHON_FOUND)
       set(CYTHON${python_libs_version}_FOUND ${python_libs_version})
@@ -122,12 +129,6 @@ if(NOT CYTHON${python_libs_version}_FOUND)
   return()
 endif()
 
-message(STATUS "PYTHONLIBS_VERSION_STRING: ${PYTHONLIBS_VERSION_STRING}")
-string(REPLACE "." ";" PYTHON_VERSION_LIST ${PYTHONLIBS_VERSION_STRING})
-list(GET PYTHON_VERSION_LIST 0 PYTHON_VERSION_MAJOR)
-list(GET PYTHON_VERSION_LIST 1 PYTHON_VERSION_MINOR)
-list(GET PYTHON_VERSION_LIST 1 PYTHON_VERSION_PATCH)
-
 if(NOT PYTHON_VERSION_MAJOR EQUAL ${python_libs_version})
   message(WARNING "Scripts found wrong python major version: ${PYTHON_VERSION_MAJOR} instead of ${python_libs_version}. This is most likely due to version ${python_libs_version} not being installed or not found")
   unset(PYTHONLIBS_FOUND)
@@ -142,7 +143,7 @@ set( CYTHON_C_EXTENSION "c" )
 # Create a *.c or *.cxx file from a *.pyx file.
 # Input the generated file basename.  The generate file will put into the variable
 # placed in the "generated_file" argument. Finally all the *.py and *.pyx files.
-function( compile_pyx _name c_cxx_output_subdir debug_output_dir generated_file )
+function(compile_pyx _name c_cxx_output_subdir debug_output_dir generated_file)
   # Default to assuming all files are C.
   set( cxx_arg "" )
   set( extension ${CYTHON_C_EXTENSION} )
@@ -353,9 +354,10 @@ function( cython_add_module _name c_cxx_output_subdir debug_output_dir)
 endfunction()
 
 include( CMakeParseArguments )
+
 # cython_add_standalone_executable( _name [MAIN_MODULE src3.py] src1 src2 ... srcN )
 # Creates a standalone executable the given sources.
-function( cython_add_standalone_executable _name debug_output_dir)
+function(cython_add_standalone_executable _name debug_output_dir)
   set( pyx_module_sources "" )
   set( other_module_sources "" )
   set( main_module "" )
diff --git a/python/mxnet/cython/mxcython.pyx b/python/mxnet/cython/mxcython.pyx
index 98b102c..ed3601d 100644
--- a/python/mxnet/cython/mxcython.pyx
+++ b/python/mxnet/cython/mxcython.pyx
@@ -43,7 +43,6 @@ cdef extern from "../../../src/cython/cpp_api.h" namespace "shapes":
         void getSize(int* width, int* height)
         void move(int, int)
 
-
 # Cython class: CythonTestClass
 cdef class CythonTestClass:
     """Symbol is symbolic graph."""
@@ -59,6 +58,8 @@ cdef class CythonTestClass:
         print('AFTER CythonPrintFromCPP')
         print('CythonTestClass::print_something( {} )'.format(the_string))
 
+# mxnet.cython.cy3.mxcython.def test_cpp_class():
+
 def test_cpp_class():
     cdef int recArea
     rec_ptr = new Rectangle(1, 2, 3, 4)
@@ -80,7 +81,14 @@ def test_perf(int count, int make_c_call):
       TrivialCPPCall(0)
     i += 1
   cdef unsigned long long stop = TimeInMilliseconds()
-  Printf("CYTHON: %d items took %f seconds\n", count, float(stop - start)/1000)
+  cdef char *msg = ""
+  if make_c_call != 0:
+    msg = " WITH API CALL"
+  Printf("CYTHON %s: %d items took %f seconds\n", msg, count, float(stop - start)/1000)
+
+def bridge_c_call(int value, int make_c_call):
+  if make_c_call != 0:
+    return TrivialCPPCall(value)
 
 def print_pi(terms):
     print(float(0.0))
diff --git a/tests/python/unittest/test_cython.py b/tests/python/unittest/test_cython.py
index d9ccd7a..e5a7def 100644
--- a/tests/python/unittest/test_cython.py
+++ b/tests/python/unittest/test_cython.py
@@ -49,6 +49,7 @@ def test_basic_cython():
   # Test using a C++ class'
   mxc.test_cpp_class()
   mxc.test_perf(10, 1)
+  test_perf_bridge(10)
 
 
 def test_perf(count, make_c_call):
@@ -63,13 +64,38 @@ def test_perf(count, make_c_call):
       _LIB.TrivialCPPCall(0)
     i += 1
   stop = _LIB.TimeInMilliseconds()
-  print("PYTHON: {} items took {} seconds".format(count, float(stop - start)/1000))
+  msg = ""
+  if make_c_call != 0:
+    msg = " WITH API CALL"
+  print("PYTHON {}: {} items took {} seconds".format(msg, count, float(stop - start)/1000))
+
+def test_perf_bridge(count, make_c_call):
+  mcc = int(make_c_call)
+  start = _LIB.TimeInMilliseconds()
+  foo = 0
+  i = 0
+  while i < count:
+    foo += i
+    if foo > count:
+      foo = 0
+    if make_c_call != 0:
+      mxc.bridge_c_call(0, mcc)
+    i += 1
+  stop = _LIB.TimeInMilliseconds()
+  msg = ""
+  if make_c_call != 0:
+    msg = " WITH API CALL"
+  print("PYTHON->CYTHON BRIDGE {}: {} items took {} seconds".format(msg, count, float(stop - start)/1000))
+
 
 if __name__ == '__main__':
   # import nose
   # nose.runmodule()
-  # test_perf(100000000, 0)
-  # mxc.test_perf(100000000, 0)
-  # test_perf(100000000, 1)
-  # mxc.test_perf(100000000, 1)
-  test_basic_cython()
+  iter_count = 100000000
+  test_perf(iter_count, 0)
+  mxc.test_perf(iter_count, 0)
+  test_perf(iter_count, 1)
+  mxc.test_perf(iter_count, 1)
+  #test_basic_cython()
+  test_perf_bridge(iter_count, 0)
+  test_perf_bridge(iter_count, 1)

-- 
To stop receiving notification emails like this one, please contact
cjolivier01@apache.org.