Procházet zdrojové kódy

- Add the starpu_execute_on_each_worker function. This function makes it
possible to execute a function over each workers.
- A test is added to make sure that this function works properly too.

Cédric Augonnet před 15 roky
rodič
revize
86f0556a11

+ 1 - 0
include/starpu-util.h

@@ -212,6 +212,7 @@ void starpu_trace_user_event(unsigned code);
 /* Some helper functions for application using CUBLAS kernels */
 void starpu_helper_init_cublas(void);
 void starpu_helper_shutdown_cublas(void);
+void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where);
 
 #ifdef __cplusplus
 }

+ 1 - 0
src/Makefile.am

@@ -135,6 +135,7 @@ libstarpu_la_SOURCES = 						\
 	datawizard/interfaces/csr_filters.c			\
 	datawizard/interfaces/vector_filters.c			\
 	util/malloc.c						\
+	util/execute_on_all.c					\
 	util/starpu_cublas.c
 	
 if USE_CPU

+ 86 - 0
src/util/execute_on_all.c

@@ -0,0 +1,86 @@
+/*
+ * StarPU
+ * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+
+#include <starpu.h>
+#include <common/config.h>
+
+typedef struct wrapper_func_args {
+	void (*func)(void *);
+	void *arg;
+} _wrapper_func_args;
+
+static void wrapper_func(starpu_data_interface_t *buffers __attribute__ ((unused)), void *_args)
+{
+	_wrapper_func_args *args = _args;
+	args->func(args->arg);
+}
+
+/* execute func(arg) on each worker that matches the "where" flag */
+void starpu_execute_on_each_worker(void (*func)(void *), void *arg, uint32_t where)
+{
+	unsigned worker;
+	unsigned nworkers = starpu_get_worker_count();
+
+	/* create a wrapper codelet */
+	struct starpu_codelet_t wrapper_cl = {
+		.where = where,
+		.cuda_func = wrapper_func,
+		.core_func = wrapper_func,
+		/* XXX we do not handle Cell .. */
+		.nbuffers = 0,
+		.model = NULL
+	};
+
+	struct starpu_task *tasks[STARPU_NMAXWORKERS];
+
+	_wrapper_func_args args = {
+		.func = func,
+		.arg = arg
+	};
+
+	for (worker = 0; worker < nworkers; worker++)
+	{
+		tasks[worker] = starpu_task_create();
+
+		tasks[worker]->cl = &wrapper_cl;
+		tasks[worker]->cl_arg = &args;
+
+		tasks[worker]->execute_on_a_specific_worker = 1;
+		tasks[worker]->workerid = worker;
+
+		tasks[worker]->detach = 0;
+		tasks[worker]->destroy = 0;
+
+		int ret = starpu_submit_task(tasks[worker]);
+		if (ret == -ENODEV)
+		{
+			/* if the worker is not able to execute this tasks, we
+			 * don't insist as this means the worker is not
+			 * designated by the "where" bitmap */
+			starpu_task_destroy(tasks[worker]);
+			tasks[worker] = NULL;
+		}
+	}
+
+	for (worker = 0; worker < nworkers; worker++)
+	{
+		if (tasks[worker])
+		{
+			starpu_wait_task(tasks[worker]);
+			starpu_task_destroy(tasks[worker]);
+		}
+	}
+}

+ 4 - 0
tests/Makefile.am

@@ -83,6 +83,7 @@ check_PROGRAMS += 				\
 	errorcheck/starpu_init_noworker		\
 	helper/cublas_init			\
 	helper/pinned_memory			\
+	helper/execute_on_all			\
 	microbenchs/async-tasks-overhead	\
 	microbenchs/sync-tasks-overhead		\
 	microbenchs/tasks-overhead		\
@@ -132,6 +133,9 @@ helper_cublas_init_SOURCES =			\
 helper_pinned_memory_SOURCES =			\
 	helper/pinned_memory.c
 
+helper_execute_on_all_SOURCES =			\
+	helper/execute_on_all.c
+
 microbenchs_async_tasks_overhead_SOURCES =	\
 	microbenchs/async-tasks-overhead.c
 

+ 40 - 0
tests/helper/execute_on_all.c

@@ -0,0 +1,40 @@
+/*
+ * StarPU
+ * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
+ *
+ * This program 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.
+ *
+ * This program 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.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <starpu.h>
+#include <stdlib.h>
+
+void func(void *arg)
+{
+	int *ptr = arg;
+	STARPU_ASSERT(*ptr == 0x42);
+}
+
+int main(int argc, char **argv)
+{
+	starpu_init(NULL);
+
+	int arg = 0x42;
+
+	starpu_execute_on_each_worker(func, &arg, CORE|CUDA);
+
+	starpu_shutdown();
+
+	return 0;
+}