浏览代码

opencl: StarPU can now deal with very long opencl kernel sources. It
introduces a new function void
starpu_opencl_load_program_source_malloc() with the same behaviour as
starpu_opencl_load_program_source() instead if allocates the pointers
located_file_name, located_dir_name and opencl_program_source.

Nathalie Furmento 9 年之前
父节点
当前提交
64a00c3b0a

+ 6 - 1
ChangeLog

@@ -1,7 +1,7 @@
 # StarPU --- Runtime system for heterogeneous multicore architectures.
 # StarPU --- Runtime system for heterogeneous multicore architectures.
 #
 #
 # Copyright (C) 2009-2016  Université de Bordeaux
 # Copyright (C) 2009-2016  Université de Bordeaux
-# Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015  CNRS
+# Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016  CNRS
 # Copyright (C) 2014 INRIA
 # Copyright (C) 2014 INRIA
 #
 #
 # StarPU is free software; you can redistribute it and/or modify
 # StarPU is free software; you can redistribute it and/or modify
@@ -173,6 +173,11 @@ Small features:
   * Add configure option to disable the build of tests --disable-build-tests
   * Add configure option to disable the build of tests --disable-build-tests
   * Add starpu-all-tasks debugging support
   * Add starpu-all-tasks debugging support
   * Ranges can be provided in STARPU_WORKERS_CPUID
   * Ranges can be provided in STARPU_WORKERS_CPUID
+  * New function
+    void starpu_opencl_load_program_source_malloc(const char *source_file_name, char **located_file_name, char **located_dir_name, char **opencl_program_source)
+    which allocates the pointers located_file_name, located_dir_name
+    and opencl_program_source.
+
 
 
 Changes:
 Changes:
   * Data interfaces (variable, vector, matrix and block) now define
   * Data interfaces (variable, vector, matrix and block) now define

+ 5 - 1
doc/doxygen/chapters/api/opencl_extensions.doxy

@@ -1,7 +1,7 @@
 /*
 /*
  * This file is part of the StarPU Handbook.
  * This file is part of the StarPU Handbook.
  * Copyright (C) 2009--2011  Universit@'e de Bordeaux
  * Copyright (C) 2009--2011  Universit@'e de Bordeaux
- * Copyright (C) 2010, 2011, 2012, 2013, 2014  CNRS
+ * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2016  CNRS
  * Copyright (C) 2011, 2012 INRIA
  * Copyright (C) 2011, 2012 INRIA
  * See the file version.doxy for copying conditions.
  * See the file version.doxy for copying conditions.
  */
  */
@@ -117,6 +117,10 @@ has been located on the system, \p located_dir_name the directory
 where it has been located. Otherwise, they are both set to the empty
 where it has been located. Otherwise, they are both set to the empty
 string.
 string.
 
 
+\fn void starpu_opencl_load_program_source_malloc(const char *source_file_name, char **located_file_name, char **located_dir_name, char **opencl_program_source)
+\ingroup API_OpenCL_Extensions
+Similar to function starpu_opencl_load_program_source() but it allocates the buffers located_file_name, located_dir_name and opencl_program_source.
+
 \fn int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char *build_options)
 \fn int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char *build_options)
 \ingroup API_OpenCL_Extensions
 \ingroup API_OpenCL_Extensions
 Compile the OpenCL kernel stored in the file \p source_file_name
 Compile the OpenCL kernel stored in the file \p source_file_name

+ 23 - 3
examples/binary/binary.c

@@ -39,7 +39,7 @@ struct starpu_codelet cl =
 	.modes = {STARPU_RW}
 	.modes = {STARPU_RW}
 };
 };
 
 
-int compute(char *file_name, int load_as_file)
+int compute(char *file_name, int load_as_file, int with_malloc)
 {
 {
 	float float_array[4] STARPU_ATTRIBUTE_ALIGNED(16) = { 0.0f, 0.0f, 0.0f, 0.0f};
 	float float_array[4] STARPU_ATTRIBUTE_ALIGNED(16) = { 0.0f, 0.0f, 0.0f, 0.0f};
 	starpu_data_handle_t float_array_handle;
 	starpu_data_handle_t float_array_handle;
@@ -61,6 +61,20 @@ int compute(char *file_name, int load_as_file)
 		ret = starpu_opencl_load_binary_opencl(file_name, &opencl_program);
 		ret = starpu_opencl_load_binary_opencl(file_name, &opencl_program);
 		STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
 		STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
 	}
 	}
+	else if (with_malloc)
+	{
+		char *located_file_name;
+		char *located_dir_name;
+		char *opencl_program_source;
+		starpu_opencl_load_program_source_malloc(file_name, &located_file_name, &located_dir_name, &opencl_program_source);
+		ret = starpu_opencl_compile_opencl_from_string(opencl_program_source, "incrementer", NULL);
+		STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_compile_opencl_from_file");
+		ret = starpu_opencl_load_binary_opencl("incrementer", &opencl_program);
+		STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_binary_opencl");
+		free(located_file_name);
+		free(located_dir_name);
+		free(opencl_program_source);
+	}
 	else
 	else
 	{
 	{
 		char located_file_name[1024];
 		char located_file_name[1024];
@@ -122,9 +136,15 @@ int main(int argc, char **argv)
 	}
 	}
 	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
 	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
 
 
-	ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 1);
+	ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 1, -1);
+	if (ret == 0)
+		ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 0, 0);
+	else
+		FPRINTF(stderr, "Error when calling compute %d\n", ret);
 	if (ret == 0)
 	if (ret == 0)
-		ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 0);
+	     ret = compute("examples/incrementer/incrementer_kernels_opencl_kernel.cl", 0, 1);
+	else
+		FPRINTF(stderr, "Error when calling compute %d\n", ret);
 
 
 	starpu_shutdown();
 	starpu_shutdown();
 	return ret;
 	return ret;

+ 2 - 1
include/starpu_opencl.h

@@ -1,7 +1,7 @@
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
 /* StarPU --- Runtime system for heterogeneous multicore architectures.
  *
  *
  * Copyright (C) 2010-2014  Université de Bordeaux
  * Copyright (C) 2010-2014  Université de Bordeaux
- * Copyright (C) 2010, 2011, 2012, 2013  CNRS
+ * Copyright (C) 2010, 2011, 2012, 2013, 2016  CNRS
  *
  *
  * StarPU is free software; you can redistribute it and/or modify
  * 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
  * it under the terms of the GNU Lesser General Public License as published by
@@ -60,6 +60,7 @@ void starpu_opencl_get_current_context(cl_context *context);
 void starpu_opencl_get_current_queue(cl_command_queue *queue);
 void starpu_opencl_get_current_queue(cl_command_queue *queue);
 
 
 void starpu_opencl_load_program_source(const char *source_file_name, char *located_file_name, char *located_dir_name, char *opencl_program_source);
 void starpu_opencl_load_program_source(const char *source_file_name, char *located_file_name, char *located_dir_name, char *opencl_program_source);
+void starpu_opencl_load_program_source_malloc(const char *source_file_name, char **located_file_name, char **located_dir_name, char **opencl_program_source);
 int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char *build_options);
 int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char *build_options);
 int starpu_opencl_compile_opencl_from_string(const char *opencl_program_source, const char *file_name, const char *build_options);
 int starpu_opencl_compile_opencl_from_string(const char *opencl_program_source, const char *file_name, const char *build_options);
 
 

+ 74 - 31
src/drivers/opencl/driver_opencl_utils.c

@@ -43,59 +43,66 @@ char *_starpu_opencl_program_dir;
 #define _STARPU_STRINGIFY(x) _STARPU_STRINGIFY_(x)
 #define _STARPU_STRINGIFY(x) _STARPU_STRINGIFY_(x)
 
 
 static
 static
-int _starpu_opencl_locate_file(const char *source_file_name, char *located_file_name, char *located_dir_name)
+int _starpu_opencl_locate_file(const char *source_file_name, char **located_file_name, char **located_dir_name)
 {
 {
 	int ret = EXIT_FAILURE;
 	int ret = EXIT_FAILURE;
 
 
+	*located_file_name = NULL;
+	*located_dir_name = NULL;
+
 	_STARPU_DEBUG("Trying to locate <%s>\n", source_file_name);
 	_STARPU_DEBUG("Trying to locate <%s>\n", source_file_name);
 	if (access(source_file_name, R_OK) == 0)
 	if (access(source_file_name, R_OK) == 0)
 	{
 	{
-		strcpy(located_file_name, source_file_name);
+		*located_file_name = calloc(1, strlen(source_file_name)+1);
+		sprintf(*located_file_name, "%s", source_file_name);
 		ret = EXIT_SUCCESS;
 		ret = EXIT_SUCCESS;
 	}
 	}
 
 
 	if (ret == EXIT_FAILURE && _starpu_opencl_program_dir)
 	if (ret == EXIT_FAILURE && _starpu_opencl_program_dir)
 	{
 	{
-		sprintf(located_file_name, "%s/%s", _starpu_opencl_program_dir, source_file_name);
-		_STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
-		if (access(located_file_name, R_OK) == 0)
+		*located_file_name = calloc(1, strlen(_starpu_opencl_program_dir)+1+strlen(source_file_name)+1);
+		sprintf(*located_file_name, "%s/%s", _starpu_opencl_program_dir, source_file_name);
+		_STARPU_DEBUG("Trying to locate <%s>\n", *located_file_name);
+		if (access(*located_file_name, R_OK) == 0)
 			ret = EXIT_SUCCESS;
 			ret = EXIT_SUCCESS;
 	}
 	}
 
 
 	if (ret == EXIT_FAILURE)
 	if (ret == EXIT_FAILURE)
 	{
 	{
-		sprintf(located_file_name, "%s/%s", STARPU_SRC_DIR, source_file_name);
-		_STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
-		if (access(located_file_name, R_OK) == 0)
+		*located_file_name = calloc(1, strlen(STARPU_SRC_DIR)+1+strlen(source_file_name)+1);
+		sprintf(*located_file_name, "%s/%s", STARPU_SRC_DIR, source_file_name);
+		_STARPU_DEBUG("Trying to locate <%s>\n", *located_file_name);
+		if (access(*located_file_name, R_OK) == 0)
 			ret = EXIT_SUCCESS;
 			ret = EXIT_SUCCESS;
 	}
 	}
 
 
 	if (ret == EXIT_FAILURE)
 	if (ret == EXIT_FAILURE)
 	{
 	{
-		sprintf(located_file_name, "%s/%s", _STARPU_STRINGIFY(STARPU_OPENCL_DATADIR), source_file_name);
-		_STARPU_DEBUG("Trying to locate <%s>\n", located_file_name);
-		if (access(located_file_name, R_OK) == 0)
+		*located_file_name = calloc(1, strlen(_STARPU_STRINGIFY(STARPU_OPENCL_DATADIR))+1+strlen(source_file_name)+1);
+		sprintf(*located_file_name, "%s/%s", _STARPU_STRINGIFY(STARPU_OPENCL_DATADIR), source_file_name);
+		_STARPU_DEBUG("Trying to locate <%s>\n", *located_file_name);
+		if (access(*located_file_name, R_OK) == 0)
 			ret = EXIT_SUCCESS;
 			ret = EXIT_SUCCESS;
 	}
 	}
 
 
 	if (ret == EXIT_FAILURE)
 	if (ret == EXIT_FAILURE)
 	{
 	{
-		strcpy(located_file_name, "");
-		strcpy(located_dir_name, "");
 		_STARPU_ERROR("Cannot locate file <%s>\n", source_file_name);
 		_STARPU_ERROR("Cannot locate file <%s>\n", source_file_name);
 	}
 	}
 	else
 	else
 	{
 	{
-		char *last = strrchr(located_file_name, '/');
+		char *last = strrchr(*located_file_name, '/');
 
 
 		if (!last)
 		if (!last)
 		{
 		{
-			strcpy(located_dir_name, "");
+			*located_dir_name = calloc(2, sizeof(char));
+			sprintf(*located_dir_name, "%s", "");
 		}
 		}
 		else
 		else
 		{
 		{
-			sprintf(located_dir_name, "%s", located_file_name);
-			located_dir_name[strlen(located_file_name)-strlen(last)+1] = '\0';
+			*located_dir_name=calloc(1, 1+strlen(*located_file_name));
+			sprintf(*located_dir_name, "%s", *located_file_name);
+			(*located_dir_name)[strlen(*located_file_name)-strlen(last)+1] = '\0';
 		}
 		}
 	}
 	}
 
 
@@ -270,7 +277,9 @@ int _starpu_opencl_compile_or_load_opencl_from_string(const char *opencl_program
 		cl_int       err;
 		cl_int       err;
 
 
 		if (opencl_programs)
 		if (opencl_programs)
+		{
 			opencl_programs->programs[dev] = NULL;
 			opencl_programs->programs[dev] = NULL;
+		}
 
 
 		starpu_opencl_get_device(dev, &device);
 		starpu_opencl_get_device(dev, &device);
 		starpu_opencl_get_context(dev, &context);
 		starpu_opencl_get_context(dev, &context);
@@ -357,18 +366,42 @@ int _starpu_opencl_compile_or_load_opencl_from_string(const char *opencl_program
 	return EXIT_SUCCESS;
 	return EXIT_SUCCESS;
 }
 }
 
 
-void starpu_opencl_load_program_source(const char *source_file_name, char *located_file_name, char *located_dir_name, char *opencl_program_source)
+void starpu_opencl_load_program_source_malloc(const char *source_file_name, char **located_file_name, char **located_dir_name, char **opencl_program_source)
 {
 {
 	// Locate source file
 	// Locate source file
 	_starpu_opencl_locate_file(source_file_name, located_file_name, located_dir_name);
 	_starpu_opencl_locate_file(source_file_name, located_file_name, located_dir_name);
-	_STARPU_DEBUG("Source file name : <%s>\n", located_file_name);
-	_STARPU_DEBUG("Source directory name : <%s>\n", located_dir_name);
+	_STARPU_DEBUG("Source file name : <%s>\n", *located_file_name);
+	_STARPU_DEBUG("Source directory name : <%s>\n", *located_dir_name);
 
 
 	// Load the compute program from disk into a char *
 	// Load the compute program from disk into a char *
-	char *source = _starpu_opencl_load_program_source(located_file_name);
+	char *source = _starpu_opencl_load_program_source(*located_file_name);
 	if(!source)
 	if(!source)
-		_STARPU_ERROR("Failed to load compute program from file <%s>!\n", located_file_name);
+		_STARPU_ERROR("Failed to load compute program from file <%s>!\n", *located_file_name);
+
+	*opencl_program_source = malloc(strlen(source)+1);
+	sprintf(*opencl_program_source, "%s", source);
+	free(source);
+}
+
+void starpu_opencl_load_program_source(const char *source_file_name, char *located_file_name, char *located_dir_name, char *opencl_program_source)
+{
+	char *_located_file_name;
+	char *_located_dir_name;
+
+	// Locate source file
+	_starpu_opencl_locate_file(source_file_name, &_located_file_name, &_located_dir_name);
+	_STARPU_DEBUG("Source file name : <%s>\n", _located_file_name);
+	_STARPU_DEBUG("Source directory name : <%s>\n", _located_dir_name);
 
 
+	// Load the compute program from disk into a char *
+	char *source = _starpu_opencl_load_program_source(_located_file_name);
+	if(!source)
+		_STARPU_ERROR("Failed to load compute program from file <%s>!\n", _located_file_name);
+
+	sprintf(located_file_name, "%s", _located_file_name);
+	free(_located_file_name);
+	sprintf(located_dir_name, "%s", _located_dir_name);
+	free(_located_dir_name);
 	sprintf(opencl_program_source, "%s", source);
 	sprintf(opencl_program_source, "%s", source);
 	free(source);
 	free(source);
 }
 }
@@ -377,30 +410,40 @@ static
 int _starpu_opencl_compile_or_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs, const char* build_options)
 int _starpu_opencl_compile_or_load_opencl_from_file(const char *source_file_name, struct starpu_opencl_program *opencl_programs, const char* build_options)
 {
 {
 	int nb_devices;
 	int nb_devices;
-	char located_file_name[1024];
-	char located_dir_name[1024];
+	int ret;
+	char *located_file_name;
+	char *located_dir_name;
 	char new_build_options[1024];
 	char new_build_options[1024];
-#ifdef STARPU_DEVEL
-#warning Use dynamic allocation
-#endif
-	char opencl_program_source[256*1024];
+	char *opencl_program_source;
 
 
 	// Do not try to load and compile the file if there is no devices
 	// Do not try to load and compile the file if there is no devices
 	nb_devices = starpu_opencl_worker_get_count();
 	nb_devices = starpu_opencl_worker_get_count();
 	if (nb_devices == 0) return EXIT_SUCCESS;
 	if (nb_devices == 0) return EXIT_SUCCESS;
 
 
-	starpu_opencl_load_program_source(source_file_name, located_file_name, located_dir_name, opencl_program_source);
+	starpu_opencl_load_program_source_malloc(source_file_name, &located_file_name, &located_dir_name, &opencl_program_source);
 
 
 	if (!build_options)
 	if (!build_options)
 		build_options = "";
 		build_options = "";
 
 
 	if (!strcmp(located_dir_name, ""))
 	if (!strcmp(located_dir_name, ""))
-		strcpy(new_build_options, build_options);
+	{
+		sprintf(new_build_options, "%s", build_options);
+	}
 	else
 	else
+	{
 		sprintf(new_build_options, "-I %s %s", located_dir_name, build_options);
 		sprintf(new_build_options, "-I %s %s", located_dir_name, build_options);
+	}
 	_STARPU_DEBUG("Build options: <%s>\n", new_build_options);
 	_STARPU_DEBUG("Build options: <%s>\n", new_build_options);
 
 
-	return _starpu_opencl_compile_or_load_opencl_from_string(opencl_program_source, new_build_options, opencl_programs, source_file_name);
+	ret = _starpu_opencl_compile_or_load_opencl_from_string(opencl_program_source, new_build_options, opencl_programs, source_file_name);
+
+	_STARPU_DEBUG("located_file_name : <%s>\n", located_file_name);
+	_STARPU_DEBUG("located_dir_name : <%s>\n", located_dir_name);
+	free(located_file_name);
+	free(located_dir_name);
+	free(opencl_program_source);
+
+	return ret;
 }
 }
 
 
 int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char* build_options)
 int starpu_opencl_compile_opencl_from_file(const char *source_file_name, const char* build_options)