test_tiling.c 20.7 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1
2
3
4
5
6
7
8
9
10
/*******************************************************************************
 * Copyright 2019 UChicago Argonne, LLC.
 * (c.f. AUTHORS, LICENSE)
 *
 * This file is part of the AML project.
 * For more info, see https://xgitlab.cels.anl.gov/argo/aml
 *
 * SPDX-License-Identifier: BSD-3-Clause
*******************************************************************************/

11
#include "aml.h"
12
13
14
#include "aml/layout/dense.h"
#include "aml/layout/native.h"
#include "aml/tiling/resize.h"
Swann Perarnau's avatar
Swann Perarnau committed
15
#include "aml/tiling/pad.h"
16
17
#include <assert.h>

18
19
20
21
22
23
24
25
26
27
28
29
30
31
void test_tiling_even_mixed(void)
{
	int memory[9][10][8];
	int memoryres[9][10][8];
	size_t dims_col[3] = {8, 10, 9};
	size_t dims_row[3] = {9, 10, 8};

	size_t stride[3] = {1, 1, 1};

	size_t dims_tile_col[3] = {4, 10, 3};
	size_t dims_tile_row[3] = {3, 10, 4};

	size_t expected_dims_col[3] = {2, 1, 3};

32
33
34
35
36
37
38
39
	int l = 0;

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++) {
				memory[i][j][k] = l;
				memoryres[i][j][k] = 0;
			}
40
41

	struct aml_layout *a, *ares;
42

43
44
45
46
47
48
49
50
51
52
53
	aml_layout_dense_create(&a, (void *)memory,
				  AML_LAYOUT_ORDER_COLUMN_MAJOR,
				  sizeof(int), 3, dims_col,
				  stride, dims_col);
	aml_layout_dense_create(&ares, (void *)memoryres,
				AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row,
				stride, dims_row);


	struct aml_tiling *t, *tres;
54

55
56
57
58
59
60
	aml_tiling_resize_create(&t, AML_TILING_ORDER_COLUMN_MAJOR,
				     a, 3, dims_tile_col);
	aml_tiling_resize_create(&tres, AML_TILING_ORDER_ROW_MAJOR,
				     ares, 3, dims_tile_row);


61
62
63
64
65
66
67
68
69
	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){k, j, i});
				bres = aml_tiling_index(tres,
							(size_t[]){i, j, k});
				aml_copy_layout_generic(bres, b, NULL);
70
71
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
72
73
			}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 * sizeof(int)) == 0);
74

75
76
77
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

78
79
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
	aml_tiling_resize_destroy(&t);
	aml_tiling_resize_destroy(&tres);

	aml_layout_dense_create(&a, memory, AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col,
				  stride, dims_col);
	aml_layout_dense_create(&ares, memoryres, AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row,
				  stride, dims_row);


	aml_tiling_resize_create(&t, AML_TILING_ORDER_ROW_MAJOR,
				     a, 3, dims_tile_row);
	aml_tiling_resize_create(&tres, AML_TILING_ORDER_COLUMN_MAJOR,
				     ares, 3, dims_tile_col);

96
97
98
99
100
101
102
103
104
105
106
107
108
109
	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++)
				memoryres[i][j][k] = 0.0;

	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){i, j, k});
				bres = aml_tiling_index(tres,
							(size_t[]){k, j, i});
				aml_copy_layout_generic(bres, b, NULL);
110
111
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
112
113
			}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 * sizeof(int)) == 0);
114

115
116
117
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

118
119
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
120
121
122
123
124
125
	aml_tiling_resize_destroy(&t);
	aml_tiling_resize_destroy(&tres);

}

void test_tiling_even(void)
126
{
127
128
129
130
131
132
133
134
135
136
137
138
139
	int memory[9][10][8];
	int memoryres[9][10][8];
	size_t dims_col[3] = {8, 10, 9};
	size_t dims_row[3] = {9, 10, 8};

	size_t stride[3] = {1, 1, 1};

	size_t dims_tile_col[3] = {4, 10, 3};
	size_t dims_tile_row[3] = {3, 10, 4};

	size_t expected_dims_col[3] = {2, 1, 3};
	size_t expected_dims_row[3] = {3, 1, 2};

140
141
142
143
144
145
146
147
	int l = 0;

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++) {
				memory[i][j][k] = l;
				memoryres[i][j][k] = 0.0;
			}
148

149
	struct aml_layout *a, *ares;
150

151
152
	aml_layout_dense_create(&a, memory, AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col,
153
				stride, dims_col);
154
155
	aml_layout_dense_create(&ares, memoryres, AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col,
156
				stride, dims_col);
157
158
159


	struct aml_tiling *t, *tres;
160

161
162
163
164
165
166
167
168
169
	aml_tiling_resize_create(&t, AML_TILING_ORDER_COLUMN_MAJOR,
				     a, 3, dims_tile_col);
	aml_tiling_resize_create(&tres, AML_TILING_ORDER_COLUMN_MAJOR,
				     ares, 3, dims_tile_col);


	assert(aml_tiling_order(t) == AML_TILING_ORDER_COLUMN_MAJOR);
	assert(aml_tiling_ndims(t) == 3);

170
171
	size_t dims[3];

172
173
174
175
176
	aml_tiling_tile_dims(t, dims);
	assert(memcmp(dims, dims_tile_col, 3*sizeof(size_t)) == 0);
	aml_tiling_dims(t, dims);
	assert(memcmp(dims, expected_dims_col, 3*sizeof(size_t)) == 0);

177
178
179
180
181
182
183
184
185
	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){k, j, i});
				bres = aml_tiling_index(tres,
							(size_t[]){k, j, i});
				aml_copy_layout_generic(bres, b, NULL);
186
187
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
188
189
			}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 * sizeof(int)) == 0);
190

191
192
193
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

194
195
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
196
197
198
199
200
	aml_tiling_resize_destroy(&t);
	aml_tiling_resize_destroy(&tres);

	aml_layout_dense_create(&a, memory, AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row,
201
				stride, dims_row);
202
203
	aml_layout_dense_create(&ares, memoryres, AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row,
204
				stride, dims_row);
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219


	aml_tiling_resize_create(&t, AML_TILING_ORDER_ROW_MAJOR,
				     a, 3, dims_tile_row);
	aml_tiling_resize_create(&tres, AML_TILING_ORDER_ROW_MAJOR,
				     ares, 3, dims_tile_row);

	assert(aml_tiling_order(t) == AML_TILING_ORDER_ROW_MAJOR);
	assert(aml_tiling_ndims(t) == 3);

	aml_tiling_tile_dims(t, dims);
	assert(memcmp(dims, dims_tile_row, 3*sizeof(size_t)) == 0);
	aml_tiling_dims(t, dims);
	assert(memcmp(dims, expected_dims_row, 3*sizeof(size_t)) == 0);

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++)
				memoryres[i][j][k] = 0.0;

	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){i, j, k});
				bres = aml_tiling_index(tres,
							(size_t[]){i, j, k});
				assert(b != NULL && bres != NULL);
				aml_copy_layout_generic(bres, b, NULL);
235
236
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
237
238
			}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 * sizeof(int)) == 0);
239

240
241
242
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

243
244
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
245
246
247
	aml_tiling_resize_destroy(&t);
	aml_tiling_resize_destroy(&tres);

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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
void test_tiling_excit(void)
{
	int memory[9][10][8];
	int memoryres[9][10][8];
	size_t dims_col[3] = {8, 10, 9};

	size_t stride[3] = {1, 1, 1};

	size_t dims_tile_col[3] = {4, 10, 3};

	int l = 0;

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++) {
				memory[i][j][k] = l;
				memoryres[i][j][k] = 0.0;
			}

	struct aml_layout *a, *ares;

	aml_layout_dense_create(&a, memory, AML_LAYOUT_ORDER_COLUMN_MAJOR,
	                        sizeof(int), 3, dims_col, stride, dims_col);
	aml_layout_dense_create(&ares, memoryres, AML_LAYOUT_ORDER_COLUMN_MAJOR,
	                        sizeof(int), 3, dims_col, stride, dims_col);

	struct aml_tiling *t, *tres;

	aml_tiling_resize_create(&t, AML_TILING_ORDER_COLUMN_MAJOR, a, 3,
	                         dims_tile_col);
	aml_tiling_resize_create(&tres, AML_TILING_ORDER_COLUMN_MAJOR, ares, 3,
	                         dims_tile_col);

	excit_t it, iter;

	it = excit_alloc(EXCIT_RANGE);
	iter = excit_alloc(EXCIT_PRODUCT);
	assert(!excit_range_init(it, 0, 1, 1));
	assert(!excit_product_add(iter, it));
	it = excit_alloc(EXCIT_RANGE);
	assert(!excit_range_init(it, 0, 0, 1));
	assert(!excit_product_add(iter, it));
	it = excit_alloc(EXCIT_RANGE);
	assert(!excit_range_init(it, 0, 2, 1));
	assert(!excit_product_add(iter, it));

	for (ssize_t coords[3]; !excit_next(iter, coords);) {
		struct aml_layout *b, *bres;

		b = aml_tiling_index(t, (size_t *)coords);
		bres = aml_tiling_index(tres, (size_t *)coords);
		aml_copy_layout_generic(bres, b, NULL);
		aml_layout_destroy(&b);
		aml_layout_destroy(&bres);
	}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 * sizeof(int)) == 0);
	memset(memoryres, 0, 8 * 10 * 9 * sizeof(int));

	assert(!excit_rewind(iter));

	for (; !excit_peek(iter, NULL); excit_next(iter, NULL)) {
		struct aml_layout *b, *bres;

		b = aml_tiling_index_byiter(t, iter);
		bres = aml_tiling_index_byiter(tres, iter);
		aml_copy_layout_generic(bres, b, NULL);
		aml_layout_destroy(&b);
		aml_layout_destroy(&bres);
	}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 * sizeof(int)) == 0);

	excit_free(iter);
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
	aml_tiling_resize_destroy(&t);
	aml_tiling_resize_destroy(&tres);
}

328
void test_tiling_uneven(void)
329
330
{

331
332
333
334
335
336
337
338
339
340
341
342
343
	int memory[8][10][7];
	int memoryres[8][10][7];
	size_t dims_col[3] = {7, 10, 8};
	size_t dims_row[3] = {8, 10, 7};

	size_t stride[3] = {1, 1, 1};

	size_t dims_tile_col[3] = {4, 10, 3};
	size_t dims_tile_row[3] = {3, 10, 4};

	size_t expected_dims_col[3] = {2, 1, 3};
	size_t expected_dims_row[3] = {3, 1, 2};

344
345
346
347
348
349
350
351
	int l = 0;

	for (size_t i = 0; i < 8; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 7; k++, l++) {
				memory[i][j][k] = l;
				memoryres[i][j][k] = 0.0;
			}
352
353

	struct aml_layout *a, *ares;
354

355
356
	aml_layout_dense_create(&a, memory, AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col,
357
				stride, dims_col);
358
359
	aml_layout_dense_create(&ares, memoryres, AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col,
360
				stride, dims_col);
361
362
363


	struct aml_tiling *t, *tres;
364

365
366
367
368
	aml_tiling_resize_create(&t, AML_TILING_ORDER_COLUMN_MAJOR,
				     a, 3, dims_tile_col);
	aml_tiling_resize_create(&tres, AML_TILING_ORDER_COLUMN_MAJOR,
				     ares, 3, dims_tile_col);
369
370


371
372
	assert(aml_tiling_order(t) == AML_TILING_ORDER_COLUMN_MAJOR);
	assert(aml_tiling_ndims(t) == 3);
373

374
375
	size_t dims[3];

376
377
378
379
	aml_tiling_tile_dims(t, dims);
	assert(memcmp(dims, dims_tile_col, 3*sizeof(size_t)) == 0);
	aml_tiling_dims(t, dims);
	assert(memcmp(dims, expected_dims_col, 3*sizeof(size_t)) == 0);
380

381
382
383
384
385
386
387
388
389
	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){k, j, i});
				bres = aml_tiling_index(tres,
							(size_t[]){k, j, i});
				aml_copy_layout_generic(bres, b, NULL);
390
391
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
392
393
			}
	assert(memcmp(memory, memoryres, 7 * 10 * 8 * sizeof(int)) == 0);
394

395
396
397
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

398
399
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
	aml_tiling_resize_destroy(&t);
	aml_tiling_resize_destroy(&tres);

	aml_layout_dense_create(&a, memory, AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row,
				  stride, dims_row);
	aml_layout_dense_create(&ares, memoryres, AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row,
				  stride, dims_row);

	aml_tiling_resize_create(&t, AML_TILING_ORDER_ROW_MAJOR,
				     a, 3, dims_tile_row);
	aml_tiling_resize_create(&tres, AML_TILING_ORDER_ROW_MAJOR,
				     ares, 3, dims_tile_row);

	assert(aml_tiling_order(t) == AML_TILING_ORDER_ROW_MAJOR);
	assert(aml_tiling_ndims(t) == 3);

	aml_tiling_tile_dims(t, dims);
	assert(memcmp(dims, dims_tile_row, 3*sizeof(size_t)) == 0);
	aml_tiling_dims(t, dims);
	assert(memcmp(dims, expected_dims_row, 3*sizeof(size_t)) == 0);

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
	for (size_t i = 0; i < 8; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 7; k++, l++) {
				memory[i][j][k] = l;
				memoryres[i][j][k] = 0.0;
			}

	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){i, j, k});
				bres = aml_tiling_index(tres,
							(size_t[]){i, j, k});
				aml_copy_layout_generic(bres, b, NULL);
439
440
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
441
442
			}
	assert(memcmp(memory, memoryres, 7 * 10 * 8 * sizeof(int)) == 0);
443

444
445
446
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

447
448
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
449
450
451
452
	aml_tiling_resize_destroy(&t);
	aml_tiling_resize_destroy(&tres);
}

Swann Perarnau's avatar
Swann Perarnau committed
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
void test_tiling_pad_even(void)
{
	int memory[9][10][8];
	int memoryres[9][10][8];
	size_t dims_col[3] = {8, 10, 9};
	size_t dims_row[3] = {9, 10, 8};

	size_t stride[3] = {1, 1, 1};

	size_t dims_tile_col[3] = {4, 10, 3};
	size_t dims_tile_row[3] = {3, 10, 4};

	size_t expected_dims_col[3] = {2, 1, 3};
	size_t expected_dims_row[3] = {3, 1, 2};

	int l = 0;

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++) {
				memory[i][j][k] = l;
				memoryres[i][j][k] = 0.0;
			}

	struct aml_layout *a, *ares;

	aml_layout_dense_create(&a, memory,
				AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col, stride, dims_col);
	aml_layout_dense_create(&ares, memoryres,
				AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col, stride, dims_col);


	struct aml_tiling *t, *tres;
	int neutral = 0xdeadbeef;

	aml_tiling_pad_create(&t, AML_TILING_ORDER_COLUMN_MAJOR,
			      a, 3, dims_tile_col, &neutral);
	aml_tiling_pad_create(&tres, AML_TILING_ORDER_COLUMN_MAJOR,
			      ares, 3, dims_tile_col, &neutral);


	assert(aml_tiling_order(t) == AML_TILING_ORDER_COLUMN_MAJOR);
	assert(aml_tiling_ndims(t) == 3);

	size_t dims[3];

	aml_tiling_tile_dims(t, dims);
	assert(memcmp(dims, dims_tile_col, 3*sizeof(size_t)) == 0);
	aml_tiling_dims(t, dims);
	assert(memcmp(dims, expected_dims_col, 3*sizeof(size_t)) == 0);

	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){k, j, i});
				bres = aml_tiling_index(tres,
							(size_t[]){k, j, i});
				aml_copy_layout_generic(bres, b, NULL);
515
516
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
Swann Perarnau's avatar
Swann Perarnau committed
517
518
519
			}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 * sizeof(int)) == 0);

520
521
522
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

523
524
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
Swann Perarnau's avatar
Swann Perarnau committed
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
	aml_tiling_pad_destroy(&t);
	aml_tiling_pad_destroy(&tres);

	aml_layout_dense_create(&a, memory,
				AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row, stride, dims_row);
	aml_layout_dense_create(&ares, memoryres,
				AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row, stride, dims_row);

	aml_tiling_pad_create(&t, AML_TILING_ORDER_ROW_MAJOR,
				  a, 3, dims_tile_row, &neutral);
	aml_tiling_pad_create(&tres, AML_TILING_ORDER_ROW_MAJOR,
			      ares, 3, dims_tile_row, &neutral);

	assert(aml_tiling_order(t) == AML_TILING_ORDER_ROW_MAJOR);
	assert(aml_tiling_ndims(t) == 3);

	aml_tiling_tile_dims(t, dims);
	assert(memcmp(dims, dims_tile_row, 3*sizeof(size_t)) == 0);
	aml_tiling_dims(t, dims);
	assert(memcmp(dims, expected_dims_row, 3*sizeof(size_t)) == 0);

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++)
				memoryres[i][j][k] = 0.0;

	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){i, j, k});
				bres = aml_tiling_index(tres,
							(size_t[]){i, j, k});
				aml_copy_layout_generic(bres, b, NULL);
562
563
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
Swann Perarnau's avatar
Swann Perarnau committed
564
565
566
			}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 * sizeof(int)) == 0);

567
568
569
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

570
571
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
Swann Perarnau's avatar
Swann Perarnau committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
	aml_tiling_pad_destroy(&t);
	aml_tiling_pad_destroy(&tres);
}

void test_tiling_pad_uneven(void)
{

	int memory[8][10][7];
	int memoryres[9][10][8];
	size_t dims_col[3] = {7, 10, 8};
	size_t dims_row[3] = {8, 10, 7};
	size_t dims_col_res[3] = {8, 10, 9};
	size_t dims_row_res[3] = {9, 10, 8};

	size_t stride[3] = {1, 1, 1};

	size_t dims_tile_col[3] = {4, 10, 3};
	size_t dims_tile_row[3] = {3, 10, 4};

	size_t expected_dims_col[3] = {2, 1, 3};
	size_t expected_dims_row[3] = {3, 1, 2};

	int l = 0;

	for (size_t i = 0; i < 8; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 7; k++, l++)
				memory[i][j][k] = l;

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++)
				memoryres[i][j][k] = 0.0;


	struct aml_layout *a, *ares;

	aml_layout_dense_create(&a, memory,
				AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col, stride, dims_col);
	aml_layout_dense_create(&ares, memoryres,
				AML_LAYOUT_ORDER_COLUMN_MAJOR,
				sizeof(int), 3, dims_col_res,
				stride, dims_col_res);

	struct aml_tiling *t, *tres;
	int neutral = 0xdeadbeef;

	aml_tiling_pad_create(&t, AML_TILING_ORDER_COLUMN_MAJOR,
			      a, 3, dims_tile_col, &neutral);
	aml_tiling_pad_create(&tres, AML_TILING_ORDER_COLUMN_MAJOR,
			      ares, 3, dims_tile_col, &neutral);


	assert(aml_tiling_order(t) == AML_TILING_ORDER_COLUMN_MAJOR);
	assert(aml_tiling_ndims(t) == 3);

	size_t dims[3];

	aml_tiling_tile_dims(t, dims);
	assert(memcmp(dims, dims_tile_col, 3*sizeof(size_t)) == 0);
	aml_tiling_dims(t, dims);
	assert(memcmp(dims, expected_dims_col, 3*sizeof(size_t)) == 0);

	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){k, j, i});
				bres = aml_tiling_index(tres,
							(size_t[]){k, j, i});
				aml_copy_layout_generic(bres, b, NULL);
645
646
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
Swann Perarnau's avatar
Swann Perarnau committed
647
648
649
650
651
652
653
654
655
656
657
658
			}

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++)
				if (k >= 7 || i >= 8)
					assert(memoryres[i][j][k] ==
					       neutral);
				else
					assert(memoryres[i][j][k] ==
					       memory[i][j][k]);

659
660
661
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

662
663
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
Swann Perarnau's avatar
Swann Perarnau committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
	aml_tiling_pad_destroy(&t);
	aml_tiling_pad_destroy(&tres);

	aml_layout_dense_create(&a, memory,
				AML_LAYOUT_ORDER_ROW_MAJOR,
				sizeof(int), 3, dims_row, stride, dims_row);
	aml_layout_dense_create(&ares, memoryres,
				  AML_LAYOUT_ORDER_ROW_MAJOR,
				  sizeof(int), 3, dims_row_res,
				  stride, dims_row_res);

	aml_tiling_pad_create(&t, AML_TILING_ORDER_ROW_MAJOR,
				  a, 3, dims_tile_row, &neutral);
	aml_tiling_pad_create(&tres, AML_TILING_ORDER_ROW_MAJOR,
			      ares, 3, dims_tile_row, &neutral);

	assert(aml_tiling_order(t) == AML_TILING_ORDER_ROW_MAJOR);
	assert(aml_tiling_ndims(t) == 3);


	aml_tiling_tile_dims(t, dims);
	assert(memcmp(dims, dims_tile_row, 3*sizeof(size_t)) == 0);
	aml_tiling_dims(t, dims);
	assert(memcmp(dims, expected_dims_row, 3*sizeof(size_t)) == 0);

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++)
				memoryres[i][j][k] = 0.0;

	for (size_t i = 0; i < expected_dims_col[2]; i++)
		for (size_t j = 0; j < expected_dims_col[1]; j++)
			for (size_t k = 0; k < expected_dims_col[0]; k++) {
				struct aml_layout *b, *bres;

				b = aml_tiling_index(t, (size_t[]){i, j, k});
				bres = aml_tiling_index(tres,
							(size_t[]){i, j, k});
				aml_copy_layout_generic(bres, b, NULL);
703
704
				aml_layout_destroy(&b);
				aml_layout_destroy(&bres);
Swann Perarnau's avatar
Swann Perarnau committed
705
706
707
708
709
710
711
712
713
714
715
716
			}

	for (size_t i = 0; i < 9; i++)
		for (size_t j = 0; j < 10; j++)
			for (size_t k = 0; k < 8; k++, l++)
				if (k >= 7 || i >= 8)
					assert(memoryres[i][j][k] ==
					       neutral);
				else
					assert(memoryres[i][j][k] ==
					       memory[i][j][k]);

717
718
719
	assert(!aml_tiling_fprintf(stderr, "test", t));
	assert(!aml_tiling_fprintf(stderr, "test", tres));

720
721
	aml_layout_destroy(&a);
	aml_layout_destroy(&ares);
Swann Perarnau's avatar
Swann Perarnau committed
722
723
724
725
	aml_tiling_pad_destroy(&t);
	aml_tiling_pad_destroy(&tres);
}

726
727
728
729
730
731
732
733
int main(int argc, char *argv[])
{
	/* library initialization */
	aml_init(&argc, &argv);

	test_tiling_even();
	test_tiling_uneven();
	test_tiling_even_mixed();
Swann Perarnau's avatar
Swann Perarnau committed
734
735
	test_tiling_pad_even();
	test_tiling_pad_uneven();
736

737
	test_tiling_excit();
Swann Perarnau's avatar
Swann Perarnau committed
738
	aml_finalize();
739
740
	return 0;
}
741