vector_scal_opencl.c 1.94 KB
Newer Older
THIBAULT Samuel's avatar
THIBAULT Samuel committed
1 2
/* StarPU --- Runtime system for heterogeneous multicore architectures.
 *
THIBAULT Samuel's avatar
THIBAULT Samuel committed
3
 * Copyright (C) 2010, 2011, 2019, 2021  Université de Bordeaux 1
THIBAULT Samuel's avatar
THIBAULT Samuel committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Copyright (C) 2010, 2011, 2012, 2013, 2014  Centre National de la Recherche Scientifique
 *
 * 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
 * the Free Software Foundation; either version 2.1 of the License, or (at
 * your option) any later version.
 *
 * StarPU 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>

extern struct starpu_opencl_program programs;

void vector_scal_opencl(void *buffers[], void *_args)
{
	/* length of the vector */
	unsigned n = STARPU_VECTOR_GET_NX(buffers[0]);
	/* OpenCL copy of the vector pointer */
	cl_mem val = (cl_mem) STARPU_VECTOR_GET_DEV_HANDLE(buffers[0]);

	/* extract the value arguments */
THIBAULT Samuel's avatar
THIBAULT Samuel committed
30
	float factor;
THIBAULT Samuel's avatar
THIBAULT Samuel committed
31 32
	starpu_codelet_unpack_args(_args, &factor);

THIBAULT Samuel's avatar
THIBAULT Samuel committed
33 34 35 36 37
	/* Load the OpenCL kernel */
	int id, devid, err;
	cl_kernel kernel;
	cl_command_queue queue;

THIBAULT Samuel's avatar
THIBAULT Samuel committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	id = starpu_worker_get_id();
	devid = starpu_worker_get_devid(id);

	err = starpu_opencl_load_kernel(&kernel, &queue, &programs,
					"vector_mult_opencl", devid);   /* Name of the codelet defined above */
	if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);

	err = clSetKernelArg(kernel, 0, sizeof(val), &val);
	err |= clSetKernelArg(kernel, 1, sizeof(n), &n);
	err |= clSetKernelArg(kernel, 2, sizeof(factor), &factor);
	if (err) STARPU_OPENCL_REPORT_ERROR(err);

	{
		size_t local=64;
		size_t global=n;
THIBAULT Samuel's avatar
THIBAULT Samuel committed
53
		err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
THIBAULT Samuel's avatar
THIBAULT Samuel committed
54 55 56 57 58 59 60
		if (err != CL_SUCCESS) STARPU_OPENCL_REPORT_ERROR(err);
	}

	clFinish(queue);

	starpu_opencl_release_kernel(kernel);
}