Skip to content

Commit eef0c39

Browse files
committed
Adding error messages when necessary for CPP API functions
1 parent c693385 commit eef0c39

6 files changed

Lines changed: 45 additions & 29 deletions

File tree

include/af/exception.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class AFAPI exception
2121
exception(const char *msg);
2222
exception(const char *file, unsigned line);
2323
exception(const char *file, unsigned line, af_err err);
24+
exception(const char *msg, const char *file, unsigned line, af_err err);
2425
virtual ~exception() throw() {}
2526
virtual const char *what() const throw() { return m_msg; }
2627

src/api/cpp/array.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ namespace af
8484
switch (src) {
8585
case afHost: AF_THROW(af_create_array(arr, (const void * const)ptr, 4, my_dims, ty)); break;
8686
case afDevice: AF_THROW(af_device_array(arr, (const void * )ptr, 4, my_dims, ty)); break;
87-
default: AF_THROW(AF_ERR_INVALID_ARG);
87+
default: AF_THROW_MSG("Can not create array from the requested source pointer",
88+
AF_ERR_INVALID_ARG);
8889
}
8990
}
9091

@@ -647,31 +648,32 @@ namespace af
647648
AF_THROW(af_eval(get()));
648649
}
649650

650-
#define INSTANTIATE(T) \
651-
template<> AFAPI T *array::host() const \
652-
{ \
653-
if (type() != (af::dtype)dtype_traits<T>::af_type) { \
654-
AF_THROW(AF_ERR_INVALID_TYPE); \
655-
} \
656-
\
657-
T *res = new T[elements()]; \
658-
AF_THROW(af_get_data_ptr((void *)res, get())); \
659-
\
660-
return res; \
661-
} \
662-
template<> AFAPI T array::scalar() const \
663-
{ \
664-
T *h_ptr = host<T>(); \
665-
T scalar = h_ptr[0]; \
666-
delete[] h_ptr; \
667-
return scalar; \
668-
} \
669-
template<> AFAPI T* array::device() const \
670-
{ \
671-
void *ptr = NULL; \
672-
AF_THROW(af_get_device_ptr(&ptr, get(), true)); \
673-
return (T *)ptr; \
674-
} \
651+
#define INSTANTIATE(T) \
652+
template<> AFAPI T *array::host() const \
653+
{ \
654+
if (type() != (af::dtype)dtype_traits<T>::af_type) { \
655+
AF_THROW_MSG("Requested type does'nt match with array", \
656+
AF_ERR_INVALID_TYPE); \
657+
} \
658+
\
659+
T *res = new T[elements()]; \
660+
AF_THROW(af_get_data_ptr((void *)res, get())); \
661+
\
662+
return res; \
663+
} \
664+
template<> AFAPI T array::scalar() const \
665+
{ \
666+
T *h_ptr = host<T>(); \
667+
T scalar = h_ptr[0]; \
668+
delete[] h_ptr; \
669+
return scalar; \
670+
} \
671+
template<> AFAPI T* array::device() const \
672+
{ \
673+
void *ptr = NULL; \
674+
AF_THROW(af_get_device_ptr(&ptr, get(), true)); \
675+
return (T *)ptr; \
676+
} \
675677

676678
INSTANTIATE(cdouble)
677679
INSTANTIATE(cfloat)

src/api/cpp/error.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,8 @@
1414
if (__err == AF_SUCCESS) break; \
1515
throw af::exception(__FILE__, __LINE__, __err); \
1616
} while(0)
17+
18+
#define AF_THROW_MSG(__msg, __err) do { \
19+
if (__err == AF_SUCCESS) break; \
20+
throw af::exception(__msg, __FILE__, __LINE__, __err); \
21+
} while(0);

src/api/cpp/exception.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,11 @@ exception::exception(const char *file, unsigned line, af_err err)
4040
m_msg[sizeof(m_msg)-1] = '\0';
4141
}
4242

43+
exception::exception(const char *msg, const char *file, unsigned line, af_err err)
44+
{
45+
snprintf(m_msg, sizeof(m_msg)-1, "%s\n%s:%d: AF_ERROR %d", msg, file, line, (int)(err));
46+
m_msg[sizeof(m_msg)-1] = '\0';
47+
}
48+
49+
4350
}

src/api/cpp/gfor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ namespace af
3232

3333
array batchFunc(const array &lhs, const array &rhs, batchFunc_t func)
3434
{
35-
if (gforGet()) AF_THROW(AF_ERR_INVALID_ARG);
35+
if (gforGet()) AF_THROW_MSG("batchFunc can not be used inside GFOR",
36+
AF_ERR_INVALID_ARG);
3637
gforSet(true);
3738
array res = func(lhs, rhs);
3839
gforSet(false);

src/api/cpp/seq.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ seq::seq(double begin, double end, double step): m_gfor(false)
6262

6363
if (step == 0) {
6464
if (begin != end) // Span
65-
AF_THROW(AF_ERR_INVALID_ARG);
65+
AF_THROW_MSG("Invalid step size", AF_ERR_INVALID_ARG);
6666
}
6767
if (end >= 0 && begin >= 0 && signbit(end-begin) != signbit(step))
68-
AF_THROW(AF_ERR_INVALID_ARG);
68+
AF_THROW_MSG("Sequence is invalid", AF_ERR_INVALID_ARG);
6969
//AF_THROW("step must match direction of sequence");
7070
init(begin, end, step);
7171
}

0 commit comments

Comments
 (0)