File: mpi_scatter_gather.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 (228 lines) | stat: -rw-r--r-- 5,509 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
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
/* StarPU --- Runtime system for heterogeneous multicore architectures.
 *
 * Copyright (C) 2011, 2012  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_mpi.h>

/* Returns the MPI node number where data indexes index is */
int my_distrib(int x, int y, int nb_nodes)
{
        return (x+y) % nb_nodes;
}

void cpu_codelet(void *descr[], void *_args)
{
	float *block;
	unsigned nx = STARPU_MATRIX_GET_NY(descr[0]);
	unsigned ld = STARPU_MATRIX_GET_LD(descr[0]);
	unsigned i,j;
	int rank;
	float factor;

	block = (float *)STARPU_MATRIX_GET_PTR(descr[0]);
        starpu_codelet_unpack_args(_args, &rank);
	factor = block[0];

	//fprintf(stderr,"rank %d factor %f\n", rank, factor);
	for (j = 0; j < nx; j++)
	{
		for (i = 0; i < nx; i++)
		{
			//fprintf(stderr,"rank %d factor %f --> %f %f\n", rank, factor, block[j+i*ld], block[j+i*ld]*factor);
			block[j+i*ld] *= factor;
		}
	}
}

static struct starpu_codelet cl =
{
	.where = STARPU_CPU,
	.cpu_funcs = {cpu_codelet, NULL},
	.nbuffers = 1,
	.modes = {STARPU_RW},
};

int main(int argc, char **argv)
{
        int rank, nodes;
	float ***bmat = NULL;
        starpu_data_handle_t *data_handles;

	unsigned i,j,x,y;

	unsigned nblocks=4;
	unsigned block_size=2;
	unsigned size = nblocks*block_size;
	unsigned ld = size / nblocks;

	int ret = starpu_init(NULL);
	STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
	starpu_mpi_initialize_extended(&rank, &nodes);

	if (rank == 0)
	{
		/* Allocate the matrix */
		int block_number=10;
		bmat = malloc(nblocks * sizeof(float *));
		for(x=0 ; x<nblocks ; x++)
		{
			bmat[x] = malloc(nblocks * sizeof(float *));
			for(y=0 ; y<nblocks ; y++)
			{
				float value=0.0;
				starpu_malloc((void **)&bmat[x][y], block_size*block_size*sizeof(float));
				for (i = 0; i < block_size; i++)
				{
					for (j = 0; j < block_size; j++)
					{
						bmat[x][y][j +i*block_size] = block_number + value;
						value++;
					}
				}
				block_number += 10;
			}
		}
	}

#if 0
	// Print matrix
	if (rank == 0)
	{
		fprintf(stderr, "Input matrix\n");
		for(x=0 ; x<nblocks ; x++)
		{
			for(y=0 ; y<nblocks ; y++)
			{
				for (j = 0; j < block_size; j++)
				{
					for (i = 0; i < block_size; i++)
					{
						fprintf(stderr, "%2.2f\t", bmat[x][y][j+i*block_size]);
					}
					fprintf(stderr,"\n");
				}
				fprintf(stderr,"\n");
			}
		}
	}
#endif

	/* Allocate data handles and register data to StarPU */
        data_handles = malloc(nblocks*nblocks*sizeof(starpu_data_handle_t *));
        for(x = 0; x < nblocks ;  x++)
	{
                for (y = 0; y < nblocks; y++)
		{
			int mpi_rank = my_distrib(x, y, nodes);
			if (rank == 0)
			{
				starpu_matrix_data_register(&data_handles[x+y*nblocks], 0, (uintptr_t)bmat[x][y],
							    ld, size/nblocks, size/nblocks, sizeof(float));
			}
			else if ((mpi_rank == rank) || ((rank == mpi_rank+1 || rank == mpi_rank-1)))
			{
				/* I own that index, or i will need it for my computations */
				//fprintf(stderr, "[%d] Owning or neighbor of data[%d][%d]\n", rank, x, y);
				starpu_matrix_data_register(&data_handles[x+y*nblocks], -1, (uintptr_t)NULL,
							    ld, size/nblocks, size/nblocks, sizeof(float));
			}
			else
			{
				/* I know it's useless to allocate anything for this */
				data_handles[x+y*nblocks] = NULL;
			}
                        if (data_handles[x+y*nblocks])
			{
                                starpu_data_set_rank(data_handles[x+y*nblocks], mpi_rank);
                                starpu_data_set_tag(data_handles[x+y*nblocks], (y*nblocks)+x);
			}
                }
        }

	/* Scatter the matrix among the nodes */
	starpu_mpi_scatter_detached(data_handles, nblocks*nblocks, 0, MPI_COMM_WORLD);

	/* Calculation */
	for(x = 0; x < nblocks*nblocks ;  x++)
	{
		if (data_handles[x])
		{
			int owner = starpu_data_get_rank(data_handles[x]);
			if (owner == rank)
			{
				//fprintf(stderr,"[%d] Computing on data[%d]\n", rank, x);
				starpu_insert_task(&cl,
						   STARPU_VALUE, &rank, sizeof(rank),
						   STARPU_RW, data_handles[x],
						   0);
			}
		}
	}

	/* Gather the matrix on main node */
	starpu_mpi_gather_detached(data_handles, nblocks*nblocks, 0, MPI_COMM_WORLD);

	/* Unregister matrix from StarPU */
	for(x=0 ; x<nblocks*nblocks ; x++)
	{
		if (data_handles[x])
		{
			starpu_data_unregister(data_handles[x]);
		}
	}

#if 0
	// Print matrix
	if (rank == 0)
	{
		fprintf(stderr, "Output matrix\n");
		for(x=0 ; x<nblocks ; x++)
		{
			for(y=0 ; y<nblocks ; y++)
			{
				for (j = 0; j < block_size; j++)
				{
					for (i = 0; i < block_size; i++)
					{
						fprintf(stderr, "%2.2f\t", bmat[x][y][j+i*block_size]);
					}
					fprintf(stderr,"\n");
				}
				fprintf(stderr,"\n");
			}
		}
	}
#endif

	// Free memory
        free(data_handles);
	if (rank == 0)
	{
		for(x=0 ; x<nblocks ; x++)
		{
			for(y=0 ; y<nblocks ; y++)
			{
				starpu_free((void *)bmat[x][y]);
			}
			free(bmat[x]);
		}
		free(bmat);
	}


	starpu_mpi_shutdown();
	starpu_shutdown();
	return 0;
}