Browse Source

starpupy: fix code when numpy is not available

Nathalie Furmento 4 years ago
parent
commit
f34ff54983

+ 2 - 1
configure.ac

@@ -3404,6 +3404,7 @@ then
 	AC_SUBST(PYTHON_SETUP_OPTIONS)
 	AC_SUBST(PYTHON_SETUP_OPTIONS)
 fi
 fi
 AM_CONDITIONAL(STARPU_BUILD_STARPUPY, [test x$starpupy_support = xyes])
 AM_CONDITIONAL(STARPU_BUILD_STARPUPY, [test x$starpupy_support = xyes])
+AM_CONDITIONAL(STARPU_STARPUPY_NUMPY, [test x$numpy_avail = xyes])
 
 
 ##########################################
 ##########################################
 # Documentation                          #
 # Documentation                          #
@@ -3567,7 +3568,7 @@ AC_CONFIG_COMMANDS([executable-scripts], [
   test -e tools/starpu_trace_state_stats.py || ln -sf $ac_abs_top_srcdir/tools/starpu_trace_state_stats.py tools/starpu_trace_state_stats.py
   test -e tools/starpu_trace_state_stats.py || ln -sf $ac_abs_top_srcdir/tools/starpu_trace_state_stats.py tools/starpu_trace_state_stats.py
   chmod +x tools/starpu_trace_state_stats.py
   chmod +x tools/starpu_trace_state_stats.py
   chmod +x starpupy/examples/execute.sh
   chmod +x starpupy/examples/execute.sh
-  for x in starpupy/examples/starpu_py.sh starpupy/examples/starpu_py_parallel.sh; do
+  for x in starpupy/examples/starpu_py.sh starpupy/examples/starpu_py_np.sh starpupy/examples/starpu_py_parallel.sh; do
       test -e $x || mkdir -p $(dirname $x) && ln -sf $ac_abs_top_srcdir/$x $(dirname $x)
       test -e $x || mkdir -p $(dirname $x) && ln -sf $ac_abs_top_srcdir/$x $(dirname $x)
   done
   done
   chmod +x julia/examples/execute.sh
   chmod +x julia/examples/execute.sh

+ 7 - 1
starpupy/examples/Makefile.am

@@ -24,11 +24,17 @@ TESTS	=
 TESTS	+=	starpu_py.sh
 TESTS	+=	starpu_py.sh
 TESTS	+=	starpu_py_parallel.sh
 TESTS	+=	starpu_py_parallel.sh
 
 
+if STARPU_STARPUPY_NUMPY
+TESTS	+=	starpu_py_np.sh
+endif
+
 EXTRA_DIST	=		\
 EXTRA_DIST	=		\
 	starpu_py_parallel.py	\
 	starpu_py_parallel.py	\
 	starpu_py_parallel.sh	\
 	starpu_py_parallel.sh	\
 	starpu_py.py		\
 	starpu_py.py		\
-	starpu_py.sh
+	starpu_py.sh		\
+	starpu_py_np.py		\
+	starpu_py_np.sh
 
 
 python_sourcesdir = $(libdir)/starpu/python
 python_sourcesdir = $(libdir)/starpu/python
 dist_python_sources_DATA	=	\
 dist_python_sources_DATA	=	\

+ 1 - 15
starpupy/examples/starpu_py.py

@@ -16,7 +16,6 @@
 import starpu
 import starpu
 import time
 import time
 import asyncio
 import asyncio
-import numpy as np
 
 
 ############################################################################
 ############################################################################
 #function no input no output print hello world
 #function no input no output print hello world
@@ -92,13 +91,6 @@ def sub_deco(x,a):
 
 
 ###############################################################################
 ###############################################################################
 
 
-def scal(a, t):
-	for i in range(len(t)):
-		t[i]=t[i]*a
-	return t
-
-t=np.array([1,2,3,4,5,6,7,8,9,10])
-
 async def main():
 async def main():
 	#submit function "hello"
 	#submit function "hello"
     fut = starpu.task_submit()(hello)
     fut = starpu.task_submit()(hello)
@@ -143,13 +135,7 @@ async def main():
     print("The first argument of this function is the result of Example 8")
     print("The first argument of this function is the result of Example 8")
     print("The result of function is", res7)
     print("The result of function is", res7)
 
 
-    fut8 = starpu.task_submit()(scal, 2, t)
-    res8 = await fut8
-    print("The result of Example 10 is", res8)
-    print("The return array is", t)
-    #print("The result type is", type(res8))
-
 asyncio.run(main())
 asyncio.run(main())
 
 
 
 
-#starpu.task_wait_for_all()
+#starpu.task_wait_for_all()

+ 40 - 0
starpupy/examples/starpu_py_np.py

@@ -0,0 +1,40 @@
+# StarPU --- Runtime system for heterogeneous multicore architectures.
+#
+# Copyright (C) 2020       Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
+#
+# StarPU 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.1 of the License, or (at
+# your option) any later version.
+#
+# StarPU 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 in COPYING.LGPL for more details.
+#
+import starpu
+import asyncio
+import numpy as np
+
+
+###############################################################################
+
+def scal(a, t):
+	for i in range(len(t)):
+		t[i]=t[i]*a
+	return t
+
+t=np.array([1,2,3,4,5,6,7,8,9,10])
+
+async def main():
+    fut8 = starpu.task_submit()(scal, 2, t)
+    res8 = await fut8
+    print("The result of Example 10 is", res8)
+    print("The return array is", t)
+    #print("The result type is", type(res8))
+
+asyncio.run(main())
+
+
+#starpu.task_wait_for_all()

+ 19 - 0
starpupy/examples/starpu_py_np.sh

@@ -0,0 +1,19 @@
+#!/bin/bash
+# StarPU --- Runtime system for heterogeneous multicore architectures.
+#
+# Copyright (C) 2020       Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
+#
+# StarPU 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.1 of the License, or (at
+# your option) any later version.
+#
+# StarPU 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 in COPYING.LGPL for more details.
+#
+
+$(dirname $0)/execute.sh starpu_py_np.py
+

+ 11 - 5
starpupy/src/starpu_task_wrapper.c

@@ -22,7 +22,10 @@
 
 
 #define PY_SSIZE_T_CLEAN
 #define PY_SSIZE_T_CLEAN
 #include <Python.h>
 #include <Python.h>
+
+#ifdef STARPU_PYTHON_HAVE_NUMPY
 #include <numpy/arrayobject.h>
 #include <numpy/arrayobject.h>
+#endif
 
 
 /*macro*/
 /*macro*/
 #if defined(Py_DEBUG) || defined(DEBUG)
 #if defined(Py_DEBUG) || defined(DEBUG)
@@ -167,11 +170,14 @@ static size_t sizebase (struct starpu_task *task, unsigned nimpl)
 	PyObject *obj=cst->rv;
 	PyObject *obj=cst->rv;
 	/*get the length of result*/
 	/*get the length of result*/
 	const char *tp = Py_TYPE(obj)->tp_name;
 	const char *tp = Py_TYPE(obj)->tp_name;
+#ifdef STARPU_PYTHON_HAVE_NUMPY
 	/*if the result is a numpy array*/ 
 	/*if the result is a numpy array*/ 
 	if (strcmp(tp, "numpy.ndarray")==0)
 	if (strcmp(tp, "numpy.ndarray")==0)
 		n = PyArray_SIZE(obj);
 		n = PyArray_SIZE(obj);
+	else
+#endif
 	/*if the result is a list*/
 	/*if the result is a list*/
-	else if (strcmp(tp, "list")==0)
+	if (strcmp(tp, "list")==0)
 		n = PyList_Size(obj);
 		n = PyList_Size(obj);
 	/*else error*/
 	/*else error*/
 	else
 	else
@@ -520,10 +526,10 @@ PyInit_starpupy(void)
 	assert(ret==0);
 	assert(ret==0);
 	/*python asysncio import*/
 	/*python asysncio import*/
 	asyncio_module = PyImport_ImportModule("asyncio");
 	asyncio_module = PyImport_ImportModule("asyncio");
-  #ifdef STARPU_PYTHON_HAVE_NUMPY
-  /*numpy import array*/
-  import_array();
-  #endif
+#ifdef STARPU_PYTHON_HAVE_NUMPY
+	/*numpy import array*/
+	import_array();
+#endif
 	/*module import initialization*/
 	/*module import initialization*/
 	return PyModule_Create(&starpupymodule);
 	return PyModule_Create(&starpupymodule);
 }
 }