Skip to content

Commit

Permalink
g.fix
Browse files Browse the repository at this point in the history
  • Loading branch information
i-evi committed Sep 23, 2020
1 parent eb7dc1a commit abea6af
Show file tree
Hide file tree
Showing 25 changed files with 171 additions and 149 deletions.
37 changes: 23 additions & 14 deletions src/cc_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ case _DT: \
break;

#define CC_ARRAY_CAST_IMPLEMENTATION(dtype) \
void cc_array_cast_ ## dtype(void *dst, void *src, int arrlen, int dt) \
void cc_array_cast_ ## dtype( \
void *dst, const void *src, int arrlen, int dt) \
{ \
cc_int32 i; \
switch (dt) { \
Expand All @@ -74,7 +75,7 @@ void cc_array_cast_ ## dtype(void *dst, void *src, int arrlen, int dt) \
case _DT: \
ARRAY_ELEM_SET(arr, x, arrlen, _dt) \
break;
void cc_array_set(void *arr, int arrlen, void *x, int dt)
void cc_array_set(void *arr, int arrlen, const void *x, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -100,7 +101,7 @@ case _DT: \
ARRAY_ELEM_CLIP(arr, min, max, arrlen, _dt); \
break;
void cc_array_clip_by_value(
void *arr, int arrlen, void *min, void *max, int dt)
void *arr, int arrlen, const void *min, const void *max, int dt)
{
cc_int32 i;
switch (dt) {
Expand Down Expand Up @@ -136,7 +137,8 @@ CC_ARRAY_CAST_IMPLEMENTATION (float64)
case _DT: \
ARRAY_SC_OPS(+, oup, a, x, arrlen, _dt); \
break;
void cc_array_add_by(void *oup, int arrlen, void *a, void *x, int dt)
void cc_array_add_by(void *oup,
int arrlen, const void *a, const void *x, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -161,7 +163,8 @@ void cc_array_add_by(void *oup, int arrlen, void *a, void *x, int dt)
case _DT: \
ARRAY_SC_OPS(-, oup, a, x, arrlen, _dt); \
break;
void cc_array_sub_by(void *oup, int arrlen, void *a, void *x, int dt)
void cc_array_sub_by(void *oup,
int arrlen, const void *a, const void *x, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -186,7 +189,8 @@ void cc_array_sub_by(void *oup, int arrlen, void *a, void *x, int dt)
case _DT: \
ARRAY_SC_OPS(*, oup, a, x, arrlen, _dt); \
break;
void cc_array_mul_by(void *oup, int arrlen, void *a, void *x, int dt)
void cc_array_mul_by(void *oup,
int arrlen, const void *a, const void *x, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -211,7 +215,8 @@ void cc_array_mul_by(void *oup, int arrlen, void *a, void *x, int dt)
case _DT: \
ARRAY_SC_OPS(/, oup, a, x, arrlen, _dt); \
break;
void cc_array_div_by(void *oup, int arrlen, void *a, void *x, int dt)
void cc_array_div_by(void *oup,
int arrlen, const void *a, const void *x, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -236,7 +241,8 @@ void cc_array_div_by(void *oup, int arrlen, void *a, void *x, int dt)
case _DT: \
ARRAY_EW_OPS(+, oup, a, b, arrlen, _dt); \
break;
void cc_array_add_ew(void *oup, int arrlen, void *a, void *b, int dt)
void cc_array_add_ew(void *oup,
int arrlen, const void *a, const void *b, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -261,7 +267,8 @@ void cc_array_add_ew(void *oup, int arrlen, void *a, void *b, int dt)
case _DT: \
ARRAY_EW_OPS(-, oup, a, b, arrlen, _dt); \
break;
void cc_array_sub_ew(void *oup, int arrlen, void *a, void *b, int dt)
void cc_array_sub_ew(void *oup,
int arrlen, const void *a, const void *b, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -286,7 +293,8 @@ void cc_array_sub_ew(void *oup, int arrlen, void *a, void *b, int dt)
case _DT: \
ARRAY_EW_OPS(*, oup, a, b, arrlen, _dt); \
break;
void cc_array_mul_ew(void *oup, int arrlen, void *a, void *b, int dt)
void cc_array_mul_ew(void *oup,
int arrlen, const void *a, const void *b, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -311,7 +319,8 @@ void cc_array_mul_ew(void *oup, int arrlen, void *a, void *b, int dt)
case _DT: \
ARRAY_EW_OPS(/, oup, a, b, arrlen, _dt); \
break;
void cc_array_div_ew(void *oup, int arrlen, void *a, void *b, int dt)
void cc_array_div_ew(void *oup,
int arrlen, const void *a, const void *b, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -336,7 +345,7 @@ void cc_array_div_ew(void *oup, int arrlen, void *a, void *b, int dt)
case _DT: \
ARRAY_SUM(arr, arrlen, _dt, x); \
break;
void cc_array_sum(void *arr, int arrlen, void *x, int dt)
void cc_array_sum(const void *arr, int arrlen, void *x, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -362,7 +371,7 @@ case _DT: \
ARRAY_SUM(arr, arrlen, _dt, x); \
*(_dt*)x /= arrlen; \
break;
void cc_array_mean(void *arr, int arrlen, void *x, int dt)
void cc_array_mean(const void *arr, int arrlen, void *x, int dt)
{
cc_int32 i;
switch (dt) {
Expand All @@ -387,7 +396,7 @@ void cc_array_mean(void *arr, int arrlen, void *x, int dt)
case _DT: \
fprintf(fp, pat, *((_dt*)a + i)); \
break;
void cc_print_array(void *a, int arrlen, int dt, void *stream)
void cc_print_array(const void *a, int arrlen, int dt, void *stream)
{
cc_int32 i;
const char *pat = NULL;
Expand Down
49 changes: 29 additions & 20 deletions src/cc_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
*/

#define CC_ARRAY_CAST_DEFINITION(dtype) \
void cc_array_cast_ ## dtype(void *dst, void *src, int arrlen, int dt);
void cc_array_cast_ ## dtype( \
void *dst, const void *src, int arrlen, int dt);

CC_ARRAY_CAST_DEFINITION (uint8)
CC_ARRAY_CAST_DEFINITION (uint16)
Expand All @@ -23,25 +24,33 @@ CC_ARRAY_CAST_DEFINITION (int64)
CC_ARRAY_CAST_DEFINITION (float32)
CC_ARRAY_CAST_DEFINITION (float64)

void cc_array_set(void *arr, int arrlen, void *x, int dt);

void cc_array_clip_by_value(
void *arr, int arrlen, void *min, void *max, int dt);

void cc_array_add_by(void *oup, int arrlen, void *a, void *x, int dt);
void cc_array_sub_by(void *oup, int arrlen, void *a, void *x, int dt);
void cc_array_mul_by(void *oup, int arrlen, void *a, void *x, int dt);
void cc_array_div_by(void *oup, int arrlen, void *a, void *x, int dt);

void cc_array_add_ew(void *oup, int arrlen, void *a, void *b, int dt);
void cc_array_sub_ew(void *oup, int arrlen, void *a, void *b, int dt);
void cc_array_mul_ew(void *oup, int arrlen, void *a, void *b, int dt);
void cc_array_div_ew(void *oup, int arrlen, void *a, void *b, int dt);

void cc_array_sum(void *arr, int arrlen, void *x, int dt);
void cc_array_mean(void *arr, int arrlen, void *x, int dt);

void cc_print_array(void *a, int arrlen, int dt, void *stream);
void cc_array_set(void *arr, int arrlen, const void *x, int dt);

void cc_array_clip_by_value(void *arr,
int arrlen, const void *min, const void *max, int dt);

void cc_array_add_by(void *oup,
int arrlen, const void *a, const void *x, int dt);
void cc_array_sub_by(void *oup,
int arrlen, const void *a, const void *x, int dt);
void cc_array_mul_by(void *oup,
int arrlen, const void *a, const void *x, int dt);
void cc_array_div_by(void *oup,
int arrlen, const void *a, const void *x, int dt);

void cc_array_add_ew(void *oup,
int arrlen, const void *a, const void *b, int dt);
void cc_array_sub_ew(void *oup,
int arrlen, const void *a, const void *b, int dt);
void cc_array_mul_ew(void *oup,
int arrlen, const void *a, const void *b, int dt);
void cc_array_div_ew(void *oup,
int arrlen, const void *a, const void *b, int dt);

void cc_array_sum (const void *arr, int arrlen, void *x, int dt);
void cc_array_mean(const void *arr, int arrlen, void *x, int dt);

void cc_print_array(const void *a, int arrlen, int dt, void *stream);

#ifdef __cplusplus
}
Expand Down
43 changes: 16 additions & 27 deletions src/cc_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,18 @@ EXT_ARRAY_CAST_DEFINITION (int64)
EXT_ARRAY_CAST_DEFINITION (float32)
EXT_ARRAY_CAST_DEFINITION (float64)

extern void (*_array_set)(
void *arr, int arrlen, void *x, int dt);
extern fn_array_set _array_set;
extern fn_array_clip_by_value _array_clip_by_value;

extern void (*_array_clip_by_value)(
void *arr, int arrlen, void *min, void *max, int dt);
extern fn_array_add_by _array_add_by;
extern fn_array_sub_by _array_sub_by;
extern fn_array_mul_by _array_mul_by;
extern fn_array_div_by _array_div_by;

extern void (*_array_add_by)(
void *oup,int arrlen, void *a, void *x, int dt);
extern void (*_array_sub_by)(
void *oup,int arrlen, void *a, void *x, int dt);
extern void (*_array_mul_by)(
void *oup,int arrlen, void *a, void *x, int dt);
extern void (*_array_div_by)(
void *oup,int arrlen, void *a, void *x, int dt);

extern void (*_array_add_ew)(
void *oup, int arrlen,void *a, void *b, int dt);
extern void (*_array_sub_ew)(
void *oup, int arrlen,void *a, void *b, int dt);
extern void (*_array_mul_ew)(
void *oup, int arrlen,void *a, void *b, int dt);
extern void (*_array_div_ew)(
void *oup, int arrlen,void *a, void *b, int dt);
extern fn_array_add_ew _array_add_ew;
extern fn_array_sub_ew _array_sub_ew;
extern fn_array_mul_ew _array_mul_ew;
extern fn_array_div_ew _array_div_ew;

static cc_int32 _calc_elems(const cc_int32 *shape)
{
Expand All @@ -59,7 +48,7 @@ static cc_int32 _calc_elems(const cc_int32 *shape)
return elems;
}

cc_int32 cc_tensor_elements(cc_tensor_t *tensor)
cc_int32 cc_tensor_elements(const cc_tensor_t *tensor)
{
cc_int32 elems;
if (!tensor)
Expand Down Expand Up @@ -91,7 +80,7 @@ void cc_tensor_shape_fix(cc_int32 *shape, cc_int32 elems)
}
}

cc_int32 cc_tensor_dimension(cc_tensor_t *tensor)
cc_int32 cc_tensor_dimension(const cc_tensor_t *tensor)
{
cc_int32 dim = 0;
const cc_int32 *sptr;
Expand Down Expand Up @@ -126,7 +115,7 @@ cc_tensor_t *cc_tensor_reshape(cc_tensor_t *tensor, cc_int32 *shape)
return tensor;
}

int cc_tsrcmp_by_shape(cc_tensor_t *a, cc_tensor_t *b)
int cc_tsrcmp_by_shape(const cc_tensor_t *a, const cc_tensor_t *b)
{
int ret = 0;
const cc_int32 *ptra = a->shape;
Expand All @@ -142,7 +131,7 @@ int cc_tsrcmp_by_shape(cc_tensor_t *a, cc_tensor_t *b)
return 0;
}

void cc_print_tensor(cc_tensor_t *tensor)
void cc_print_tensor(const cc_tensor_t *tensor)
{
cc_print_tensor_property(tensor);
cc_print_array(tensor->data,
Expand All @@ -157,7 +146,7 @@ void cc_set_tensor(cc_tensor_t *tensor, void *v)
}

cc_tensor_t *cc_clip_by_value(cc_tensor_t *tensor,
void *min, void *max, const char *name)
const void *min, const void *max, const char *name)
{
cc_tensor_t *yield;
cc_int32 elems = *tensor->shape;
Expand Down Expand Up @@ -251,7 +240,7 @@ cc_tensor_t *cc_cast_tensor(cc_tensor_t *tensor,
}

cc_tensor_t *cc_tensor_by_scalar(cc_tensor_t *tensor,
char op, void *data, const char *name)
char op, const void *data, const char *name)
{
cc_tensor_t *yield;
cc_int32 elems = *tensor->shape;
Expand Down
21 changes: 14 additions & 7 deletions src/cc_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@

#include "cc_tensor.h"

cc_int32 cc_tensor_elements(cc_tensor_t *tensor);
cc_int32 cc_tensor_elements (const cc_tensor_t *tensor);

cc_int32 cc_tensor_dimension(cc_tensor_t *tensor);
cc_int32 cc_tensor_dimension(const cc_tensor_t *tensor);

void cc_tensor_shape_fix(cc_int32 *shape, cc_int32 elems);

cc_tensor_t *cc_tensor_reshape(cc_tensor_t *tensor, cc_int32 *shape);
/*
* Before reshape, `cc_tensor_reshape` will check and fix shape
* e.g. For a tensor shape with `[3, 3, 3]`, it can be reshaped through
* the argument `shape = [-1, 3]`. After reshaping, the tensor's shape
* should be `[9, 3]`, and `shape` will be modified to `[9, 3]`.
*/
cc_tensor_t *cc_tensor_reshape(
cc_tensor_t *tensor, cc_int32 *shape);

int cc_tsrcmp_by_shape(cc_tensor_t *a, cc_tensor_t *b);
int cc_tsrcmp_by_shape(const cc_tensor_t *a, const cc_tensor_t *b);

void cc_print_tensor(cc_tensor_t *tensor);
void cc_print_tensor(const cc_tensor_t *tensor);

void cc_set_tensor(cc_tensor_t *tensor, void *v);

cc_tensor_t *cc_cast_tensor(cc_tensor_t *tensor,
cc_dtype dtype, const char *name);

cc_tensor_t *cc_tensor_by_scalar(cc_tensor_t *tensor,
char op, void *data, const char *name);
char op, const void *data, const char *name);

/*
* Element wise operations
Expand All @@ -36,7 +43,7 @@ cc_tensor_t *cc_tensor_ewop(cc_tensor_t *a,
cc_tensor_t *b, char op, const char *name);

cc_tensor_t *cc_clip_by_value(cc_tensor_t *tensor,
void *min, void *max, const char *name);
const void *min, const void *max, const char *name);

#ifdef __cplusplus
}
Expand Down
13 changes: 7 additions & 6 deletions src/cc_conv2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@
extern fn_conv2d _conv2d;
extern fn_array_add_ew _array_add_ew;

cc_int32 cc_conv2d_shape_calc(cc_int32 i,
cc_int32 k, cc_int32 s, cc_int32 p)
cc_int32 cc_conv2d_shape_calc(
cc_int32 i, cc_int32 k, cc_int32 s, cc_int32 p)
{
return (cc_int32)((i - k + 2 * p) / s) + 1;
}

cc_tensor_t *cc_conv2d(cc_tensor_t *inp,
cc_tensor_t *kernel, cc_tensor_t *bias, cc_int32 s,
cc_int32 p, cc_int32 off, const char *name)
cc_tensor_t *cc_conv2d(const cc_tensor_t *inp,
const cc_tensor_t *kernel, const cc_tensor_t *bias,
cc_int32 s, cc_int32 p, cc_int32 off, const char *name)
{
cc_uint8 *omp_out_buf = NULL;
cc_tensor_t *inp_pad, *oup = NULL;
cc_tensor_t *oup = NULL;
const cc_tensor_t *inp_pad;
cc_int32 o_ch_size, p_ch_mem_size, o_ch_mem_size,
k_ch_mem_size, k_mem_size, num_omp_threads, i, j;
cc_int32 shape[CC_CNN2D_SHAPE_LEN] = {0};
Expand Down
10 changes: 5 additions & 5 deletions src/cc_conv2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ enum cc_conv2d_kernel {

#include "cc_tensor.h"

cc_int32 cc_conv2d_shape_calc(cc_int32 i,
cc_int32 k, cc_int32 s, cc_int32 p);
cc_int32 cc_conv2d_shape_calc(
cc_int32 i, cc_int32 k, cc_int32 s, cc_int32 p);

cc_tensor_t *cc_conv2d(cc_tensor_t *inp,
cc_tensor_t *kernel, cc_tensor_t *bias, cc_int32 s,
cc_int32 p, cc_int32 off, const char *name);
cc_tensor_t *cc_conv2d(const cc_tensor_t *inp,
const cc_tensor_t *kernel, const cc_tensor_t *bias,
cc_int32 s, cc_int32 p, cc_int32 off, const char *name);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit abea6af

Please sign in to comment.