Ejemplo n.º 1
0
void socl_init_starpu(void) {
  starpu_pthread_mutex_lock(&_socl_mutex);
  if( ! _starpu_init ){
    starpu_conf_init(&conf);
    conf.ncuda = 0;
    conf.ncpus = 0;


    _starpu_init_failed = starpu_init(&conf);
    if (_starpu_init_failed != 0)
    {
       DEBUG_MSG("Error when calling starpu_init: %d\n", _starpu_init_failed);
    }
    else {
       if (starpu_opencl_worker_get_count() == 0)
       {
	    DEBUG_MSG("StarPU didn't find any OpenCL device. Try disabling CUDA support in StarPU (export STARPU_NCUDA=0).\n");
	    _starpu_init_failed = -ENODEV;
       }
    }

    /* Disable dataflow implicit dependencies */
    starpu_data_set_default_sequential_consistency_flag(0);
    _starpu_init = 1;
  }
  starpu_pthread_mutex_unlock(&_socl_mutex);

}
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 nb_devices;
	char located_file_name[1024];
	char located_dir_name[1024];
	char new_build_options[1024];
	char opencl_program_source[16384];

	// Do not try to load and compile the file if there is no devices
	nb_devices = starpu_opencl_worker_get_count();
	if (nb_devices == 0) return EXIT_SUCCESS;

	starpu_opencl_load_program_source(source_file_name, located_file_name, located_dir_name, opencl_program_source);

	if (!build_options)
		build_options = "";

	if (!strcmp(located_dir_name, ""))
		strcpy(new_build_options, build_options);
	else if (build_options)
		sprintf(new_build_options, "-I %s %s", located_dir_name, build_options);
	else
		sprintf(new_build_options, "-I %s", located_dir_name);
	_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);
}
int
main(void)
{
#ifdef STARPU_USE_CPU
	int ret;

	ret = starpu_init(NULL);
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");

	ncpu = starpu_cpu_worker_get_count();
#ifdef STARPU_USE_CUDA
	ncuda = starpu_cuda_worker_get_count();
#endif
#ifdef STARPU_USE_OPENCL
	nopencl = starpu_opencl_worker_get_count();
#endif

	if (ncpu == 0 || !gpus_available())
	{
		starpu_shutdown();
		return 77;
	}

#ifdef STARPU_USE_OPENCL
	ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_opencl_kernel.cl",
						  &opencl_program, NULL);
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
	ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/multiformat_conversion_codelets_opencl_kernel.cl", 
						  &opencl_conversion_program, NULL);
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
#endif
	init_problem_data();

	print_it();

	register_data();

	create_and_submit_tasks();

	unregister_data();

	print_it();

#ifdef STARPU_USE_OPENCL
        ret = starpu_opencl_unload_opencl(&opencl_program);
        STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_unload_opencl");
        starpu_opencl_unload_opencl(&opencl_conversion_program);
#endif
	starpu_shutdown();


	return check_it();
#else
	/* Without the CPU, there is no point in using the multiformat
	 * interface, so this test is pointless. */
	return 77;
#endif
}
int starpu_opencl_unload_opencl(struct starpu_opencl_program *opencl_programs)
{
	unsigned int dev;
	unsigned int nb_devices;

	if (!starpu_opencl_worker_get_count())
		return 0;

	nb_devices = _starpu_opencl_get_device_count();
	// Iterate over each device
	for(dev = 0; dev < nb_devices; dev ++)
	{
		if (opencl_programs->programs[dev])
		{
			cl_int err;
			err = clReleaseProgram(opencl_programs->programs[dev]);
			if (STARPU_UNLIKELY(err != CL_SUCCESS))
				STARPU_OPENCL_REPORT_ERROR(err);
		}
	}
	return 0;
}
Ejemplo n.º 5
0
static int
test_cpu(void)
{
	int ret, var = 0;
	static starpu_pthread_t driver_thread;
	struct starpu_conf conf;
	struct starpu_driver d =
	{
		.type = STARPU_CPU_WORKER,
		.id.cpu_id = 0
	};

	starpu_conf_init(&conf);
	conf.n_not_launched_drivers = 1;
	conf.not_launched_drivers = &d;
	conf.ncpus = 1;
	ret = starpu_init(&conf);
	if (ret == -ENODEV || starpu_cpu_worker_get_count() == 0)
	{
		FPRINTF(stderr, "WARNING: No CPU worker found\n");
		if (ret == 0)
			starpu_shutdown();
		return STARPU_TEST_SKIPPED;
	}

	ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
	if (ret != 0)
	{
		ret = 1;
		goto out2;
	}

	struct starpu_task *task;
	task = starpu_task_create();
	cl.where = STARPU_CPU;
	task->cl = &cl;
	task->cl_arg = &var;
	task->synchronous = 1;

	ret = starpu_task_submit(task);
	if (ret == -ENODEV)
	{
		FPRINTF(stderr, "WARNING: No worker can execute this task\n");
		ret = STARPU_TEST_SKIPPED;
		goto out;
	}

	FPRINTF(stderr, "[CPU] Var = %d (expected value: 1)\n", var);
	ret = !!(var != 1);
out:
	starpu_drivers_request_termination();
	if (starpu_pthread_join(driver_thread, NULL) != 0)
		return 1;
out2:
	starpu_shutdown();
	return ret;
}
#endif /* STARPU_USE_CPU */

#ifdef STARPU_USE_CUDA
static int
test_cuda(void)
{
	int ret, var = 0;
	static starpu_pthread_t driver_thread;
	struct starpu_conf conf;
	struct starpu_driver d =
	{
		.type = STARPU_CUDA_WORKER,
		.id.cuda_id = 0
	};

	starpu_conf_init(&conf);
	conf.n_not_launched_drivers = 1;
	conf.not_launched_drivers = &d;
	conf.ncuda = 1;
	ret = starpu_init(&conf);
	if (ret == -ENODEV || starpu_cuda_worker_get_count() == 0)
	{
		FPRINTF(stderr, "WARNING: No CUDA worker found\n");
		if (ret == 0)
			starpu_shutdown();
		return STARPU_TEST_SKIPPED;
	}

	ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
	if (ret == -1)
	{
		ret = 1;
		goto out;
	}

	struct starpu_task *task;
	task = starpu_task_create();
	cl.where = STARPU_CUDA;
	task->cl = &cl;
	task->cl_arg = &var;
	task->synchronous = 1;

	ret = starpu_task_submit(task);
	if (ret == -ENODEV)
	{
		FPRINTF(stderr, "WARNING: No worker can execute this task\n");
		ret = STARPU_TEST_SKIPPED;
		goto out;
	}

out:
	starpu_drivers_request_termination();
	if (starpu_pthread_join(driver_thread, NULL) != 0)
		return 1;
	starpu_shutdown();

	FPRINTF(stderr, "[CUDA] Var = %d (expected value: 1)\n", var);
	ret = !!(var != 1);
	return ret;
}
#endif /* STARPU_USE_CUDA */

#ifdef STARPU_USE_OPENCL
static int
test_opencl(void)
{
	int ret, var = 0;
	static starpu_pthread_t driver_thread;
	struct starpu_conf conf;

	cl_int err;
        cl_uint pdummy;
        cl_platform_id platform;
        err = clGetPlatformIDs(1, &platform, &pdummy);
        if (err != CL_SUCCESS)
	{
		FPRINTF(stderr, "WARNING: No OpenCL platform found\n");
		return STARPU_TEST_SKIPPED;
	}

	cl_device_type device_type = CL_DEVICE_TYPE_GPU|CL_DEVICE_TYPE_ACCELERATOR;
	if (starpu_get_env_number("STARPU_OPENCL_ON_CPUS") > 0)
		device_type |= CL_DEVICE_TYPE_CPU;
	if (starpu_get_env_number("STARPU_OPENCL_ONLY_ON_CPUS") > 0)
		device_type = CL_DEVICE_TYPE_CPU;

	cl_device_id device_id;
        err = clGetDeviceIDs(platform, device_type, 1, &device_id, NULL);
        if (err != CL_SUCCESS)
	{
		FPRINTF(stderr, "WARNING: No GPU devices found on OpenCL platform\n");
		return STARPU_TEST_SKIPPED;
	}

	struct starpu_driver d =
	{
		.type = STARPU_OPENCL_WORKER,
		.id.opencl_id = device_id
	};

	starpu_conf_init(&conf);
	conf.n_not_launched_drivers = 1;
	conf.not_launched_drivers = &d;
	conf.ncuda = 0;
	conf.nopencl = 1;
	ret = starpu_init(&conf);
	if (ret == -ENODEV || starpu_opencl_worker_get_count() == 0)
	{
		FPRINTF(stderr, "WARNING: No OpenCL workers found\n");
		if (ret == 0)
			starpu_shutdown();
		return STARPU_TEST_SKIPPED;
	}

	ret = starpu_pthread_create(&driver_thread, NULL, run_driver, &d);
	if (ret == -1)
	{
		ret = 1;
		goto out;
	}

	struct starpu_task *task;
	task = starpu_task_create();
	cl.where = STARPU_OPENCL;
	task->cl = &cl;
	task->cl_arg = &var;
	task->synchronous = 1;

	ret = starpu_task_submit(task);
	if (ret == -ENODEV)
	{
		FPRINTF(stderr, "WARNING: No worker can execute the task\n");
		ret = STARPU_TEST_SKIPPED;
		goto out;
	}

out:
	starpu_drivers_request_termination();
	if (starpu_pthread_join(driver_thread, NULL) != 0)
		return 1;
	starpu_shutdown();

	FPRINTF(stderr, "[OpenCL] Var = %d (expected value: 1)\n", var);
	ret = !!(var != 1);
	return ret;
}
#endif /* STARPU_USE_OPENCL */

int
main(void)
{
	int ret = STARPU_TEST_SKIPPED;
#ifdef STARPU_USE_CPU
	ret = test_cpu();
	if (ret == 1)
		return 1;
#endif
#ifdef STARPU_USE_CUDA
	ret = test_cuda();
	if (ret == 1)
		return 1;
#endif
#ifdef STARPU_USE_OPENCL
	ret = test_opencl();
	if (ret == 1)
		return 1;
#endif
	return ret;
}