File: block.c

package info (click to toggle)
starpu 1.4.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 39,388 kB
  • sloc: ansic: 325,221; sh: 7,196; makefile: 6,666; lisp: 6,056; xml: 5,031; f90: 4,994; python: 2,398; cpp: 1,353; java: 330; sed: 162; pascal: 57; fortran: 25
file content (146 lines) | stat: -rw-r--r-- 3,802 bytes parent folder | download | duplicates (2)
1
2
3
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* StarPU --- Runtime system for heterogeneous multicore architectures.
 *
 * Copyright (C) 2010-2025  University of Bordeaux, CNRS (LaBRI UMR 5800), Inria
 *
 * 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>
#include <math.h>

#define FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)

extern void cpu_codelet(void *descr[], void *_args);
#ifdef STARPU_USE_CUDA
extern void cuda_codelet(void *descr[], void *_args);
#endif
#ifdef STARPU_USE_HIP
extern void hip_codelet(void *descr[], void *_args);
#endif
#ifdef STARPU_USE_OPENCL
extern void opencl_codelet(void *descr[], void *_args);
struct starpu_opencl_program opencl_code;
#endif

typedef void (*device_func)(void **, void *);

int execute_on(uint32_t where, device_func func, float *block, int pnx, int pny, int pnz, float multiplier)
{
	struct starpu_codelet cl;
	starpu_data_handle_t block_handle;
	int i;

	starpu_block_data_register(&block_handle, STARPU_MAIN_RAM, (uintptr_t)block, pnx, pnx*pny, pnx, pny, pnz, sizeof(float));

	starpu_codelet_init(&cl);
	cl.where = where;
	cl.cuda_funcs[0] = func;
	cl.hip_funcs[0] = func;
	cl.cpu_funcs[0] = func;
	cl.opencl_funcs[0] = func;
	cl.nbuffers = 1;
	cl.modes[0] = STARPU_RW,
	cl.model = NULL;
	cl.name = "block_scale";

	struct starpu_task *task = starpu_task_create();
	task->cl = &cl;
	task->callback_func = NULL;
	task->handles[0] = block_handle;
	task->cl_arg = &multiplier;
	task->cl_arg_size = sizeof(multiplier);

	int ret = starpu_task_submit(task);
	if (STARPU_UNLIKELY(ret == -ENODEV))
	{
		FPRINTF(stderr, "No worker may execute this task\n");
		task->destroy = 0;
		starpu_task_destroy(task);
		return 1;
	}

	starpu_task_wait_for_all();

	/* update the array in RAM */
	starpu_data_unregister(block_handle);

	for(i=0 ; i<pnx*pny*pnz; i++)
	{
		FPRINTF(stderr, "%f ", block[i]);
	}
	FPRINTF(stderr, "\n");

	return 0;
}

int main(void)
{
	float *block, n=1.0;
	int i, j, k, ret;
	int nx=3;
	int ny=2;
	int nz=4;
	float multiplier=1.0;

	ret = starpu_init(NULL);
	if (ret == -ENODEV)
		return 77;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");

	starpu_malloc((void **)&block, nx*ny*nz*sizeof(float));
	assert(block);
	for(k=0 ; k<nz ; k++)
	{
		for(j=0 ; j<ny ; j++)
		{
			for(i=0 ; i<nx ; i++)
			{
				block[(k*nx*ny)+(j*nx)+i] = n++;
			}
		}
	}

	ret = execute_on(STARPU_CPU, cpu_codelet, block, nx, ny, nz, 1.0);
	if (!ret) multiplier *= 1.0;
#ifdef STARPU_USE_OPENCL
	ret = starpu_opencl_load_opencl_from_file("examples/basic_examples/block_opencl_kernel.cl", &opencl_code, NULL);
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
	ret = execute_on(STARPU_OPENCL, opencl_codelet, block, nx, ny, nz, 2.0);
	if (!ret) multiplier *= 2.0;
#endif
#ifdef STARPU_USE_CUDA
	ret = execute_on(STARPU_CUDA, cuda_codelet, block, nx, ny, nz, 3.0);
	if (!ret) multiplier *= 3.0;
#endif
#ifdef STARPU_USE_HIP
	ret = execute_on(STARPU_HIP, hip_codelet, block, nx, ny, nz, 3.0);
	if (!ret) multiplier *= 3.0;
#endif

	/* Check result is correct */
	ret=1;
	for(i=0 ; i<nx*ny*nz ; i++)
	{
		if (block[i] != (i+1) * multiplier)
		{
			ret=0;
			break;
		}
	}

	FPRINTF(stderr,"TEST %s\n", ret==1?"PASSED":"FAILED");
	starpu_free_noflag(block, nx*ny*nz*sizeof(float));

	starpu_shutdown();

	return (ret!=1);
}