Skip to content

Commit dd7982f

Browse files
committed
Add tests with unsigned loop bounds/kernel args
1 parent 5e93d51 commit dd7982f

File tree

5 files changed

+133
-8
lines changed

5 files changed

+133
-8
lines changed

tests/nomp-api-200-impl.h

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,58 @@
11
#include "nomp-test.h"
22

3+
#define nomp_api_200_aux_ui TOKEN_PASTE(nomp_api_200_aux_ui, TEST_SUFFIX)
4+
static int nomp_api_200_aux_ui(const char *fmt, TEST_TYPE *a, TEST_TYPE *b,
5+
unsigned n) {
6+
nomp_test_chk(nomp_update(a, 0, n, sizeof(TEST_TYPE), NOMP_TO));
7+
nomp_test_chk(nomp_update(b, 0, n, sizeof(TEST_TYPE), NOMP_TO));
8+
9+
int id = -1;
10+
const char *clauses[4] = {"transform", "nomp-api-200", "transform", 0};
11+
char *knl = generate_knl(fmt, 2, TOSTRING(TEST_TYPE), TOSTRING(TEST_TYPE));
12+
nomp_test_chk(nomp_jit(&id, knl, clauses, 3, "a", sizeof(TEST_TYPE), NOMP_PTR,
13+
"b", sizeof(TEST_TYPE), NOMP_PTR, "N",
14+
sizeof(unsigned), NOMP_UINT));
15+
nomp_free(&knl);
16+
17+
nomp_test_chk(nomp_run(id, a, b, &n));
18+
19+
nomp_test_chk(nomp_sync());
20+
21+
nomp_test_chk(nomp_update(a, 0, n, sizeof(TEST_TYPE), NOMP_FROM));
22+
nomp_test_chk(nomp_update(a, 0, n, sizeof(TEST_TYPE), NOMP_FREE));
23+
nomp_test_chk(nomp_update(b, 0, n, sizeof(TEST_TYPE), NOMP_FREE));
24+
25+
return 0;
26+
}
27+
28+
#define nomp_api_200_add_ui TOKEN_PASTE(nomp_api_200_add_ui, TEST_SUFFIX)
29+
static int nomp_api_200_add_ui(unsigned n) {
30+
nomp_test_assert(n <= TEST_MAX_SIZE);
31+
32+
TEST_TYPE a[TEST_MAX_SIZE], b[TEST_MAX_SIZE];
33+
for (unsigned i = 0; i < n; i++)
34+
a[i] = n - i, b[i] = i;
35+
36+
const char *knl_fmt =
37+
"void foo(%s *a, %s *b, int N) { \n"
38+
" for (int i = 0; i < N; i++) \n"
39+
" a[i] += b[i]; \n"
40+
"} \n";
41+
nomp_api_200_aux_ui(knl_fmt, a, b, n);
42+
43+
#if defined(TEST_TOL)
44+
for (unsigned i = 0; i < n; i++)
45+
nomp_test_assert(fabs(a[i] - n) < TEST_TOL);
46+
#else
47+
for (unsigned i = 0; i < n; i++)
48+
nomp_test_assert(a[i] == (TEST_TYPE)n);
49+
#endif
50+
51+
return 0;
52+
}
53+
#undef nomp_api_200_add_ui
54+
#undef nomp_api_200_aux_ui
55+
356
#define nomp_api_200_aux TOKEN_PASTE(nomp_api_200_aux, TEST_SUFFIX)
457
static int nomp_api_200_aux(const char *fmt, TEST_TYPE *a, TEST_TYPE *b,
558
int n) {
@@ -161,8 +214,8 @@ static int nomp_api_200_square(unsigned n) {
161214
}
162215
#undef nomp_api_200_square
163216

164-
#define nomp_api_200_linear TOKEN_PASTE(nomp_api_200_linear, TEST_SUFFIX)
165-
static int nomp_api_200_linear(unsigned n) {
217+
#define nomp_api_200_assign TOKEN_PASTE(nomp_api_200_assign, TEST_SUFFIX)
218+
static int nomp_api_200_assign(unsigned n) {
166219
nomp_test_assert(n <= TEST_MAX_SIZE);
167220

168221
TEST_TYPE a[TEST_MAX_SIZE] = {0}, b[TEST_MAX_SIZE] = {1, 2, 3, 4, 5};
@@ -184,5 +237,5 @@ static int nomp_api_200_linear(unsigned n) {
184237

185238
return 0;
186239
}
187-
#undef nomp_api_200_linear
240+
#undef nomp_api_200_assign
188241
#undef nomp_api_200_aux

tests/nomp-api-200.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ static int test_vector_addition(void) {
1111
return err;
1212
}
1313

14+
static int test_vector_addition_unsigned_bounds(void) {
15+
int err = 0;
16+
TEST_BUILTIN_TYPES(200_add_ui, 10)
17+
TEST_BUILTIN_TYPES(200_add_ui, 50)
18+
return err;
19+
}
20+
1421
static int test_vector_subtraction(void) {
1522
int err = 0;
1623
TEST_BUILTIN_TYPES(200_sub, 10)
@@ -39,10 +46,10 @@ static int test_vector_square_sum(void) {
3946
return err;
4047
}
4148

42-
static int test_vector_linear(void) {
49+
static int test_vector_assign(void) {
4350
int err = 0;
44-
TEST_BUILTIN_TYPES(200_linear, 10)
45-
TEST_BUILTIN_TYPES(200_linear, 50)
51+
TEST_BUILTIN_TYPES(200_assign, 10)
52+
TEST_BUILTIN_TYPES(200_assign, 50)
4653
return err;
4754
}
4855

@@ -51,11 +58,12 @@ int main(int argc, const char *argv[]) {
5158
nomp_check(err);
5259

5360
err |= SUBTEST(test_vector_addition);
61+
err |= SUBTEST(test_vector_addition_unsigned_bounds);
5462
err |= SUBTEST(test_vector_subtraction);
5563
err |= SUBTEST(test_vector_multiplication_sum);
5664
err |= SUBTEST(test_vector_multiplication);
5765
err |= SUBTEST(test_vector_square_sum);
58-
err |= SUBTEST(test_vector_linear);
66+
err |= SUBTEST(test_vector_assign);
5967

6068
err |= nomp_finalize();
6169
nomp_check(err);

tests/nomp-api-300-impl.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,62 @@
22

33
#define TEST_MAX_SIZE2 (TEST_MAX_SIZE * TEST_MAX_SIZE)
44

5+
#define nomp_api_300_aux_ui TOKEN_PASTE(nomp_api_300_aux_ui, TEST_SUFFIX)
6+
static int nomp_api_300_aux_ui(const char *fmt, TEST_TYPE *a, TEST_TYPE *b,
7+
unsigned rows, unsigned cols, int n) {
8+
nomp_test_chk(nomp_update(a, 0, n, sizeof(TEST_TYPE), NOMP_TO));
9+
nomp_test_chk(nomp_update(b, 0, n, sizeof(TEST_TYPE), NOMP_TO));
10+
11+
int id = -1;
12+
const char *clauses[4] = {"transform", "nomp-api-300", "transform", 0};
13+
char *knl = generate_knl(fmt, 2, TOSTRING(TEST_TYPE), TOSTRING(TEST_TYPE));
14+
nomp_test_chk(nomp_jit(&id, knl, clauses, 4, "a", sizeof(TEST_TYPE), NOMP_PTR,
15+
"b", sizeof(TEST_TYPE), NOMP_PTR, "rows",
16+
sizeof(unsigned), NOMP_UINT, "cols", sizeof(unsigned),
17+
NOMP_UINT));
18+
nomp_free(&knl);
19+
20+
nomp_test_chk(nomp_run(id, a, b, &rows, &cols));
21+
22+
nomp_test_chk(nomp_sync());
23+
24+
nomp_test_chk(nomp_update(a, 0, n, sizeof(TEST_TYPE), NOMP_FROM));
25+
nomp_test_chk(nomp_update(a, 0, n, sizeof(TEST_TYPE), NOMP_FREE));
26+
nomp_test_chk(nomp_update(b, 0, n, sizeof(TEST_TYPE), NOMP_FREE));
27+
28+
return 0;
29+
}
30+
31+
#define nomp_api_300_add_ui TOKEN_PASTE(nomp_api_300_add_ui, TEST_SUFFIX)
32+
static int nomp_api_300_add_ui(unsigned rows, unsigned cols) {
33+
const unsigned n = rows * cols;
34+
nomp_test_assert(n <= TEST_MAX_SIZE2);
35+
36+
TEST_TYPE a[TEST_MAX_SIZE2], b[TEST_MAX_SIZE2];
37+
for (unsigned i = 0; i < n; i++)
38+
a[i] = 2 * n - i, b[i] = i;
39+
40+
const char *knl_fmt =
41+
"void foo(%s *a, %s *b, unsigned rows, unsigned cols) { \n"
42+
" for (int e = 0; e < rows; e++) \n"
43+
" for (int i = 0; i < cols; i++) \n"
44+
" a[e * cols + i] = a[e * cols + i] + b[e * cols + i]; \n"
45+
"} \n";
46+
nomp_api_300_aux_ui(knl_fmt, a, b, rows, cols, n);
47+
48+
#if defined(TEST_TOL)
49+
for (unsigned i = 0; i < n; i++)
50+
nomp_test_assert(fabs(a[i] - 2 * n) < TEST_TOL);
51+
#else
52+
for (unsigned i = 0; i < n; i++)
53+
nomp_test_assert(a[i] == (TEST_TYPE)(2 * n));
54+
#endif
55+
56+
return 0;
57+
}
58+
#undef nomp_api_300_add_ui
59+
#undef nomp_api_300_aux_ui
60+
561
#define nomp_api_300_aux TOKEN_PASTE(nomp_api_300_aux, TEST_SUFFIX)
662
static int nomp_api_300_aux(const char *fmt, TEST_TYPE *a, TEST_TYPE *b,
763
int rows, int cols, int n) {

tests/nomp-api-300.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ static int test_matrix_addition(void) {
1111
return err;
1212
}
1313

14+
static int test_matrix_addition_unsigned_bounds(void) {
15+
int err = 0;
16+
TEST_BUILTIN_TYPES(300_add_ui, 40, 5)
17+
TEST_BUILTIN_TYPES(300_add_ui, 16, 16)
18+
return err;
19+
}
20+
1421
static int test_matrix_transpose(void) {
1522
int err = 0;
1623
TEST_BUILTIN_TYPES(300_transpose, 40, 5)
@@ -37,6 +44,7 @@ int main(int argc, const char *argv[]) {
3744
nomp_check(err);
3845

3946
err |= SUBTEST(test_matrix_addition);
47+
err |= SUBTEST(test_matrix_addition_unsigned_bounds);
4048
err |= SUBTEST(test_matrix_transpose);
4149
err |= SUBTEST(test_matrix_vector_multiplication);
4250
err |= SUBTEST(test_matrix_matrix_multiplication);

tests/nomp-api-500-impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static int nomp_api_500_multiple_reductions(unsigned N, unsigned iterations) {
229229
"} \n";
230230

231231
TEST_TYPE a[TEST_MAX_SIZE];
232-
for (unsigned i = 1; i < iterations; ++i) {
232+
for (unsigned i = 0; i < iterations; ++i) {
233233
for (unsigned j = 0; j < N; j++)
234234
a[j] = i * j;
235235

0 commit comments

Comments
 (0)