File: stencil5.c

package info (click to toggle)
starpu-contrib 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: wheezy
  • size: 13,836 kB
  • sloc: ansic: 77,357; cpp: 23,334; sh: 12,088; makefile: 2,086; lisp: 758; yacc: 185; sed: 126; fortran: 13
file content (128 lines) | stat: -rw-r--r-- 3,514 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
/* StarPU --- Runtime system for heterogeneous multicore architectures.
 *
 * Copyright (C) 2011  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 <stdio.h>
#include <stdlib.h>
#include <math.h>

#ifndef STARPU_GCC_PLUGIN
# error must be compiled with the StarPU GCC plug-in
#endif

/* Definition of the StarPU task and its CPU implementation.  */
static void stencil5(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
	__attribute__ ((task));

static void stencil5_cpu(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
	__attribute__ ((task_implementation ("cpu", stencil5)));

static void stencil5_cpu(float *xy, const float *xm1y, const float *xp1y, const float *xym1, const float *xyp1)
{
	*xy = (*xy + *xm1y + *xp1y + *xym1 + *xyp1) / 5;
}

#define NITER_DEF 20000
#define X         10
#define Y         10

int display = 0;
int niter = NITER_DEF;

static void parse_args(int argc, char **argv)
{
	int i;
	for (i = 1; i < argc; i++) {
		if (strcmp(argv[i], "-iter") == 0) {
			char *argptr;
			niter = strtol(argv[++i], &argptr, 10);
		}
		if (strcmp(argv[i], "-display") == 0) {
			display = 1;
		}
	}
}

static float my_rand (void)
{
	return (float) rand () / (float) RAND_MAX;
}

int main(int argc, char **argv)
{
        int x, y;
        float mean=0;
        float matrix[X][Y];

        parse_args(argc, argv);

	srand (time (NULL));
        for(x = 0; x < X; x++) {
                for (y = 0; y < Y; y++) {
                        matrix[x][y] = my_rand () * 100;
                        mean += matrix[x][y];
                }
        }
        mean /= (x*y);

        if (display) {
                fprintf(stdout, "mean=%f\n", mean);
                for(x = 0; x < X; x++) {
                        for (y = 0; y < Y; y++) {
                                fprintf(stdout, "%3f ", matrix[x][y]);
                        }
                        fprintf(stdout, "\n");
                }
        }

#pragma starpu initialize

        for(x = 0; x < X; x++) {
		for (y = 0; y < Y; y++) {
#pragma starpu register &matrix[x][y] 1
		}
	}

	while(niter--) {
                for (x = 1; x < X-1; x++) {
                        for (y = 1; y < Y-1; y++) {
                                stencil5(&matrix[x][y], &matrix[x-1][y], &matrix[x+1][y],
					 &matrix[x][y-1], &matrix[x][y+1]);
                        }
                }
        }

#pragma starpu wait

        for(x = 0; x < X; x++) {
                for (y = 0; y < Y; y++) {
#pragma starpu unregister &matrix[x][y]
                }
        }

#pragma starpu shutdown

        if (display) {
                fprintf(stdout, "mean=%f\n", mean);
                for(x = 0; x < X; x++) {
                        for (y = 0; y < Y; y++) {
                                fprintf(stdout, "%3f ", matrix[x][y]);
                        }
                        fprintf(stdout, "\n");
                }
        }

	return EXIT_SUCCESS;
}