소스 검색

Fix worker initialization order: initialize fields before creating the first context.

Samuel Thibault 11 년 전
부모
커밋
5bd1d28ab6

+ 66 - 59
src/core/workers.c

@@ -383,7 +383,68 @@ static unsigned _starpu_may_launch_driver(struct starpu_conf *conf,
 struct itimerval prof_itimer;
 #endif
 
-void _starpu_worker_init(struct _starpu_worker *worker, unsigned fut_key)
+static void _starpu_worker_init(struct _starpu_worker *workerarg, struct _starpu_machine_config *pconfig)
+{
+	workerarg->config = pconfig;
+	STARPU_PTHREAD_MUTEX_INIT(&workerarg->mutex, NULL);
+	/* arch initialized by topology.c */
+	/* worker_mask initialized by topology.c */
+	/* perf_arch initialized by topology.c */
+	/* worker_thread initialized by _starpu_launch_drivers */
+	/* mp_nodeid initialized by topology.c */
+	/* devid initialized by topology.c */
+	/* bindid initialized by topology.c */
+	/* workerid initialized by topology.c */
+	workerarg->combined_workerid = workerarg->workerid;
+	workerarg->current_rank = 0;
+	workerarg->worker_size = 1;
+	STARPU_PTHREAD_COND_INIT(&workerarg->started_cond, NULL);
+	STARPU_PTHREAD_COND_INIT(&workerarg->ready_cond, NULL);
+	/* memory_node initialized by topology.c */
+	STARPU_PTHREAD_COND_INIT(&workerarg->sched_cond, NULL);
+	STARPU_PTHREAD_MUTEX_INIT(&workerarg->sched_mutex, NULL);
+	starpu_task_list_init(&workerarg->local_tasks);
+	workerarg->current_task = NULL;
+	workerarg->set = NULL;
+
+	/* if some codelet's termination cannot be handled directly :
+	 * for instance in the Gordon driver, Gordon tasks' callbacks
+	 * may be executed by another thread than that of the Gordon
+	 * driver so that we cannot call the push_codelet_output method
+	 * directly */
+	workerarg->terminated_jobs = _starpu_job_list_new();
+
+	workerarg->worker_is_running = 0;
+	workerarg->worker_is_initialized = 0;
+	workerarg->status = STATUS_INITIALIZING;
+	/* name initialized by driver */
+	/* short_name initialized by driver */
+	workerarg->run_by_starpu = 1;
+
+	workerarg->sched_ctx_list = NULL;
+	workerarg->nsched_ctxs = 0;
+	_starpu_barrier_counter_init(&workerarg->tasks_barrier, 0);
+
+	workerarg->has_prev_init = 0;
+
+	int ctx;
+	for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
+		workerarg->removed_from_ctx[ctx] = 0;
+
+	workerarg->spinning_backoff = 1;
+
+	STARPU_PTHREAD_COND_INIT(&workerarg->parallel_sect_cond, NULL);
+	STARPU_PTHREAD_MUTEX_INIT(&workerarg->parallel_sect_mutex, NULL);
+
+	workerarg->parallel_sect = 0;
+
+	for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
+		workerarg->shares_tasks_lists[ctx] = 0;
+
+	/* cpu_set/hwloc_cpu_set initialized in topology.c */
+}
+
+void _starpu_worker_start(struct _starpu_worker *worker, unsigned fut_key)
 {
 	(void) fut_key;
 	int devid = worker->devid;
@@ -445,64 +506,6 @@ static void _starpu_launch_drivers(struct _starpu_machine_config *pconfig)
 		unsigned mp_nodeid = workerarg->mp_nodeid;
 #endif
 
-		workerarg->config = pconfig;
-		STARPU_PTHREAD_MUTEX_INIT(&workerarg->mutex, NULL);
-		/* arch initialized by topology.c */
-		/* worker_mask initialized by topology.c */
-		/* perf_arch initialized by topology.c */
-		/* worker_thread initialized below */
-		/* mp_nodeid initialized by topology.c */
-		/* devid initialized by topology.c */
-		/* bindid initialized by topology.c */
-		/* workerid initialized by topology.c */
-		workerarg->combined_workerid = workerarg->workerid;
-		workerarg->current_rank = 0;
-		workerarg->worker_size = 1;
-		STARPU_PTHREAD_COND_INIT(&workerarg->started_cond, NULL);
-		STARPU_PTHREAD_COND_INIT(&workerarg->ready_cond, NULL);
-		/* memory_node initialized by topology.c */
-		STARPU_PTHREAD_COND_INIT(&workerarg->sched_cond, NULL);
-		STARPU_PTHREAD_MUTEX_INIT(&workerarg->sched_mutex, NULL);
-		starpu_task_list_init(&workerarg->local_tasks);
-		workerarg->current_task = NULL;
-		workerarg->set = NULL;
-
-		/* if some codelet's termination cannot be handled directly :
-		 * for instance in the Gordon driver, Gordon tasks' callbacks
-		 * may be executed by another thread than that of the Gordon
-		 * driver so that we cannot call the push_codelet_output method
-		 * directly */
-		workerarg->terminated_jobs = _starpu_job_list_new();
-
-		workerarg->worker_is_running = 0;
-		workerarg->worker_is_initialized = 0;
-		workerarg->status = STATUS_INITIALIZING;
-		/* name initialized by driver */
-		/* short_name initialized by driver */
-		workerarg->run_by_starpu = 1;
-
-		workerarg->sched_ctx_list = NULL;
-		workerarg->nsched_ctxs = 0;
-		_starpu_barrier_counter_init(&workerarg->tasks_barrier, 0);
-
-		workerarg->has_prev_init = 0;
-
-		int ctx;
-		for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
-			workerarg->removed_from_ctx[ctx] = 0;
-
-		workerarg->spinning_backoff = 1;
-
-		STARPU_PTHREAD_COND_INIT(&workerarg->parallel_sect_cond, NULL);
-		STARPU_PTHREAD_MUTEX_INIT(&workerarg->parallel_sect_mutex, NULL);
-
-		workerarg->parallel_sect = 0;
-
-		for(ctx = 0; ctx < STARPU_NMAX_SCHED_CTXS; ctx++)
-			workerarg->shares_tasks_lists[ctx] = 0;
-
-		/* cpu_set/hwloc_cpu_set initialized in topology.c */
-
 		_STARPU_DEBUG("initialising worker %u/%u\n", worker, nworkers);
 
 		_starpu_init_worker_queue(workerarg);
@@ -869,6 +872,7 @@ int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
 {
 	int is_a_sink = 0; /* Always defined. If the MP infrastructure is not
 			    * used, we cannot be a sink. */
+	unsigned worker;
 #ifdef STARPU_USE_MP
 	_starpu_set_argc_argv(argc, argv);
 
@@ -1020,6 +1024,9 @@ int starpu_initialize(struct starpu_conf *user_conf, int *argc, char ***argv)
 	 * threads */
 	_starpu_initialize_current_task_key();
 
+	for (worker = 0; worker < config.topology.nworkers; worker++)
+		_starpu_worker_init(&config.workers[worker], &config);
+
 	if (!is_a_sink)
 	{
 		struct starpu_sched_policy *selected_policy = _starpu_select_sched_policy(&config, config.conf->sched_policy_name);

+ 2 - 2
src/core/workers.h

@@ -52,7 +52,7 @@
 
 #include <starpu_parameters.h>
 
-/* This is initialized from in _starpu_launch_drivers */
+/* This is initialized from in _starpu_worker_init */
 struct _starpu_worker
 {
 	struct _starpu_machine_config *config;
@@ -342,7 +342,7 @@ void _starpu_block_worker(int workerid, starpu_pthread_cond_t *cond, starpu_pthr
 void _starpu_set_local_worker_key(struct _starpu_worker *worker);
 
 /* This function initializes the current thread for the given worker */
-void _starpu_worker_init(struct _starpu_worker *worker, unsigned fut_key);
+void _starpu_worker_start(struct _starpu_worker *worker, unsigned fut_key);
 
 /* Returns the _starpu_worker structure that describes the state of the
  * current worker. */

+ 1 - 1
src/drivers/cpu/driver_cpu.c

@@ -238,7 +238,7 @@ int _starpu_cpu_driver_init(struct starpu_driver *d)
 
 	int devid = cpu_worker->devid;
 
-	_starpu_worker_init(cpu_worker, _STARPU_FUT_CPU_KEY);
+	_starpu_worker_start(cpu_worker, _STARPU_FUT_CPU_KEY);
 	/* FIXME: when we have NUMA support, properly turn node number into NUMA node number */
 	_starpu_memory_manager_set_global_memory_size(cpu_worker->memory_node, _starpu_cpu_get_global_mem_size(cpu_worker->memory_node, cpu_worker->config));
 

+ 1 - 1
src/drivers/cuda/driver_cuda.c

@@ -391,7 +391,7 @@ int _starpu_cuda_driver_init(struct starpu_driver *d)
 	STARPU_ASSERT(args);
 	unsigned devid = args->devid;
 
-	_starpu_worker_init(args, _STARPU_FUT_CUDA_KEY);
+	_starpu_worker_start(args, _STARPU_FUT_CUDA_KEY);
 
 #ifndef STARPU_SIMGRID
 	init_context(devid);

+ 1 - 1
src/drivers/mic/driver_mic_source.c

@@ -517,7 +517,7 @@ void *_starpu_mic_src_worker(void *arg)
 
 	/* unsigned memnode = baseworker->memory_node; */
 
-	_starpu_worker_init(baseworker, _STARPU_FUT_MIC_KEY);
+	_starpu_worker_start(baseworker, _STARPU_FUT_MIC_KEY);
 
 	// Current task for a thread managing a worker set has no sense.
 	_starpu_set_current_task(NULL);

+ 1 - 1
src/drivers/opencl/driver_opencl.c

@@ -599,7 +599,7 @@ int _starpu_opencl_driver_init(struct starpu_driver *d)
 	STARPU_ASSERT(args);
 	int devid = args->devid;
 
-	_starpu_worker_init(args, _STARPU_FUT_OPENCL_KEY);
+	_starpu_worker_start(args, _STARPU_FUT_OPENCL_KEY);
 
 #ifndef STARPU_SIMGRID
 	_starpu_opencl_init_context(devid);

+ 1 - 1
src/drivers/scc/driver_scc_source.c

@@ -291,7 +291,7 @@ void *_starpu_scc_src_worker(void *arg)
 	unsigned mp_nodeid = args->mp_nodeid;
 	unsigned i;
 
-	_starpu_worker_init(args, _STARPU_FUT_SCC_KEY);
+	_starpu_worker_start(args, _STARPU_FUT_SCC_KEY);
 
 	_starpu_scc_src_init_context(devid);