Bläddra i källkod

starpupy: use task_submit arguments starting from the second as the arguments of the function passed in

HE Kun 4 år sedan
förälder
incheckning
e520ec7da2
3 ändrade filer med 64 tillägg och 70 borttagningar
  1. 2 4
      starpupy/src/starpu/delay.py
  2. 19 16
      starpupy/src/starpu/starpu_task_wrapper.c
  3. 43 50
      starpupy/tests/starpu_py.py

+ 2 - 4
starpupy/src/starpu/delay.py

@@ -18,8 +18,6 @@ import asyncio
 
 def delayed(f):
 	def submit(*args,**kwargs):
-		async def fut_wait():
-			fut = task.task_submit(f, list(args))
-			res = await fut
-		asyncio.run(fut_wait())
+		fut = task.task_submit(f, *args,**kwargs)
+		return fut
 	return submit

+ 19 - 16
starpupy/src/starpu/starpu_task_wrapper.c

@@ -51,14 +51,13 @@ void codelet_func(void *buffers[], void *cl_arg){
     }
     
     /*call the python function*/
-    PyObject *pRetVal = PyObject_CallObject(cst->f, PyList_AsTuple(cst->argList));
+    PyObject *pRetVal = PyObject_CallObject(cst->f, cst->argList);
     cst->rv=pRetVal;
 
     Py_DECREF(cst->f);
-    for(int i = 0; i < PyList_Size(cst->argList); i++){
-        Py_DECREF(PyList_GetItem(cst->argList, i));
+    for(int i = 0; i < PyTuple_Size(cst->argList); i++){
+        Py_DECREF(PyTuple_GetItem(cst->argList, i));
     }
-    Py_DECREF(cst->argList);
 
     /*restore previous GIL state*/
     PyGILState_Release(state);
@@ -106,11 +105,14 @@ static PyObject* starpu_task_submit_wrapper(PyObject *self, PyObject *args){
     /*the args of function*/
     PyObject* pArgs;
 
-	if (!PyArg_ParseTuple(args, "OO", &func_py, &pArgs))
-		return NULL;
+    /*first argument in args is always the function*/
+    func_py = PyTuple_GetItem(args, 0); 
 
-	/*allocate a task structure and initialize it with default values*/
+	  /*allocate a task structure and initialize it with default values*/
     struct starpu_task *task=starpu_task_create();
+
+    // int PyObject_SetAttrString(fut, starpu_task, int(task))
+    
     /*allocate a codelet structure*/
     struct starpu_codelet *func_cl=(struct starpu_codelet*)malloc(sizeof(struct starpu_codelet));
     /*initialize func_cl with default values*/
@@ -126,16 +128,17 @@ static PyObject* starpu_task_submit_wrapper(PyObject *self, PyObject *args){
     Py_INCREF(fut);
     Py_INCREF(loop);
 
-    /*allocate a new list of length*/
-    cst->argList = PyList_New (PyList_Size(pArgs));
-
-    /*pass pArgs in argList*/
-    for(int i = 0; i < PyList_Size(pArgs); i++){
-        PyList_SetItem(cst->argList, i, pArgs);
-        Py_INCREF(PyList_GetItem(cst->argList, i));
+    /*pass args in argList*/
+    if (PyTuple_Size(args)==1)
+      cst->argList = PyTuple_New(0);
+    else{
+      cst->argList = PyTuple_New(PyTuple_Size(args)-1);
+      for (int i=0; i < PyTuple_Size(args)-1; i++){
+        PyObject* tmp=PyTuple_GetItem(args, i+1);
+        PyTuple_SetItem(cst->argList, i, tmp);
+        Py_INCREF(PyTuple_GetItem(cst->argList, i));
+     }
     }
-    cst->argList = pArgs;
-    Py_INCREF(pArgs);
 
     task->cl=func_cl;
     task->cl_arg=cst;

+ 43 - 50
starpupy/tests/starpu_py.py

@@ -23,12 +23,6 @@ def hello():
 	print ("Example 1:")
 	print ("Hello, world!")
 
-#submit function "hello"
-async def hello_wait():
-    fut = starpu.task_submit(hello,[])
-    await fut
-asyncio.run(hello_wait())
-
 #############################################################################
 
 #function no input no output
@@ -36,12 +30,6 @@ def func1():
 	print ("Example 2:")
 	print ("This is a function no input no output")
 
-#submit function "func1"
-async def func1_wait():
-    fut1 = starpu.task_submit(func1,[])
-    await fut1
-asyncio.run(func1_wait())
-
 ##############################################################################
 
 #using decorator wrap the function no input no output
@@ -51,9 +39,6 @@ def func1_deco():
 	print ("Example 3:")
 	print ("This is a function no input no output wrapped by the decorator function")
 
-#apply starpu.delayed(func1_deco())
-func1_deco()
-
 ##############################################################################
 
 #function no input return a value
@@ -61,27 +46,12 @@ def func2():
 	print ("Example 4:")
 	return 12
 
-#submit function "func2"
-async def func2_wait():
-    fut2 = starpu.task_submit(func2, [])
-    res2 = await fut2
-    #print the result of function
-    print("This is a function no input and the return value is", res2)
-asyncio.run(func2_wait())
-
 ###############################################################################
  
 #function has 2 int inputs and 1 int output
 def multi(a,b):
 	print ("Example 5:")
 	return a*b
-
-#submit function "multi"
-async def multi_wait():
-	fut3 = starpu.task_submit(multi, [2, 3])
-	res3=await fut3
-	print("The result of function multi is :", res3)
-asyncio.run(multi_wait())
 #print(multi(2, 3))
 
 ###############################################################################
@@ -90,13 +60,6 @@ asyncio.run(multi_wait())
 def add(a,b,c,d):
 	print ("Example 6:")
 	return a+b+c+d
-
-#submit function "add"
-async def add_wait():
-	fut4 = starpu.task_submit(add, [1.2, 2.5, 3.6, 4.9])
-	res4=await fut4
-	print("The result of function add is :", res4)
-asyncio.run(add_wait())
 #print(add(1.2, 2.5, 3.6, 4.9))
 
 ###############################################################################
@@ -105,13 +68,6 @@ asyncio.run(add_wait())
 def sub(a,b,c):
 	print ("Example 7:")
 	return a-b-c, a-b
-
-#submit function "sub"
-async def sub_wait():
-	fut5 = starpu.task_submit(sub, [6, 2, 5.9])
-	res5 = await fut5
-	print("The result of function sub is:", res5)
-asyncio.run(sub_wait())
 #print(sub(6, 2, 5.9))
 
 ###############################################################################
@@ -121,12 +77,49 @@ asyncio.run(sub_wait())
 def add_deco(a,b,c):
 	#time.sleep(1)
 	print ("Example 8:")
-	print ("This is a function with input wrapped by the decorator function:")
-	print ("The result of function is:", a, "+", b, "+", c, "=", a+b+c)
-
-#apply starpu.delayed(add_deco)
-add_deco(1,2,3)
+	print ("This is a function with input and output wrapped by the decorator function:")
+	return a+b+c
 
 ###############################################################################
 
-starpu.task_wait_for_all()
+async def main():
+	#submit function "hello"
+    fut = starpu.task_submit(hello)
+    await fut
+
+    #submit function "func1"
+    fut1 = starpu.task_submit(func1)
+    await fut1
+
+    #apply starpu.delayed(func1_deco())
+    await func1_deco()
+
+	#submit function "func2"
+    fut2 = starpu.task_submit(func2)
+    res2 = await fut2
+	#print the result of function
+    print("This is a function no input and the return value is", res2)
+
+    #submit function "multi"
+    fut3 = starpu.task_submit(multi, 2, 3)
+    res3 = await fut3
+    print("The result of function multi is :", res3)
+
+	#submit function "add"
+    fut4 = starpu.task_submit(add, 1.2, 2.5, 3.6, 4.9)
+    res4 = await fut4
+    print("The result of function add is :", res4)
+
+	#submit function "sub"
+    fut5 = starpu.task_submit(sub, 6, 2, 5.9)
+    res5 = await fut5
+    print("The result of function sub is:", res5)
+
+	#apply starpu.delayed(add_deco)
+    res6 = await add_deco(1,2,3)
+    print("The result of function is", res6)
+
+asyncio.run(main())
+
+
+#starpu.task_wait_for_all()