浏览代码

Let tags be signaled several times. Add an example for that.

Samuel Thibault 13 年之前
父节点
当前提交
bc938d8482

+ 4 - 2
examples/Makefile.am

@@ -166,8 +166,9 @@ examplebin_PROGRAMS +=				\
 	filters/fblock				\
 	filters/fmatrix				\
 	tag_example/tag_example			\
-	tag_example/tag_example3		\
 	tag_example/tag_example2		\
+	tag_example/tag_example3		\
+	tag_example/tag_example4		\
 	tag_example/tag_restartable		\
 	spmv/spmv				\
 	callback/callback			\
@@ -229,8 +230,9 @@ STARPU_EXAMPLES +=				\
 	filters/fblock				\
 	filters/fmatrix				\
 	tag_example/tag_example			\
-	tag_example/tag_example3		\
 	tag_example/tag_example2		\
+	tag_example/tag_example3		\
+	tag_example/tag_example4		\
 	tag_example/tag_restartable		\
 	spmv/spmv				\
 	callback/callback			\

+ 12 - 1
examples/tag_example/tag_example.c

@@ -1,6 +1,6 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
- * Copyright (C) 2009, 2010  Université de Bordeaux 1
+ * Copyright (C) 2009-2010, 2012  Université de Bordeaux 1
  * Copyright (C) 2010, 2011, 2012  Centre National de la Recherche Scientifique
  *
  * StarPU is free software; you can redistribute it and/or modify
@@ -15,6 +15,17 @@
  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  */
 
+/* This example shows how to use tags to define a grid of dependencies, shaped this way:
+ *
+ *           ...               ...
+ *            v                 v
+ * ... -> task (i,  j) --> task (i,  j+1) --> ...
+ *            v                 v
+ * ... -> task (i+1,j) --> task (i+1,j+1) --> ...
+ *            v                 v
+ *           ...               ...
+ */
+
 #include <string.h>
 #include <math.h>
 #include <sys/types.h>

+ 16 - 9
examples/tag_example/tag_example2.c

@@ -15,6 +15,13 @@
  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  */
 
+/* This example shows how to submit a series of tasks in a chain of dependency:
+ *
+ * ... -> task (i) --> task (i+1) --> ...
+ *
+ * This is repeated several times
+ */
+
 #include <string.h>
 #include <math.h>
 #include <sys/types.h>
@@ -63,19 +70,20 @@ static void parse_args(int argc, char **argv)
 
 void callback_cpu(void *argcb);
 
-static void tag_cleanup_grid(unsigned ni, unsigned it)
+static void tag_cleanup_grid(unsigned ni, unsigned iter)
 {
 	unsigned i;
+
 	for (i = 0; i < ni; i++)
-		starpu_tag_remove(TAG(i,it));
+		starpu_tag_remove(TAG(i,iter));
 } 
 
-static void create_task_grid(unsigned it)
+static void create_task_grid(unsigned iter)
 {
-	unsigned i;
+	int i;
 	int ret;
 
-/*	FPRINTF(stderr, "start iter %d ni %d...\n", it, ni); */
+/*	FPRINTF(stderr, "start iter %d ni %d...\n", iter, ni); */
 
 	for (i = 0; i < ni; i++)
 	{
@@ -86,10 +94,10 @@ static void create_task_grid(unsigned it)
 		task->cl_arg = NULL;
 
 		task->use_tag = 1;
-		task->tag_id = TAG(i, it);
+		task->tag_id = TAG(i, iter);
 
 		if (i != 0)
-			starpu_tag_declare_deps(TAG(i,it), 1, TAG(i-1,it));
+			starpu_tag_declare_deps(TAG(i,iter), 1, TAG(i-1,iter));
 
 		ret = starpu_task_submit(task);
 		STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
@@ -97,8 +105,7 @@ static void create_task_grid(unsigned it)
 
 }
 
-void cpu_codelet(void *descr[] __attribute__ ((unused)),
-			void *_args __attribute__ ((unused)))
+void cpu_codelet(void *descr[] __attribute__ ((unused)), void *_args __attribute__ ((unused)))
 {
 }
 

+ 10 - 5
examples/tag_example/tag_example3.c

@@ -15,6 +15,15 @@
  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  */
 
+/* This example shows how to submit a series of tasks in a chain of dependency:
+ *
+ * ... -> task (i) --> task (i+1) --> ...
+ *
+ * but here submitted in reverse order.
+ *
+ * This is repeated several times
+ */
+
 #include <string.h>
 #include <math.h>
 #include <sys/types.h>
@@ -68,11 +77,7 @@ static void tag_cleanup_grid(unsigned ni, unsigned iter)
 	unsigned i;
 
 	for (i = 0; i < ni; i++)
-	{
 		starpu_tag_remove(TAG(i,iter));
-	}
-
-
 } 
 
 static void create_task_grid(unsigned iter)
@@ -102,7 +107,7 @@ static void create_task_grid(unsigned iter)
 
 }
 
-void cpu_codelet(void *descr[], void *_args __attribute__ ((unused)))
+void cpu_codelet(void *descr[] __attribute__ ((unused)), void *_args __attribute__ ((unused)))
 {
 }
 

+ 148 - 0
examples/tag_example/tag_example4.c

@@ -0,0 +1,148 @@
+/* StarPU --- Runtime system for heterogeneous multicore architectures.
+ *
+ * Copyright (C) 2009, 2010, 2012  Université de Bordeaux 1
+ * Copyright (C) 2010, 2011, 2012  Centre National de la Recherche Scientifique
+ *
+ * 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.
+ */
+
+/* This example shows how to make a task depend on either of a series of tasks.
+ *
+ * For each i, we submit i tasks of type A, which fill the i-th variable, and i
+ * tasks of type B, which check that the i-th variable is filled.  Thanks to
+ * tag dependency, B tasks are scheduled as soon as one of the corresponding A
+ * task is finished.
+ */
+
+#include <string.h>
+#include <math.h>
+#include <sys/types.h>
+#include <pthread.h>
+#include <signal.h>
+
+#include <starpu.h>
+
+#define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
+#define TAG(i, iter)	((starpu_tag_t)  (((uint64_t)i)<<32 | (iter)) )
+
+void cpu_codelet_A(void *descr[], void *_args)
+{
+	int *arg = _args;
+	*arg = 1;
+	fprintf(stderr,"A");
+}
+
+void cpu_codelet_B(void *descr[], void *_args)
+{
+	int *arg = _args;
+	STARPU_ASSERT(*arg == 1);
+	fprintf(stderr,"B");
+}
+
+struct starpu_codelet cl_A = {
+	.cpu_funcs = { cpu_codelet_A, NULL},
+	.cuda_funcs = { cpu_codelet_A, NULL},
+	.where = STARPU_CPU|STARPU_CUDA,
+	.nbuffers = 0,
+};
+
+struct starpu_codelet cl_B = {
+	.cpu_funcs = { cpu_codelet_B, NULL},
+	.cuda_funcs = { cpu_codelet_B, NULL},
+	.where = STARPU_CPU|STARPU_CUDA,
+	.nbuffers = 0,
+};
+
+#define Ni	64
+
+static unsigned ni = Ni;
+
+static void parse_args(int argc, char **argv)
+{
+	int i;
+	for (i = 1; i < argc; i++)
+	{
+		if (strcmp(argv[i], "-iter") == 0)
+		{
+		        char *argptr;
+			ni = strtol(argv[++i], &argptr, 10);
+		}
+
+		if (strcmp(argv[i], "-h") == 0)
+		{
+			printf("usage : %s [-iter iter]\n", argv[0]);
+		}
+	}
+}
+
+int main(int argc __attribute__((unused)) , char **argv __attribute__((unused)))
+{
+	unsigned i, j;
+	int ret;
+
+	ret = starpu_init(NULL);
+	if (ret == -ENODEV)
+		exit(77);
+	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
+
+	parse_args(argc, argv);
+
+	FPRINTF(stderr, "ITER : %u\n", ni);
+
+	{
+		int array[ni];
+
+		memset(array, 0, sizeof(array));
+
+		for (i = 1; i < ni; i++)
+		{
+			for (j = 1; j < i; j++) {
+				struct starpu_task *task_A = starpu_task_create();
+				task_A->cl = &cl_A;
+				task_A->cl_arg = &array[i];
+				task_A->use_tag = 1;
+				task_A->tag_id = TAG(0, i);
+
+				ret = starpu_task_submit(task_A);
+				STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
+			}
+
+			for (j = 1; j < i; j++) {
+				struct starpu_task *task_B = starpu_task_create();
+				task_B->cl = &cl_B;
+				task_B->cl_arg = &array[i];
+				task_B->use_tag = 1;
+				task_B->tag_id = TAG(j, i);
+
+				starpu_tag_declare_deps(TAG(j, i), 1, TAG(0, i));
+
+				ret = starpu_task_submit(task_B);
+				STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
+			}
+		}
+
+		starpu_task_wait_for_all();
+	}
+
+	for (i = 1; i < ni; i++)
+	{
+		starpu_tag_remove(TAG(0, i));
+		for (j = 1; j < i; j++)
+			starpu_tag_remove(TAG(j, i));
+	}
+
+	starpu_shutdown();
+
+	FPRINTF(stderr, "TEST DONE ...\n");
+
+	return 0;
+}

+ 5 - 0
src/core/dependencies/tags.c

@@ -195,6 +195,11 @@ void _starpu_notify_tag_dependencies(struct _starpu_tag *tag)
 {
 	_starpu_spin_lock(&tag->lock);
 
+	if (tag->state == STARPU_DONE) {
+		_starpu_spin_unlock(&tag->lock);
+		return;
+	}
+
 	tag->state = STARPU_DONE;
 	_STARPU_TRACE_TAG_DONE(tag);