File: complex.c

package info (click to toggle)
starpu 1.4.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 38,692 kB
  • sloc: ansic: 324,396; sh: 7,181; makefile: 6,636; lisp: 6,055; xml: 5,031; f90: 4,994; python: 2,398; cpp: 1,353; java: 330; sed: 162; pascal: 57; fortran: 25
file content (292 lines) | stat: -rw-r--r-- 8,876 bytes parent folder | download | duplicates (3)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* StarPU --- Runtime system for heterogeneous multicore architectures.
 *
 * Copyright (C) 2012-2023  Université de 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 "complex_interface.h"
#include "complex_codelet.h"

void copy_complex_codelet_cpu(void *descr[], void *_args)
{
	int i;
	int nx = STARPU_COMPLEX_GET_NX(descr[0]);

	double *i_real = STARPU_COMPLEX_GET_REAL(descr[0]);
	double *i_imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[0]);

	double *o_real = STARPU_COMPLEX_GET_REAL(descr[1]);
	double *o_imaginary = STARPU_COMPLEX_GET_IMAGINARY(descr[1]);

	for(i=0 ; i<nx ; i++)
	{
		o_real[i] = i_real[i];
		o_imaginary[i] = i_imaginary[i];
	}

}

static int can_execute(unsigned workerid, struct starpu_task *task, unsigned nimpl)
{
	(void) task;
	(void) nimpl;

	if (starpu_worker_get_type(workerid) != STARPU_CUDA_WORKER)
		return 1;

#ifdef STARPU_USE_CUDA
#ifdef STARPU_SIMGRID
	/* We don't know, let's assume it can */
	return 1;
#else
	/* Cuda device */
	const struct cudaDeviceProp *props;
	props = starpu_cuda_get_device_properties(workerid);
	if (props->major >= 2 || props->minor >= 3)
	{
		/* At least compute capability 1.3, supports doubles */
		return 1;
	}
	else
	{
		/* Old card does not support doubles */
		return 0;
	}
#endif
#else
	return 1;
#endif
}

#ifdef STARPU_USE_CUDA
extern void copy_complex_codelet_cuda(void *descr[], void *_args);
#endif
#ifdef STARPU_USE_OPENCL
extern void copy_complex_codelet_opencl(void *buffers[], void *args);
#endif

struct starpu_codelet cl_copy =
{
	.cpu_funcs = {copy_complex_codelet_cpu},
#ifdef STARPU_USE_CUDA
	.cuda_funcs = {copy_complex_codelet_cuda},
	.cuda_flags = {STARPU_CUDA_ASYNC},
#endif
#ifdef STARPU_USE_OPENCL
	.opencl_funcs = {copy_complex_codelet_opencl},
	.opencl_flags = {STARPU_OPENCL_ASYNC},
#endif
	.nbuffers = 2,
	.modes = {STARPU_R, STARPU_W},
	.can_execute = can_execute,
	.name = "cl_copy"
};

#ifdef STARPU_USE_OPENCL
struct starpu_opencl_program opencl_program;
#endif

int main(void)
{
	int ret = 0;
	starpu_data_handle_t handle1;
	starpu_data_handle_t handle2;
	starpu_data_handle_t handle3;
	starpu_data_handle_t handle4;

	double real = 45.0;
	double imaginary = 12.0;
	double copy_real = 78.0;
	double copy_imaginary = 78.0;

	int compare;
	int *compare_ptr = &compare;

	starpu_data_handle_t vectorh;
	struct starpu_vector_interface *vectori;
	double *vector;

	// When using master-slave MPI mode, it is necessary for the slaves to know about the complex interface
	starpu_complex_data_register_ops();

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

#ifdef STARPU_USE_OPENCL
	ret = starpu_opencl_load_opencl_from_file("examples/interface/complex_kernels.cl",
						  &opencl_program, NULL);
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_opencl_load_opencl_from_file");
#endif
	starpu_complex_data_register(&handle1, STARPU_MAIN_RAM, &real, &imaginary, 1);
	starpu_complex_data_register(&handle2, STARPU_MAIN_RAM, &copy_real, &copy_imaginary, 1);
	/* Create a vector of two complexs.  */
	starpu_complex_data_register(&handle3, -1, 0, 0, 2);
	starpu_complex_data_register(&handle4, -1, 0, 0, 1);

	ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	/* Compare two different complexs.  */
	ret = starpu_task_insert(&cl_compare,
				 STARPU_R, handle1,
				 STARPU_R, handle2,
				 STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
				 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
	starpu_task_wait_for_all();
	if (compare != 0)
	{
	     _FPRINTF(stderr, "Complex numbers should NOT be similar\n");
	     goto end;
	}

	/* Copy one into the other.  */
	ret = starpu_task_insert(&cl_copy,
				 STARPU_R, handle1,
				 STARPU_W, handle2,
				 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle1", strlen("handle1")+1, STARPU_R, handle1, 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle2", strlen("handle2")+1, STARPU_R, handle2, 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	/* And compare again.  */
	ret = starpu_task_insert(&cl_compare,
				 STARPU_R, handle1,
				 STARPU_R, handle2,
				 STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
				 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	starpu_task_wait_for_all();

	if (compare != 1)
	{
	     _FPRINTF(stderr, "Complex numbers should be similar\n");
	}

	/* Put another value again */
	starpu_data_acquire(handle2, STARPU_W);
	copy_real = 78.0;
	copy_imaginary = 77.0;
	starpu_data_release(handle2);

	/* Split it in two pieces (thus one complex each).  */
	struct starpu_data_filter f =
	{
		.filter_func = starpu_complex_filter_block,
		.nchildren = 2,
	};
	starpu_data_partition(handle3, &f);

	/* Copy the two complexs into each part */
	ret = starpu_task_insert(&cl_copy,
				 STARPU_R, handle1,
				 STARPU_W, starpu_data_get_sub_data(handle3, 1, 0),
				 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
	ret = starpu_task_insert(&cl_copy,
				 STARPU_R, handle2,
				 STARPU_W, starpu_data_get_sub_data(handle3, 1, 1),
				 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	/* Gather the two pieces.  */
	starpu_data_unpartition(handle3, STARPU_MAIN_RAM);

	/* Show it.  */
	ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle3", strlen("handle3")+1, STARPU_R, handle3, 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	/* Get the real and imaginary vectors.  */
	struct starpu_data_filter fcanon =
	{
		.filter_func = starpu_complex_filter_canonical,
		.nchildren = 2,
		.get_child_ops = starpu_complex_filter_canonical_child_ops,
	};
	starpu_data_partition(handle3, &fcanon);

	/* Check the corresponding data.  */
	vectorh = starpu_data_get_sub_data(handle3, 1, 0);
	starpu_data_acquire(vectorh, STARPU_R);
	vectori = starpu_data_get_interface_on_node(vectorh, STARPU_MAIN_RAM);
	vector = (double*) vectori->ptr;
	STARPU_ASSERT_MSG(vector[0] == 45., "Bogus value: %f instead of %f", vector[0], 45.);
	STARPU_ASSERT_MSG(vector[1] == 78., "Bogus value: %f instead of %f", vector[1], 78.);
	starpu_data_release(vectorh);

	vectorh = starpu_data_get_sub_data(handle3, 1, 1);
	starpu_data_acquire(vectorh, STARPU_R);
	vectori = starpu_data_get_interface_on_node(vectorh, STARPU_MAIN_RAM);
	vector = (double*) vectori->ptr;
	STARPU_ASSERT_MSG(vector[0] == 12., "Bogus value: %f instead of %f", vector[0], 12.);
	STARPU_ASSERT_MSG(vector[1] == 77., "Bogus value: %f instead of %f", vector[1], 77.);
	starpu_data_release(vectorh);

	starpu_data_unpartition(handle3, STARPU_MAIN_RAM);

	/* Use helper starpu_data_cpy */
	ret = starpu_data_cpy(handle4, handle1, 0, NULL, NULL);
	if (ret == -ENODEV) goto end;
	ret = starpu_task_insert(&cl_display, STARPU_VALUE, "handle4", strlen("handle4")+1, STARPU_R, handle4, 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");

	/* Compare two different complexs.  */
	ret = starpu_task_insert(&cl_compare,
				 STARPU_R, handle1,
				 STARPU_R, handle4,
				 STARPU_VALUE, &compare_ptr, sizeof(compare_ptr),
				 0);
	if (ret == -ENODEV) goto end;
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
	starpu_task_wait_for_all();
	if (compare != 1)
	{
	     _FPRINTF(stderr, "Complex numbers should be similar\n");
	     goto end;
	}

end:
#ifdef STARPU_USE_OPENCL
	{
	     int ret2 = starpu_opencl_unload_opencl(&opencl_program);
	     STARPU_CHECK_RETURN_VALUE(ret2, "starpu_opencl_unload_opencl");
	}
#endif
	starpu_data_unregister(handle1);
	starpu_data_unregister(handle2);
	starpu_data_unregister(handle3);
	starpu_data_unregister(handle4);
	starpu_shutdown();
	if (ret == -ENODEV) return 77; else return !compare;
}