test_tiling.c 9.1 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
#include "aml/tiling/resize.h"
13 14
#include <assert.h>

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
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};
	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;
	}

	struct aml_layout *a, *ares;
39
	aml_layout_native_create(&a, AML_TYPE_LAYOUT_COLUMN_ORDER,
40 41
				  (void *)memory, sizeof(int), 3, dims_col,
				  stride, dims_col);
42
	aml_layout_native_create(&ares, AML_TYPE_LAYOUT_ROW_ORDER,
43 44 45 46 47
				  (void *)memoryres, sizeof(int), 3, dims_row,
				  stride, dims_row);


	struct aml_tiling *t, *tres;
48
	aml_tiling_resize_create(&t, AML_TYPE_TILING_COLUMN_ORDER,
49
				     a, 3, dims_tile_col);
50
	aml_tiling_resize_create(&tres, AML_TYPE_TILING_ROW_ORDER,
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
				     ares, 3, dims_tile_row);


	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, k, j, i);
		bres = aml_tiling_index(tres, i, j, k);
		aml_copy_layout_generic(bres, b);
		free(b);
		free(bres);
	}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 *sizeof(int)) == 0);

	free(a);
	free(ares);
	free(t);
	free(tres);

71
	aml_layout_native_create(&a, AML_TYPE_LAYOUT_COLUMN_ORDER,
72 73
				  (void *)memory, sizeof(int), 3, dims_col,
				  stride, dims_col);
74
	aml_layout_native_create(&ares, AML_TYPE_LAYOUT_ROW_ORDER,
75 76 77 78
				  (void *)memoryres, sizeof(int), 3, dims_row,
				  stride, dims_row);


79
	aml_tiling_resize_create(&t, AML_TYPE_TILING_ROW_ORDER,
80
				     a, 3, dims_tile_row);
81
	aml_tiling_resize_create(&tres, AML_TYPE_TILING_COLUMN_ORDER,
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
				     ares, 3, dims_tile_col);

	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, i, j, k);
		bres = aml_tiling_index(tres, k, j, i);
		aml_copy_layout_generic(bres, b);
		free(b);
		free(bres);
	}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 *sizeof(int)) == 0);
100

101 102 103 104 105 106 107 108
	free(a);
	free(ares);
	free(t);
	free(tres);

}

void test_tiling_even(void)
109
{
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	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;
129 130
	}

131
	struct aml_layout *a, *ares;
132
	aml_layout_native_create(&a, AML_TYPE_LAYOUT_COLUMN_ORDER,
133 134
				  (void *)memory, sizeof(int), 3, dims_col,
				  stride, dims_col);
135
	aml_layout_native_create(&ares, AML_TYPE_LAYOUT_COLUMN_ORDER,
136 137 138 139 140
				  (void *)memoryres, sizeof(int), 3, dims_col,
				  stride, dims_col);


	struct aml_tiling *t, *tres;
141
	aml_tiling_resize_create(&t, AML_TYPE_TILING_COLUMN_ORDER,
142
				     a, 3, dims_tile_col);
143
	aml_tiling_resize_create(&tres, AML_TYPE_TILING_COLUMN_ORDER,
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
				     ares, 3, dims_tile_col);


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

        size_t dims[3];
	aml_tiling_tile_adims(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, k, j, i);
		bres = aml_tiling_index(tres, k, j, i);
		aml_copy_layout_generic(bres, b);
		free(b);
		free(bres);
165
	}
166 167 168 169 170 171 172
	assert(memcmp(memory, memoryres, 8 * 10 * 9 *sizeof(int)) == 0);

	free(a);
	free(ares);
	free(t);
	free(tres);

173
	aml_layout_native_create(&a, AML_TYPE_LAYOUT_ROW_ORDER,
174 175
				  (void *)memory, sizeof(int), 3, dims_row,
				  stride, dims_row);
176
	aml_layout_native_create(&ares, AML_TYPE_LAYOUT_ROW_ORDER,
177 178 179 180
				  (void *)memoryres, sizeof(int), 3, dims_row,
				  stride, dims_row);


181
	aml_tiling_resize_create(&t, AML_TYPE_TILING_ROW_ORDER,
182
				     a, 3, dims_tile_row);
183
	aml_tiling_resize_create(&tres, AML_TYPE_TILING_ROW_ORDER,
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
				     ares, 3, dims_tile_row);

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

	aml_tiling_tile_adims(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, i, j, k);
		bres = aml_tiling_index(tres, i, j, k);
		aml_copy_layout_generic(bres, b);
		free(b);
		free(bres);
	}
	assert(memcmp(memory, memoryres, 8 * 10 * 9 *sizeof(int)) == 0);

	free(a);
	free(ares);
	free(t);
	free(tres);

216 217
}

218
void test_tiling_uneven(void)
219 220
{

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	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};

        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;
	}

	struct aml_layout *a, *ares;
243
	aml_layout_native_create(&a, AML_TYPE_LAYOUT_COLUMN_ORDER,
244 245
				  (void *)memory, sizeof(int), 3, dims_col,
				  stride, dims_col);
246
	aml_layout_native_create(&ares, AML_TYPE_LAYOUT_COLUMN_ORDER,
247 248 249 250 251
				  (void *)memoryres, sizeof(int), 3, dims_col,
				  stride, dims_col);


	struct aml_tiling *t, *tres;
252
	aml_tiling_resize_create(&t, AML_TYPE_TILING_COLUMN_ORDER,
253
				     a, 3, dims_tile_col);
254
	aml_tiling_resize_create(&tres, AML_TYPE_TILING_COLUMN_ORDER,
255
				     ares, 3, dims_tile_col);
256 257


258 259
	assert(aml_tiling_order(t) == AML_TYPE_TILING_COLUMN_ORDER);
	assert(aml_tiling_ndims(t) == 3);
260

261 262 263 264 265
        size_t dims[3];
	aml_tiling_tile_adims(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);
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	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, k, j, i);
		bres = aml_tiling_index(tres, k, j, i);
		aml_copy_layout_generic(bres, b);
		free(b);
		free(bres);
	}
	assert(memcmp(memory, memoryres, 7 * 10 * 8 *sizeof(int)) == 0);

	free(a);
	free(ares);
	free(t);
	free(tres);

284
	aml_layout_native_create(&a, AML_TYPE_LAYOUT_ROW_ORDER,
285 286
				  (void *)memory, sizeof(int), 3, dims_row,
				  stride, dims_row);
287
	aml_layout_native_create(&ares, AML_TYPE_LAYOUT_ROW_ORDER,
288 289 290
				  (void *)memoryres, sizeof(int), 3, dims_row,
				  stride, dims_row);

291
	aml_tiling_resize_create(&t, AML_TYPE_TILING_ROW_ORDER,
292
				     a, 3, dims_tile_row);
293
	aml_tiling_resize_create(&tres, AML_TYPE_TILING_ROW_ORDER,
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
				     ares, 3, dims_tile_row);

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

	aml_tiling_tile_adims(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 < 8; i++)
	for(size_t j = 0; j < 10; j++)
	for(size_t k = 0; k < 7; 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, i, j, k);
		bres = aml_tiling_index(tres, i, j, k);
		aml_copy_layout_generic(bres, b);
		free(b);
		free(bres);
	}
	assert(memcmp(memory, memoryres, 7 * 10 * 8 *sizeof(int)) == 0);
320

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
	free(a);
	free(ares);
	free(t);
	free(tres);

}

int main(int argc, char *argv[])
{
	/* library initialization */
	aml_init(&argc, &argv);

	test_tiling_even();
	test_tiling_uneven();
	test_tiling_even_mixed();
336 337 338

	return 0;
}
339