Skip to content

Commit

Permalink
Fix bugs in the jit implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
thilinarmtb committed Dec 9, 2023
1 parent 5b83d2c commit 4900535
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
11 changes: 5 additions & 6 deletions src/loopy.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ int nomp_py_realize_reduction(PyObject **kernel, const char *const variable,
*/
int nomp_py_transform(PyObject **kernel, const char *const file,
const char *function, const PyObject *const context) {
// If either kernel, file, or function are NULL, we don't have to do anything:
if (kernel == NULL || *kernel == NULL || file == NULL || function == NULL)
// If either file, or function are NULL, we don't have to do anything:
if (file == NULL || function == NULL)
return 0;

#define check_error(obj) \
Expand Down Expand Up @@ -553,18 +553,17 @@ int nomp_py_fix_parameters(PyObject **knl, const PyObject *py_dict) {
*
* @brief Get the string representation of a Python object.
*
* @param obj Python object to be printed.
* @return void
* @param obj Python object.
* @return char*
*/
char *nomp_py_get_str(PyObject *const obj) {
PyObject *py_repr = PyObject_Str(obj);
PyObject *py_str = PyUnicode_AsEncodedString(py_repr, "utf-8", "~E~");
const char *str_ = PyBytes_AS_STRING(py_str);

char *str = nomp_calloc(char, strnlen(str_, NOMP_MAX_BUFFER_SIZE));
Py_XDECREF(py_repr), Py_XDECREF(py_str);
strncpy(str, str_, NOMP_MAX_BUFFER_SIZE);

Py_XDECREF(py_repr), Py_XDECREF(py_str);
return str;
}

Expand Down
18 changes: 11 additions & 7 deletions src/nomp.c
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,11 @@ static inline PyObject *nomp_get_py_object_from_type(nomp_arg_type_t type,
return value;
}

static inline nomp_prog_t *nomp_jit_init_args(int progs_n, int nargs,
static inline nomp_prog_t *nomp_jit_init_args(unsigned progs_n, unsigned nargs,
va_list args) {
// Allocate memory for the program.
nomp_prog_t *prg = progs[progs_n] = nomp_calloc(nomp_prog_t, 1);
prg->args = nomp_calloc(nomp_arg_t, nargs);
prg->nargs = nargs;
// Reduction index is set to -1 by default.
prg->reduction_index = -1;
// SymEngine map to store grid size expressions.
Expand All @@ -445,7 +444,8 @@ static inline nomp_prog_t *nomp_jit_init_args(int progs_n, int nargs,
// Dictionary to hold jit kernel arguments.
prg->py_dict = PyDict_New();

for (unsigned i = 0; i < prg->nargs; i++) {
unsigned current_narg = 0;
for (unsigned i = 0; i < nargs; i++) {
const char *name = va_arg(args, const char *);
const size_t size = va_arg(args, size_t);
int type = va_arg(args, int);
Expand All @@ -463,10 +463,13 @@ static inline nomp_prog_t *nomp_jit_init_args(int progs_n, int nargs,
continue;
}

strncpy(prg->args[i].name, name, NOMP_MAX_BUFFER_SIZE);
prg->args[i].size = size;
prg->args[i].type = type;
strncpy(prg->args[current_narg].name, name, NOMP_MAX_BUFFER_SIZE);
prg->args[current_narg].size = size;
prg->args[current_narg].type = type;
current_narg++;
}
prg->nargs = current_narg;

return prg;
}

Expand Down Expand Up @@ -537,7 +540,8 @@ int nomp_jit(int *id, const char *csrc, const char **clauses, int nargs, ...) {
}

// Call fix_parameters on the loopy kernel.
nomp_py_fix_parameters(&knl, prg->py_dict);
if (PyDict_Size(prg->py_dict))
nomp_py_fix_parameters(&knl, prg->py_dict);

// Get OpenCL, CUDA, etc. source and name from the loopy kernel and build
// the program.
Expand Down
2 changes: 1 addition & 1 deletion tests/nomp-api-400-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static int nomp_api_400_dynamic_aux(const char *fmt, TEST_TYPE *b, TEST_TYPE *a,

int id = -1;
const char *clauses[4] = {"transform", "nomp_api_400", "static", 0};
nomp_test_check(nomp_jit(&id, knl, clauses, 3, "b", sizeof(TEST_TYPE),
nomp_test_check(nomp_jit(&id, knl, clauses, 4, "b", sizeof(TEST_TYPE),
NOMP_PTR, "a", sizeof(TEST_TYPE), NOMP_PTR, "n",
sizeof(int), NOMP_INT, "m", sizeof(int),
NOMP_INT | NOMP_JIT, &m));
Expand Down

0 comments on commit 4900535

Please sign in to comment.