Selaa lähdekoodia

Move simgrid platform file generation to its own function. Also implement _starpu_pthread_key_delete.

Samuel Thibault 12 vuotta sitten
vanhempi
commit
31c3809e54
1 muutettua tiedostoa jossa 105 lisäystä ja 93 poistoa
  1. 105 93
      src/core/simgrid.c

+ 105 - 93
src/core/simgrid.c

@@ -20,27 +20,8 @@
 #include <core/perfmodel/perfmodel.h>
 #include <core/perfmodel/perfmodel.h>
 #include <core/workers.h>
 #include <core/workers.h>
 
 
-#ifdef STARPU_SIMGRID
-#include <msg/msg.h>
-
-#define MAX_TSD 16
-
-#pragma weak starpu_main
-extern int starpu_main(int argc, char *argv[]);
-
 static struct starpu_conf conf;
 static struct starpu_conf conf;
 
 
-struct main_args {
-	int argc;
-	char **argv;
-};
-
-int do_starpu_main(int argc STARPU_ATTRIBUTE_UNUSED, char *argv[] STARPU_ATTRIBUTE_UNUSED)
-{
-	struct main_args *args = MSG_process_get_data(MSG_process_self());
-	return starpu_main(args->argc, args->argv);
-}
-
 static void bus_name(char *s, size_t size, int num)
 static void bus_name(char *s, size_t size, int num)
 {
 {
 	if (!num)
 	if (!num)
@@ -51,68 +32,10 @@ static void bus_name(char *s, size_t size, int num)
 		snprintf(s, size, "OpenCL%d", num - conf.ncuda - 1);
 		snprintf(s, size, "OpenCL%d", num - conf.ncuda - 1);
 }
 }
 
 
-#ifdef STARPU_DEVEL
-#warning TODO: use another way to start main, when simgrid provides it, and then include the application-provided configuration for platform numbers
-#endif
-#undef main
-int main(int argc, char **argv)
+void starpu_simgrid_write_platform(struct starpu_conf *conf, FILE *file)
 {
 {
-	xbt_dynar_t hosts;
 	int i, j;
 	int i, j;
-	char name[] = "/tmp/starpu-simgrid-platform.xml.XXXXXX";
-	int fd;
-	FILE *file;
-	struct starpu_machine_topology *topology = &_starpu_get_machine_config()->topology;
 
 
-	if (!starpu_main)
-	{
-		_STARPU_ERROR("The main file of this application needs to be compiled with starpu.h included, to properly define starpu_main\n");
-		exit(EXIT_FAILURE);
-	}
-
-	MSG_init(&argc, argv);
-#if SIMGRID_VERSION_MAJOR < 3 || (SIMGRID_VERSION_MAJOR == 3 && SIMGRID_VERSION_MINOR < 9)
-	/* Versions earlier than 3.9 didn't support our communication tasks */
-	MSG_config("workstation/model", "ptask_L07");
-#endif
-
-	/* Create platform file */
-	starpu_conf_init(&conf);
-	if ((!getenv("STARPU_NCPUS") && !getenv("STARPU_NCPU"))
-#ifdef STARPU_USE_CUDA
-	 || !getenv("STARPU_NCUDA")
-#endif
-#ifdef STARPU_USE_OPENCL
-	 || !getenv("STARPU_NOPENCL")
-#endif
-			)
-	{
-		_STARPU_ERROR("Please specify the number of cpus and gpus by setting the environment variables STARPU_NCPU%s%s\n",
-#ifdef STARPU_USE_CUDA
-			      ", STARPU_NCUDA",
-#else
-			      "",
-#endif
-#ifdef STARPU_USE_OPENCL
-			      ", STARPU_NOPENCL"
-#else
-			      ""
-#endif
-			);
-		exit(EXIT_FAILURE);
-	}
-	_starpu_conf_check_environment(&conf);
-
-	_starpu_load_bus_performance_files();
-
-	topology->ncpus = conf.ncpus;
-	topology->ncudagpus = conf.ncuda;
-	topology->nopenclgpus = conf.nopencl;
-
-	/* TODO: rather use simgrid/platf.h */
-	/* TODO: but still permit the user to provide his own xml */
-	fd = mkstemp(name);
-	file = fdopen(fd, "w");
 	fprintf(file,
 	fprintf(file,
 "<?xml version='1.0'?>\n"
 "<?xml version='1.0'?>\n"
 " <!DOCTYPE platform SYSTEM 'http://simgrid.gforge.inria.fr/simgrid.dtd'>\n"
 " <!DOCTYPE platform SYSTEM 'http://simgrid.gforge.inria.fr/simgrid.dtd'>\n"
@@ -126,20 +49,20 @@ int main(int argc, char **argv)
 "   <host id='MAIN' power='1'/>\n"
 "   <host id='MAIN' power='1'/>\n"
 		);
 		);
 
 
-	for (i = 0; i < conf.ncpus; i++)
+	for (i = 0; i < conf->ncpus; i++)
 		fprintf(file, "   <host id='CPU%d' power='2000000000'/>\n", i);
 		fprintf(file, "   <host id='CPU%d' power='2000000000'/>\n", i);
 
 
-	for (i = 0; i < conf.ncuda; i++)
+	for (i = 0; i < conf->ncuda; i++)
 		fprintf(file, "   <host id='CUDA%d' power='2000000000'/>\n", i);
 		fprintf(file, "   <host id='CUDA%d' power='2000000000'/>\n", i);
 
 
-	for (i = 0; i < conf.nopencl; i++)
+	for (i = 0; i < conf->nopencl; i++)
 		fprintf(file, "   <host id='OpenCL%d' power='2000000000'/>\n", i);
 		fprintf(file, "   <host id='OpenCL%d' power='2000000000'/>\n", i);
 
 
 	fprintf(file, "\n   <host id='RAM' power='1'/>\n");
 	fprintf(file, "\n   <host id='RAM' power='1'/>\n");
 
 
 	/* Compute maximum bandwidth, taken as machine bandwidth */
 	/* Compute maximum bandwidth, taken as machine bandwidth */
 	double max_bandwidth = 0;
 	double max_bandwidth = 0;
-	for (i = 1; i < conf.ncuda + conf.nopencl + 1; i++)
+	for (i = 1; i < conf->ncuda + conf->nopencl + 1; i++)
 	{
 	{
 		if (max_bandwidth < _starpu_transfer_bandwidth(0, i))
 		if (max_bandwidth < _starpu_transfer_bandwidth(0, i))
 			max_bandwidth = _starpu_transfer_bandwidth(0, i);
 			max_bandwidth = _starpu_transfer_bandwidth(0, i);
@@ -148,12 +71,12 @@ int main(int argc, char **argv)
 	}
 	}
 	fprintf(file, "\n   <link id='Share' bandwidth='%f' latency='0.000000'/>\n\n", max_bandwidth*1000000);
 	fprintf(file, "\n   <link id='Share' bandwidth='%f' latency='0.000000'/>\n\n", max_bandwidth*1000000);
 
 
-	for (i = 0; i < conf.ncuda + conf.nopencl + 1; i++)
+	for (i = 0; i < conf->ncuda + conf->nopencl + 1; i++)
 	{
 	{
 		char i_name[16];
 		char i_name[16];
 		bus_name(i_name, sizeof(i_name), i);
 		bus_name(i_name, sizeof(i_name), i);
 
 
-		for (j = 0; j < conf.ncuda + conf.nopencl + 1; j++)
+		for (j = 0; j < conf->ncuda + conf->nopencl + 1; j++)
 		{
 		{
 			char j_name[16];
 			char j_name[16];
 			if (j == i)
 			if (j == i)
@@ -166,12 +89,12 @@ int main(int argc, char **argv)
 		}
 		}
 	}
 	}
 
 
-	for (i = 0; i < conf.ncuda + conf.nopencl + 1; i++)
+	for (i = 0; i < conf->ncuda + conf->nopencl + 1; i++)
 	{
 	{
 		char i_name[16];
 		char i_name[16];
 		bus_name(i_name, sizeof(i_name), i);
 		bus_name(i_name, sizeof(i_name), i);
 
 
-		for (j = 0; j < conf.ncuda + conf.nopencl + 1; j++)
+		for (j = 0; j < conf->ncuda + conf->nopencl + 1; j++)
 		{
 		{
 			char j_name[16];
 			char j_name[16];
 			if (j == i)
 			if (j == i)
@@ -188,6 +111,90 @@ int main(int argc, char **argv)
 " </platform>\n"
 " </platform>\n"
 		);
 		);
 	fclose(file);
 	fclose(file);
+}
+
+#ifdef STARPU_SIMGRID
+#include <msg/msg.h>
+
+#define MAX_TSD 16
+
+#pragma weak starpu_main
+extern int starpu_main(int argc, char *argv[]);
+
+struct main_args {
+	int argc;
+	char **argv;
+};
+
+int do_starpu_main(int argc STARPU_ATTRIBUTE_UNUSED, char *argv[] STARPU_ATTRIBUTE_UNUSED)
+{
+	struct main_args *args = MSG_process_get_data(MSG_process_self());
+	return starpu_main(args->argc, args->argv);
+}
+
+#ifdef STARPU_DEVEL
+#warning TODO: use another way to start main, when simgrid provides it, and then include the application-provided configuration for platform numbers
+#endif
+#undef main
+int main(int argc, char **argv)
+{
+	xbt_dynar_t hosts;
+	int i;
+	char name[] = "/tmp/starpu-simgrid-platform.xml.XXXXXX";
+	int fd;
+	FILE *file;
+	struct starpu_machine_topology *topology = &_starpu_get_machine_config()->topology;
+
+	if (!starpu_main)
+	{
+		_STARPU_ERROR("The main file of this application needs to be compiled with starpu.h included, to properly define starpu_main\n");
+		exit(EXIT_FAILURE);
+	}
+
+	MSG_init(&argc, argv);
+#if SIMGRID_VERSION_MAJOR < 3 || (SIMGRID_VERSION_MAJOR == 3 && SIMGRID_VERSION_MINOR < 9)
+	/* Versions earlier than 3.9 didn't support our communication tasks */
+	MSG_config("workstation/model", "ptask_L07");
+#endif
+
+	/* Create platform file */
+	starpu_conf_init(&conf);
+	if ((!getenv("STARPU_NCPUS") && !getenv("STARPU_NCPU"))
+#ifdef STARPU_USE_CUDA
+	 || !getenv("STARPU_NCUDA")
+#endif
+#ifdef STARPU_USE_OPENCL
+	 || !getenv("STARPU_NOPENCL")
+#endif
+			)
+	{
+		_STARPU_ERROR("Please specify the number of cpus and gpus by setting the environment variables STARPU_NCPU%s%s\n",
+#ifdef STARPU_USE_CUDA
+			      ", STARPU_NCUDA",
+#else
+			      "",
+#endif
+#ifdef STARPU_USE_OPENCL
+			      ", STARPU_NOPENCL"
+#else
+			      ""
+#endif
+			);
+		exit(EXIT_FAILURE);
+	}
+	_starpu_conf_check_environment(&conf);
+
+	_starpu_load_bus_performance_files();
+
+	topology->ncpus = conf.ncpus;
+	topology->ncudagpus = conf.ncuda;
+	topology->nopenclgpus = conf.nopencl;
+
+	/* TODO: rather use simgrid/platf.h */
+	/* TODO: but still permit the user to provide his own xml */
+	fd = mkstemp(name);
+	file = fdopen(fd, "w");
+	starpu_simgrid_write_platform(&conf, file);
 	close(fd);
 	close(fd);
 
 
 	/* and load it */
 	/* and load it */
@@ -437,21 +444,26 @@ int _starpu_simgrid_transfer(size_t size, unsigned src_node, unsigned dst_node,
 	}
 	}
 }
 }
 
 
-static int last_key;
+static int used_key[MAX_TSD];
 
 
 int _starpu_pthread_key_create(_starpu_pthread_key_t *key)
 int _starpu_pthread_key_create(_starpu_pthread_key_t *key)
 {
 {
+	unsigned i;
+
 	/* Note: no synchronization here, we are actually monothreaded anyway. */
 	/* Note: no synchronization here, we are actually monothreaded anyway. */
-	STARPU_ASSERT(last_key < MAX_TSD);
-	*key = last_key++;
+	for (i = 0; i < MAX_TSD; i++)
+		if (!used_key[i]) {
+			used_key[i] = 1;
+			break;
+		}
+	STARPU_ASSERT(i < MAX_TSD);
+	*key = i;
 	return 0;
 	return 0;
 }
 }
 
 
-int _starpu_pthread_key_delete(_starpu_pthread_key_t key STARPU_ATTRIBUTE_UNUSED)
+int _starpu_pthread_key_delete(_starpu_pthread_key_t key)
 {
 {
-#ifdef STARPU_DEVEL
-#warning TODO: implement pthread_key_delete so simgridified starpu can be restarted at will
-#endif
+	used_key[key] = 0;
 	return 0;
 	return 0;
 }
 }